test_dfs.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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])
  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")
  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. if hapd is None:
  230. if not os.path.exists("dfs"):
  231. return "skip"
  232. raise Exception("Failed to start DFS AP")
  233. time.sleep(1)
  234. dfs_simulate_radar(hapd)
  235. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  236. if ev is None:
  237. raise Exception("Timeout on DFS aborted event")
  238. if "success=0 freq=5260" not in ev:
  239. raise Exception("Unexpected DFS aborted event contents: " + ev)
  240. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  241. if "freq=5260" not in ev:
  242. raise Exception("Unexpected DFS radar detection freq")
  243. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  244. if "freq=5180 chan=36 sec_chan=1" not in ev:
  245. raise Exception("Unexpected DFS new freq: " + ev)
  246. ev = wait_dfs_event(hapd, None, 5)
  247. if "AP-ENABLED" not in ev:
  248. raise Exception("Unexpected DFS event")
  249. dev[0].connect("dfs", key_mgmt="NONE")
  250. if hapd.get_status_field('vht_oper_centr_freq_seg0_idx') != "42":
  251. raise Exception("Unexpected seg0 idx")
  252. finally:
  253. dev[0].request("DISCONNECT")
  254. if hapd:
  255. hapd.request("DISABLE")
  256. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  257. dev[0].flush_scan_cache()
  258. def test_dfs_radar_chanlist_vht20(dev, apdev):
  259. """DFS chanlist when radar is detected and VHT40 configured"""
  260. try:
  261. hapd = start_dfs_ap(apdev[0], chanlist="36", vht20=True)
  262. if hapd is None:
  263. if not os.path.exists("dfs"):
  264. return "skip"
  265. raise Exception("Failed to start DFS AP")
  266. time.sleep(1)
  267. dfs_simulate_radar(hapd)
  268. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  269. if ev is None:
  270. raise Exception("Timeout on DFS aborted event")
  271. if "success=0 freq=5260" not in ev:
  272. raise Exception("Unexpected DFS aborted event contents: " + ev)
  273. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  274. if "freq=5260" not in ev:
  275. raise Exception("Unexpected DFS radar detection freq")
  276. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  277. if "freq=5180 chan=36 sec_chan=0" not in ev:
  278. raise Exception("Unexpected DFS new freq: " + ev)
  279. ev = wait_dfs_event(hapd, None, 5)
  280. if "AP-ENABLED" not in ev:
  281. raise Exception("Unexpected DFS event")
  282. dev[0].connect("dfs", key_mgmt="NONE")
  283. finally:
  284. dev[0].request("DISCONNECT")
  285. if hapd:
  286. hapd.request("DISABLE")
  287. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  288. dev[0].flush_scan_cache()
  289. def test_dfs_radar_no_ht(dev, apdev):
  290. """DFS chanlist when radar is detected and no HT configured"""
  291. try:
  292. hapd = start_dfs_ap(apdev[0], chanlist="36", ht=False)
  293. if hapd is None:
  294. if not os.path.exists("dfs"):
  295. return "skip"
  296. raise Exception("Failed to start DFS AP")
  297. time.sleep(1)
  298. dfs_simulate_radar(hapd)
  299. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  300. if ev is None:
  301. raise Exception("Timeout on DFS aborted event")
  302. if "success=0 freq=5260" not in ev:
  303. raise Exception("Unexpected DFS aborted event contents: " + ev)
  304. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  305. if "freq=5260 ht_enabled=0" not in ev:
  306. raise Exception("Unexpected DFS radar detection freq: " + ev)
  307. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  308. if "freq=5180 chan=36 sec_chan=0" not in ev:
  309. raise Exception("Unexpected DFS new freq: " + ev)
  310. ev = wait_dfs_event(hapd, None, 5)
  311. if "AP-ENABLED" not in ev:
  312. raise Exception("Unexpected DFS event")
  313. dev[0].connect("dfs", key_mgmt="NONE")
  314. finally:
  315. dev[0].request("DISCONNECT")
  316. if hapd:
  317. hapd.request("DISABLE")
  318. subprocess.call(['iw', 'reg', 'set', '00'])
  319. dev[0].flush_scan_cache()
  320. def test_dfs_radar_ht40minus(dev, apdev):
  321. """DFS chanlist when radar is detected and HT40- configured"""
  322. try:
  323. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40minus=True)
  324. if hapd is None:
  325. if not os.path.exists("dfs"):
  326. return "skip"
  327. raise Exception("Failed to start DFS AP")
  328. time.sleep(1)
  329. dfs_simulate_radar(hapd)
  330. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  331. if ev is None:
  332. raise Exception("Timeout on DFS aborted event")
  333. if "success=0 freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  334. raise Exception("Unexpected DFS aborted event contents: " + ev)
  335. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  336. if "freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  337. raise Exception("Unexpected DFS radar detection freq: " + ev)
  338. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  339. if "freq=5180 chan=36 sec_chan=1" not in ev:
  340. raise Exception("Unexpected DFS new freq: " + ev)
  341. ev = wait_dfs_event(hapd, None, 5)
  342. if "AP-ENABLED" not in ev:
  343. raise Exception("Unexpected DFS event")
  344. dev[0].connect("dfs", key_mgmt="NONE")
  345. finally:
  346. dev[0].request("DISCONNECT")
  347. if hapd:
  348. hapd.request("DISABLE")
  349. subprocess.call(['iw', 'reg', 'set', '00'])
  350. dev[0].flush_scan_cache()