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 (!config
.load_balancing_threshold
)
35 if (ref_5g
&& !node_5g
)
36 n_assoc_new
+= config
.band_steering_threshold
;
37 else if (!ref_5g
&& node_5g
)
38 n_assoc_cur
+= config
.band_steering_threshold
;
40 n_assoc_new
+= config
.load_balancing_threshold
;
42 return n_assoc_new
<= n_assoc_cur
;
46 better_signal_strength(int signal_cur
, int signal_new
)
48 const bool is_better
= signal_new
- signal_cur
49 > (int) config
.signal_diff_threshold
;
51 if (!config
.signal_diff_threshold
)
58 below_load_threshold(struct usteer_node
*node
)
60 return node
->n_assoc
>= config
.load_kick_min_clients
&&
61 node
->load
> config
.load_kick_threshold
;
65 has_better_load(struct usteer_node
*node_cur
, struct usteer_node
*node_new
)
67 return !below_load_threshold(node_cur
) && below_load_threshold(node_new
);
71 usteer_policy_node_below_max_assoc(struct usteer_node
*node
)
73 return !node
->max_assoc
|| node
->n_assoc
< node
->max_assoc
;
77 over_min_signal(struct usteer_node
*node
, int signal
)
79 if (config
.min_snr
&& signal
< usteer_snr_to_signal(node
, config
.min_snr
))
82 if (config
.roam_trigger_snr
&& signal
< usteer_snr_to_signal(node
, config
.roam_trigger_snr
))
89 is_better_candidate(struct sta_info
*si_cur
, struct sta_info
*si_new
)
91 struct usteer_node
*current_node
= si_cur
->node
;
92 struct usteer_node
*new_node
= si_new
->node
;
93 int current_signal
= si_cur
->signal
;
94 int new_signal
= si_new
->signal
;
97 if (!usteer_policy_node_below_max_assoc(new_node
))
100 if (!over_min_signal(new_node
, new_signal
))
103 if (below_assoc_threshold(current_node
, new_node
) &&
104 !below_assoc_threshold(new_node
, current_node
))
105 reasons
|= (1 << UEV_SELECT_REASON_NUM_ASSOC
);
107 if (better_signal_strength(current_signal
, new_signal
))
108 reasons
|= (1 << UEV_SELECT_REASON_SIGNAL
);
110 if (has_better_load(current_node
, new_node
) &&
111 !has_better_load(current_node
, new_node
))
112 reasons
|= (1 << UEV_SELECT_REASON_LOAD
);
117 static struct sta_info
*
118 find_better_candidate(struct sta_info
*si_ref
, struct uevent
*ev
, uint32_t required_criteria
, uint64_t max_age
)
120 struct sta_info
*si
, *candidate
= NULL
;
121 struct sta
*sta
= si_ref
->sta
;
124 list_for_each_entry(si
, &sta
->nodes
, list
) {
128 if (current_time
- si
->seen
> config
.seen_policy_timeout
)
131 if (strcmp(si
->node
->ssid
, si_ref
->node
->ssid
) != 0)
134 if (max_age
&& max_age
< current_time
- si
->seen
)
137 reasons
= is_better_candidate(si_ref
, si
);
141 if (!(reasons
& required_criteria
))
146 ev
->select_reasons
= reasons
;
149 if (!candidate
|| si
->signal
> candidate
->signal
)
157 usteer_snr_to_signal(struct usteer_node
*node
, int snr
)
171 usteer_check_request(struct sta_info
*si
, enum usteer_event_type type
)
179 if (type
== EVENT_TYPE_PROBE
&& !config
.probe_steering
)
182 if (type
== EVENT_TYPE_AUTH
)
185 if (type
== EVENT_TYPE_ASSOC
) {
186 /* Check if assoc request has lower signal than min_signal.
187 * If this is the case, block assoc even when assoc steering is enabled.
189 * Otherwise, the client potentially ends up in a assoc - kick loop.
191 if (config
.min_snr
&& si
->signal
< usteer_snr_to_signal(si
->node
, config
.min_snr
)) {
192 ev
.reason
= UEV_REASON_LOW_SIGNAL
;
193 ev
.threshold
.cur
= si
->signal
;
194 ev
.threshold
.ref
= usteer_snr_to_signal(si
->node
, config
.min_snr
);
197 } else if (!config
.assoc_steering
) {
202 min_signal
= usteer_snr_to_signal(si
->node
, config
.min_connect_snr
);
203 if (si
->signal
< min_signal
) {
204 ev
.reason
= UEV_REASON_LOW_SIGNAL
;
205 ev
.threshold
.cur
= si
->signal
;
206 ev
.threshold
.ref
= min_signal
;
211 if (current_time
- si
->created
< config
.initial_connect_delay
) {
212 ev
.reason
= UEV_REASON_CONNECT_DELAY
;
213 ev
.threshold
.cur
= current_time
- si
->created
;
214 ev
.threshold
.ref
= config
.initial_connect_delay
;
219 if (!find_better_candidate(si
, &ev
, UEV_SELECT_REASON_ALL
, 0))
222 ev
.reason
= UEV_REASON_BETTER_CANDIDATE
;
223 ev
.node_cur
= si
->node
;
228 case EVENT_TYPE_PROBE
:
229 ev
.type
= ret
? UEV_PROBE_REQ_ACCEPT
: UEV_PROBE_REQ_DENY
;
231 case EVENT_TYPE_ASSOC
:
232 ev
.type
= ret
? UEV_ASSOC_REQ_ACCEPT
: UEV_ASSOC_REQ_DENY
;
234 case EVENT_TYPE_AUTH
:
235 ev
.type
= ret
? UEV_AUTH_REQ_ACCEPT
: UEV_AUTH_REQ_DENY
;
241 if (!ret
&& si
->stats
[type
].blocked_cur
>= config
.max_retry_band
) {
242 ev
.reason
= UEV_REASON_RETRY_EXCEEDED
;
243 ev
.threshold
.cur
= si
->stats
[type
].blocked_cur
;
244 ev
.threshold
.ref
= config
.max_retry_band
;
252 is_more_kickable(struct sta_info
*si_cur
, struct sta_info
*si_new
)
257 if (si_new
->kick_count
> si_cur
->kick_count
)
260 return si_cur
->signal
> si_new
->signal
;
264 usteer_roam_set_state(struct sta_info
*si
, enum roam_trigger_state state
,
267 si
->roam_event
= current_time
;
269 if (si
->roam_state
== state
) {
270 if (si
->roam_state
== ROAM_TRIGGER_IDLE
) {
280 si
->roam_state
= state
;
285 usteer_roam_sm_start_scan(struct sta_info
*si
, struct uevent
*ev
)
287 /* Start scanning in case we are not timeout-constrained or timeout has expired */
288 if (!config
.roam_scan_timeout
||
289 current_time
> si
->roam_scan_timeout_start
+ config
.roam_scan_timeout
) {
290 usteer_roam_set_state(si
, ROAM_TRIGGER_SCAN
, ev
);
294 /* We are currently in scan timeout / cooldown.
295 * Check if we are in ROAM_TRIGGER_IDLE state. Enter this state if not.
297 if (si
->roam_state
== ROAM_TRIGGER_IDLE
)
300 /* Enter idle state */
301 usteer_roam_set_state(si
, ROAM_TRIGGER_IDLE
, ev
);
304 static struct sta_info
*
305 usteer_roam_sm_found_better_node(struct sta_info
*si
, struct uevent
*ev
, enum roam_trigger_state next_state
)
307 uint64_t max_age
= 2 * config
.roam_scan_interval
;
308 struct sta_info
*candidate
;
310 if (max_age
> current_time
- si
->roam_scan_start
)
311 max_age
= current_time
- si
->roam_scan_start
;
313 candidate
= find_better_candidate(si
, ev
, (1 << UEV_SELECT_REASON_SIGNAL
), max_age
);
315 usteer_roam_set_state(si
, next_state
, ev
);
321 usteer_roam_trigger_sm(struct usteer_local_node
*ln
, struct sta_info
*si
)
323 struct sta_info
*candidate
;
328 switch (si
->roam_state
) {
329 case ROAM_TRIGGER_SCAN
:
330 if (!si
->roam_tries
) {
331 si
->roam_scan_start
= current_time
;
334 /* Check if we've found a better node regardless of the scan-interval */
335 if (usteer_roam_sm_found_better_node(si
, &ev
, ROAM_TRIGGER_SCAN_DONE
))
338 /* Only scan every scan-interval */
339 if (current_time
- si
->roam_event
< config
.roam_scan_interval
)
342 /* Check if no node was found within roam_scan_tries tries */
343 if (config
.roam_scan_tries
&& si
->roam_tries
>= config
.roam_scan_tries
) {
344 if (!config
.roam_scan_timeout
) {
345 /* Prepare to kick client */
346 usteer_roam_set_state(si
, ROAM_TRIGGER_SCAN_DONE
, &ev
);
348 /* Kick in scan timeout */
349 si
->roam_scan_timeout_start
= current_time
;
350 usteer_roam_set_state(si
, ROAM_TRIGGER_IDLE
, &ev
);
355 /* Send beacon-request to client */
356 usteer_ubus_trigger_client_scan(si
);
357 usteer_roam_sm_start_scan(si
, &ev
);
360 case ROAM_TRIGGER_IDLE
:
361 usteer_roam_sm_start_scan(si
, &ev
);
364 case ROAM_TRIGGER_SCAN_DONE
:
365 candidate
= usteer_roam_sm_found_better_node(si
, &ev
, ROAM_TRIGGER_SCAN_DONE
);
366 /* Kick back in case no better node is found */
368 usteer_roam_set_state(si
, ROAM_TRIGGER_IDLE
, &ev
);
372 usteer_ubus_bss_transition_request(si
, 1, false, false, 100, candidate
->node
);
373 si
->kick_time
= current_time
+ config
.roam_kick_delay
;
374 usteer_roam_set_state(si
, ROAM_TRIGGER_IDLE
, &ev
);
381 bool usteer_policy_can_perform_roam(struct sta_info
*si
)
383 /* Only trigger for connected STAs */
384 if (si
->connected
!= STA_CONNECTED
)
387 /* Skip on pending kick */
391 /* Skip on rejected transition */
392 if (si
->bss_transition_response
.status_code
&& current_time
- si
->bss_transition_response
.timestamp
< config
.steer_reject_timeout
)
395 /* Skip on previous kick attempt */
396 if (current_time
- si
->roam_kick
< config
.roam_trigger_interval
)
399 /* Skip if connection is established shorter than the trigger-interval */
400 if (current_time
- si
->connected_since
< config
.roam_trigger_interval
)
407 usteer_local_node_roam_sm_active(struct sta_info
*si
, int min_signal
)
409 if (!usteer_policy_can_perform_roam(si
))
412 /* Signal has to be below scan / roam threshold */
413 if (si
->signal
>= min_signal
)
420 usteer_local_node_roam_check(struct usteer_local_node
*ln
, struct uevent
*ev
)
425 if (config
.roam_scan_snr
)
426 min_signal
= config
.roam_scan_snr
;
427 else if (config
.roam_trigger_snr
)
428 min_signal
= config
.roam_trigger_snr
;
432 usteer_update_time();
433 min_signal
= usteer_snr_to_signal(&ln
->node
, min_signal
);
435 list_for_each_entry(si
, &ln
->node
.sta_info
, node_list
) {
436 if (!usteer_local_node_roam_sm_active(si
, min_signal
)) {
437 usteer_roam_set_state(si
, ROAM_TRIGGER_IDLE
, ev
);
442 * If the state machine kicked a client, other clients should wait
443 * until the next turn
445 if (usteer_roam_trigger_sm(ln
, si
))
451 usteer_local_node_snr_kick(struct usteer_local_node
*ln
)
453 unsigned int min_count
= DIV_ROUND_UP(config
.min_snr_kick_delay
, config
.local_sta_update
);
455 .node_local
= &ln
->node
,
463 min_signal
= usteer_snr_to_signal(&ln
->node
, config
.min_snr
);
464 ev
.threshold
.ref
= min_signal
;
466 list_for_each_entry(si
, &ln
->node
.sta_info
, node_list
) {
467 if (si
->connected
!= STA_CONNECTED
)
470 if (si
->signal
>= min_signal
) {
471 si
->below_min_snr
= 0;
477 if (si
->below_min_snr
<= min_count
)
482 ev
.type
= UEV_SIGNAL_KICK
;
483 ev
.threshold
.cur
= si
->signal
;
484 ev
.count
= si
->kick_count
;
487 usteer_ubus_kick_client(si
);
493 usteer_local_node_load_kick(struct usteer_local_node
*ln
)
495 struct usteer_node
*node
= &ln
->node
;
496 struct sta_info
*kick1
= NULL
, *kick2
= NULL
;
497 struct sta_info
*candidate
= NULL
;
500 .node_local
= &ln
->node
,
502 unsigned int min_count
= DIV_ROUND_UP(config
.load_kick_delay
, config
.local_sta_update
);
504 if (!config
.load_kick_enabled
|| !config
.load_kick_threshold
||
505 !config
.load_kick_delay
)
508 if (node
->load
< config
.load_kick_threshold
) {
509 if (!ln
->load_thr_count
)
512 ln
->load_thr_count
= 0;
513 ev
.type
= UEV_LOAD_KICK_RESET
;
514 ev
.threshold
.cur
= node
->load
;
515 ev
.threshold
.ref
= config
.load_kick_threshold
;
519 if (++ln
->load_thr_count
<= min_count
) {
520 if (ln
->load_thr_count
> 1)
523 ev
.type
= UEV_LOAD_KICK_TRIGGER
;
524 ev
.threshold
.cur
= node
->load
;
525 ev
.threshold
.ref
= config
.load_kick_threshold
;
529 ln
->load_thr_count
= 0;
530 if (node
->n_assoc
< config
.load_kick_min_clients
) {
531 ev
.type
= UEV_LOAD_KICK_MIN_CLIENTS
;
532 ev
.threshold
.cur
= node
->n_assoc
;
533 ev
.threshold
.ref
= config
.load_kick_min_clients
;
537 list_for_each_entry(si
, &ln
->node
.sta_info
, node_list
) {
538 struct sta_info
*tmp
;
540 if (si
->connected
!= STA_CONNECTED
)
543 if (is_more_kickable(kick1
, si
))
546 tmp
= find_better_candidate(si
, NULL
, (1 << UEV_SELECT_REASON_LOAD
), 0);
550 if (is_more_kickable(kick2
, si
)) {
557 ev
.type
= UEV_LOAD_KICK_NO_CLIENT
;
566 ev
.type
= UEV_LOAD_KICK_CLIENT
;
568 ev
.si_other
= candidate
;
569 ev
.count
= kick1
->kick_count
;
571 usteer_ubus_kick_client(kick1
);
578 usteer_local_node_perform_kick(struct usteer_local_node
*ln
)
582 list_for_each_entry(si
, &ln
->node
.sta_info
, node_list
) {
583 if (!si
->kick_time
|| si
->kick_time
> current_time
)
586 usteer_ubus_kick_client(si
);
591 usteer_local_node_kick(struct usteer_local_node
*ln
)
594 .node_local
= &ln
->node
,
597 usteer_local_node_perform_kick(ln
);
599 usteer_local_node_snr_kick(ln
);
600 usteer_local_node_load_kick(ln
);
601 usteer_local_node_roam_check(ln
, &ev
);