test_dfs.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. # Test cases for DFS
  2. # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import os
  7. import subprocess
  8. import time
  9. import logging
  10. logger = logging.getLogger()
  11. import hwsim_utils
  12. import hostapd
  13. from utils import HwsimSkip
  14. def wait_dfs_event(hapd, event, timeout):
  15. dfs_events = [ "DFS-RADAR-DETECTED", "DFS-NEW-CHANNEL",
  16. "DFS-CAC-START", "DFS-CAC-COMPLETED",
  17. "DFS-NOP-FINISHED", "AP-ENABLED", "AP-CSA-FINISHED" ]
  18. ev = hapd.wait_event(dfs_events, timeout=timeout)
  19. if not ev:
  20. raise Exception("DFS event timed out")
  21. if event and event not in ev:
  22. raise Exception("Unexpected DFS event")
  23. return ev
  24. def start_dfs_ap(ap, allow_failure=False, ssid="dfs", ht=True, ht40=False,
  25. ht40minus=False, vht80=False, vht20=False, chanlist=None,
  26. channel=None):
  27. ifname = ap['ifname']
  28. logger.info("Starting AP " + ifname + " on DFS channel")
  29. hapd = hostapd.add_ap(ap, {}, no_enable=True)
  30. hapd.set("ssid", ssid)
  31. hapd.set("country_code", "FI")
  32. hapd.set("ieee80211d", "1")
  33. hapd.set("ieee80211h", "1")
  34. hapd.set("hw_mode", "a")
  35. hapd.set("channel", "52")
  36. if not ht:
  37. hapd.set("ieee80211n", "0")
  38. if ht40:
  39. hapd.set("ht_capab", "[HT40+]")
  40. elif ht40minus:
  41. hapd.set("ht_capab", "[HT40-]")
  42. hapd.set("channel", "56")
  43. if vht80:
  44. hapd.set("ieee80211ac", "1")
  45. hapd.set("vht_oper_chwidth", "1")
  46. hapd.set("vht_oper_centr_freq_seg0_idx", "58")
  47. if vht20:
  48. hapd.set("ieee80211ac", "1")
  49. hapd.set("vht_oper_chwidth", "0")
  50. hapd.set("vht_oper_centr_freq_seg0_idx", "0")
  51. if chanlist:
  52. hapd.set("chanlist", chanlist)
  53. if channel:
  54. hapd.set("channel", str(channel))
  55. hapd.enable()
  56. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  57. if "DFS-CAC-START" not in ev:
  58. raise Exception("Unexpected DFS event")
  59. state = hapd.get_status_field("state")
  60. if state != "DFS":
  61. if allow_failure:
  62. logger.info("Interface state not DFS: " + state)
  63. if not os.path.exists("dfs"):
  64. raise HwsimSkip("Assume DFS testing not supported")
  65. raise Exception("Failed to start DFS AP")
  66. raise Exception("Unexpected interface state: " + state)
  67. return hapd
  68. def dfs_simulate_radar(hapd):
  69. logger.info("Trigger a simulated radar event")
  70. phyname = hapd.get_driver_status_field("phyname")
  71. radar_file = '/sys/kernel/debug/ieee80211/' + phyname + '/hwsim/dfs_simulate_radar'
  72. with open(radar_file, 'w') as f:
  73. f.write('1')
  74. def test_dfs(dev, apdev):
  75. """DFS CAC functionality on clear channel"""
  76. try:
  77. hapd = None
  78. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  79. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  80. if "success=1" not in ev:
  81. raise Exception("CAC failed")
  82. if "freq=5260" not in ev:
  83. raise Exception("Unexpected DFS freq result")
  84. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  85. if not ev:
  86. raise Exception("AP setup timed out")
  87. state = hapd.get_status_field("state")
  88. if state != "ENABLED":
  89. raise Exception("Unexpected interface state")
  90. freq = hapd.get_status_field("freq")
  91. if freq != "5260":
  92. raise Exception("Unexpected frequency")
  93. dev[0].connect("dfs", key_mgmt="NONE")
  94. hwsim_utils.test_connectivity(dev[0], hapd)
  95. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  96. ev = hapd.wait_event(["DFS-RADAR-DETECTED"], timeout=10)
  97. if ev is None:
  98. raise Exception("DFS-RADAR-DETECTED event not reported")
  99. if "freq=5260" not in ev:
  100. raise Exception("Incorrect frequency in radar detected event: " + ev);
  101. ev = hapd.wait_event(["DFS-NEW-CHANNEL"], timeout=70)
  102. if ev is None:
  103. raise Exception("DFS-NEW-CHANNEL event not reported")
  104. if "freq=5260" in ev:
  105. raise Exception("Channel did not change after radar was detected");
  106. ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=70)
  107. if ev is None:
  108. raise Exception("AP-CSA-FINISHED event not reported")
  109. if "freq=5260" in ev:
  110. raise Exception("Channel did not change after radar was detected(2)");
  111. time.sleep(1)
  112. hwsim_utils.test_connectivity(dev[0], hapd)
  113. finally:
  114. dev[0].request("DISCONNECT")
  115. if hapd:
  116. hapd.request("DISABLE")
  117. subprocess.call(['iw', 'reg', 'set', '00'])
  118. dev[0].flush_scan_cache()
  119. def test_dfs_radar(dev, apdev):
  120. """DFS CAC functionality with radar detected"""
  121. try:
  122. hapd = None
  123. hapd2 = None
  124. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  125. time.sleep(1)
  126. dfs_simulate_radar(hapd)
  127. hapd2 = start_dfs_ap(apdev[1], ssid="dfs2", ht40=True)
  128. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  129. if ev is None:
  130. raise Exception("Timeout on DFS aborted event")
  131. if "success=0 freq=5260" not in ev:
  132. raise Exception("Unexpected DFS aborted event contents: " + ev)
  133. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  134. if "freq=5260" not in ev:
  135. raise Exception("Unexpected DFS radar detection freq")
  136. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  137. if "freq=5260" in ev:
  138. raise Exception("Unexpected DFS new freq")
  139. ev = wait_dfs_event(hapd, None, 5)
  140. if "AP-ENABLED" in ev:
  141. logger.info("Started AP on non-DFS channel")
  142. else:
  143. logger.info("Trying to start AP on another DFS channel")
  144. if "DFS-CAC-START" not in ev:
  145. raise Exception("Unexpected DFS event")
  146. if "freq=5260" in ev:
  147. raise Exception("Unexpected DFS CAC freq")
  148. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  149. if "success=1" not in ev:
  150. raise Exception("CAC failed")
  151. if "freq=5260" in ev:
  152. raise Exception("Unexpected DFS freq result - radar channel")
  153. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  154. if not ev:
  155. raise Exception("AP setup timed out")
  156. state = hapd.get_status_field("state")
  157. if state != "ENABLED":
  158. raise Exception("Unexpected interface state")
  159. freq = hapd.get_status_field("freq")
  160. if freq == "5260":
  161. raise Exception("Unexpected frequency: " + freq)
  162. dev[0].connect("dfs", key_mgmt="NONE")
  163. ev = hapd2.wait_event(["AP-ENABLED"], timeout=70)
  164. if not ev:
  165. raise Exception("AP2 setup timed out")
  166. dfs_simulate_radar(hapd2)
  167. ev = wait_dfs_event(hapd2, "DFS-RADAR-DETECTED", 5)
  168. if "freq=5260 ht_enabled=1 chan_offset=1 chan_width=2" not in ev:
  169. raise Exception("Unexpected DFS radar detection freq from AP2")
  170. ev = wait_dfs_event(hapd2, "DFS-NEW-CHANNEL", 5)
  171. if "freq=5260" in ev:
  172. raise Exception("Unexpected DFS new freq for AP2")
  173. wait_dfs_event(hapd2, None, 5)
  174. finally:
  175. dev[0].request("DISCONNECT")
  176. if hapd:
  177. hapd.request("DISABLE")
  178. if hapd2:
  179. hapd2.request("DISABLE")
  180. subprocess.call(['iw', 'reg', 'set', '00'])
  181. dev[0].flush_scan_cache()
  182. def test_dfs_radar_on_non_dfs_channel(dev, apdev):
  183. """DFS radar detection test code on non-DFS channel"""
  184. params = { "ssid": "radar" }
  185. hapd = hostapd.add_ap(apdev[0], params)
  186. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  187. hapd.request("RADAR DETECTED freq=2412 ht_enabled=1 chan_width=1")
  188. def test_dfs_radar_chanlist(dev, apdev):
  189. """DFS chanlist when radar is detected"""
  190. try:
  191. hapd = None
  192. hapd = start_dfs_ap(apdev[0], chanlist="40 44", allow_failure=True)
  193. time.sleep(1)
  194. dfs_simulate_radar(hapd)
  195. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  196. if ev is None:
  197. raise Exception("Timeout on DFS aborted event")
  198. if "success=0 freq=5260" not in ev:
  199. raise Exception("Unexpected DFS aborted event contents: " + ev)
  200. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  201. if "freq=5260" not in ev:
  202. raise Exception("Unexpected DFS radar detection freq")
  203. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  204. if "freq=5200 chan=40" not in ev and "freq=5220 chan=44" not in ev:
  205. raise Exception("Unexpected DFS new freq: " + ev)
  206. ev = wait_dfs_event(hapd, None, 5)
  207. if "AP-ENABLED" not in ev:
  208. raise Exception("Unexpected DFS event")
  209. dev[0].connect("dfs", key_mgmt="NONE")
  210. finally:
  211. dev[0].request("DISCONNECT")
  212. if hapd:
  213. hapd.request("DISABLE")
  214. subprocess.call(['iw', 'reg', 'set', '00'])
  215. dev[0].flush_scan_cache()
  216. def test_dfs_radar_chanlist_vht80(dev, apdev):
  217. """DFS chanlist when radar is detected and VHT80 configured"""
  218. try:
  219. hapd = None
  220. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40=True, vht80=True,
  221. allow_failure=True)
  222. time.sleep(1)
  223. dfs_simulate_radar(hapd)
  224. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  225. if ev is None:
  226. raise Exception("Timeout on DFS aborted event")
  227. if "success=0 freq=5260" not in ev:
  228. raise Exception("Unexpected DFS aborted event contents: " + ev)
  229. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  230. if "freq=5260" not in ev:
  231. raise Exception("Unexpected DFS radar detection freq")
  232. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  233. if "freq=5180 chan=36 sec_chan=1" not in ev:
  234. raise Exception("Unexpected DFS new freq: " + ev)
  235. ev = wait_dfs_event(hapd, None, 5)
  236. if "AP-ENABLED" not in ev:
  237. raise Exception("Unexpected DFS event")
  238. dev[0].connect("dfs", key_mgmt="NONE")
  239. if hapd.get_status_field('vht_oper_centr_freq_seg0_idx') != "42":
  240. raise Exception("Unexpected seg0 idx")
  241. finally:
  242. dev[0].request("DISCONNECT")
  243. if hapd:
  244. hapd.request("DISABLE")
  245. subprocess.call(['iw', 'reg', 'set', '00'])
  246. dev[0].flush_scan_cache()
  247. def test_dfs_radar_chanlist_vht20(dev, apdev):
  248. """DFS chanlist when radar is detected and VHT40 configured"""
  249. try:
  250. hapd = None
  251. hapd = start_dfs_ap(apdev[0], chanlist="36", vht20=True,
  252. allow_failure=True)
  253. time.sleep(1)
  254. dfs_simulate_radar(hapd)
  255. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  256. if ev is None:
  257. raise Exception("Timeout on DFS aborted event")
  258. if "success=0 freq=5260" not in ev:
  259. raise Exception("Unexpected DFS aborted event contents: " + ev)
  260. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  261. if "freq=5260" not in ev:
  262. raise Exception("Unexpected DFS radar detection freq")
  263. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  264. if "freq=5180 chan=36 sec_chan=0" not in ev:
  265. raise Exception("Unexpected DFS new freq: " + ev)
  266. ev = wait_dfs_event(hapd, None, 5)
  267. if "AP-ENABLED" not in ev:
  268. raise Exception("Unexpected DFS event")
  269. dev[0].connect("dfs", key_mgmt="NONE")
  270. finally:
  271. dev[0].request("DISCONNECT")
  272. if hapd:
  273. hapd.request("DISABLE")
  274. subprocess.call(['iw', 'reg', 'set', '00'])
  275. dev[0].flush_scan_cache()
  276. def test_dfs_radar_no_ht(dev, apdev):
  277. """DFS chanlist when radar is detected and no HT configured"""
  278. try:
  279. hapd = None
  280. hapd = start_dfs_ap(apdev[0], chanlist="36", ht=False,
  281. allow_failure=True)
  282. time.sleep(1)
  283. dfs_simulate_radar(hapd)
  284. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  285. if ev is None:
  286. raise Exception("Timeout on DFS aborted event")
  287. if "success=0 freq=5260" not in ev:
  288. raise Exception("Unexpected DFS aborted event contents: " + ev)
  289. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  290. if "freq=5260 ht_enabled=0" not in ev:
  291. raise Exception("Unexpected DFS radar detection freq: " + ev)
  292. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  293. if "freq=5180 chan=36 sec_chan=0" not in ev:
  294. raise Exception("Unexpected DFS new freq: " + ev)
  295. ev = wait_dfs_event(hapd, None, 5)
  296. if "AP-ENABLED" not in ev:
  297. raise Exception("Unexpected DFS event")
  298. dev[0].connect("dfs", key_mgmt="NONE")
  299. finally:
  300. dev[0].request("DISCONNECT")
  301. if hapd:
  302. hapd.request("DISABLE")
  303. subprocess.call(['iw', 'reg', 'set', '00'])
  304. dev[0].flush_scan_cache()
  305. def test_dfs_radar_ht40minus(dev, apdev):
  306. """DFS chanlist when radar is detected and HT40- configured"""
  307. try:
  308. hapd = None
  309. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40minus=True,
  310. allow_failure=True)
  311. time.sleep(1)
  312. dfs_simulate_radar(hapd)
  313. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  314. if ev is None:
  315. raise Exception("Timeout on DFS aborted event")
  316. if "success=0 freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  317. raise Exception("Unexpected DFS aborted event contents: " + ev)
  318. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  319. if "freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  320. raise Exception("Unexpected DFS radar detection freq: " + ev)
  321. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  322. if "freq=5180 chan=36 sec_chan=1" not in ev:
  323. raise Exception("Unexpected DFS new freq: " + ev)
  324. ev = wait_dfs_event(hapd, None, 5)
  325. if "AP-ENABLED" not in ev:
  326. raise Exception("Unexpected DFS event")
  327. dev[0].connect("dfs", key_mgmt="NONE")
  328. finally:
  329. dev[0].request("DISCONNECT")
  330. if hapd:
  331. hapd.request("DISABLE")
  332. subprocess.call(['iw', 'reg', 'set', '00'])
  333. dev[0].flush_scan_cache()
  334. def test_dfs_ht40_minus(dev, apdev, params):
  335. """DFS CAC functionality on channel 104 HT40- [long]"""
  336. if not params['long']:
  337. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  338. try:
  339. hapd = None
  340. hapd = start_dfs_ap(apdev[0], allow_failure=True, ht40minus=True,
  341. channel=104)
  342. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  343. if "success=1" not in ev:
  344. raise Exception("CAC failed")
  345. if "freq=5520" not in ev:
  346. raise Exception("Unexpected DFS freq result")
  347. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  348. if not ev:
  349. raise Exception("AP setup timed out")
  350. state = hapd.get_status_field("state")
  351. if state != "ENABLED":
  352. raise Exception("Unexpected interface state")
  353. freq = hapd.get_status_field("freq")
  354. if freq != "5520":
  355. raise Exception("Unexpected frequency")
  356. dev[0].connect("dfs", key_mgmt="NONE", scan_freq="5520")
  357. hwsim_utils.test_connectivity(dev[0], hapd)
  358. finally:
  359. dev[0].request("DISCONNECT")
  360. if hapd:
  361. hapd.request("DISABLE")
  362. subprocess.call(['iw', 'reg', 'set', '00'])
  363. dev[0].flush_scan_cache()