test_dfs.py 16 KB

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