Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ACS - Automatic Channel Selection module |
| 3 | * Copyright (c) 2011, Atheros Communications |
| 4 | * Copyright (c) 2013, Qualcomm Atheros, Inc. |
| 5 | * |
| 6 | * This software may be distributed under the terms of the BSD license. |
| 7 | * See README for more details. |
| 8 | */ |
| 9 | |
| 10 | #include "utils/includes.h" |
| 11 | #include <math.h> |
| 12 | |
| 13 | #include "utils/common.h" |
| 14 | #include "utils/list.h" |
| 15 | #include "common/ieee802_11_defs.h" |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 16 | #include "common/hw_features_common.h" |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 17 | #include "common/wpa_ctrl.h" |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 18 | #include "drivers/driver.h" |
| 19 | #include "hostapd.h" |
| 20 | #include "ap_drv_ops.h" |
| 21 | #include "ap_config.h" |
| 22 | #include "hw_features.h" |
| 23 | #include "acs.h" |
| 24 | |
| 25 | /* |
| 26 | * Automatic Channel Selection |
| 27 | * =========================== |
| 28 | * |
| 29 | * More info at |
| 30 | * ------------ |
| 31 | * http://wireless.kernel.org/en/users/Documentation/acs |
| 32 | * |
| 33 | * How to use |
| 34 | * ---------- |
| 35 | * - make sure you have CONFIG_ACS=y in hostapd's .config |
| 36 | * - use channel=0 or channel=acs to enable ACS |
| 37 | * |
| 38 | * How does it work |
| 39 | * ---------------- |
| 40 | * 1. passive scans are used to collect survey data |
| 41 | * (it is assumed that scan trigger collection of survey data in driver) |
| 42 | * 2. interference factor is calculated for each channel |
| 43 | * 3. ideal channel is picked depending on channel width by using adjacent |
| 44 | * channel interference factors |
| 45 | * |
| 46 | * Known limitations |
| 47 | * ----------------- |
| 48 | * - Current implementation depends heavily on the amount of time willing to |
| 49 | * spend gathering survey data during hostapd startup. Short traffic bursts |
| 50 | * may be missed and a suboptimal channel may be picked. |
| 51 | * - Ideal channel may end up overlapping a channel with 40 MHz intolerant BSS |
| 52 | * |
| 53 | * Todo / Ideas |
| 54 | * ------------ |
| 55 | * - implement other interference computation methods |
| 56 | * - BSS/RSSI based |
| 57 | * - spectral scan based |
| 58 | * (should be possibly to hook this up with current ACS scans) |
| 59 | * - add wpa_supplicant support (for P2P) |
| 60 | * - collect a histogram of interference over time allowing more educated |
| 61 | * guess about an ideal channel (perhaps CSA could be used to migrate AP to a |
| 62 | * new "better" channel while running) |
| 63 | * - include neighboring BSS scan to avoid conflicts with 40 MHz intolerant BSSs |
| 64 | * when choosing the ideal channel |
| 65 | * |
| 66 | * Survey interference factor implementation details |
| 67 | * ------------------------------------------------- |
| 68 | * Generic interference_factor in struct hostapd_channel_data is used. |
| 69 | * |
| 70 | * The survey interference factor is defined as the ratio of the |
| 71 | * observed busy time over the time we spent on the channel, |
| 72 | * this value is then amplified by the observed noise floor on |
| 73 | * the channel in comparison to the lowest noise floor observed |
| 74 | * on the entire band. |
| 75 | * |
| 76 | * This corresponds to: |
| 77 | * --- |
| 78 | * (busy time - tx time) / (active time - tx time) * 2^(chan_nf + band_min_nf) |
| 79 | * --- |
| 80 | * |
| 81 | * The coefficient of 2 reflects the way power in "far-field" |
| 82 | * radiation decreases as the square of distance from the antenna [1]. |
| 83 | * What this does is it decreases the observed busy time ratio if the |
| 84 | * noise observed was low but increases it if the noise was high, |
| 85 | * proportionally to the way "far field" radiation changes over |
| 86 | * distance. |
| 87 | * |
| 88 | * If channel busy time is not available the fallback is to use channel RX time. |
| 89 | * |
| 90 | * Since noise floor is in dBm it is necessary to convert it into Watts so that |
| 91 | * combined channel interference (e.g., HT40, which uses two channels) can be |
| 92 | * calculated easily. |
| 93 | * --- |
| 94 | * (busy time - tx time) / (active time - tx time) * |
| 95 | * 2^(10^(chan_nf/10) + 10^(band_min_nf/10)) |
| 96 | * --- |
| 97 | * |
| 98 | * However to account for cases where busy/rx time is 0 (channel load is then |
| 99 | * 0%) channel noise floor signal power is combined into the equation so a |
| 100 | * channel with lower noise floor is preferred. The equation becomes: |
| 101 | * --- |
| 102 | * 10^(chan_nf/5) + (busy time - tx time) / (active time - tx time) * |
| 103 | * 2^(10^(chan_nf/10) + 10^(band_min_nf/10)) |
| 104 | * --- |
| 105 | * |
| 106 | * All this "interference factor" is purely subjective and only time |
| 107 | * will tell how usable this is. By using the minimum noise floor we |
| 108 | * remove any possible issues due to card calibration. The computation |
| 109 | * of the interference factor then is dependent on what the card itself |
| 110 | * picks up as the minimum noise, not an actual real possible card |
| 111 | * noise value. |
| 112 | * |
| 113 | * Total interference computation details |
| 114 | * -------------------------------------- |
| 115 | * The above channel interference factor is calculated with no respect to |
| 116 | * target operational bandwidth. |
| 117 | * |
| 118 | * To find an ideal channel the above data is combined by taking into account |
| 119 | * the target operational bandwidth and selected band. E.g., on 2.4 GHz channels |
| 120 | * overlap with 20 MHz bandwidth, but there is no overlap for 20 MHz bandwidth |
| 121 | * on 5 GHz. |
| 122 | * |
| 123 | * Each valid and possible channel spec (i.e., channel + width) is taken and its |
| 124 | * interference factor is computed by summing up interferences of each channel |
| 125 | * it overlaps. The one with least total interference is picked up. |
| 126 | * |
| 127 | * Note: This implies base channel interference factor must be non-negative |
| 128 | * allowing easy summing up. |
| 129 | * |
| 130 | * Example ACS analysis printout |
| 131 | * ----------------------------- |
| 132 | * |
| 133 | * ACS: Trying survey-based ACS |
| 134 | * ACS: Survey analysis for channel 1 (2412 MHz) |
| 135 | * ACS: 1: min_nf=-113 interference_factor=0.0802469 nf=-113 time=162 busy=0 rx=13 |
| 136 | * ACS: 2: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12 |
| 137 | * ACS: 3: min_nf=-113 interference_factor=0.0679012 nf=-113 time=162 busy=0 rx=11 |
| 138 | * ACS: 4: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5 |
| 139 | * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4 |
| 140 | * ACS: * interference factor average: 0.0557166 |
| 141 | * ACS: Survey analysis for channel 2 (2417 MHz) |
| 142 | * ACS: 1: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3 |
| 143 | * ACS: 2: min_nf=-113 interference_factor=0.0246914 nf=-113 time=162 busy=0 rx=4 |
| 144 | * ACS: 3: min_nf=-113 interference_factor=0.037037 nf=-113 time=162 busy=0 rx=6 |
| 145 | * ACS: 4: min_nf=-113 interference_factor=0.149068 nf=-113 time=161 busy=0 rx=24 |
| 146 | * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4 |
| 147 | * ACS: * interference factor average: 0.050832 |
| 148 | * ACS: Survey analysis for channel 3 (2422 MHz) |
| 149 | * ACS: 1: min_nf=-113 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0 |
| 150 | * ACS: 2: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3 |
| 151 | * ACS: 3: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3 |
| 152 | * ACS: 4: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3 |
| 153 | * ACS: 5: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3 |
| 154 | * ACS: * interference factor average: 0.0148838 |
| 155 | * ACS: Survey analysis for channel 4 (2427 MHz) |
| 156 | * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 |
| 157 | * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9 |
| 158 | * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0 |
| 159 | * ACS: 4: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3 |
| 160 | * ACS: 5: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1 |
| 161 | * ACS: * interference factor average: 0.0160801 |
| 162 | * ACS: Survey analysis for channel 5 (2432 MHz) |
| 163 | * ACS: 1: min_nf=-114 interference_factor=0.409938 nf=-113 time=161 busy=0 rx=66 |
| 164 | * ACS: 2: min_nf=-114 interference_factor=0.0432099 nf=-113 time=162 busy=0 rx=7 |
| 165 | * ACS: 3: min_nf=-114 interference_factor=0.0124224 nf=-113 time=161 busy=0 rx=2 |
| 166 | * ACS: 4: min_nf=-114 interference_factor=0.677019 nf=-113 time=161 busy=0 rx=109 |
| 167 | * ACS: 5: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3 |
| 168 | * ACS: * interference factor average: 0.232244 |
| 169 | * ACS: Survey analysis for channel 6 (2437 MHz) |
| 170 | * ACS: 1: min_nf=-113 interference_factor=0.552795 nf=-113 time=161 busy=0 rx=89 |
| 171 | * ACS: 2: min_nf=-113 interference_factor=0.0807453 nf=-112 time=161 busy=0 rx=13 |
| 172 | * ACS: 3: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5 |
| 173 | * ACS: 4: min_nf=-113 interference_factor=0.434783 nf=-112 time=161 busy=0 rx=70 |
| 174 | * ACS: 5: min_nf=-113 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10 |
| 175 | * ACS: * interference factor average: 0.232298 |
| 176 | * ACS: Survey analysis for channel 7 (2442 MHz) |
| 177 | * ACS: 1: min_nf=-113 interference_factor=0.440994 nf=-112 time=161 busy=0 rx=71 |
| 178 | * ACS: 2: min_nf=-113 interference_factor=0.385093 nf=-113 time=161 busy=0 rx=62 |
| 179 | * ACS: 3: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6 |
| 180 | * ACS: 4: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6 |
| 181 | * ACS: 5: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12 |
| 182 | * ACS: * interference factor average: 0.195031 |
| 183 | * ACS: Survey analysis for channel 8 (2447 MHz) |
| 184 | * ACS: 1: min_nf=-114 interference_factor=0.0496894 nf=-112 time=161 busy=0 rx=8 |
| 185 | * ACS: 2: min_nf=-114 interference_factor=0.0496894 nf=-114 time=161 busy=0 rx=8 |
| 186 | * ACS: 3: min_nf=-114 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6 |
| 187 | * ACS: 4: min_nf=-114 interference_factor=0.12963 nf=-113 time=162 busy=0 rx=21 |
| 188 | * ACS: 5: min_nf=-114 interference_factor=0.166667 nf=-114 time=162 busy=0 rx=27 |
| 189 | * ACS: * interference factor average: 0.0865885 |
| 190 | * ACS: Survey analysis for channel 9 (2452 MHz) |
| 191 | * ACS: 1: min_nf=-114 interference_factor=0.0124224 nf=-114 time=161 busy=0 rx=2 |
| 192 | * ACS: 2: min_nf=-114 interference_factor=0.0310559 nf=-114 time=161 busy=0 rx=5 |
| 193 | * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0 |
| 194 | * ACS: 4: min_nf=-114 interference_factor=0.00617284 nf=-114 time=162 busy=0 rx=1 |
| 195 | * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 |
| 196 | * ACS: * interference factor average: 0.00993022 |
| 197 | * ACS: Survey analysis for channel 10 (2457 MHz) |
| 198 | * ACS: 1: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1 |
| 199 | * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1 |
| 200 | * ACS: 3: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1 |
| 201 | * ACS: 4: min_nf=-114 interference_factor=0.0493827 nf=-114 time=162 busy=0 rx=8 |
| 202 | * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 |
| 203 | * ACS: * interference factor average: 0.0136033 |
| 204 | * ACS: Survey analysis for channel 11 (2462 MHz) |
| 205 | * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0 |
| 206 | * ACS: 2: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0 |
| 207 | * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0 |
| 208 | * ACS: 4: min_nf=-114 interference_factor=0.0432099 nf=-114 time=162 busy=0 rx=7 |
| 209 | * ACS: 5: min_nf=-114 interference_factor=0.0925926 nf=-114 time=162 busy=0 rx=15 |
| 210 | * ACS: * interference factor average: 0.0271605 |
| 211 | * ACS: Survey analysis for channel 12 (2467 MHz) |
| 212 | * ACS: 1: min_nf=-114 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10 |
| 213 | * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1 |
| 214 | * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0 |
| 215 | * ACS: 4: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0 |
| 216 | * ACS: 5: min_nf=-114 interference_factor=0.00617284 nf=-113 time=162 busy=0 rx=1 |
| 217 | * ACS: * interference factor average: 0.0148992 |
| 218 | * ACS: Survey analysis for channel 13 (2472 MHz) |
| 219 | * ACS: 1: min_nf=-114 interference_factor=0.0745342 nf=-114 time=161 busy=0 rx=12 |
| 220 | * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9 |
| 221 | * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 |
| 222 | * ACS: 4: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 |
| 223 | * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0 |
| 224 | * ACS: * interference factor average: 0.0260179 |
| 225 | * ACS: Survey analysis for selected bandwidth 20MHz |
| 226 | * ACS: * channel 1: total interference = 0.121432 |
| 227 | * ACS: * channel 2: total interference = 0.137512 |
| 228 | * ACS: * channel 3: total interference = 0.369757 |
| 229 | * ACS: * channel 4: total interference = 0.546338 |
| 230 | * ACS: * channel 5: total interference = 0.690538 |
| 231 | * ACS: * channel 6: total interference = 0.762242 |
| 232 | * ACS: * channel 7: total interference = 0.756092 |
| 233 | * ACS: * channel 8: total interference = 0.537451 |
| 234 | * ACS: * channel 9: total interference = 0.332313 |
| 235 | * ACS: * channel 10: total interference = 0.152182 |
| 236 | * ACS: * channel 11: total interference = 0.0916111 |
| 237 | * ACS: * channel 12: total interference = 0.0816809 |
| 238 | * ACS: * channel 13: total interference = 0.0680776 |
| 239 | * ACS: Ideal channel is 13 (2472 MHz) with total interference factor of 0.0680776 |
| 240 | * |
| 241 | * [1] http://en.wikipedia.org/wiki/Near_and_far_field |
| 242 | */ |
| 243 | |
| 244 | |
| 245 | static int acs_request_scan(struct hostapd_iface *iface); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 246 | static int acs_survey_is_sufficient(struct freq_survey *survey); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 247 | |
| 248 | |
| 249 | static void acs_clean_chan_surveys(struct hostapd_channel_data *chan) |
| 250 | { |
| 251 | struct freq_survey *survey, *tmp; |
| 252 | |
| 253 | if (dl_list_empty(&chan->survey_list)) |
| 254 | return; |
| 255 | |
| 256 | dl_list_for_each_safe(survey, tmp, &chan->survey_list, |
| 257 | struct freq_survey, list) { |
| 258 | dl_list_del(&survey->list); |
| 259 | os_free(survey); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 264 | void acs_cleanup(struct hostapd_iface *iface) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 265 | { |
| 266 | int i; |
| 267 | struct hostapd_channel_data *chan; |
| 268 | |
| 269 | for (i = 0; i < iface->current_mode->num_channels; i++) { |
| 270 | chan = &iface->current_mode->channels[i]; |
| 271 | |
| 272 | if (chan->flag & HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED) |
| 273 | acs_clean_chan_surveys(chan); |
| 274 | |
| 275 | dl_list_init(&chan->survey_list); |
| 276 | chan->flag |= HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED; |
| 277 | chan->min_nf = 0; |
| 278 | } |
| 279 | |
| 280 | iface->chans_surveyed = 0; |
| 281 | iface->acs_num_completed_scans = 0; |
| 282 | } |
| 283 | |
| 284 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 285 | static void acs_fail(struct hostapd_iface *iface) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 286 | { |
| 287 | wpa_printf(MSG_ERROR, "ACS: Failed to start"); |
| 288 | acs_cleanup(iface); |
Dmitry Shmidt | 2ac5f60 | 2014-03-07 10:08:21 -0800 | [diff] [blame] | 289 | hostapd_disable_iface(iface); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
| 293 | static long double |
| 294 | acs_survey_interference_factor(struct freq_survey *survey, s8 min_nf) |
| 295 | { |
| 296 | long double factor, busy, total; |
| 297 | |
| 298 | if (survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) |
| 299 | busy = survey->channel_time_busy; |
| 300 | else if (survey->filled & SURVEY_HAS_CHAN_TIME_RX) |
| 301 | busy = survey->channel_time_rx; |
| 302 | else { |
| 303 | /* This shouldn't really happen as survey data is checked in |
| 304 | * acs_sanity_check() */ |
| 305 | wpa_printf(MSG_ERROR, "ACS: Survey data missing"); |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | total = survey->channel_time; |
| 310 | |
| 311 | if (survey->filled & SURVEY_HAS_CHAN_TIME_TX) { |
| 312 | busy -= survey->channel_time_tx; |
| 313 | total -= survey->channel_time_tx; |
| 314 | } |
| 315 | |
| 316 | /* TODO: figure out the best multiplier for noise floor base */ |
| 317 | factor = pow(10, survey->nf / 5.0L) + |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 318 | (total ? (busy / total) : 0) * |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 319 | pow(2, pow(10, (long double) survey->nf / 10.0L) - |
| 320 | pow(10, (long double) min_nf / 10.0L)); |
| 321 | |
| 322 | return factor; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | static void |
| 327 | acs_survey_chan_interference_factor(struct hostapd_iface *iface, |
| 328 | struct hostapd_channel_data *chan) |
| 329 | { |
| 330 | struct freq_survey *survey; |
| 331 | unsigned int i = 0; |
| 332 | long double int_factor = 0; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 333 | unsigned count = 0; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 334 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 335 | if (dl_list_empty(&chan->survey_list) || |
| 336 | (chan->flag & HOSTAPD_CHAN_DISABLED)) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 337 | return; |
| 338 | |
| 339 | chan->interference_factor = 0; |
| 340 | |
| 341 | dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list) |
| 342 | { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 343 | i++; |
| 344 | |
| 345 | if (!acs_survey_is_sufficient(survey)) { |
| 346 | wpa_printf(MSG_DEBUG, "ACS: %d: insufficient data", i); |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | count++; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 351 | int_factor = acs_survey_interference_factor(survey, |
| 352 | iface->lowest_nf); |
| 353 | chan->interference_factor += int_factor; |
| 354 | wpa_printf(MSG_DEBUG, "ACS: %d: min_nf=%d interference_factor=%Lg nf=%d time=%lu busy=%lu rx=%lu", |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 355 | i, chan->min_nf, int_factor, |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 356 | survey->nf, (unsigned long) survey->channel_time, |
| 357 | (unsigned long) survey->channel_time_busy, |
| 358 | (unsigned long) survey->channel_time_rx); |
| 359 | } |
| 360 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 361 | if (count) |
| 362 | chan->interference_factor /= count; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 366 | static int acs_usable_ht40_chan(const struct hostapd_channel_data *chan) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 367 | { |
| 368 | const int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, |
| 369 | 157, 184, 192 }; |
| 370 | unsigned int i; |
| 371 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 372 | for (i = 0; i < ARRAY_SIZE(allowed); i++) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 373 | if (chan->chan == allowed[i]) |
| 374 | return 1; |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 380 | static int acs_usable_vht80_chan(const struct hostapd_channel_data *chan) |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 381 | { |
| 382 | const int allowed[] = { 36, 52, 100, 116, 132, 149 }; |
| 383 | unsigned int i; |
| 384 | |
| 385 | for (i = 0; i < ARRAY_SIZE(allowed); i++) |
| 386 | if (chan->chan == allowed[i]) |
| 387 | return 1; |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 393 | static int acs_usable_vht160_chan(const struct hostapd_channel_data *chan) |
| 394 | { |
| 395 | const int allowed[] = { 36, 100 }; |
| 396 | unsigned int i; |
| 397 | |
| 398 | for (i = 0; i < ARRAY_SIZE(allowed); i++) |
| 399 | if (chan->chan == allowed[i]) |
| 400 | return 1; |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 406 | static int acs_survey_is_sufficient(struct freq_survey *survey) |
| 407 | { |
| 408 | if (!(survey->filled & SURVEY_HAS_NF)) { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 409 | wpa_printf(MSG_INFO, "ACS: Survey is missing noise floor"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | if (!(survey->filled & SURVEY_HAS_CHAN_TIME)) { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 414 | wpa_printf(MSG_INFO, "ACS: Survey is missing channel time"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | if (!(survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) && |
| 419 | !(survey->filled & SURVEY_HAS_CHAN_TIME_RX)) { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 420 | wpa_printf(MSG_INFO, |
| 421 | "ACS: Survey is missing RX and busy time (at least one is required)"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | return 1; |
| 426 | } |
| 427 | |
| 428 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 429 | static int acs_survey_list_is_sufficient(struct hostapd_channel_data *chan) |
| 430 | { |
| 431 | struct freq_survey *survey; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 432 | int ret = -1; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 433 | |
| 434 | dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list) |
| 435 | { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 436 | if (acs_survey_is_sufficient(survey)) { |
| 437 | ret = 1; |
| 438 | break; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 439 | } |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 440 | ret = 0; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 443 | if (ret == -1) |
| 444 | ret = 1; /* no survey list entries */ |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 445 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 446 | if (!ret) { |
| 447 | wpa_printf(MSG_INFO, |
| 448 | "ACS: Channel %d has insufficient survey data", |
| 449 | chan->chan); |
| 450 | } |
| 451 | |
| 452 | return ret; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 456 | static int acs_surveys_are_sufficient(struct hostapd_iface *iface) |
| 457 | { |
| 458 | int i; |
| 459 | struct hostapd_channel_data *chan; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 460 | int valid = 0; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 461 | |
| 462 | for (i = 0; i < iface->current_mode->num_channels; i++) { |
| 463 | chan = &iface->current_mode->channels[i]; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 464 | if (!(chan->flag & HOSTAPD_CHAN_DISABLED) && |
| 465 | acs_survey_list_is_sufficient(chan)) |
| 466 | valid++; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 469 | /* We need at least survey data for one channel */ |
| 470 | return !!valid; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | static int acs_usable_chan(struct hostapd_channel_data *chan) |
| 475 | { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 476 | return !dl_list_empty(&chan->survey_list) && |
| 477 | !(chan->flag & HOSTAPD_CHAN_DISABLED) && |
| 478 | acs_survey_list_is_sufficient(chan); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 482 | static int is_in_chanlist(struct hostapd_iface *iface, |
| 483 | struct hostapd_channel_data *chan) |
| 484 | { |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 485 | if (!iface->conf->acs_ch_list.num) |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 486 | return 1; |
| 487 | |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 488 | return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 492 | static void acs_survey_all_chans_intereference_factor( |
| 493 | struct hostapd_iface *iface) |
| 494 | { |
| 495 | int i; |
| 496 | struct hostapd_channel_data *chan; |
| 497 | |
| 498 | for (i = 0; i < iface->current_mode->num_channels; i++) { |
| 499 | chan = &iface->current_mode->channels[i]; |
| 500 | |
| 501 | if (!acs_usable_chan(chan)) |
| 502 | continue; |
| 503 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 504 | if (!is_in_chanlist(iface, chan)) |
| 505 | continue; |
| 506 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 507 | wpa_printf(MSG_DEBUG, "ACS: Survey analysis for channel %d (%d MHz)", |
| 508 | chan->chan, chan->freq); |
| 509 | |
| 510 | acs_survey_chan_interference_factor(iface, chan); |
| 511 | |
| 512 | wpa_printf(MSG_DEBUG, "ACS: * interference factor average: %Lg", |
| 513 | chan->interference_factor); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | |
| 518 | static struct hostapd_channel_data *acs_find_chan(struct hostapd_iface *iface, |
| 519 | int freq) |
| 520 | { |
| 521 | struct hostapd_channel_data *chan; |
| 522 | int i; |
| 523 | |
| 524 | for (i = 0; i < iface->current_mode->num_channels; i++) { |
| 525 | chan = &iface->current_mode->channels[i]; |
| 526 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 527 | if (chan->flag & HOSTAPD_CHAN_DISABLED) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 528 | continue; |
| 529 | |
| 530 | if (chan->freq == freq) |
| 531 | return chan; |
| 532 | } |
| 533 | |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 538 | static int is_24ghz_mode(enum hostapd_hw_mode mode) |
| 539 | { |
| 540 | return mode == HOSTAPD_MODE_IEEE80211B || |
| 541 | mode == HOSTAPD_MODE_IEEE80211G; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | static int is_common_24ghz_chan(int chan) |
| 546 | { |
| 547 | return chan == 1 || chan == 6 || chan == 11; |
| 548 | } |
| 549 | |
| 550 | |
| 551 | #ifndef ACS_ADJ_WEIGHT |
| 552 | #define ACS_ADJ_WEIGHT 0.85 |
| 553 | #endif /* ACS_ADJ_WEIGHT */ |
| 554 | |
| 555 | #ifndef ACS_NEXT_ADJ_WEIGHT |
| 556 | #define ACS_NEXT_ADJ_WEIGHT 0.55 |
| 557 | #endif /* ACS_NEXT_ADJ_WEIGHT */ |
| 558 | |
| 559 | #ifndef ACS_24GHZ_PREFER_1_6_11 |
| 560 | /* |
| 561 | * Select commonly used channels 1, 6, 11 by default even if a neighboring |
| 562 | * channel has a smaller interference factor as long as it is not better by more |
| 563 | * than this multiplier. |
| 564 | */ |
| 565 | #define ACS_24GHZ_PREFER_1_6_11 0.8 |
| 566 | #endif /* ACS_24GHZ_PREFER_1_6_11 */ |
| 567 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 568 | /* |
| 569 | * At this point it's assumed chan->interface_factor has been computed. |
| 570 | * This function should be reusable regardless of interference computation |
| 571 | * option (survey, BSS, spectral, ...). chan->interference factor must be |
| 572 | * summable (i.e., must be always greater than zero). |
| 573 | */ |
| 574 | static struct hostapd_channel_data * |
| 575 | acs_find_ideal_chan(struct hostapd_iface *iface) |
| 576 | { |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 577 | struct hostapd_channel_data *chan, *adj_chan, *ideal_chan = NULL, |
| 578 | *rand_chan = NULL; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 579 | long double factor, ideal_factor = 0; |
| 580 | int i, j; |
| 581 | int n_chans = 1; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 582 | u32 bw; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 583 | unsigned int k; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 584 | |
| 585 | /* TODO: HT40- support */ |
| 586 | |
| 587 | if (iface->conf->ieee80211n && |
| 588 | iface->conf->secondary_channel == -1) { |
| 589 | wpa_printf(MSG_ERROR, "ACS: HT40- is not supported yet. Please try HT40+"); |
| 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | if (iface->conf->ieee80211n && |
| 594 | iface->conf->secondary_channel) |
| 595 | n_chans = 2; |
| 596 | |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 597 | if (iface->conf->ieee80211ac) { |
| 598 | switch (iface->conf->vht_oper_chwidth) { |
| 599 | case VHT_CHANWIDTH_80MHZ: |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 600 | n_chans = 4; |
| 601 | break; |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 602 | case VHT_CHANWIDTH_160MHZ: |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 603 | n_chans = 8; |
| 604 | break; |
| 605 | } |
| 606 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 607 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 608 | bw = num_chan_to_bw(n_chans); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 609 | |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 610 | /* TODO: VHT80+80. Update acs_adjust_vht_center_freq() too. */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 611 | |
| 612 | wpa_printf(MSG_DEBUG, |
| 613 | "ACS: Survey analysis for selected bandwidth %d MHz", bw); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 614 | |
| 615 | for (i = 0; i < iface->current_mode->num_channels; i++) { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 616 | double total_weight; |
| 617 | struct acs_bias *bias, tmp_bias; |
| 618 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 619 | chan = &iface->current_mode->channels[i]; |
| 620 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 621 | /* Since in the current ACS implementation the first channel is |
| 622 | * always a primary channel, skip channels not available as |
| 623 | * primary until more sophisticated channel selection is |
| 624 | * implemented. */ |
| 625 | if (!chan_pri_allowed(chan)) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 626 | continue; |
| 627 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 628 | if (!is_in_chanlist(iface, chan)) |
| 629 | continue; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 630 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 631 | if (!chan_bw_allowed(chan, bw, 1, 1)) { |
| 632 | wpa_printf(MSG_DEBUG, |
| 633 | "ACS: Channel %d: BW %u is not supported", |
| 634 | chan->chan, bw); |
| 635 | continue; |
| 636 | } |
| 637 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 638 | /* HT40 on 5 GHz has a limited set of primary channels as per |
| 639 | * 11n Annex J */ |
| 640 | if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A && |
| 641 | iface->conf->ieee80211n && |
| 642 | iface->conf->secondary_channel && |
| 643 | !acs_usable_ht40_chan(chan)) { |
| 644 | wpa_printf(MSG_DEBUG, "ACS: Channel %d: not allowed as primary channel for HT40", |
| 645 | chan->chan); |
| 646 | continue; |
| 647 | } |
| 648 | |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 649 | if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A && |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 650 | iface->conf->ieee80211ac) { |
| 651 | if (iface->conf->vht_oper_chwidth == |
| 652 | VHT_CHANWIDTH_80MHZ && |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 653 | !acs_usable_vht80_chan(chan)) { |
| 654 | wpa_printf(MSG_DEBUG, |
| 655 | "ACS: Channel %d: not allowed as primary channel for VHT80", |
| 656 | chan->chan); |
| 657 | continue; |
| 658 | } |
| 659 | |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 660 | if (iface->conf->vht_oper_chwidth == |
| 661 | VHT_CHANWIDTH_160MHZ && |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 662 | !acs_usable_vht160_chan(chan)) { |
| 663 | wpa_printf(MSG_DEBUG, |
| 664 | "ACS: Channel %d: not allowed as primary channel for VHT160", |
| 665 | chan->chan); |
| 666 | continue; |
| 667 | } |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 670 | factor = 0; |
| 671 | if (acs_usable_chan(chan)) |
| 672 | factor = chan->interference_factor; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 673 | total_weight = 1; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 674 | |
| 675 | for (j = 1; j < n_chans; j++) { |
| 676 | adj_chan = acs_find_chan(iface, chan->freq + (j * 20)); |
| 677 | if (!adj_chan) |
| 678 | break; |
| 679 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 680 | if (!chan_bw_allowed(adj_chan, bw, 1, 0)) { |
| 681 | wpa_printf(MSG_DEBUG, |
| 682 | "ACS: PRI Channel %d: secondary channel %d BW %u is not supported", |
| 683 | chan->chan, adj_chan->chan, bw); |
| 684 | break; |
| 685 | } |
| 686 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 687 | if (acs_usable_chan(adj_chan)) { |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 688 | factor += adj_chan->interference_factor; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 689 | total_weight += 1; |
| 690 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | if (j != n_chans) { |
| 694 | wpa_printf(MSG_DEBUG, "ACS: Channel %d: not enough bandwidth", |
| 695 | chan->chan); |
| 696 | continue; |
| 697 | } |
| 698 | |
| 699 | /* 2.4 GHz has overlapping 20 MHz channels. Include adjacent |
| 700 | * channel interference factor. */ |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 701 | if (is_24ghz_mode(iface->current_mode->mode)) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 702 | for (j = 0; j < n_chans; j++) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 703 | adj_chan = acs_find_chan(iface, chan->freq + |
| 704 | (j * 20) - 5); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 705 | if (adj_chan && acs_usable_chan(adj_chan)) { |
| 706 | factor += ACS_ADJ_WEIGHT * |
| 707 | adj_chan->interference_factor; |
| 708 | total_weight += ACS_ADJ_WEIGHT; |
| 709 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 710 | |
| 711 | adj_chan = acs_find_chan(iface, chan->freq + |
| 712 | (j * 20) - 10); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 713 | if (adj_chan && acs_usable_chan(adj_chan)) { |
| 714 | factor += ACS_NEXT_ADJ_WEIGHT * |
| 715 | adj_chan->interference_factor; |
| 716 | total_weight += ACS_NEXT_ADJ_WEIGHT; |
| 717 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 718 | |
| 719 | adj_chan = acs_find_chan(iface, chan->freq + |
| 720 | (j * 20) + 5); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 721 | if (adj_chan && acs_usable_chan(adj_chan)) { |
| 722 | factor += ACS_ADJ_WEIGHT * |
| 723 | adj_chan->interference_factor; |
| 724 | total_weight += ACS_ADJ_WEIGHT; |
| 725 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 726 | |
| 727 | adj_chan = acs_find_chan(iface, chan->freq + |
| 728 | (j * 20) + 10); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 729 | if (adj_chan && acs_usable_chan(adj_chan)) { |
| 730 | factor += ACS_NEXT_ADJ_WEIGHT * |
| 731 | adj_chan->interference_factor; |
| 732 | total_weight += ACS_NEXT_ADJ_WEIGHT; |
| 733 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 737 | factor /= total_weight; |
| 738 | |
| 739 | bias = NULL; |
| 740 | if (iface->conf->acs_chan_bias) { |
| 741 | for (k = 0; k < iface->conf->num_acs_chan_bias; k++) { |
| 742 | bias = &iface->conf->acs_chan_bias[k]; |
| 743 | if (bias->channel == chan->chan) |
| 744 | break; |
| 745 | bias = NULL; |
| 746 | } |
| 747 | } else if (is_24ghz_mode(iface->current_mode->mode) && |
| 748 | is_common_24ghz_chan(chan->chan)) { |
| 749 | tmp_bias.channel = chan->chan; |
| 750 | tmp_bias.bias = ACS_24GHZ_PREFER_1_6_11; |
| 751 | bias = &tmp_bias; |
| 752 | } |
| 753 | |
| 754 | if (bias) { |
| 755 | factor *= bias->bias; |
| 756 | wpa_printf(MSG_DEBUG, |
| 757 | "ACS: * channel %d: total interference = %Lg (%f bias)", |
| 758 | chan->chan, factor, bias->bias); |
| 759 | } else { |
| 760 | wpa_printf(MSG_DEBUG, |
| 761 | "ACS: * channel %d: total interference = %Lg", |
| 762 | chan->chan, factor); |
| 763 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 764 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 765 | if (acs_usable_chan(chan) && |
| 766 | (!ideal_chan || factor < ideal_factor)) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 767 | ideal_factor = factor; |
| 768 | ideal_chan = chan; |
| 769 | } |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 770 | |
| 771 | /* This channel would at least be usable */ |
| 772 | if (!rand_chan) |
| 773 | rand_chan = chan; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 776 | if (ideal_chan) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 777 | wpa_printf(MSG_DEBUG, "ACS: Ideal channel is %d (%d MHz) with total interference factor of %Lg", |
| 778 | ideal_chan->chan, ideal_chan->freq, ideal_factor); |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 779 | return ideal_chan; |
| 780 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 781 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 782 | return rand_chan; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 786 | static void acs_adjust_vht_center_freq(struct hostapd_iface *iface) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 787 | { |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 788 | int offset; |
| 789 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 790 | wpa_printf(MSG_DEBUG, "ACS: Adjusting VHT center frequency"); |
| 791 | |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 792 | switch (iface->conf->vht_oper_chwidth) { |
| 793 | case VHT_CHANWIDTH_USE_HT: |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 794 | offset = 2 * iface->conf->secondary_channel; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 795 | break; |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 796 | case VHT_CHANWIDTH_80MHZ: |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 797 | offset = 6; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 798 | break; |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 799 | case VHT_CHANWIDTH_160MHZ: |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 800 | offset = 14; |
| 801 | break; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 802 | default: |
| 803 | /* TODO: How can this be calculated? Adjust |
| 804 | * acs_find_ideal_chan() */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 805 | wpa_printf(MSG_INFO, |
| 806 | "ACS: Only VHT20/40/80/160 is supported now"); |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 807 | return; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 808 | } |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 809 | |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 810 | iface->conf->vht_oper_centr_freq_seg0_idx = |
| 811 | iface->conf->channel + offset; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | |
| 815 | static int acs_study_survey_based(struct hostapd_iface *iface) |
| 816 | { |
| 817 | wpa_printf(MSG_DEBUG, "ACS: Trying survey-based ACS"); |
| 818 | |
| 819 | if (!iface->chans_surveyed) { |
| 820 | wpa_printf(MSG_ERROR, "ACS: Unable to collect survey data"); |
| 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | if (!acs_surveys_are_sufficient(iface)) { |
| 825 | wpa_printf(MSG_ERROR, "ACS: Surveys have insufficient data"); |
| 826 | return -1; |
| 827 | } |
| 828 | |
| 829 | acs_survey_all_chans_intereference_factor(iface); |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | static int acs_study_options(struct hostapd_iface *iface) |
| 835 | { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 836 | if (acs_study_survey_based(iface) == 0) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 837 | return 0; |
| 838 | |
| 839 | /* TODO: If no surveys are available/sufficient this is a good |
| 840 | * place to fallback to BSS-based ACS */ |
| 841 | |
| 842 | return -1; |
| 843 | } |
| 844 | |
| 845 | |
| 846 | static void acs_study(struct hostapd_iface *iface) |
| 847 | { |
| 848 | struct hostapd_channel_data *ideal_chan; |
| 849 | int err; |
| 850 | |
| 851 | err = acs_study_options(iface); |
| 852 | if (err < 0) { |
| 853 | wpa_printf(MSG_ERROR, "ACS: All study options have failed"); |
| 854 | goto fail; |
| 855 | } |
| 856 | |
| 857 | ideal_chan = acs_find_ideal_chan(iface); |
| 858 | if (!ideal_chan) { |
| 859 | wpa_printf(MSG_ERROR, "ACS: Failed to compute ideal channel"); |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 860 | err = -1; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 861 | goto fail; |
| 862 | } |
| 863 | |
| 864 | iface->conf->channel = ideal_chan->chan; |
| 865 | |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame^] | 866 | if (iface->conf->ieee80211ac) |
| 867 | acs_adjust_vht_center_freq(iface); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 868 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 869 | err = 0; |
| 870 | fail: |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 871 | /* |
| 872 | * hostapd_setup_interface_complete() will return -1 on failure, |
| 873 | * 0 on success and 0 is HOSTAPD_CHAN_VALID :) |
| 874 | */ |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 875 | if (hostapd_acs_completed(iface, err) == HOSTAPD_CHAN_VALID) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 876 | acs_cleanup(iface); |
| 877 | return; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 880 | /* This can possibly happen if channel parameters (secondary |
| 881 | * channel, center frequencies) are misconfigured */ |
| 882 | wpa_printf(MSG_ERROR, "ACS: Possibly channel configuration is invalid, please report this along with your config file."); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 883 | acs_fail(iface); |
| 884 | } |
| 885 | |
| 886 | |
| 887 | static void acs_scan_complete(struct hostapd_iface *iface) |
| 888 | { |
| 889 | int err; |
| 890 | |
| 891 | iface->scan_cb = NULL; |
| 892 | |
| 893 | wpa_printf(MSG_DEBUG, "ACS: Using survey based algorithm (acs_num_scans=%d)", |
| 894 | iface->conf->acs_num_scans); |
| 895 | |
| 896 | err = hostapd_drv_get_survey(iface->bss[0], 0); |
| 897 | if (err) { |
| 898 | wpa_printf(MSG_ERROR, "ACS: Failed to get survey data"); |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 899 | goto fail; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | if (++iface->acs_num_completed_scans < iface->conf->acs_num_scans) { |
| 903 | err = acs_request_scan(iface); |
| 904 | if (err) { |
| 905 | wpa_printf(MSG_ERROR, "ACS: Failed to request scan"); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 906 | goto fail; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | return; |
| 910 | } |
| 911 | |
| 912 | acs_study(iface); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 913 | return; |
| 914 | fail: |
| 915 | hostapd_acs_completed(iface, 1); |
| 916 | acs_fail(iface); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | |
| 920 | static int acs_request_scan(struct hostapd_iface *iface) |
| 921 | { |
| 922 | struct wpa_driver_scan_params params; |
| 923 | struct hostapd_channel_data *chan; |
| 924 | int i, *freq; |
| 925 | |
| 926 | os_memset(¶ms, 0, sizeof(params)); |
| 927 | params.freqs = os_calloc(iface->current_mode->num_channels + 1, |
| 928 | sizeof(params.freqs[0])); |
| 929 | if (params.freqs == NULL) |
| 930 | return -1; |
| 931 | |
| 932 | freq = params.freqs; |
| 933 | for (i = 0; i < iface->current_mode->num_channels; i++) { |
| 934 | chan = &iface->current_mode->channels[i]; |
| 935 | if (chan->flag & HOSTAPD_CHAN_DISABLED) |
| 936 | continue; |
| 937 | |
Dmitry Shmidt | 8bd70b7 | 2015-05-26 16:02:19 -0700 | [diff] [blame] | 938 | if (!is_in_chanlist(iface, chan)) |
| 939 | continue; |
| 940 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 941 | *freq++ = chan->freq; |
| 942 | } |
| 943 | *freq = 0; |
| 944 | |
| 945 | iface->scan_cb = acs_scan_complete; |
| 946 | |
| 947 | wpa_printf(MSG_DEBUG, "ACS: Scanning %d / %d", |
| 948 | iface->acs_num_completed_scans + 1, |
| 949 | iface->conf->acs_num_scans); |
| 950 | |
| 951 | if (hostapd_driver_scan(iface->bss[0], ¶ms) < 0) { |
| 952 | wpa_printf(MSG_ERROR, "ACS: Failed to request initial scan"); |
| 953 | acs_cleanup(iface); |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 954 | os_free(params.freqs); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 955 | return -1; |
| 956 | } |
| 957 | |
| 958 | os_free(params.freqs); |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | |
| 963 | enum hostapd_chan_status acs_init(struct hostapd_iface *iface) |
| 964 | { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 965 | wpa_printf(MSG_INFO, "ACS: Automatic channel selection started, this may take a bit"); |
| 966 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 967 | if (iface->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) { |
| 968 | wpa_printf(MSG_INFO, "ACS: Offloading to driver"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 969 | if (hostapd_drv_do_acs(iface->bss[0])) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 970 | return HOSTAPD_CHAN_INVALID; |
| 971 | return HOSTAPD_CHAN_ACS; |
| 972 | } |
| 973 | |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 974 | if (!iface->current_mode) |
| 975 | return HOSTAPD_CHAN_INVALID; |
| 976 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 977 | acs_cleanup(iface); |
| 978 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 979 | if (acs_request_scan(iface) < 0) |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 980 | return HOSTAPD_CHAN_INVALID; |
| 981 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 982 | hostapd_set_state(iface, HAPD_IFACE_ACS); |
| 983 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_STARTED); |
| 984 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 985 | return HOSTAPD_CHAN_ACS; |
| 986 | } |