test_dfs.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  112. def test_dfs_radar(dev, apdev):
  113. """DFS CAC functionality with radar detected"""
  114. try:
  115. hapd = start_dfs_ap(apdev[0])
  116. if hapd is None:
  117. if not os.path.exists("dfs"):
  118. return "skip"
  119. raise Exception("Failed to start DFS AP")
  120. time.sleep(1)
  121. dfs_simulate_radar(hapd)
  122. hapd2 = start_dfs_ap(apdev[1], ssid="dfs2", ht40=True)
  123. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  124. if ev is None:
  125. raise Exception("Timeout on DFS aborted event")
  126. if "success=0 freq=5260" not in ev:
  127. raise Exception("Unexpected DFS aborted event contents: " + ev)
  128. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  129. if "freq=5260" not in ev:
  130. raise Exception("Unexpected DFS radar detection freq")
  131. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  132. if "freq=5260" in ev:
  133. raise Exception("Unexpected DFS new freq")
  134. ev = wait_dfs_event(hapd, None, 5)
  135. if "AP-ENABLED" in ev:
  136. logger.info("Started AP on non-DFS channel")
  137. else:
  138. logger.info("Trying to start AP on another DFS channel")
  139. if "DFS-CAC-START" not in ev:
  140. raise Exception("Unexpected DFS event")
  141. if "freq=5260" in ev:
  142. raise Exception("Unexpected DFS CAC freq")
  143. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  144. if "success=1" not in ev:
  145. raise Exception("CAC failed")
  146. if "freq=5260" in ev:
  147. raise Exception("Unexpected DFS freq result - radar channel")
  148. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  149. if not ev:
  150. raise Exception("AP setup timed out")
  151. state = hapd.get_status_field("state")
  152. if state != "ENABLED":
  153. raise Exception("Unexpected interface state")
  154. freq = hapd.get_status_field("freq")
  155. if freq == "5260":
  156. raise Exception("Unexpected frequency: " + freq)
  157. dev[0].connect("dfs", key_mgmt="NONE")
  158. ev = hapd2.wait_event(["AP-ENABLED"], timeout=70)
  159. if not ev:
  160. raise Exception("AP2 setup timed out")
  161. dfs_simulate_radar(hapd2)
  162. ev = wait_dfs_event(hapd2, "DFS-RADAR-DETECTED", 5)
  163. if "freq=5260 ht_enabled=1 chan_offset=1 chan_width=2" not in ev:
  164. raise Exception("Unexpected DFS radar detection freq from AP2")
  165. ev = wait_dfs_event(hapd2, "DFS-NEW-CHANNEL", 5)
  166. if "freq=5260" in ev:
  167. raise Exception("Unexpected DFS new freq for AP2")
  168. wait_dfs_event(hapd2, None, 5)
  169. finally:
  170. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  171. def test_dfs_radar_on_non_dfs_channel(dev, apdev):
  172. """DFS radar detection test code on non-DFS channel"""
  173. params = { "ssid": "radar" }
  174. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  175. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  176. hapd.request("RADAR DETECTED freq=2412 ht_enabled=1 chan_width=1")
  177. def test_dfs_radar_chanlist(dev, apdev):
  178. """DFS chanlist when radar is detected"""
  179. try:
  180. hapd = start_dfs_ap(apdev[0], chanlist="40 44")
  181. if hapd is None:
  182. if not os.path.exists("dfs"):
  183. return "skip"
  184. raise Exception("Failed to start DFS AP")
  185. time.sleep(1)
  186. dfs_simulate_radar(hapd)
  187. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  188. if ev is None:
  189. raise Exception("Timeout on DFS aborted event")
  190. if "success=0 freq=5260" not in ev:
  191. raise Exception("Unexpected DFS aborted event contents: " + ev)
  192. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  193. if "freq=5260" not in ev:
  194. raise Exception("Unexpected DFS radar detection freq")
  195. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  196. if "freq=5200 chan=40" not in ev and "freq=5220 chan=44" not in ev:
  197. raise Exception("Unexpected DFS new freq: " + ev)
  198. ev = wait_dfs_event(hapd, None, 5)
  199. if "AP-ENABLED" not in ev:
  200. raise Exception("Unexpected DFS event")
  201. dev[0].connect("dfs", key_mgmt="NONE")
  202. finally:
  203. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  204. def test_dfs_radar_chanlist_vht80(dev, apdev):
  205. """DFS chanlist when radar is detected and VHT80 configured"""
  206. try:
  207. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40=True, vht80=True)
  208. if hapd is None:
  209. if not os.path.exists("dfs"):
  210. return "skip"
  211. raise Exception("Failed to start DFS AP")
  212. time.sleep(1)
  213. dfs_simulate_radar(hapd)
  214. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  215. if ev is None:
  216. raise Exception("Timeout on DFS aborted event")
  217. if "success=0 freq=5260" not in ev:
  218. raise Exception("Unexpected DFS aborted event contents: " + ev)
  219. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  220. if "freq=5260" not in ev:
  221. raise Exception("Unexpected DFS radar detection freq")
  222. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  223. if "freq=5180 chan=36 sec_chan=1" not in ev:
  224. raise Exception("Unexpected DFS new freq: " + ev)
  225. ev = wait_dfs_event(hapd, None, 5)
  226. if "AP-ENABLED" not in ev:
  227. raise Exception("Unexpected DFS event")
  228. dev[0].connect("dfs", key_mgmt="NONE")
  229. if hapd.get_status_field('vht_oper_centr_freq_seg0_idx') != "42":
  230. raise Exception("Unexpected seg0 idx")
  231. finally:
  232. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  233. def test_dfs_radar_chanlist_vht20(dev, apdev):
  234. """DFS chanlist when radar is detected and VHT40 configured"""
  235. try:
  236. hapd = start_dfs_ap(apdev[0], chanlist="36", vht20=True)
  237. if hapd is None:
  238. if not os.path.exists("dfs"):
  239. return "skip"
  240. raise Exception("Failed to start DFS AP")
  241. time.sleep(1)
  242. dfs_simulate_radar(hapd)
  243. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  244. if ev is None:
  245. raise Exception("Timeout on DFS aborted event")
  246. if "success=0 freq=5260" not in ev:
  247. raise Exception("Unexpected DFS aborted event contents: " + ev)
  248. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  249. if "freq=5260" not in ev:
  250. raise Exception("Unexpected DFS radar detection freq")
  251. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  252. if "freq=5180 chan=36 sec_chan=0" not in ev:
  253. raise Exception("Unexpected DFS new freq: " + ev)
  254. ev = wait_dfs_event(hapd, None, 5)
  255. if "AP-ENABLED" not in ev:
  256. raise Exception("Unexpected DFS event")
  257. dev[0].connect("dfs", key_mgmt="NONE")
  258. finally:
  259. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])