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.
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.
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.
15 * Copyright (C) 2020 embedd.ch
16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
17 * Copyright (C) 2020 John Crispin <john@phrozen.org>
25 below_assoc_threshold(struct usteer_node
*node_cur
, struct usteer_node
*node_new
)
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;
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
;
37 n_assoc_new
+= config
.load_balancing_threshold
;
39 return n_assoc_new
<= n_assoc_cur
;
43 better_signal_strength(int signal_cur
, int signal_new
)
45 const bool is_better
= signal_new
- signal_cur
46 > (int) config
.signal_diff_threshold
;
48 if (!config
.signal_diff_threshold
)
55 below_load_threshold(struct usteer_node
*node
)
57 return node
->n_assoc
>= config
.load_kick_min_clients
&&
58 node
->load
> config
.load_kick_threshold
;
62 has_better_load(struct usteer_node
*node_cur
, struct usteer_node
*node_new
)
64 return !below_load_threshold(node_cur
) && below_load_threshold(node_new
);
68 usteer_policy_node_below_max_assoc(struct usteer_node
*node
)
70 return !node
->max_assoc
|| node
->n_assoc
< node
->max_assoc
;
74 over_min_signal(struct usteer_node
*node
, int signal
)
76 if (config
.min_snr
&& signal
< usteer_snr_to_signal(node
, config
.min_snr
))
79 if (config
.roam_trigger_snr
&& signal
< usteer_snr_to_signal(node
, config
.roam_trigger_snr
))
86 is_better_candidate(struct sta_info
*si_cur
, struct sta_info
*si_new
)
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
;
94 if (!usteer_policy_node_below_max_assoc(new_node
))
97 if (!over_min_signal(new_node
, new_signal
))
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
);
104 if (better_signal_strength(current_signal
, new_signal
))
105 reasons
|= (1 << UEV_SELECT_REASON_SIGNAL
);
107 if (has_better_load(current_node
, new_node
) &&
108 !has_better_load(current_node
, new_node
))
109 reasons
|= (1 << UEV_SELECT_REASON_LOAD
);
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
)
118 struct sta
*sta
= si_ref
->sta
;
121 list_for_each_entry(si
, &sta
->nodes
, list
) {
125 if (current_time
- si
->seen
> config
.seen_policy_timeout
)
128 if (strcmp(si
->node
->ssid
, si_ref
->node
->ssid
) != 0)
131 if (max_age
&& max_age
< current_time
- si
->seen
)
134 reasons
= is_better_candidate(si_ref
, si
);
138 if (!(reasons
& required_criteria
))
143 ev
->select_reasons
= reasons
;
153 usteer_snr_to_signal(struct usteer_node
*node
, int snr
)
167 usteer_check_request(struct sta_info
*si
, enum usteer_event_type type
)
175 if (type
== EVENT_TYPE_AUTH
)
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.
182 * Otherwise, the client potentially ends up in a assoc - kick loop.
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
);
190 } else if (!config
.assoc_steering
) {
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
;
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
;
212 if (!find_better_candidate(si
, &ev
, UEV_SELECT_REASON_ALL
, 0))
215 ev
.reason
= UEV_REASON_BETTER_CANDIDATE
;
216 ev
.node_cur
= si
->node
;
221 case EVENT_TYPE_PROBE
:
222 ev
.type
= UEV_PROBE_REQ_ACCEPT
;
224 case EVENT_TYPE_ASSOC
:
225 ev
.type
= UEV_ASSOC_REQ_ACCEPT
;
227 case EVENT_TYPE_AUTH
:
228 ev
.type
= UEV_AUTH_REQ_ACCEPT
;
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
;
248 is_more_kickable(struct sta_info
*si_cur
, struct sta_info
*si_new
)
253 if (si_new
->kick_count
> si_cur
->kick_count
)
256 return si_cur
->signal
> si_new
->signal
;
260 usteer_roam_set_state(struct sta_info
*si
, enum roam_trigger_state state
,
263 si
->roam_event
= current_time
;
265 if (si
->roam_state
== state
) {
266 if (si
->roam_state
== ROAM_TRIGGER_IDLE
) {
276 si
->roam_state
= state
;
281 usteer_roam_sm_start_scan(struct sta_info
*si
, struct uevent
*ev
)
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
);
290 /* We are currently in scan timeout / cooldown.
291 * Check if we are in ROAM_TRIGGER_IDLE state. Enter this state if not.
293 if (si
->roam_state
== ROAM_TRIGGER_IDLE
)
296 /* Enter idle state */
297 usteer_roam_set_state(si
, ROAM_TRIGGER_IDLE
, ev
);
301 usteer_roam_sm_found_better_node(struct sta_info
*si
, struct uevent
*ev
, enum roam_trigger_state next_state
)
303 uint64_t max_age
= 2 * config
.roam_scan_interval
;
305 if (max_age
> current_time
- si
->roam_scan_start
)
306 max_age
= current_time
- si
->roam_scan_start
;
308 if (find_better_candidate(si
, ev
, (1 << UEV_SELECT_REASON_SIGNAL
), max_age
)) {
309 usteer_roam_set_state(si
, next_state
, ev
);
317 usteer_roam_trigger_sm(struct usteer_local_node
*ln
, struct sta_info
*si
)
323 switch (si
->roam_state
) {
324 case ROAM_TRIGGER_SCAN
:
325 if (!si
->roam_tries
) {
326 si
->roam_scan_start
= current_time
;
329 /* Check if we've found a better node regardless of the scan-interval */
330 if (usteer_roam_sm_found_better_node(si
, &ev
, ROAM_TRIGGER_SCAN_DONE
))
333 /* Only scan every scan-interval */
334 if (current_time
- si
->roam_event
< config
.roam_scan_interval
)
337 /* Check if no node was found within roam_scan_tries tries */
338 if (config
.roam_scan_tries
&& si
->roam_tries
>= config
.roam_scan_tries
) {
339 if (!config
.roam_scan_timeout
) {
340 /* Prepare to kick client */
341 usteer_roam_set_state(si
, ROAM_TRIGGER_SCAN_DONE
, &ev
);
343 /* Kick in scan timeout */
344 si
->roam_scan_timeout_start
= current_time
;
345 usteer_roam_set_state(si
, ROAM_TRIGGER_IDLE
, &ev
);
350 /* Send beacon-request to client */
351 usteer_ubus_trigger_client_scan(si
);
352 usteer_roam_sm_start_scan(si
, &ev
);
355 case ROAM_TRIGGER_IDLE
:
356 usteer_roam_sm_start_scan(si
, &ev
);
359 case ROAM_TRIGGER_SCAN_DONE
:
360 usteer_ubus_bss_transition_request(si
, 1, false, false, 100);
361 si
->kick_time
= current_time
;
362 usteer_roam_set_state(si
, ROAM_TRIGGER_IDLE
, &ev
);
370 usteer_local_node_roam_check(struct usteer_local_node
*ln
, struct uevent
*ev
)
375 if (config
.roam_scan_snr
)
376 min_signal
= config
.roam_scan_snr
;
377 else if (config
.roam_trigger_snr
)
378 min_signal
= config
.roam_trigger_snr
;
382 usteer_update_time();
383 min_signal
= usteer_snr_to_signal(&ln
->node
, min_signal
);
385 list_for_each_entry(si
, &ln
->node
.sta_info
, node_list
) {
386 if (si
->connected
!= STA_CONNECTED
|| si
->signal
>= min_signal
||
388 (si
->bss_transition_response
.status_code
&& current_time
- si
->bss_transition_response
.timestamp
< config
.steer_reject_timeout
) ||
389 current_time
- si
->roam_kick
< config
.roam_trigger_interval
) {
390 usteer_roam_set_state(si
, ROAM_TRIGGER_IDLE
, ev
);
395 * If the state machine kicked a client, other clients should wait
396 * until the next turn
398 if (usteer_roam_trigger_sm(ln
, si
))
404 usteer_local_node_snr_kick(struct usteer_local_node
*ln
)
406 unsigned int min_count
= DIV_ROUND_UP(config
.min_snr_kick_delay
, config
.local_sta_update
);
408 .node_local
= &ln
->node
,
416 min_signal
= usteer_snr_to_signal(&ln
->node
, config
.min_snr
);
417 ev
.threshold
.ref
= min_signal
;
419 list_for_each_entry(si
, &ln
->node
.sta_info
, node_list
) {
420 if (si
->connected
!= STA_CONNECTED
)
423 if (si
->signal
>= min_signal
) {
424 si
->below_min_snr
= 0;
430 if (si
->below_min_snr
<= min_count
)
435 ev
.type
= UEV_SIGNAL_KICK
;
436 ev
.threshold
.cur
= si
->signal
;
437 ev
.count
= si
->kick_count
;
440 usteer_ubus_kick_client(si
);
446 usteer_local_node_load_kick(struct usteer_local_node
*ln
)
448 struct usteer_node
*node
= &ln
->node
;
449 struct sta_info
*kick1
= NULL
, *kick2
= NULL
;
450 struct sta_info
*candidate
= NULL
;
453 .node_local
= &ln
->node
,
455 unsigned int min_count
= DIV_ROUND_UP(config
.load_kick_delay
, config
.local_sta_update
);
457 if (!config
.load_kick_enabled
|| !config
.load_kick_threshold
||
458 !config
.load_kick_delay
)
461 if (node
->load
< config
.load_kick_threshold
) {
462 if (!ln
->load_thr_count
)
465 ln
->load_thr_count
= 0;
466 ev
.type
= UEV_LOAD_KICK_RESET
;
467 ev
.threshold
.cur
= node
->load
;
468 ev
.threshold
.ref
= config
.load_kick_threshold
;
472 if (++ln
->load_thr_count
<= min_count
) {
473 if (ln
->load_thr_count
> 1)
476 ev
.type
= UEV_LOAD_KICK_TRIGGER
;
477 ev
.threshold
.cur
= node
->load
;
478 ev
.threshold
.ref
= config
.load_kick_threshold
;
482 ln
->load_thr_count
= 0;
483 if (node
->n_assoc
< config
.load_kick_min_clients
) {
484 ev
.type
= UEV_LOAD_KICK_MIN_CLIENTS
;
485 ev
.threshold
.cur
= node
->n_assoc
;
486 ev
.threshold
.ref
= config
.load_kick_min_clients
;
490 list_for_each_entry(si
, &ln
->node
.sta_info
, node_list
) {
491 struct sta_info
*tmp
;
493 if (si
->connected
!= STA_CONNECTED
)
496 if (is_more_kickable(kick1
, si
))
499 tmp
= find_better_candidate(si
, NULL
, (1 << UEV_SELECT_REASON_LOAD
), 0);
503 if (is_more_kickable(kick2
, si
)) {
510 ev
.type
= UEV_LOAD_KICK_NO_CLIENT
;
519 ev
.type
= UEV_LOAD_KICK_CLIENT
;
521 ev
.si_other
= candidate
;
522 ev
.count
= kick1
->kick_count
;
524 usteer_ubus_kick_client(kick1
);
531 usteer_local_node_perform_kick(struct usteer_local_node
*ln
)
535 list_for_each_entry(si
, &ln
->node
.sta_info
, node_list
) {
536 if (!si
->kick_time
|| si
->kick_time
> current_time
)
539 usteer_ubus_kick_client(si
);
544 usteer_local_node_kick(struct usteer_local_node
*ln
)
547 .node_local
= &ln
->node
,
550 usteer_local_node_perform_kick(ln
);
552 usteer_local_node_snr_kick(ln
);
553 usteer_local_node_load_kick(ln
);
554 usteer_local_node_roam_check(ln
, &ev
);