usteer: Initial import
[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
23 static bool
24 below_assoc_threshold(struct sta_info *si_cur, struct sta_info *si_new)
25 {
26 int n_assoc_cur = si_cur->node->n_assoc;
27 int n_assoc_new = si_new->node->n_assoc;
28 bool ref_5g = si_cur->node->freq > 4000;
29 bool node_5g = si_new->node->freq > 4000;
30
31 if (ref_5g && !node_5g)
32 n_assoc_new += config.band_steering_threshold;
33 else if (!ref_5g && node_5g)
34 n_assoc_cur += config.band_steering_threshold;
35
36 n_assoc_new += config.load_balancing_threshold;
37
38 if (n_assoc_new > n_assoc_cur) {
39 MSG_T_STA("band_steering_threshold,load_balancing_threshold",
40 si_cur->sta->addr, "exeeded (bs=%u, lb=%u)\n",
41 config.band_steering_threshold,
42 config.load_balancing_threshold);
43 }
44 return n_assoc_new <= n_assoc_cur;
45 }
46
47 static bool
48 better_signal_strength(struct sta_info *si_cur, struct sta_info *si_new)
49 {
50 const bool is_better = si_new->signal - si_cur->signal
51 > (int) config.signal_diff_threshold;
52
53 if (!config.signal_diff_threshold)
54 return false;
55
56 if (is_better) {
57 MSG_T_STA("signal_diff_threshold", si_cur->sta->addr,
58 "exceeded (config=%i) (real=%i)\n",
59 config.signal_diff_threshold,
60 si_new->signal - si_cur->signal);
61 }
62 return is_better;
63 }
64
65 static bool
66 below_load_threshold(struct sta_info *si)
67 {
68 return si->node->n_assoc >= config.load_kick_min_clients &&
69 si->node->load > config.load_kick_threshold;
70 }
71
72 static bool
73 has_better_load(struct sta_info *si_cur, struct sta_info *si_new)
74 {
75 return !below_load_threshold(si_cur) && below_load_threshold(si_new);
76 }
77
78 static bool
79 below_max_assoc(struct sta_info *si)
80 {
81 struct usteer_node *node = si->node;
82
83 return !node->max_assoc || node->n_assoc < node->max_assoc;
84 }
85
86 static bool
87 is_better_candidate(struct sta_info *si_cur, struct sta_info *si_new)
88 {
89 if (!below_max_assoc(si_new))
90 return false;
91
92 return below_assoc_threshold(si_cur, si_new) ||
93 better_signal_strength(si_cur, si_new) ||
94 has_better_load(si_cur, si_new);
95 }
96
97 static struct sta_info *
98 find_better_candidate(struct sta_info *si_ref)
99 {
100 struct sta_info *si;
101 struct sta *sta = si_ref->sta;
102
103 list_for_each_entry(si, &sta->nodes, list) {
104 if (si == si_ref)
105 continue;
106
107 if (current_time - si->seen > config.seen_policy_timeout) {
108 MSG_T_STA("seen_policy_timeout", si->sta->addr,
109 "timeout exceeded (%u)\n", config.seen_policy_timeout);
110 continue;
111 }
112
113 if (strcmp(si->node->ssid, si_ref->node->ssid) != 0)
114 continue;
115
116 if (is_better_candidate(si_ref, si) &&
117 !is_better_candidate(si, si_ref))
118 return si;
119 }
120 return NULL;
121 }
122
123 static int
124 snr_to_signal(struct usteer_node *node, int snr)
125 {
126 int noise = -95;
127
128 if (snr < 0)
129 return snr;
130
131 if (node->noise)
132 noise = node->noise;
133
134 return noise + snr;
135 }
136
137 bool
138 usteer_check_request(struct sta_info *si, enum usteer_event_type type)
139 {
140 struct sta_info *si_new;
141 int min_signal;
142
143 if (type == EVENT_TYPE_ASSOC)
144 return true;
145
146 if (si->stats[type].blocked_cur >= config.max_retry_band) {
147 MSG_T_STA("max_retry_band", si->sta->addr,
148 "max retry (%u) exceeded\n", config.max_retry_band);
149 return true;
150 }
151
152 min_signal = snr_to_signal(si->node, config.min_connect_snr);
153 if (si->signal < min_signal) {
154 if (type != EVENT_TYPE_PROBE || config.debug_level >= MSG_DEBUG)
155 MSG(VERBOSE, "Ignoring %s request from "MAC_ADDR_FMT" due to low signal (%d < %d)\n",
156 event_types[type], MAC_ADDR_DATA(si->sta->addr),
157 si->signal, min_signal);
158 MSG_T_STA("min_connect_snr", si->sta->addr,
159 "snr to low (config=%i) (real=%i)\n",
160 min_signal, si->signal);
161 return false;
162 }
163
164 if (current_time - si->created < config.initial_connect_delay) {
165 if (type != EVENT_TYPE_PROBE || config.debug_level >= MSG_DEBUG)
166 MSG(VERBOSE, "Ignoring %s request from "MAC_ADDR_FMT" during initial connect delay\n",
167 event_types[type], MAC_ADDR_DATA(si->sta->addr));
168 MSG_T_STA("initial_connect_delay", si->sta->addr,
169 "is below delay (%u)\n", config.initial_connect_delay);
170 return false;
171 }
172
173 si_new = find_better_candidate(si);
174 if (!si_new)
175 return true;
176
177 if (type != EVENT_TYPE_PROBE || config.debug_level >= MSG_DEBUG)
178 MSG(VERBOSE, "Ignoring %s request from "MAC_ADDR_FMT", "
179 "node (local/remote): %s/%s, "
180 "signal=%d/%d, n_assoc=%d/%d\n", event_types[type],
181 MAC_ADDR_DATA(si->sta->addr),
182 usteer_node_name(si->node), usteer_node_name(si_new->node),
183 si->signal, si_new->signal,
184 si->node->n_assoc, si_new->node->n_assoc);
185
186 return false;
187 }
188
189 static bool
190 is_more_kickable(struct sta_info *si_cur, struct sta_info *si_new)
191 {
192 if (!si_cur)
193 return true;
194
195 if (si_new->kick_count > si_cur->kick_count)
196 return false;
197
198 return si_cur->signal > si_new->signal;
199 }
200
201 static void
202 usteer_roam_set_state(struct sta_info *si, enum roam_trigger_state state)
203 {
204 static const char * const state_names[] = {
205 #define _S(n) [ROAM_TRIGGER_##n] = #n,
206 __roam_trigger_states
207 #undef _S
208 };
209
210 si->roam_event = current_time;
211
212 if (si->roam_state == state) {
213 if (si->roam_state == ROAM_TRIGGER_IDLE) {
214 si->roam_tries = 0;
215 return;
216 }
217
218 si->roam_tries++;
219 } else {
220 si->roam_tries = 0;
221 }
222
223 si->roam_state = state;
224
225 MSG(VERBOSE, "Roam trigger SM for client "MAC_ADDR_FMT": state=%s, tries=%d, signal=%d\n",
226 MAC_ADDR_DATA(si->sta->addr), state_names[state], si->roam_tries, si->signal);
227 }
228
229 static bool
230 usteer_roam_trigger_sm(struct sta_info *si)
231 {
232 struct sta_info *si_new;
233 int min_signal;
234
235 min_signal = snr_to_signal(si->node, config.roam_trigger_snr);
236
237 switch (si->roam_state) {
238 case ROAM_TRIGGER_SCAN:
239 if (current_time - si->roam_event < config.roam_scan_interval)
240 break;
241
242 if (find_better_candidate(si) ||
243 si->roam_scan_done > si->roam_event) {
244 usteer_roam_set_state(si, ROAM_TRIGGER_SCAN_DONE);
245 break;
246 }
247
248 if (config.roam_scan_tries &&
249 si->roam_tries >= config.roam_scan_tries) {
250 usteer_roam_set_state(si, ROAM_TRIGGER_WAIT_KICK);
251 break;
252 }
253
254 usteer_ubus_trigger_client_scan(si);
255 usteer_roam_set_state(si, ROAM_TRIGGER_SCAN);
256 break;
257
258 case ROAM_TRIGGER_IDLE:
259 if (find_better_candidate(si)) {
260 usteer_roam_set_state(si, ROAM_TRIGGER_SCAN_DONE);
261 break;
262 }
263
264 usteer_roam_set_state(si, ROAM_TRIGGER_SCAN);
265 break;
266
267 case ROAM_TRIGGER_SCAN_DONE:
268 /* Check for stale scan results, kick back to SCAN state if necessary */
269 if (current_time - si->roam_scan_done > 2 * config.roam_scan_interval) {
270 usteer_roam_set_state(si, ROAM_TRIGGER_SCAN);
271 break;
272 }
273
274 si_new = find_better_candidate(si);
275 if (!si_new)
276 break;
277
278 usteer_roam_set_state(si, ROAM_TRIGGER_WAIT_KICK);
279 break;
280
281 case ROAM_TRIGGER_WAIT_KICK:
282 if (si->signal > min_signal)
283 break;
284
285 usteer_roam_set_state(si, ROAM_TRIGGER_NOTIFY_KICK);
286 usteer_ubus_notify_client_disassoc(si);
287 break;
288 case ROAM_TRIGGER_NOTIFY_KICK:
289 if (current_time - si->roam_event < config.roam_kick_delay * 100)
290 break;
291
292 usteer_roam_set_state(si, ROAM_TRIGGER_KICK);
293 break;
294 case ROAM_TRIGGER_KICK:
295 usteer_ubus_kick_client(si);
296 usteer_roam_set_state(si, ROAM_TRIGGER_IDLE);
297 return true;
298 }
299
300 return false;
301 }
302
303 static void
304 usteer_local_node_roam_check(struct usteer_local_node *ln)
305 {
306 struct sta_info *si;
307 int min_signal;
308
309 if (config.roam_scan_snr)
310 min_signal = config.roam_scan_snr;
311 else if (config.roam_trigger_snr)
312 min_signal = config.roam_trigger_snr;
313 else
314 return;
315
316 usteer_update_time();
317 min_signal = snr_to_signal(&ln->node, min_signal);
318
319 list_for_each_entry(si, &ln->node.sta_info, node_list) {
320 if (!si->connected || si->signal >= min_signal ||
321 current_time - si->roam_kick < config.roam_trigger_interval) {
322 usteer_roam_set_state(si, ROAM_TRIGGER_IDLE);
323 continue;
324 }
325
326 /*
327 * If the state machine kicked a client, other clients should wait
328 * until the next turn
329 */
330 if (usteer_roam_trigger_sm(si))
331 return;
332 }
333 }
334
335 static void
336 usteer_local_node_snr_kick(struct usteer_local_node *ln)
337 {
338 struct sta_info *si;
339 int min_signal;
340
341 if (!config.min_snr)
342 return;
343
344 min_signal = snr_to_signal(&ln->node, config.min_snr);
345
346 list_for_each_entry(si, &ln->node.sta_info, node_list) {
347 if (!si->connected)
348 continue;
349
350 if (si->signal >= min_signal)
351 continue;
352
353 si->kick_count++;
354
355 MSG(VERBOSE, "Kicking client "MAC_ADDR_FMT" due to low SNR, signal=%d\n",
356 MAC_ADDR_DATA(si->sta->addr), si->signal);
357
358 usteer_ubus_kick_client(si);
359 return;
360 }
361 }
362
363 void
364 usteer_local_node_kick(struct usteer_local_node *ln)
365 {
366 struct usteer_node *node = &ln->node;
367 struct sta_info *kick1 = NULL, *kick2 = NULL;
368 struct sta_info *candidate = NULL;
369 struct sta_info *si;
370
371 usteer_local_node_roam_check(ln);
372 usteer_local_node_snr_kick(ln);
373
374 if (!config.load_kick_enabled || !config.load_kick_threshold ||
375 !config.load_kick_delay)
376 return;
377
378 if (node->load < config.load_kick_threshold) {
379 MSG_T("load_kick_threshold",
380 "is below load for this node (config=%i) (real=%i)\n",
381 config.load_kick_threshold, node->load);
382 ln->load_thr_count = 0;
383 return;
384 }
385
386 if (++ln->load_thr_count <=
387 DIV_ROUND_UP(config.load_kick_delay, config.local_sta_update)) {
388 MSG_T("load_kick_delay", "delay kicking (config=%i)\n",
389 config.load_kick_delay);
390 return;
391 }
392
393 MSG(VERBOSE, "AP load threshold exceeded on %s (%d), try to kick a client\n",
394 usteer_node_name(node), node->load);
395
396 ln->load_thr_count = 0;
397 if (node->n_assoc < config.load_kick_min_clients) {
398 MSG_T("load_kick_min_clients",
399 "min limit reached, stop kicking clients on this node "
400 "(n_assoc=%i) (config=%i)\n",
401 node->n_assoc, config.load_kick_min_clients);
402 return;
403 }
404
405 list_for_each_entry(si, &ln->node.sta_info, node_list) {
406 struct sta_info *tmp;
407
408 if (!si->connected)
409 continue;
410
411 if (is_more_kickable(kick1, si))
412 kick1 = si;
413
414 tmp = find_better_candidate(si);
415 if (!tmp)
416 continue;
417
418 if (is_more_kickable(kick2, si)) {
419 kick2 = si;
420 candidate = tmp;
421 }
422 }
423
424 if (!kick1)
425 return;
426
427 if (kick2)
428 kick1 = kick2;
429
430 MSG(VERBOSE, "Kicking client "MAC_ADDR_FMT", signal=%d, better_candidate=%s\n",
431 MAC_ADDR_DATA(kick1->sta->addr), kick1->signal,
432 candidate ? usteer_node_name(candidate->node) : "(none)");
433
434 kick1->kick_count++;
435 usteer_ubus_kick_client(kick1);
436 }