test_dfs.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. def wait_dfs_event(hapd, event, timeout):
  14. dfs_events = [ "DFS-RADAR-DETECTED", "DFS-NEW-CHANNEL",
  15. "DFS-CAC-START", "DFS-CAC-COMPLETED",
  16. "DFS-NOP-FINISHED", "AP-ENABLED", "AP-CSA-FINISHED" ]
  17. ev = hapd.wait_event(dfs_events, timeout=timeout)
  18. if not ev:
  19. raise Exception("DFS event timed out")
  20. if event and event not in ev:
  21. raise Exception("Unexpected DFS event")
  22. return ev
  23. def start_dfs_ap(ap, allow_failure=False, ssid="dfs", ht=True, ht40=False,
  24. ht40minus=False, vht80=False, vht20=False, chanlist=None):
  25. ifname = ap['ifname']
  26. logger.info("Starting AP " + ifname + " on DFS channel")
  27. hapd_global = hostapd.HostapdGlobal()
  28. hapd_global.remove(ifname)
  29. hapd_global.add(ifname)
  30. hapd = hostapd.Hostapd(ifname)
  31. if not hapd.ping():
  32. raise Exception("Could not ping hostapd")
  33. hapd.set_defaults()
  34. hapd.set("ssid", ssid)
  35. hapd.set("country_code", "FI")
  36. hapd.set("ieee80211d", "1")
  37. hapd.set("ieee80211h", "1")
  38. hapd.set("hw_mode", "a")
  39. hapd.set("channel", "52")
  40. if not ht:
  41. hapd.set("ieee80211n", "0")
  42. if ht40:
  43. hapd.set("ht_capab", "[HT40+]")
  44. elif ht40minus:
  45. hapd.set("ht_capab", "[HT40-]")
  46. hapd.set("channel", "56")
  47. if vht80:
  48. hapd.set("ieee80211ac", "1")
  49. hapd.set("vht_oper_chwidth", "1")
  50. hapd.set("vht_oper_centr_freq_seg0_idx", "58")
  51. if vht20:
  52. hapd.set("ieee80211ac", "1")
  53. hapd.set("vht_oper_chwidth", "0")
  54. hapd.set("vht_oper_centr_freq_seg0_idx", "0")
  55. if chanlist:
  56. hapd.set("chanlist", chanlist)
  57. hapd.enable()
  58. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  59. if "DFS-CAC-START" not in ev:
  60. raise Exception("Unexpected DFS event")
  61. state = hapd.get_status_field("state")
  62. if state != "DFS":
  63. if allow_failure:
  64. logger.info("Interface state not DFS: " + state)
  65. return None
  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 = start_dfs_ap(apdev[0], allow_failure=True)
  78. if hapd is None:
  79. if not os.path.exists("dfs"):
  80. return "skip"
  81. raise Exception("Failed to start DFS AP")
  82. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  83. if "success=1" not in ev:
  84. raise Exception("CAC failed")
  85. if "freq=5260" not in ev:
  86. raise Exception("Unexpected DFS freq result")
  87. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  88. if not ev:
  89. raise Exception("AP setup timed out")
  90. state = hapd.get_status_field("state")
  91. if state != "ENABLED":
  92. raise Exception("Unexpected interface state")
  93. freq = hapd.get_status_field("freq")
  94. if freq != "5260":
  95. raise Exception("Unexpected frequency")
  96. dev[0].connect("dfs", key_mgmt="NONE")
  97. hwsim_utils.test_connectivity(dev[0], hapd)
  98. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  99. ev = hapd.wait_event(["DFS-RADAR-DETECTED"], timeout=10)
  100. if ev is None:
  101. raise Exception("DFS-RADAR-DETECTED event not reported")
  102. if "freq=5260" not in ev:
  103. raise Exception("Incorrect frequency in radar detected event: " + ev);
  104. ev = hapd.wait_event(["DFS-NEW-CHANNEL"], timeout=70)
  105. if ev is None:
  106. raise Exception("DFS-NEW-CHANNEL event not reported")
  107. if "freq=5260" in ev:
  108. raise Exception("Channel did not change after radar was detected");
  109. ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=70)
  110. if ev is None:
  111. raise Exception("AP-CSA-FINISHED event not reported")
  112. if "freq=5260" in ev:
  113. raise Exception("Channel did not change after radar was detected(2)");
  114. time.sleep(1)
  115. hwsim_utils.test_connectivity(dev[0], hapd)
  116. finally:
  117. dev[0].request("DISCONNECT")
  118. if hapd:
  119. hapd.request("DISABLE")
  120. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  121. dev[0].flush_scan_cache()
  122. def test_dfs_radar(dev, apdev):
  123. """DFS CAC functionality with radar detected"""
  124. try:
  125. hapd2 = None
  126. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  127. if hapd is None:
  128. if not os.path.exists("dfs"):
  129. return "skip"
  130. raise Exception("Failed to start DFS AP")
  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(['sudo', '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 = start_dfs_ap(apdev[0], chanlist="40 44", allow_failure=True)
  198. if hapd is None:
  199. if not os.path.exists("dfs"):
  200. return "skip"
  201. raise Exception("Failed to start DFS AP")
  202. time.sleep(1)
  203. dfs_simulate_radar(hapd)
  204. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  205. if ev is None:
  206. raise Exception("Timeout on DFS aborted event")
  207. if "success=0 freq=5260" not in ev:
  208. raise Exception("Unexpected DFS aborted event contents: " + ev)
  209. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  210. if "freq=5260" not in ev:
  211. raise Exception("Unexpected DFS radar detection freq")
  212. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  213. if "freq=5200 chan=40" not in ev and "freq=5220 chan=44" not in ev:
  214. raise Exception("Unexpected DFS new freq: " + ev)
  215. ev = wait_dfs_event(hapd, None, 5)
  216. if "AP-ENABLED" not in ev:
  217. raise Exception("Unexpected DFS event")
  218. dev[0].connect("dfs", key_mgmt="NONE")
  219. finally:
  220. dev[0].request("DISCONNECT")
  221. if hapd:
  222. hapd.request("DISABLE")
  223. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  224. dev[0].flush_scan_cache()
  225. def test_dfs_radar_chanlist_vht80(dev, apdev):
  226. """DFS chanlist when radar is detected and VHT80 configured"""
  227. try:
  228. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40=True, vht80=True,
  229. allow_failure=True)
  230. if hapd is None:
  231. if not os.path.exists("dfs"):
  232. return "skip"
  233. raise Exception("Failed to start DFS AP")
  234. time.sleep(1)
  235. dfs_simulate_radar(hapd)
  236. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  237. if ev is None:
  238. raise Exception("Timeout on DFS aborted event")
  239. if "success=0 freq=5260" not in ev:
  240. raise Exception("Unexpected DFS aborted event contents: " + ev)
  241. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  242. if "freq=5260" not in ev:
  243. raise Exception("Unexpected DFS radar detection freq")
  244. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  245. if "freq=5180 chan=36 sec_chan=1" not in ev:
  246. raise Exception("Unexpected DFS new freq: " + ev)
  247. ev = wait_dfs_event(hapd, None, 5)
  248. if "AP-ENABLED" not in ev:
  249. raise Exception("Unexpected DFS event")
  250. dev[0].connect("dfs", key_mgmt="NONE")
  251. if hapd.get_status_field('vht_oper_centr_freq_seg0_idx') != "42":
  252. raise Exception("Unexpected seg0 idx")
  253. finally:
  254. dev[0].request("DISCONNECT")
  255. if hapd:
  256. hapd.request("DISABLE")
  257. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  258. dev[0].flush_scan_cache()
  259. def test_dfs_radar_chanlist_vht20(dev, apdev):
  260. """DFS chanlist when radar is detected and VHT40 configured"""
  261. try:
  262. hapd = start_dfs_ap(apdev[0], chanlist="36", vht20=True,
  263. allow_failure=True)
  264. if hapd is None:
  265. if not os.path.exists("dfs"):
  266. return "skip"
  267. raise Exception("Failed to start DFS AP")
  268. time.sleep(1)
  269. dfs_simulate_radar(hapd)
  270. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  271. if ev is None:
  272. raise Exception("Timeout on DFS aborted event")
  273. if "success=0 freq=5260" not in ev:
  274. raise Exception("Unexpected DFS aborted event contents: " + ev)
  275. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  276. if "freq=5260" not in ev:
  277. raise Exception("Unexpected DFS radar detection freq")
  278. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  279. if "freq=5180 chan=36 sec_chan=0" not in ev:
  280. raise Exception("Unexpected DFS new freq: " + ev)
  281. ev = wait_dfs_event(hapd, None, 5)
  282. if "AP-ENABLED" not in ev:
  283. raise Exception("Unexpected DFS event")
  284. dev[0].connect("dfs", key_mgmt="NONE")
  285. finally:
  286. dev[0].request("DISCONNECT")
  287. if hapd:
  288. hapd.request("DISABLE")
  289. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  290. dev[0].flush_scan_cache()
  291. def test_dfs_radar_no_ht(dev, apdev):
  292. """DFS chanlist when radar is detected and no HT configured"""
  293. try:
  294. hapd = start_dfs_ap(apdev[0], chanlist="36", ht=False,
  295. allow_failure=True)
  296. if hapd is None:
  297. if not os.path.exists("dfs"):
  298. return "skip"
  299. raise Exception("Failed to start DFS AP")
  300. time.sleep(1)
  301. dfs_simulate_radar(hapd)
  302. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  303. if ev is None:
  304. raise Exception("Timeout on DFS aborted event")
  305. if "success=0 freq=5260" not in ev:
  306. raise Exception("Unexpected DFS aborted event contents: " + ev)
  307. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  308. if "freq=5260 ht_enabled=0" not in ev:
  309. raise Exception("Unexpected DFS radar detection freq: " + ev)
  310. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  311. if "freq=5180 chan=36 sec_chan=0" not in ev:
  312. raise Exception("Unexpected DFS new freq: " + ev)
  313. ev = wait_dfs_event(hapd, None, 5)
  314. if "AP-ENABLED" not in ev:
  315. raise Exception("Unexpected DFS event")
  316. dev[0].connect("dfs", key_mgmt="NONE")
  317. finally:
  318. dev[0].request("DISCONNECT")
  319. if hapd:
  320. hapd.request("DISABLE")
  321. subprocess.call(['iw', 'reg', 'set', '00'])
  322. dev[0].flush_scan_cache()
  323. def test_dfs_radar_ht40minus(dev, apdev):
  324. """DFS chanlist when radar is detected and HT40- configured"""
  325. try:
  326. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40minus=True,
  327. allow_failure=True)
  328. if hapd is None:
  329. if not os.path.exists("dfs"):
  330. return "skip"
  331. raise Exception("Failed to start DFS AP")
  332. time.sleep(1)
  333. dfs_simulate_radar(hapd)
  334. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  335. if ev is None:
  336. raise Exception("Timeout on DFS aborted event")
  337. if "success=0 freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  338. raise Exception("Unexpected DFS aborted event contents: " + ev)
  339. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  340. if "freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  341. raise Exception("Unexpected DFS radar detection freq: " + ev)
  342. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  343. if "freq=5180 chan=36 sec_chan=1" not in ev:
  344. raise Exception("Unexpected DFS new freq: " + ev)
  345. ev = wait_dfs_event(hapd, None, 5)
  346. if "AP-ENABLED" not in ev:
  347. raise Exception("Unexpected DFS event")
  348. dev[0].connect("dfs", key_mgmt="NONE")
  349. finally:
  350. dev[0].request("DISCONNECT")
  351. if hapd:
  352. hapd.request("DISABLE")
  353. subprocess.call(['iw', 'reg', 'set', '00'])
  354. dev[0].flush_scan_cache()