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>
24 below_assoc_threshold(struct sta_info
*si_cur
, struct sta_info
*si_new
)
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;
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
;
36 n_assoc_new
+= config
.load_balancing_threshold
;
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
);
44 return n_assoc_new
<= n_assoc_cur
;
48 better_signal_strength(struct sta_info
*si_cur
, struct sta_info
*si_new
)
50 const bool is_better
= si_new
->signal
- si_cur
->signal
51 > (int) config
.signal_diff_threshold
;
53 if (!config
.signal_diff_threshold
)
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
);
66 below_load_threshold(struct sta_info
*si
)
68 return si
->node
->n_assoc
>= config
.load_kick_min_clients
&&
69 si
->node
->load
> config
.load_kick_threshold
;
73 has_better_load(struct sta_info
*si_cur
, struct sta_info
*si_new
)
75 return !below_load_threshold(si_cur
) && below_load_threshold(si_new
);
79 below_max_assoc(struct sta_info
*si
)
81 struct usteer_node
*node
= si
->node
;
83 return !node
->max_assoc
|| node
->n_assoc
< node
->max_assoc
;
87 is_better_candidate(struct sta_info
*si_cur
, struct sta_info
*si_new
)
89 if (!below_max_assoc(si_new
))
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
);
97 static struct sta_info
*
98 find_better_candidate(struct sta_info
*si_ref
)
101 struct sta
*sta
= si_ref
->sta
;
103 list_for_each_entry(si
, &sta
->nodes
, list
) {
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
);
113 if (strcmp(si
->node
->ssid
, si_ref
->node
->ssid
) != 0)
116 if (is_better_candidate(si_ref
, si
) &&
117 !is_better_candidate(si
, si_ref
))
124 snr_to_signal(struct usteer_node
*node
, int snr
)
138 usteer_check_request(struct sta_info
*si
, enum usteer_event_type type
)
140 struct sta_info
*si_new
;
143 if (type
== EVENT_TYPE_ASSOC
)
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
);
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
);
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
);
173 si_new
= find_better_candidate(si
);
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
);
190 is_more_kickable(struct sta_info
*si_cur
, struct sta_info
*si_new
)
195 if (si_new
->kick_count
> si_cur
->kick_count
)
198 return si_cur
->signal
> si_new
->signal
;
202 usteer_roam_set_state(struct sta_info
*si
, enum roam_trigger_state state
)
204 static const char * const state_names
[] = {
205 #define _S(n) [ROAM_TRIGGER_##n] = #n,
206 __roam_trigger_states
210 si
->roam_event
= current_time
;
212 if (si
->roam_state
== state
) {
213 if (si
->roam_state
== ROAM_TRIGGER_IDLE
) {
223 si
->roam_state
= state
;
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
);
230 usteer_roam_trigger_sm(struct sta_info
*si
)
232 struct sta_info
*si_new
;
235 min_signal
= snr_to_signal(si
->node
, config
.roam_trigger_snr
);
237 switch (si
->roam_state
) {
238 case ROAM_TRIGGER_SCAN
:
239 if (current_time
- si
->roam_event
< config
.roam_scan_interval
)
242 if (find_better_candidate(si
) ||
243 si
->roam_scan_done
> si
->roam_event
) {
244 usteer_roam_set_state(si
, ROAM_TRIGGER_SCAN_DONE
);
248 if (config
.roam_scan_tries
&&
249 si
->roam_tries
>= config
.roam_scan_tries
) {
250 usteer_roam_set_state(si
, ROAM_TRIGGER_WAIT_KICK
);
254 usteer_ubus_trigger_client_scan(si
);
255 usteer_roam_set_state(si
, ROAM_TRIGGER_SCAN
);
258 case ROAM_TRIGGER_IDLE
:
259 if (find_better_candidate(si
)) {
260 usteer_roam_set_state(si
, ROAM_TRIGGER_SCAN_DONE
);
264 usteer_roam_set_state(si
, ROAM_TRIGGER_SCAN
);
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
);
274 si_new
= find_better_candidate(si
);
278 usteer_roam_set_state(si
, ROAM_TRIGGER_WAIT_KICK
);
281 case ROAM_TRIGGER_WAIT_KICK
:
282 if (si
->signal
> min_signal
)
285 usteer_roam_set_state(si
, ROAM_TRIGGER_NOTIFY_KICK
);
286 usteer_ubus_notify_client_disassoc(si
);
288 case ROAM_TRIGGER_NOTIFY_KICK
:
289 if (current_time
- si
->roam_event
< config
.roam_kick_delay
* 100)
292 usteer_roam_set_state(si
, ROAM_TRIGGER_KICK
);
294 case ROAM_TRIGGER_KICK
:
295 usteer_ubus_kick_client(si
);
296 usteer_roam_set_state(si
, ROAM_TRIGGER_IDLE
);
304 usteer_local_node_roam_check(struct usteer_local_node
*ln
)
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
;
316 usteer_update_time();
317 min_signal
= snr_to_signal(&ln
->node
, min_signal
);
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
);
327 * If the state machine kicked a client, other clients should wait
328 * until the next turn
330 if (usteer_roam_trigger_sm(si
))
336 usteer_local_node_snr_kick(struct usteer_local_node
*ln
)
344 min_signal
= snr_to_signal(&ln
->node
, config
.min_snr
);
346 list_for_each_entry(si
, &ln
->node
.sta_info
, node_list
) {
350 if (si
->signal
>= min_signal
)
355 MSG(VERBOSE
, "Kicking client "MAC_ADDR_FMT
" due to low SNR, signal=%d\n",
356 MAC_ADDR_DATA(si
->sta
->addr
), si
->signal
);
358 usteer_ubus_kick_client(si
);
364 usteer_local_node_kick(struct usteer_local_node
*ln
)
366 struct usteer_node
*node
= &ln
->node
;
367 struct sta_info
*kick1
= NULL
, *kick2
= NULL
;
368 struct sta_info
*candidate
= NULL
;
371 usteer_local_node_roam_check(ln
);
372 usteer_local_node_snr_kick(ln
);
374 if (!config
.load_kick_enabled
|| !config
.load_kick_threshold
||
375 !config
.load_kick_delay
)
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;
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
);
393 MSG(VERBOSE
, "AP load threshold exceeded on %s (%d), try to kick a client\n",
394 usteer_node_name(node
), node
->load
);
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
);
405 list_for_each_entry(si
, &ln
->node
.sta_info
, node_list
) {
406 struct sta_info
*tmp
;
411 if (is_more_kickable(kick1
, si
))
414 tmp
= find_better_candidate(si
);
418 if (is_more_kickable(kick2
, si
)) {
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)");
435 usteer_ubus_kick_client(kick1
);