test_dfs.py 15 KB

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