acs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. #include "utils/includes.h"
  10. #include <math.h>
  11. #include "utils/common.h"
  12. #include "utils/list.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "drivers/driver.h"
  16. #include "hostapd.h"
  17. #include "ap_drv_ops.h"
  18. #include "ap_config.h"
  19. #include "hw_features.h"
  20. #include "acs.h"
  21. /*
  22. * Automatic Channel Selection
  23. * ===========================
  24. *
  25. * More info at
  26. * ------------
  27. * http://wireless.kernel.org/en/users/Documentation/acs
  28. *
  29. * How to use
  30. * ----------
  31. * - make sure you have CONFIG_ACS=y in hostapd's .config
  32. * - use channel=0 or channel=acs to enable ACS
  33. *
  34. * How does it work
  35. * ----------------
  36. * 1. passive scans are used to collect survey data
  37. * (it is assumed that scan trigger collection of survey data in driver)
  38. * 2. interference factor is calculated for each channel
  39. * 3. ideal channel is picked depending on channel width by using adjacent
  40. * channel interference factors
  41. *
  42. * Known limitations
  43. * -----------------
  44. * - Current implementation depends heavily on the amount of time willing to
  45. * spend gathering survey data during hostapd startup. Short traffic bursts
  46. * may be missed and a suboptimal channel may be picked.
  47. * - Ideal channel may end up overlapping a channel with 40 MHz intolerant BSS
  48. *
  49. * Todo / Ideas
  50. * ------------
  51. * - implement other interference computation methods
  52. * - BSS/RSSI based
  53. * - spectral scan based
  54. * (should be possibly to hook this up with current ACS scans)
  55. * - add wpa_supplicant support (for P2P)
  56. * - collect a histogram of interference over time allowing more educated
  57. * guess about an ideal channel (perhaps CSA could be used to migrate AP to a
  58. * new "better" channel while running)
  59. * - include neighboring BSS scan to avoid conflicts with 40 MHz intolerant BSSs
  60. * when choosing the ideal channel
  61. *
  62. * Survey interference factor implementation details
  63. * -------------------------------------------------
  64. * Generic interference_factor in struct hostapd_channel_data is used.
  65. *
  66. * The survey interference factor is defined as the ratio of the
  67. * observed busy time over the time we spent on the channel,
  68. * this value is then amplified by the observed noise floor on
  69. * the channel in comparison to the lowest noise floor observed
  70. * on the entire band.
  71. *
  72. * This corresponds to:
  73. * ---
  74. * (busy time - tx time) / (active time - tx time) * 2^(chan_nf + band_min_nf)
  75. * ---
  76. *
  77. * The coefficient of 2 reflects the way power in "far-field"
  78. * radiation decreases as the square of distance from the antenna [1].
  79. * What this does is it decreases the observed busy time ratio if the
  80. * noise observed was low but increases it if the noise was high,
  81. * proportionally to the way "far field" radiation changes over
  82. * distance.
  83. *
  84. * If channel busy time is not available the fallback is to use channel RX time.
  85. *
  86. * Since noise floor is in dBm it is necessary to convert it into Watts so that
  87. * combined channel interference (e.g., HT40, which uses two channels) can be
  88. * calculated easily.
  89. * ---
  90. * (busy time - tx time) / (active time - tx time) *
  91. * 2^(10^(chan_nf/10) + 10^(band_min_nf/10))
  92. * ---
  93. *
  94. * However to account for cases where busy/rx time is 0 (channel load is then
  95. * 0%) channel noise floor signal power is combined into the equation so a
  96. * channel with lower noise floor is preferred. The equation becomes:
  97. * ---
  98. * 10^(chan_nf/5) + (busy time - tx time) / (active time - tx time) *
  99. * 2^(10^(chan_nf/10) + 10^(band_min_nf/10))
  100. * ---
  101. *
  102. * All this "interference factor" is purely subjective and only time
  103. * will tell how usable this is. By using the minimum noise floor we
  104. * remove any possible issues due to card calibration. The computation
  105. * of the interference factor then is dependent on what the card itself
  106. * picks up as the minimum noise, not an actual real possible card
  107. * noise value.
  108. *
  109. * Total interference computation details
  110. * --------------------------------------
  111. * The above channel interference factor is calculated with no respect to
  112. * target operational bandwidth.
  113. *
  114. * To find an ideal channel the above data is combined by taking into account
  115. * the target operational bandwidth and selected band. E.g., on 2.4 GHz channels
  116. * overlap with 20 MHz bandwidth, but there is no overlap for 20 MHz bandwidth
  117. * on 5 GHz.
  118. *
  119. * Each valid and possible channel spec (i.e., channel + width) is taken and its
  120. * interference factor is computed by summing up interferences of each channel
  121. * it overlaps. The one with least total interference is picked up.
  122. *
  123. * Note: This implies base channel interference factor must be non-negative
  124. * allowing easy summing up.
  125. *
  126. * Example ACS analysis printout
  127. * -----------------------------
  128. *
  129. * ACS: Trying survey-based ACS
  130. * ACS: Survey analysis for channel 1 (2412 MHz)
  131. * ACS: 1: min_nf=-113 interference_factor=0.0802469 nf=-113 time=162 busy=0 rx=13
  132. * ACS: 2: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12
  133. * ACS: 3: min_nf=-113 interference_factor=0.0679012 nf=-113 time=162 busy=0 rx=11
  134. * ACS: 4: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5
  135. * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4
  136. * ACS: * interference factor average: 0.0557166
  137. * ACS: Survey analysis for channel 2 (2417 MHz)
  138. * ACS: 1: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3
  139. * ACS: 2: min_nf=-113 interference_factor=0.0246914 nf=-113 time=162 busy=0 rx=4
  140. * ACS: 3: min_nf=-113 interference_factor=0.037037 nf=-113 time=162 busy=0 rx=6
  141. * ACS: 4: min_nf=-113 interference_factor=0.149068 nf=-113 time=161 busy=0 rx=24
  142. * ACS: 5: min_nf=-113 interference_factor=0.0248447 nf=-113 time=161 busy=0 rx=4
  143. * ACS: * interference factor average: 0.050832
  144. * ACS: Survey analysis for channel 3 (2422 MHz)
  145. * ACS: 1: min_nf=-113 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
  146. * ACS: 2: min_nf=-113 interference_factor=0.0185185 nf=-113 time=162 busy=0 rx=3
  147. * ACS: 3: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
  148. * ACS: 4: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
  149. * ACS: 5: min_nf=-113 interference_factor=0.0186335 nf=-113 time=161 busy=0 rx=3
  150. * ACS: * interference factor average: 0.0148838
  151. * ACS: Survey analysis for channel 4 (2427 MHz)
  152. * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
  153. * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9
  154. * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
  155. * ACS: 4: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3
  156. * ACS: 5: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
  157. * ACS: * interference factor average: 0.0160801
  158. * ACS: Survey analysis for channel 5 (2432 MHz)
  159. * ACS: 1: min_nf=-114 interference_factor=0.409938 nf=-113 time=161 busy=0 rx=66
  160. * ACS: 2: min_nf=-114 interference_factor=0.0432099 nf=-113 time=162 busy=0 rx=7
  161. * ACS: 3: min_nf=-114 interference_factor=0.0124224 nf=-113 time=161 busy=0 rx=2
  162. * ACS: 4: min_nf=-114 interference_factor=0.677019 nf=-113 time=161 busy=0 rx=109
  163. * ACS: 5: min_nf=-114 interference_factor=0.0186335 nf=-114 time=161 busy=0 rx=3
  164. * ACS: * interference factor average: 0.232244
  165. * ACS: Survey analysis for channel 6 (2437 MHz)
  166. * ACS: 1: min_nf=-113 interference_factor=0.552795 nf=-113 time=161 busy=0 rx=89
  167. * ACS: 2: min_nf=-113 interference_factor=0.0807453 nf=-112 time=161 busy=0 rx=13
  168. * ACS: 3: min_nf=-113 interference_factor=0.0310559 nf=-113 time=161 busy=0 rx=5
  169. * ACS: 4: min_nf=-113 interference_factor=0.434783 nf=-112 time=161 busy=0 rx=70
  170. * ACS: 5: min_nf=-113 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10
  171. * ACS: * interference factor average: 0.232298
  172. * ACS: Survey analysis for channel 7 (2442 MHz)
  173. * ACS: 1: min_nf=-113 interference_factor=0.440994 nf=-112 time=161 busy=0 rx=71
  174. * ACS: 2: min_nf=-113 interference_factor=0.385093 nf=-113 time=161 busy=0 rx=62
  175. * ACS: 3: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
  176. * ACS: 4: min_nf=-113 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
  177. * ACS: 5: min_nf=-113 interference_factor=0.0745342 nf=-113 time=161 busy=0 rx=12
  178. * ACS: * interference factor average: 0.195031
  179. * ACS: Survey analysis for channel 8 (2447 MHz)
  180. * ACS: 1: min_nf=-114 interference_factor=0.0496894 nf=-112 time=161 busy=0 rx=8
  181. * ACS: 2: min_nf=-114 interference_factor=0.0496894 nf=-114 time=161 busy=0 rx=8
  182. * ACS: 3: min_nf=-114 interference_factor=0.0372671 nf=-113 time=161 busy=0 rx=6
  183. * ACS: 4: min_nf=-114 interference_factor=0.12963 nf=-113 time=162 busy=0 rx=21
  184. * ACS: 5: min_nf=-114 interference_factor=0.166667 nf=-114 time=162 busy=0 rx=27
  185. * ACS: * interference factor average: 0.0865885
  186. * ACS: Survey analysis for channel 9 (2452 MHz)
  187. * ACS: 1: min_nf=-114 interference_factor=0.0124224 nf=-114 time=161 busy=0 rx=2
  188. * ACS: 2: min_nf=-114 interference_factor=0.0310559 nf=-114 time=161 busy=0 rx=5
  189. * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
  190. * ACS: 4: min_nf=-114 interference_factor=0.00617284 nf=-114 time=162 busy=0 rx=1
  191. * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
  192. * ACS: * interference factor average: 0.00993022
  193. * ACS: Survey analysis for channel 10 (2457 MHz)
  194. * ACS: 1: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
  195. * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
  196. * ACS: 3: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
  197. * ACS: 4: min_nf=-114 interference_factor=0.0493827 nf=-114 time=162 busy=0 rx=8
  198. * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
  199. * ACS: * interference factor average: 0.0136033
  200. * ACS: Survey analysis for channel 11 (2462 MHz)
  201. * ACS: 1: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=161 busy=0 rx=0
  202. * ACS: 2: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0
  203. * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=161 busy=0 rx=0
  204. * ACS: 4: min_nf=-114 interference_factor=0.0432099 nf=-114 time=162 busy=0 rx=7
  205. * ACS: 5: min_nf=-114 interference_factor=0.0925926 nf=-114 time=162 busy=0 rx=15
  206. * ACS: * interference factor average: 0.0271605
  207. * ACS: Survey analysis for channel 12 (2467 MHz)
  208. * ACS: 1: min_nf=-114 interference_factor=0.0621118 nf=-113 time=161 busy=0 rx=10
  209. * ACS: 2: min_nf=-114 interference_factor=0.00621118 nf=-114 time=161 busy=0 rx=1
  210. * ACS: 3: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
  211. * ACS: 4: min_nf=-114 interference_factor=2.51189e-23 nf=-113 time=162 busy=0 rx=0
  212. * ACS: 5: min_nf=-114 interference_factor=0.00617284 nf=-113 time=162 busy=0 rx=1
  213. * ACS: * interference factor average: 0.0148992
  214. * ACS: Survey analysis for channel 13 (2472 MHz)
  215. * ACS: 1: min_nf=-114 interference_factor=0.0745342 nf=-114 time=161 busy=0 rx=12
  216. * ACS: 2: min_nf=-114 interference_factor=0.0555556 nf=-114 time=162 busy=0 rx=9
  217. * ACS: 3: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
  218. * ACS: 4: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
  219. * ACS: 5: min_nf=-114 interference_factor=1.58489e-23 nf=-114 time=162 busy=0 rx=0
  220. * ACS: * interference factor average: 0.0260179
  221. * ACS: Survey analysis for selected bandwidth 20MHz
  222. * ACS: * channel 1: total interference = 0.121432
  223. * ACS: * channel 2: total interference = 0.137512
  224. * ACS: * channel 3: total interference = 0.369757
  225. * ACS: * channel 4: total interference = 0.546338
  226. * ACS: * channel 5: total interference = 0.690538
  227. * ACS: * channel 6: total interference = 0.762242
  228. * ACS: * channel 7: total interference = 0.756092
  229. * ACS: * channel 8: total interference = 0.537451
  230. * ACS: * channel 9: total interference = 0.332313
  231. * ACS: * channel 10: total interference = 0.152182
  232. * ACS: * channel 11: total interference = 0.0916111
  233. * ACS: * channel 12: total interference = 0.0816809
  234. * ACS: * channel 13: total interference = 0.0680776
  235. * ACS: Ideal channel is 13 (2472 MHz) with total interference factor of 0.0680776
  236. *
  237. * [1] http://en.wikipedia.org/wiki/Near_and_far_field
  238. */
  239. static int acs_request_scan(struct hostapd_iface *iface);
  240. static void acs_clean_chan_surveys(struct hostapd_channel_data *chan)
  241. {
  242. struct freq_survey *survey, *tmp;
  243. if (dl_list_empty(&chan->survey_list))
  244. return;
  245. dl_list_for_each_safe(survey, tmp, &chan->survey_list,
  246. struct freq_survey, list) {
  247. dl_list_del(&survey->list);
  248. os_free(survey);
  249. }
  250. }
  251. static void acs_cleanup(struct hostapd_iface *iface)
  252. {
  253. int i;
  254. struct hostapd_channel_data *chan;
  255. for (i = 0; i < iface->current_mode->num_channels; i++) {
  256. chan = &iface->current_mode->channels[i];
  257. if (chan->flag & HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED)
  258. acs_clean_chan_surveys(chan);
  259. dl_list_init(&chan->survey_list);
  260. chan->flag |= HOSTAPD_CHAN_SURVEY_LIST_INITIALIZED;
  261. chan->min_nf = 0;
  262. }
  263. iface->chans_surveyed = 0;
  264. iface->acs_num_completed_scans = 0;
  265. }
  266. static void acs_fail(struct hostapd_iface *iface)
  267. {
  268. wpa_printf(MSG_ERROR, "ACS: Failed to start");
  269. acs_cleanup(iface);
  270. }
  271. static long double
  272. acs_survey_interference_factor(struct freq_survey *survey, s8 min_nf)
  273. {
  274. long double factor, busy, total;
  275. if (survey->filled & SURVEY_HAS_CHAN_TIME_BUSY)
  276. busy = survey->channel_time_busy;
  277. else if (survey->filled & SURVEY_HAS_CHAN_TIME_RX)
  278. busy = survey->channel_time_rx;
  279. else {
  280. /* This shouldn't really happen as survey data is checked in
  281. * acs_sanity_check() */
  282. wpa_printf(MSG_ERROR, "ACS: Survey data missing");
  283. return 0;
  284. }
  285. total = survey->channel_time;
  286. if (survey->filled & SURVEY_HAS_CHAN_TIME_TX) {
  287. busy -= survey->channel_time_tx;
  288. total -= survey->channel_time_tx;
  289. }
  290. /* TODO: figure out the best multiplier for noise floor base */
  291. factor = pow(10, survey->nf / 5.0L) +
  292. (busy / total) *
  293. pow(2, pow(10, (long double) survey->nf / 10.0L) -
  294. pow(10, (long double) min_nf / 10.0L));
  295. return factor;
  296. }
  297. static void
  298. acs_survey_chan_interference_factor(struct hostapd_iface *iface,
  299. struct hostapd_channel_data *chan)
  300. {
  301. struct freq_survey *survey;
  302. unsigned int i = 0;
  303. long double int_factor = 0;
  304. if (dl_list_empty(&chan->survey_list))
  305. return;
  306. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  307. return;
  308. chan->interference_factor = 0;
  309. dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
  310. {
  311. int_factor = acs_survey_interference_factor(survey,
  312. iface->lowest_nf);
  313. chan->interference_factor += int_factor;
  314. wpa_printf(MSG_DEBUG, "ACS: %d: min_nf=%d interference_factor=%Lg nf=%d time=%lu busy=%lu rx=%lu",
  315. ++i, chan->min_nf, int_factor,
  316. survey->nf, (unsigned long) survey->channel_time,
  317. (unsigned long) survey->channel_time_busy,
  318. (unsigned long) survey->channel_time_rx);
  319. }
  320. chan->interference_factor = chan->interference_factor /
  321. dl_list_len(&chan->survey_list);
  322. }
  323. static int acs_usable_ht40_chan(struct hostapd_channel_data *chan)
  324. {
  325. const int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149,
  326. 157, 184, 192 };
  327. unsigned int i;
  328. for (i = 0; i < ARRAY_SIZE(allowed); i++)
  329. if (chan->chan == allowed[i])
  330. return 1;
  331. return 0;
  332. }
  333. static int acs_survey_is_sufficient(struct freq_survey *survey)
  334. {
  335. if (!(survey->filled & SURVEY_HAS_NF)) {
  336. wpa_printf(MSG_ERROR, "ACS: Survey is missing noise floor");
  337. return 0;
  338. }
  339. if (!(survey->filled & SURVEY_HAS_CHAN_TIME)) {
  340. wpa_printf(MSG_ERROR, "ACS: Survey is missing channel time");
  341. return 0;
  342. }
  343. if (!(survey->filled & SURVEY_HAS_CHAN_TIME_BUSY) &&
  344. !(survey->filled & SURVEY_HAS_CHAN_TIME_RX)) {
  345. wpa_printf(MSG_ERROR, "ACS: Survey is missing RX and busy time (at least one is required)");
  346. return 0;
  347. }
  348. return 1;
  349. }
  350. static int acs_survey_list_is_sufficient(struct hostapd_channel_data *chan)
  351. {
  352. struct freq_survey *survey;
  353. dl_list_for_each(survey, &chan->survey_list, struct freq_survey, list)
  354. {
  355. if (!acs_survey_is_sufficient(survey)) {
  356. wpa_printf(MSG_ERROR, "ACS: Channel %d has insufficient survey data",
  357. chan->chan);
  358. return 0;
  359. }
  360. }
  361. return 1;
  362. }
  363. static int acs_surveys_are_sufficient(struct hostapd_iface *iface)
  364. {
  365. int i;
  366. struct hostapd_channel_data *chan;
  367. int valid = 0;
  368. for (i = 0; i < iface->current_mode->num_channels; i++) {
  369. chan = &iface->current_mode->channels[i];
  370. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  371. continue;
  372. if (!acs_survey_list_is_sufficient(chan))
  373. continue;
  374. valid++;
  375. }
  376. /* We need at least survey data for one channel */
  377. return !!valid;
  378. }
  379. static int acs_usable_chan(struct hostapd_channel_data *chan)
  380. {
  381. if (dl_list_empty(&chan->survey_list))
  382. return 0;
  383. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  384. return 0;
  385. if (!acs_survey_list_is_sufficient(chan))
  386. return 0;
  387. return 1;
  388. }
  389. static void acs_survey_all_chans_intereference_factor(
  390. struct hostapd_iface *iface)
  391. {
  392. int i;
  393. struct hostapd_channel_data *chan;
  394. for (i = 0; i < iface->current_mode->num_channels; i++) {
  395. chan = &iface->current_mode->channels[i];
  396. if (!acs_usable_chan(chan))
  397. continue;
  398. wpa_printf(MSG_DEBUG, "ACS: Survey analysis for channel %d (%d MHz)",
  399. chan->chan, chan->freq);
  400. acs_survey_chan_interference_factor(iface, chan);
  401. wpa_printf(MSG_DEBUG, "ACS: * interference factor average: %Lg",
  402. chan->interference_factor);
  403. }
  404. }
  405. static struct hostapd_channel_data *acs_find_chan(struct hostapd_iface *iface,
  406. int freq)
  407. {
  408. struct hostapd_channel_data *chan;
  409. int i;
  410. for (i = 0; i < iface->current_mode->num_channels; i++) {
  411. chan = &iface->current_mode->channels[i];
  412. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  413. continue;
  414. if (chan->freq == freq)
  415. return chan;
  416. }
  417. return NULL;
  418. }
  419. /*
  420. * At this point it's assumed chan->interface_factor has been computed.
  421. * This function should be reusable regardless of interference computation
  422. * option (survey, BSS, spectral, ...). chan->interference factor must be
  423. * summable (i.e., must be always greater than zero).
  424. */
  425. static struct hostapd_channel_data *
  426. acs_find_ideal_chan(struct hostapd_iface *iface)
  427. {
  428. struct hostapd_channel_data *chan, *adj_chan, *ideal_chan = NULL,
  429. *rand_chan = NULL;
  430. long double factor, ideal_factor = 0;
  431. int i, j;
  432. int n_chans = 1;
  433. /* TODO: HT40- support */
  434. if (iface->conf->ieee80211n &&
  435. iface->conf->secondary_channel == -1) {
  436. wpa_printf(MSG_ERROR, "ACS: HT40- is not supported yet. Please try HT40+");
  437. return NULL;
  438. }
  439. if (iface->conf->ieee80211n &&
  440. iface->conf->secondary_channel)
  441. n_chans = 2;
  442. if (iface->conf->ieee80211ac &&
  443. iface->conf->vht_oper_chwidth == 1)
  444. n_chans = 4;
  445. /* TODO: VHT80+80, VHT160. Update acs_adjust_vht_center_freq() too. */
  446. wpa_printf(MSG_DEBUG, "ACS: Survey analysis for selected bandwidth %d MHz",
  447. n_chans == 1 ? 20 :
  448. n_chans == 2 ? 40 :
  449. n_chans == 4 ? 80 :
  450. -1);
  451. for (i = 0; i < iface->current_mode->num_channels; i++) {
  452. chan = &iface->current_mode->channels[i];
  453. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  454. continue;
  455. /* HT40 on 5 GHz has a limited set of primary channels as per
  456. * 11n Annex J */
  457. if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
  458. iface->conf->ieee80211n &&
  459. iface->conf->secondary_channel &&
  460. !acs_usable_ht40_chan(chan)) {
  461. wpa_printf(MSG_DEBUG, "ACS: Channel %d: not allowed as primary channel for HT40",
  462. chan->chan);
  463. continue;
  464. }
  465. factor = 0;
  466. if (acs_usable_chan(chan))
  467. factor = chan->interference_factor;
  468. for (j = 1; j < n_chans; j++) {
  469. adj_chan = acs_find_chan(iface, chan->freq + (j * 20));
  470. if (!adj_chan)
  471. break;
  472. if (acs_usable_chan(adj_chan))
  473. factor += adj_chan->interference_factor;
  474. }
  475. if (j != n_chans) {
  476. wpa_printf(MSG_DEBUG, "ACS: Channel %d: not enough bandwidth",
  477. chan->chan);
  478. continue;
  479. }
  480. /* 2.4 GHz has overlapping 20 MHz channels. Include adjacent
  481. * channel interference factor. */
  482. if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211B ||
  483. iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G) {
  484. for (j = 0; j < n_chans; j++) {
  485. /* TODO: perhaps a multiplier should be used
  486. * here? */
  487. adj_chan = acs_find_chan(iface, chan->freq +
  488. (j * 20) - 5);
  489. if (adj_chan && acs_usable_chan(adj_chan))
  490. factor += adj_chan->interference_factor;
  491. adj_chan = acs_find_chan(iface, chan->freq +
  492. (j * 20) - 10);
  493. if (adj_chan && acs_usable_chan(adj_chan))
  494. factor += adj_chan->interference_factor;
  495. adj_chan = acs_find_chan(iface, chan->freq +
  496. (j * 20) + 5);
  497. if (adj_chan && acs_usable_chan(adj_chan))
  498. factor += adj_chan->interference_factor;
  499. adj_chan = acs_find_chan(iface, chan->freq +
  500. (j * 20) + 10);
  501. if (adj_chan && acs_usable_chan(adj_chan))
  502. factor += adj_chan->interference_factor;
  503. }
  504. }
  505. wpa_printf(MSG_DEBUG, "ACS: * channel %d: total interference = %Lg",
  506. chan->chan, factor);
  507. if (acs_usable_chan(chan) &&
  508. (!ideal_chan || factor < ideal_factor)) {
  509. ideal_factor = factor;
  510. ideal_chan = chan;
  511. }
  512. /* This channel would at least be usable */
  513. if (!rand_chan)
  514. rand_chan = chan;
  515. }
  516. if (ideal_chan) {
  517. wpa_printf(MSG_DEBUG, "ACS: Ideal channel is %d (%d MHz) with total interference factor of %Lg",
  518. ideal_chan->chan, ideal_chan->freq, ideal_factor);
  519. return ideal_chan;
  520. }
  521. return rand_chan;
  522. }
  523. static void acs_adjust_vht_center_freq(struct hostapd_iface *iface)
  524. {
  525. wpa_printf(MSG_DEBUG, "ACS: Adjusting VHT center frequency");
  526. switch (iface->conf->vht_oper_chwidth) {
  527. case VHT_CHANWIDTH_USE_HT:
  528. iface->conf->vht_oper_centr_freq_seg0_idx =
  529. iface->conf->channel + 2;
  530. break;
  531. case VHT_CHANWIDTH_80MHZ:
  532. iface->conf->vht_oper_centr_freq_seg0_idx =
  533. iface->conf->channel + 6;
  534. break;
  535. default:
  536. /* TODO: How can this be calculated? Adjust
  537. * acs_find_ideal_chan() */
  538. wpa_printf(MSG_INFO, "ACS: Only VHT20/40/80 is supported now");
  539. break;
  540. }
  541. }
  542. static int acs_study_survey_based(struct hostapd_iface *iface)
  543. {
  544. wpa_printf(MSG_DEBUG, "ACS: Trying survey-based ACS");
  545. if (!iface->chans_surveyed) {
  546. wpa_printf(MSG_ERROR, "ACS: Unable to collect survey data");
  547. return -1;
  548. }
  549. if (!acs_surveys_are_sufficient(iface)) {
  550. wpa_printf(MSG_ERROR, "ACS: Surveys have insufficient data");
  551. return -1;
  552. }
  553. acs_survey_all_chans_intereference_factor(iface);
  554. return 0;
  555. }
  556. static int acs_study_options(struct hostapd_iface *iface)
  557. {
  558. int err;
  559. err = acs_study_survey_based(iface);
  560. if (err == 0)
  561. return 0;
  562. /* TODO: If no surveys are available/sufficient this is a good
  563. * place to fallback to BSS-based ACS */
  564. return -1;
  565. }
  566. static void acs_study(struct hostapd_iface *iface)
  567. {
  568. struct hostapd_channel_data *ideal_chan;
  569. int err;
  570. err = acs_study_options(iface);
  571. if (err < 0) {
  572. wpa_printf(MSG_ERROR, "ACS: All study options have failed");
  573. goto fail;
  574. }
  575. ideal_chan = acs_find_ideal_chan(iface);
  576. if (!ideal_chan) {
  577. wpa_printf(MSG_ERROR, "ACS: Failed to compute ideal channel");
  578. err = -1;
  579. goto fail;
  580. }
  581. iface->conf->channel = ideal_chan->chan;
  582. if (iface->conf->ieee80211ac)
  583. acs_adjust_vht_center_freq(iface);
  584. err = 0;
  585. fail:
  586. /*
  587. * hostapd_setup_interface_complete() will return -1 on failure,
  588. * 0 on success and 0 is HOSTAPD_CHAN_VALID :)
  589. */
  590. if (hostapd_acs_completed(iface, err) == HOSTAPD_CHAN_VALID) {
  591. acs_cleanup(iface);
  592. return;
  593. }
  594. /* This can possibly happen if channel parameters (secondary
  595. * channel, center frequencies) are misconfigured */
  596. wpa_printf(MSG_ERROR, "ACS: Possibly channel configuration is invalid, please report this along with your config file.");
  597. acs_fail(iface);
  598. }
  599. static void acs_scan_complete(struct hostapd_iface *iface)
  600. {
  601. int err;
  602. iface->scan_cb = NULL;
  603. wpa_printf(MSG_DEBUG, "ACS: Using survey based algorithm (acs_num_scans=%d)",
  604. iface->conf->acs_num_scans);
  605. err = hostapd_drv_get_survey(iface->bss[0], 0);
  606. if (err) {
  607. wpa_printf(MSG_ERROR, "ACS: Failed to get survey data");
  608. acs_fail(iface);
  609. }
  610. if (++iface->acs_num_completed_scans < iface->conf->acs_num_scans) {
  611. err = acs_request_scan(iface);
  612. if (err) {
  613. wpa_printf(MSG_ERROR, "ACS: Failed to request scan");
  614. goto fail;
  615. }
  616. return;
  617. }
  618. acs_study(iface);
  619. return;
  620. fail:
  621. hostapd_acs_completed(iface, 1);
  622. acs_fail(iface);
  623. }
  624. static int acs_request_scan(struct hostapd_iface *iface)
  625. {
  626. struct wpa_driver_scan_params params;
  627. struct hostapd_channel_data *chan;
  628. int i, *freq;
  629. os_memset(&params, 0, sizeof(params));
  630. params.freqs = os_calloc(iface->current_mode->num_channels + 1,
  631. sizeof(params.freqs[0]));
  632. if (params.freqs == NULL)
  633. return -1;
  634. freq = params.freqs;
  635. for (i = 0; i < iface->current_mode->num_channels; i++) {
  636. chan = &iface->current_mode->channels[i];
  637. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  638. continue;
  639. *freq++ = chan->freq;
  640. }
  641. *freq = 0;
  642. iface->scan_cb = acs_scan_complete;
  643. wpa_printf(MSG_DEBUG, "ACS: Scanning %d / %d",
  644. iface->acs_num_completed_scans + 1,
  645. iface->conf->acs_num_scans);
  646. if (hostapd_driver_scan(iface->bss[0], &params) < 0) {
  647. wpa_printf(MSG_ERROR, "ACS: Failed to request initial scan");
  648. acs_cleanup(iface);
  649. return -1;
  650. }
  651. os_free(params.freqs);
  652. return 0;
  653. }
  654. enum hostapd_chan_status acs_init(struct hostapd_iface *iface)
  655. {
  656. int err;
  657. wpa_printf(MSG_INFO, "ACS: Automatic channel selection started, this may take a bit");
  658. acs_cleanup(iface);
  659. err = acs_request_scan(iface);
  660. if (err < 0)
  661. return HOSTAPD_CHAN_INVALID;
  662. hostapd_set_state(iface, HAPD_IFACE_ACS);
  663. wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, ACS_EVENT_STARTED);
  664. return HOSTAPD_CHAN_ACS;
  665. }