test_dfs.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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", ht40=False, vht80=False, vht20=False, chanlist=None):
  24. ifname = ap['ifname']
  25. logger.info("Starting AP " + ifname + " on DFS channel")
  26. hapd_global = hostapd.HostapdGlobal()
  27. hapd_global.remove(ifname)
  28. hapd_global.add(ifname)
  29. hapd = hostapd.Hostapd(ifname)
  30. if not hapd.ping():
  31. raise Exception("Could not ping hostapd")
  32. hapd.set_defaults()
  33. hapd.set("ssid", ssid)
  34. hapd.set("country_code", "FI")
  35. hapd.set("ieee80211d", "1")
  36. hapd.set("ieee80211h", "1")
  37. hapd.set("hw_mode", "a")
  38. hapd.set("channel", "52")
  39. if ht40:
  40. hapd.set("ht_capab", "[HT40+]")
  41. if vht80:
  42. hapd.set("ieee80211ac", "1")
  43. hapd.set("vht_oper_chwidth", "1")
  44. hapd.set("vht_oper_centr_freq_seg0_idx", "58")
  45. if vht20:
  46. hapd.set("ieee80211ac", "1")
  47. hapd.set("vht_oper_chwidth", "0")
  48. hapd.set("vht_oper_centr_freq_seg0_idx", "0")
  49. if chanlist:
  50. hapd.set("chanlist", chanlist)
  51. hapd.enable()
  52. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  53. if "DFS-CAC-START" not in ev:
  54. raise Exception("Unexpected DFS event")
  55. state = hapd.get_status_field("state")
  56. if state != "DFS":
  57. if allow_failure:
  58. logger.info("Interface state not DFS: " + state)
  59. return None
  60. raise Exception("Unexpected interface state: " + state)
  61. return hapd
  62. def dfs_simulate_radar(hapd):
  63. logger.info("Trigger a simulated radar event")
  64. phyname = hapd.get_driver_status_field("phyname")
  65. radar_file = '/sys/kernel/debug/ieee80211/' + phyname + '/hwsim/dfs_simulate_radar'
  66. with open(radar_file, 'w') as f:
  67. f.write('1')
  68. def test_dfs(dev, apdev):
  69. """DFS CAC functionality on clear channel"""
  70. try:
  71. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  72. if hapd is None:
  73. if not os.path.exists("dfs"):
  74. return "skip"
  75. raise Exception("Failed to start DFS AP")
  76. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  77. if "success=1" not in ev:
  78. raise Exception("CAC failed")
  79. if "freq=5260" not in ev:
  80. raise Exception("Unexpected DFS freq result")
  81. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  82. if not ev:
  83. raise Exception("AP setup timed out")
  84. state = hapd.get_status_field("state")
  85. if state != "ENABLED":
  86. raise Exception("Unexpected interface state")
  87. freq = hapd.get_status_field("freq")
  88. if freq != "5260":
  89. raise Exception("Unexpected frequency")
  90. dev[0].connect("dfs", key_mgmt="NONE")
  91. hwsim_utils.test_connectivity(dev[0], hapd)
  92. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  93. ev = hapd.wait_event(["DFS-RADAR-DETECTED"], timeout=10)
  94. if ev is None:
  95. raise Exception("DFS-RADAR-DETECTED event not reported")
  96. if "freq=5260" not in ev:
  97. raise Exception("Incorrect frequency in radar detected event: " + ev);
  98. ev = hapd.wait_event(["DFS-NEW-CHANNEL"], timeout=70)
  99. if ev is None:
  100. raise Exception("DFS-NEW-CHANNEL event not reported")
  101. if "freq=5260" in ev:
  102. raise Exception("Channel did not change after radar was detected");
  103. ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=70)
  104. if ev is None:
  105. raise Exception("AP-CSA-FINISHED event not reported")
  106. if "freq=5260" in ev:
  107. raise Exception("Channel did not change after radar was detected(2)");
  108. time.sleep(1)
  109. hwsim_utils.test_connectivity(dev[0], hapd)
  110. finally:
  111. dev[0].request("DISCONNECT")
  112. if hapd:
  113. hapd.request("DISABLE")
  114. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  115. dev[0].flush_scan_cache()
  116. def test_dfs_radar(dev, apdev):
  117. """DFS CAC functionality with radar detected"""
  118. try:
  119. hapd2 = None
  120. hapd = start_dfs_ap(apdev[0])
  121. if hapd is None:
  122. if not os.path.exists("dfs"):
  123. return "skip"
  124. raise Exception("Failed to start DFS AP")
  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(['sudo', '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]['ifname'], 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 = start_dfs_ap(apdev[0], chanlist="40 44")
  192. if hapd is None:
  193. if not os.path.exists("dfs"):
  194. return "skip"
  195. raise Exception("Failed to start DFS AP")
  196. time.sleep(1)
  197. dfs_simulate_radar(hapd)
  198. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  199. if ev is None:
  200. raise Exception("Timeout on DFS aborted event")
  201. if "success=0 freq=5260" not in ev:
  202. raise Exception("Unexpected DFS aborted event contents: " + ev)
  203. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  204. if "freq=5260" not in ev:
  205. raise Exception("Unexpected DFS radar detection freq")
  206. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  207. if "freq=5200 chan=40" not in ev and "freq=5220 chan=44" not in ev:
  208. raise Exception("Unexpected DFS new freq: " + ev)
  209. ev = wait_dfs_event(hapd, None, 5)
  210. if "AP-ENABLED" not in ev:
  211. raise Exception("Unexpected DFS event")
  212. dev[0].connect("dfs", key_mgmt="NONE")
  213. finally:
  214. dev[0].request("DISCONNECT")
  215. if hapd:
  216. hapd.request("DISABLE")
  217. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  218. dev[0].flush_scan_cache()
  219. def test_dfs_radar_chanlist_vht80(dev, apdev):
  220. """DFS chanlist when radar is detected and VHT80 configured"""
  221. try:
  222. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40=True, vht80=True)
  223. if hapd is None:
  224. if not os.path.exists("dfs"):
  225. return "skip"
  226. raise Exception("Failed to start DFS AP")
  227. time.sleep(1)
  228. dfs_simulate_radar(hapd)
  229. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  230. if ev is None:
  231. raise Exception("Timeout on DFS aborted event")
  232. if "success=0 freq=5260" not in ev:
  233. raise Exception("Unexpected DFS aborted event contents: " + ev)
  234. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  235. if "freq=5260" not in ev:
  236. raise Exception("Unexpected DFS radar detection freq")
  237. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  238. if "freq=5180 chan=36 sec_chan=1" not in ev:
  239. raise Exception("Unexpected DFS new freq: " + ev)
  240. ev = wait_dfs_event(hapd, None, 5)
  241. if "AP-ENABLED" not in ev:
  242. raise Exception("Unexpected DFS event")
  243. dev[0].connect("dfs", key_mgmt="NONE")
  244. if hapd.get_status_field('vht_oper_centr_freq_seg0_idx') != "42":
  245. raise Exception("Unexpected seg0 idx")
  246. finally:
  247. dev[0].request("DISCONNECT")
  248. if hapd:
  249. hapd.request("DISABLE")
  250. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  251. dev[0].flush_scan_cache()
  252. def test_dfs_radar_chanlist_vht20(dev, apdev):
  253. """DFS chanlist when radar is detected and VHT40 configured"""
  254. try:
  255. hapd = start_dfs_ap(apdev[0], chanlist="36", vht20=True)
  256. if hapd is None:
  257. if not os.path.exists("dfs"):
  258. return "skip"
  259. raise Exception("Failed to start DFS AP")
  260. time.sleep(1)
  261. dfs_simulate_radar(hapd)
  262. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  263. if ev is None:
  264. raise Exception("Timeout on DFS aborted event")
  265. if "success=0 freq=5260" not in ev:
  266. raise Exception("Unexpected DFS aborted event contents: " + ev)
  267. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  268. if "freq=5260" not in ev:
  269. raise Exception("Unexpected DFS radar detection freq")
  270. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  271. if "freq=5180 chan=36 sec_chan=0" not in ev:
  272. raise Exception("Unexpected DFS new freq: " + ev)
  273. ev = wait_dfs_event(hapd, None, 5)
  274. if "AP-ENABLED" not in ev:
  275. raise Exception("Unexpected DFS event")
  276. dev[0].connect("dfs", key_mgmt="NONE")
  277. finally:
  278. dev[0].request("DISCONNECT")
  279. if hapd:
  280. hapd.request("DISABLE")
  281. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  282. dev[0].flush_scan_cache()