2 * ACS - Automatic Channel Selection module
3 * Copyright (c) 2011, Atheros Communications
4 * Copyright (c) 2013, Qualcomm Atheros, Inc.
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
10 #include "utils/includes.h"
13 #include "utils/common.h"
14 #include "utils/list.h"
15 #include "common/ieee802_11_defs.h"
16 #include "drivers/driver.h"
18 #include "ap_drv_ops.h"
19 #include "ap_config.h"
20 #include "hw_features.h"
24 * Automatic Channel Selection
25 * ===========================
29 * http://wireless.kernel.org/en/users/Documentation/acs
33 * - make sure you have CONFIG_ACS=y in hostapd's .config
34 * - use channel=0 or channel=acs to enable ACS
38 * 1. passive scans are used to collect survey data
39 * (it is assumed that scan trigger collection of survey data in driver)
40 * 2. interference factor is calculated for each channel
41 * 3. ideal channel is picked depending on channel width by using adjacent
42 * channel interference factors
46 * - Current implementation depends heavily on the amount of time willing to
47 * spend gathering survey data during hostapd startup. Short traffic bursts
48 * may be missed and a suboptimal channel may be picked.
49 * - Ideal channel may end up overlapping a channel with 40 MHz intolerant BSS
53 * - implement other interference computation methods
55 * - spectral scan based
56 * (should be possibly to hook this up with current ACS scans)
57 * - add wpa_supplicant support (for P2P)
58 * - collect a histogram of interference over time allowing more educated
59 * guess about an ideal channel (perhaps CSA could be used to migrate AP to a
60 * new "better" channel while running)
61 * - include neighboring BSS scan to avoid conflicts with 40 MHz intolerant BSSs
62 * when choosing the ideal channel
64 * Survey interference factor implementation details
65 * -------------------------------------------------
66 * Generic interference_factor in struct hostapd_channel_data is used.
68 * The survey interference factor is defined as the ratio of the
69 * observed busy time over the time we spent on the channel,
70 * this value is then amplified by the observed noise floor on
71 * the channel in comparison to the lowest noise floor observed
74 * This corresponds to:
76 * (busy time - tx time) / (active time - tx time) * 2^(chan_nf + band_min_nf)
79 * The coefficient of 2 reflects the way power in "far-field"
80 * radiation decreases as the square of distance from the antenna [1].
81 * What this does is it decreases the observed busy time ratio if the
82 * noise observed was low but increases it if the noise was high,
83 * proportionally to the way "far field" radiation changes over
86 * If channel busy time is not available the fallback is to use channel RX time.
88 * Since noise floor is in dBm it is necessary to convert it into Watts so that
89 * combined channel interference (e.g., HT40, which uses two channels) can be
92 * (busy time - tx time) / (active time - tx time) *
93 * 2^(10^(chan_nf/10) + 10^(band_min_nf/10))
96 * However to account for cases where busy/rx time is 0 (channel load is then
97 * 0%) channel noise floor signal power is combined into the equation so a
98 * channel with lower noise floor is preferred. The equation becomes:
100 * 10^(chan_nf/5) + (busy time - tx time) / (active time - tx time) *
101 * 2^(10^(chan_nf/10) + 10^(band_min_nf/10))
104 * All this "interference factor" is purely subjective and only time
105 * will tell how usable this is. By using the minimum noise floor we
106 * remove any possible issues due to card calibration. The computation
107 * of the interference factor then is dependent on what the card itself
108 * picks up as the minimum noise, not an actual real possible card
111 * Total interference computation details
112 * --------------------------------------
113 * The above channel interference factor is calculated with no respect to
114 * target operational bandwidth.
116 * To find an ideal channel the above data is combined by taking into account
117 * the target operational bandwidth and selected band. E.g., on 2.4 GHz channels
118 * overlap with 20 MHz bandwidth, but there is no overlap for 20 MHz bandwidth
121 * Each valid and possible channel spec (i.e., channel + width) is taken and its
122 * interference factor is computed by summing up interferences of each channel
123 * it overlaps. The one with least total interference is picked up.
125 * Note: This implies base channel interference factor must be non-negative
126 * allowing easy summing up.
128 * Example ACS analysis printout
129 * -----------------------------
131 * ACS: Trying survey-based ACS
132 * ACS: Survey analysis for channel 1 (2412 MHz)
133 * ACS: 1: min_nf=-113 interference_factor=0.0802469 nf=-113 time=162 busy=0 rx=13
134 * ACS: 2: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12
135 * ACS: 3: min_nf=-113 interference_factor=0.0679012 nf=-113 time=162 busy=0 rx=11
136 * ACS: 4: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5
137 * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4
138 * ACS: * interference factor average: 0.0557166
139 * ACS: Survey analysis for channel 2 (2417 MHz)
140 * ACS: 1: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3
141 * ACS: 2: min_nf=-113 interference_factor=0.0246914 nf=-113 time=162 busy=0 rx=4
142 * ACS: 3: min_nf=-113 interference_factor=0.037037 nf=-113 time=162 busy=0 rx=6
143 * ACS: 4: min_nf=-113 interference_factor=0.149068 nf=-113 time=161 busy=0 rx=24
144 * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4
145 * ACS: * interference factor average: 0.050832
146 * ACS: Survey analysis for channel 3 (2422 MHz)
147 * ACS: 1: min_nf=-113 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
148 * ACS: 2: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3
149 * ACS: 3: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
150 * ACS: 4: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
151 * ACS: 5: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
152 * ACS: * interference factor average: 0.0148838
153 * ACS: Survey analysis for channel 4 (2427 MHz)
154 * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
155 * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9
156 * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
157 * ACS: 4: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3
158 * ACS: 5: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
159 * ACS: * interference factor average: 0.0160801
160 * ACS: Survey analysis for channel 5 (2432 MHz)
161 * ACS: 1: min_nf=-114 interference_factor=0.409938 nf=-113 time=161 busy=0 rx=66
162 * ACS: 2: min_nf=-114 interference_factor=0.0432099 nf=-113 time=162 busy=0 rx=7
163 * ACS: 3: min_nf=-114 interference_factor=0.0124224 nf=-113 time=161 busy=0 rx=2
164 * ACS: 4: min_nf=-114 interference_factor=0.677019 nf=-113 time=161 busy=0 rx=109
165 * ACS: 5: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3
166 * ACS: * interference factor average: 0.232244
167 * ACS: Survey analysis for channel 6 (2437 MHz)
168 * ACS: 1: min_nf=-113 interference_factor=0.552795 nf=-113 time=161 busy=0 rx=89
169 * ACS: 2: min_nf=-113 interference_factor=0.0807453 nf=-112 time=161 busy=0 rx=13
170 * ACS: 3: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5
171 * ACS: 4: min_nf=-113 interference_factor=0.434783 nf=-112 time=161 busy=0 rx=70
172 * ACS: 5: min_nf=-113 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10
173 * ACS: * interference factor average: 0.232298
174 * ACS: Survey analysis for channel 7 (2442 MHz)
175 * ACS: 1: min_nf=-113 interference_factor=0.440994 nf=-112 time=161 busy=0 rx=71
176 * ACS: 2: min_nf=-113 interference_factor=0.385093 nf=-113 time=161 busy=0 rx=62
177 * ACS: 3: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
178 * ACS: 4: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
179 * ACS: 5: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12
180 * ACS: * interference factor average: 0.195031
181 * ACS: Survey analysis for channel 8 (2447 MHz)
182 * ACS: 1: min_nf=-114 interference_factor=0.0496894 nf=-112 time=161 busy=0 rx=8
183 * ACS: 2: min_nf=-114 interference_factor=0.0496894 nf=-114 time=161 busy=0 rx=8
184 * ACS: 3: min_nf=-114 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
185 * ACS: 4: min_nf=-114 interference_factor=0.12963 nf=-113 time=162 busy=0 rx=21
186 * ACS: 5: min_nf=-114 interference_factor=0.166667 nf=-114 time=162 busy=0 rx=27
187 * ACS: * interference factor average: 0.0865885
188 * ACS: Survey analysis for channel 9 (2452 MHz)
189 * ACS: 1: min_nf=-114 interference_factor=0.0124224 nf=-114 time=161 busy=0 rx=2
190 * ACS: 2: min_nf=-114 interference_factor=0.0310559 nf=-114 time=161 busy=0 rx=5
191 * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
192 * ACS: 4: min_nf=-114 interference_factor=0.00617284 nf=-114 time=162 busy=0 rx=1
193 * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
194 * ACS: * interference factor average: 0.00993022
195 * ACS: Survey analysis for channel 10 (2457 MHz)
196 * ACS: 1: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
197 * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
198 * ACS: 3: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
199 * ACS: 4: min_nf=-114 interference_factor=0.0493827 nf=-114 time=162 busy=0 rx=8
200 * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
201 * ACS: * interference factor average: 0.0136033
202 * ACS: Survey analysis for channel 11 (2462 MHz)
203 * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
204 * ACS: 2: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0
205 * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0
206 * ACS: 4: min_nf=-114 interference_factor=0.0432099 nf=-114 time=162 busy=0 rx=7
207 * ACS: 5: min_nf=-114 interference_factor=0.0925926 nf=-114 time=162 busy=0 rx=15
208 * ACS: * interference factor average: 0.0271605
209 * ACS: Survey analysis for channel 12 (2467 MHz)
210 * ACS: 1: min_nf=-114 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10
211 * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
212 * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
213 * ACS: 4: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
214 * ACS: 5: min_nf=-114 interference_factor=0.00617284 nf=-113 time=162 busy=0 rx=1
215 * ACS: * interference factor average: 0.0148992
216 * ACS: Survey analysis for channel 13 (2472 MHz)
217 * ACS: 1: min_nf=-114 interference_factor=0.0745342 nf=-114 time=161 busy=0 rx=12
218 * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9
219 * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
220 * ACS: 4: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
221 * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
222 * ACS: * interference factor average: 0.0260179
223 * ACS: Survey analysis for selected bandwidth 20MHz
224 * ACS: * channel 1: total interference = 0.121432
225 * ACS: * channel 2: total interference = 0.137512
226 * ACS: * channel 3: total interference = 0.369757
227 * ACS: * channel 4: total interference = 0.546338
228 * ACS: * channel 5: total interference = 0.690538
229 * ACS: * channel 6: total interference = 0.762242
230 * ACS: * channel 7: total interference = 0.756092
231 * ACS: * channel 8: total interference = 0.537451
232 * ACS: * channel 9: total interference = 0.332313
233 * ACS: * channel 10: total interference = 0.152182
234 * ACS: * channel 11: total interference = 0.0916111
235 * ACS: * channel 12: total interference = 0.0816809
236 * ACS: * channel 13: total interference = 0.0680776
237 * ACS: Ideal channel is 13 (2472 MHz) with total interference factor of 0.0680776
239 * [1] http://en.wikipedia.org/wiki/Near_and_far_field
243 static int acs_request_scan(struct hostapd_iface *iface);
246 static void acs_clean_chan_surveys(struct hostapd_channel_data *chan)
248 struct freq_survey *survey, *tmp;
250 if (dl_list_empty(&chan->survey_list))
253 dl_list_for_each_safe(survey, tmp, &chan->survey_list,
254 struct freq_survey, list) {
255 dl_list_del(&survey->list);
261 static void acs_cleanup(struct hostapd_iface *iface)
264 struct hostapd_channel_data *chan;
266 for (i = 0; i < iface->current_mode->num_channels; i++) {
267 chan = &iface->current_mode->channels[i];
269 if (chan->flag & HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED)
270 acs_clean_chan_surveys(chan);
272 dl_list_init(&chan->survey_list);
273 chan->flag |= HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED;
277 iface->chans_surveyed = 0;
278 iface->acs_num_completed_scans = 0;
282 void acs_fail(struct hostapd_iface *iface)
284 wpa_printf(MSG_ERROR, "ACS: Failed to start");
290 acs_survey_interference_factor(struct freq_survey *survey, s8 min_nf)
292 long double factor, busy, total;
294 if (survey->filled & SURVEY_HAS_CHAN_TIME_BUSY)
295 busy = survey->channel_time_busy;
296 else if (survey->filled & SURVEY_HAS_CHAN_TIME_RX)
297 busy = survey->channel_time_rx;
299 /* This shouldn't really happen as survey data is checked in
300 * acs_sanity_check() */
301 wpa_printf(MSG_ERROR, "ACS: Survey data missing");
305 total = survey->channel_time;
307 if (survey->filled & SURVEY_HAS_CHAN_TIME_TX) {
308 busy -= survey->channel_time_tx;
309 total -= survey->channel_time_tx;
312 /* TODO: figure out the best multiplier for noise floor base */
313 factor = pow(10, survey->nf / 5.0L) +
315 pow(2, pow(10, (long double) survey->nf / 10.0L) -
316 pow(10, (long double) min_nf / 10.0L));
323 acs_survey_chan_interference_factor(struct hostapd_iface *iface,
324 struct hostapd_channel_data *chan)
326 struct freq_survey *survey;
328 long double int_factor = 0;
330 if (dl_list_empty(&chan->survey_list))
333 if (chan->flag & HOSTAPD_CHAN_DISABLED)
336 chan->interference_factor = 0;
338 dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
340 int_factor = acs_survey_interference_factor(survey,
342 chan->interference_factor += int_factor;
343 wpa_printf(MSG_DEBUG, "ACS: %d: min_nf=%d interference_factor=%Lg nf=%d time=%lu busy=%lu rx=%lu",
344 ++i, chan->min_nf, int_factor,
345 survey->nf, (unsigned long) survey->channel_time,
346 (unsigned long) survey->channel_time_busy,
347 (unsigned long) survey->channel_time_rx);
350 chan->interference_factor = chan->interference_factor /
351 dl_list_len(&chan->survey_list);
355 static int acs_usable_ht40_chan(struct hostapd_channel_data *chan)
357 const int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149,
361 for (i = 0; i < ARRAY_SIZE(allowed); i++)
362 if (chan->chan == allowed[i])
369 static int acs_survey_is_sufficient(struct freq_survey *survey)
371 if (!(survey->filled & SURVEY_HAS_NF)) {
372 wpa_printf(MSG_ERROR, "ACS: Survey is missing noise floor");
376 if (!(survey->filled & SURVEY_HAS_CHAN_TIME)) {
377 wpa_printf(MSG_ERROR, "ACS: Survey is missing channel time");
381 if (!(survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) &&
382 !(survey->filled & SURVEY_HAS_CHAN_TIME_RX)) {
383 wpa_printf(MSG_ERROR, "ACS: Survey is missing RX and busy time (at least one is required)");
391 static int acs_survey_list_is_sufficient(struct hostapd_channel_data *chan)
393 struct freq_survey *survey;
395 dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
397 if (!acs_survey_is_sufficient(survey)) {
398 wpa_printf(MSG_ERROR, "ACS: Channel %d has insufficient survey data",
409 static int acs_surveys_are_sufficient(struct hostapd_iface *iface)
412 struct hostapd_channel_data *chan;
415 for (i = 0; i < iface->current_mode->num_channels; i++) {
416 chan = &iface->current_mode->channels[i];
417 if (chan->flag & HOSTAPD_CHAN_DISABLED)
420 if (!acs_survey_list_is_sufficient(chan))
426 /* We need at least survey data for one channel */
431 static int acs_usable_chan(struct hostapd_channel_data *chan)
433 if (dl_list_empty(&chan->survey_list))
435 if (chan->flag & HOSTAPD_CHAN_DISABLED)
437 if (!acs_survey_list_is_sufficient(chan))
443 static void acs_survey_all_chans_intereference_factor(
444 struct hostapd_iface *iface)
447 struct hostapd_channel_data *chan;
449 for (i = 0; i < iface->current_mode->num_channels; i++) {
450 chan = &iface->current_mode->channels[i];
452 if (!acs_usable_chan(chan))
455 wpa_printf(MSG_DEBUG, "ACS: Survey analysis for channel %d (%d MHz)",
456 chan->chan, chan->freq);
458 acs_survey_chan_interference_factor(iface, chan);
460 wpa_printf(MSG_DEBUG, "ACS: * interference factor average: %Lg",
461 chan->interference_factor);
466 static struct hostapd_channel_data *acs_find_chan(struct hostapd_iface *iface,
469 struct hostapd_channel_data *chan;
472 for (i = 0; i < iface->current_mode->num_channels; i++) {
473 chan = &iface->current_mode->channels[i];
475 if (chan->flag & HOSTAPD_CHAN_DISABLED)
478 if (chan->freq == freq)
487 * At this point it's assumed chan->interface_factor has been computed.
488 * This function should be reusable regardless of interference computation
489 * option (survey, BSS, spectral, ...). chan->interference factor must be
490 * summable (i.e., must be always greater than zero).
492 static struct hostapd_channel_data *
493 acs_find_ideal_chan(struct hostapd_iface *iface)
495 struct hostapd_channel_data *chan, *adj_chan, *ideal_chan = NULL,
497 long double factor, ideal_factor = 0;
501 /* TODO: HT40- support */
503 if (iface->conf->ieee80211n &&
504 iface->conf->secondary_channel == -1) {
505 wpa_printf(MSG_ERROR, "ACS: HT40- is not supported yet. Please try HT40+");
509 if (iface->conf->ieee80211n &&
510 iface->conf->secondary_channel)
513 if (iface->conf->ieee80211ac &&
514 iface->conf->vht_oper_chwidth == 1)
517 /* TODO: VHT80+80, VHT160. Update acs_adjust_vht_center_freq() too. */
519 wpa_printf(MSG_DEBUG, "ACS: Survey analysis for selected bandwidth %d MHz",
525 for (i = 0; i < iface->current_mode->num_channels; i++) {
526 chan = &iface->current_mode->channels[i];
528 if (chan->flag & HOSTAPD_CHAN_DISABLED)
532 /* HT40 on 5 GHz has a limited set of primary channels as per
534 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
535 iface->conf->ieee80211n &&
536 iface->conf->secondary_channel &&
537 !acs_usable_ht40_chan(chan)) {
538 wpa_printf(MSG_DEBUG, "ACS: Channel %d: not allowed as primary channel for HT40",
544 if (acs_usable_chan(chan))
545 factor = chan->interference_factor;
547 for (j = 1; j < n_chans; j++) {
548 adj_chan = acs_find_chan(iface, chan->freq + (j * 20));
552 if (acs_usable_chan(adj_chan))
553 factor += adj_chan->interference_factor;
557 wpa_printf(MSG_DEBUG, "ACS: Channel %d: not enough bandwidth",
562 /* 2.4 GHz has overlapping 20 MHz channels. Include adjacent
563 * channel interference factor. */
564 if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211B ||
565 iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G) {
566 for (j = 0; j < n_chans; j++) {
567 /* TODO: perhaps a multiplier should be used
570 adj_chan = acs_find_chan(iface, chan->freq +
572 if (adj_chan && acs_usable_chan(adj_chan))
573 factor += adj_chan->interference_factor;
575 adj_chan = acs_find_chan(iface, chan->freq +
577 if (adj_chan && acs_usable_chan(adj_chan))
578 factor += adj_chan->interference_factor;
580 adj_chan = acs_find_chan(iface, chan->freq +
582 if (adj_chan && acs_usable_chan(adj_chan))
583 factor += adj_chan->interference_factor;
585 adj_chan = acs_find_chan(iface, chan->freq +
587 if (adj_chan && acs_usable_chan(adj_chan))
588 factor += adj_chan->interference_factor;
592 wpa_printf(MSG_DEBUG, "ACS: * channel %d: total interference = %Lg",
595 if (acs_usable_chan(chan) &&
596 (!ideal_chan || factor < ideal_factor)) {
597 ideal_factor = factor;
601 /* This channel would at least be usable */
607 wpa_printf(MSG_DEBUG, "ACS: Ideal channel is %d (%d MHz) with total interference factor of %Lg",
608 ideal_chan->chan, ideal_chan->freq, ideal_factor);
616 static void acs_adjust_vht_center_freq(struct hostapd_iface *iface)
618 wpa_printf(MSG_DEBUG, "ACS: Adjusting VHT center frequency");
620 switch (iface->conf->vht_oper_chwidth) {
621 case VHT_CHANWIDTH_USE_HT:
622 iface->conf->vht_oper_centr_freq_seg0_idx =
623 iface->conf->channel + 2;
625 case VHT_CHANWIDTH_80MHZ:
626 iface->conf->vht_oper_centr_freq_seg0_idx =
627 iface->conf->channel + 6;
630 /* TODO: How can this be calculated? Adjust
631 * acs_find_ideal_chan() */
632 wpa_printf(MSG_INFO, "ACS: Only VHT20/40/80 is supported now");
638 static int acs_study_survey_based(struct hostapd_iface *iface)
640 wpa_printf(MSG_DEBUG, "ACS: Trying survey-based ACS");
642 if (!iface->chans_surveyed) {
643 wpa_printf(MSG_ERROR, "ACS: Unable to collect survey data");
647 if (!acs_surveys_are_sufficient(iface)) {
648 wpa_printf(MSG_ERROR, "ACS: Surveys have insufficient data");
652 acs_survey_all_chans_intereference_factor(iface);
657 static int acs_study_options(struct hostapd_iface *iface)
661 err = acs_study_survey_based(iface);
665 /* TODO: If no surveys are available/sufficient this is a good
666 * place to fallback to BSS-based ACS */
672 static void acs_study(struct hostapd_iface *iface)
674 struct hostapd_channel_data *ideal_chan;
677 err = acs_study_options(iface);
679 wpa_printf(MSG_ERROR, "ACS: All study options have failed");
683 ideal_chan = acs_find_ideal_chan(iface);
685 wpa_printf(MSG_ERROR, "ACS: Failed to compute ideal channel");
690 iface->conf->channel = ideal_chan->chan;
692 if (iface->conf->ieee80211ac)
693 acs_adjust_vht_center_freq(iface);
698 * hostapd_setup_interface_complete() will return -1 on failure,
699 * 0 on success and 0 is HOSTAPD_CHAN_VALID :)
701 if (hostapd_acs_completed(iface, err) == HOSTAPD_CHAN_VALID) {
706 /* This can possibly happen if channel parameters (secondary
707 * channel, center frequencies) are misconfigured */
708 wpa_printf(MSG_ERROR, "ACS: Possibly channel configuration is invalid, please report this along with your config file.");
713 static void acs_scan_complete(struct hostapd_iface *iface)
717 iface->scan_cb = NULL;
719 wpa_printf(MSG_DEBUG, "ACS: Using survey based algorithm (acs_num_scans=%d)",
720 iface->conf->acs_num_scans);
722 err = hostapd_drv_get_survey(iface->bss[0], 0);
724 wpa_printf(MSG_ERROR, "ACS: Failed to get survey data");
728 if (++iface->acs_num_completed_scans < iface->conf->acs_num_scans) {
729 err = acs_request_scan(iface);
731 wpa_printf(MSG_ERROR, "ACS: Failed to request scan");
741 hostapd_acs_completed(iface, 1);
746 static int acs_request_scan(struct hostapd_iface *iface)
748 struct wpa_driver_scan_params params;
749 struct hostapd_channel_data *chan;
752 os_memset(¶ms, 0, sizeof(params));
753 params.freqs = os_calloc(iface->current_mode->num_channels + 1,
754 sizeof(params.freqs[0]));
755 if (params.freqs == NULL)
759 for (i = 0; i < iface->current_mode->num_channels; i++) {
760 chan = &iface->current_mode->channels[i];
761 if (chan->flag & HOSTAPD_CHAN_DISABLED)
764 *freq++ = chan->freq;
768 iface->scan_cb = acs_scan_complete;
770 wpa_printf(MSG_DEBUG, "ACS: Scanning %d / %d",
771 iface->acs_num_completed_scans + 1,
772 iface->conf->acs_num_scans);
774 if (hostapd_driver_scan(iface->bss[0], ¶ms) < 0) {
775 wpa_printf(MSG_ERROR, "ACS: Failed to request initial scan");
780 os_free(params.freqs);
785 enum hostapd_chan_status acs_init(struct hostapd_iface *iface)
789 wpa_printf(MSG_INFO, "ACS: Automatic channel selection started, this may take a bit");
793 err = acs_request_scan(iface);
795 return HOSTAPD_CHAN_INVALID;
797 return HOSTAPD_CHAN_ACS;