test_dfs.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. ifname = ap['ifname']
  27. logger.info("Starting AP " + ifname + " on DFS channel")
  28. hapd_global = hostapd.HostapdGlobal()
  29. hapd_global.remove(ifname)
  30. hapd_global.add(ifname)
  31. hapd = hostapd.Hostapd(ifname)
  32. if not hapd.ping():
  33. raise Exception("Could not ping hostapd")
  34. hapd.set_defaults()
  35. hapd.set("ssid", ssid)
  36. hapd.set("country_code", "FI")
  37. hapd.set("ieee80211d", "1")
  38. hapd.set("ieee80211h", "1")
  39. hapd.set("hw_mode", "a")
  40. hapd.set("channel", "52")
  41. if not ht:
  42. hapd.set("ieee80211n", "0")
  43. if ht40:
  44. hapd.set("ht_capab", "[HT40+]")
  45. elif ht40minus:
  46. hapd.set("ht_capab", "[HT40-]")
  47. hapd.set("channel", "56")
  48. if vht80:
  49. hapd.set("ieee80211ac", "1")
  50. hapd.set("vht_oper_chwidth", "1")
  51. hapd.set("vht_oper_centr_freq_seg0_idx", "58")
  52. if vht20:
  53. hapd.set("ieee80211ac", "1")
  54. hapd.set("vht_oper_chwidth", "0")
  55. hapd.set("vht_oper_centr_freq_seg0_idx", "0")
  56. if chanlist:
  57. hapd.set("chanlist", chanlist)
  58. hapd.enable()
  59. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  60. if "DFS-CAC-START" not in ev:
  61. raise Exception("Unexpected DFS event")
  62. state = hapd.get_status_field("state")
  63. if state != "DFS":
  64. if allow_failure:
  65. logger.info("Interface state not DFS: " + state)
  66. if not os.path.exists("dfs"):
  67. raise HwsimSkip("Assume DFS testing not supported")
  68. raise Exception("Failed to start DFS AP")
  69. raise Exception("Unexpected interface state: " + state)
  70. return hapd
  71. def dfs_simulate_radar(hapd):
  72. logger.info("Trigger a simulated radar event")
  73. phyname = hapd.get_driver_status_field("phyname")
  74. radar_file = '/sys/kernel/debug/ieee80211/' + phyname + '/hwsim/dfs_simulate_radar'
  75. with open(radar_file, 'w') as f:
  76. f.write('1')
  77. def test_dfs(dev, apdev):
  78. """DFS CAC functionality on clear channel"""
  79. try:
  80. hapd = None
  81. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  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(['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. hapd = None
  126. hapd2 = None
  127. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  128. time.sleep(1)
  129. dfs_simulate_radar(hapd)
  130. hapd2 = start_dfs_ap(apdev[1], ssid="dfs2", ht40=True)
  131. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  132. if ev is None:
  133. raise Exception("Timeout on DFS aborted event")
  134. if "success=0 freq=5260" not in ev:
  135. raise Exception("Unexpected DFS aborted event contents: " + ev)
  136. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  137. if "freq=5260" not in ev:
  138. raise Exception("Unexpected DFS radar detection freq")
  139. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  140. if "freq=5260" in ev:
  141. raise Exception("Unexpected DFS new freq")
  142. ev = wait_dfs_event(hapd, None, 5)
  143. if "AP-ENABLED" in ev:
  144. logger.info("Started AP on non-DFS channel")
  145. else:
  146. logger.info("Trying to start AP on another DFS channel")
  147. if "DFS-CAC-START" not in ev:
  148. raise Exception("Unexpected DFS event")
  149. if "freq=5260" in ev:
  150. raise Exception("Unexpected DFS CAC freq")
  151. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  152. if "success=1" not in ev:
  153. raise Exception("CAC failed")
  154. if "freq=5260" in ev:
  155. raise Exception("Unexpected DFS freq result - radar channel")
  156. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  157. if not ev:
  158. raise Exception("AP setup timed out")
  159. state = hapd.get_status_field("state")
  160. if state != "ENABLED":
  161. raise Exception("Unexpected interface state")
  162. freq = hapd.get_status_field("freq")
  163. if freq == "5260":
  164. raise Exception("Unexpected frequency: " + freq)
  165. dev[0].connect("dfs", key_mgmt="NONE")
  166. ev = hapd2.wait_event(["AP-ENABLED"], timeout=70)
  167. if not ev:
  168. raise Exception("AP2 setup timed out")
  169. dfs_simulate_radar(hapd2)
  170. ev = wait_dfs_event(hapd2, "DFS-RADAR-DETECTED", 5)
  171. if "freq=5260 ht_enabled=1 chan_offset=1 chan_width=2" not in ev:
  172. raise Exception("Unexpected DFS radar detection freq from AP2")
  173. ev = wait_dfs_event(hapd2, "DFS-NEW-CHANNEL", 5)
  174. if "freq=5260" in ev:
  175. raise Exception("Unexpected DFS new freq for AP2")
  176. wait_dfs_event(hapd2, None, 5)
  177. finally:
  178. dev[0].request("DISCONNECT")
  179. if hapd:
  180. hapd.request("DISABLE")
  181. if hapd2:
  182. hapd2.request("DISABLE")
  183. subprocess.call(['iw', 'reg', 'set', '00'])
  184. dev[0].flush_scan_cache()
  185. def test_dfs_radar_on_non_dfs_channel(dev, apdev):
  186. """DFS radar detection test code on non-DFS channel"""
  187. params = { "ssid": "radar" }
  188. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  189. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  190. hapd.request("RADAR DETECTED freq=2412 ht_enabled=1 chan_width=1")
  191. def test_dfs_radar_chanlist(dev, apdev):
  192. """DFS chanlist when radar is detected"""
  193. try:
  194. hapd = None
  195. hapd = start_dfs_ap(apdev[0], chanlist="40 44", allow_failure=True)
  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(['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 = None
  223. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40=True, vht80=True,
  224. allow_failure=True)
  225. time.sleep(1)
  226. dfs_simulate_radar(hapd)
  227. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  228. if ev is None:
  229. raise Exception("Timeout on DFS aborted event")
  230. if "success=0 freq=5260" not in ev:
  231. raise Exception("Unexpected DFS aborted event contents: " + ev)
  232. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  233. if "freq=5260" not in ev:
  234. raise Exception("Unexpected DFS radar detection freq")
  235. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  236. if "freq=5180 chan=36 sec_chan=1" not in ev:
  237. raise Exception("Unexpected DFS new freq: " + ev)
  238. ev = wait_dfs_event(hapd, None, 5)
  239. if "AP-ENABLED" not in ev:
  240. raise Exception("Unexpected DFS event")
  241. dev[0].connect("dfs", key_mgmt="NONE")
  242. if hapd.get_status_field('vht_oper_centr_freq_seg0_idx') != "42":
  243. raise Exception("Unexpected seg0 idx")
  244. finally:
  245. dev[0].request("DISCONNECT")
  246. if hapd:
  247. hapd.request("DISABLE")
  248. subprocess.call(['iw', 'reg', 'set', '00'])
  249. dev[0].flush_scan_cache()
  250. def test_dfs_radar_chanlist_vht20(dev, apdev):
  251. """DFS chanlist when radar is detected and VHT40 configured"""
  252. try:
  253. hapd = None
  254. hapd = start_dfs_ap(apdev[0], chanlist="36", vht20=True,
  255. allow_failure=True)
  256. time.sleep(1)
  257. dfs_simulate_radar(hapd)
  258. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  259. if ev is None:
  260. raise Exception("Timeout on DFS aborted event")
  261. if "success=0 freq=5260" not in ev:
  262. raise Exception("Unexpected DFS aborted event contents: " + ev)
  263. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  264. if "freq=5260" not in ev:
  265. raise Exception("Unexpected DFS radar detection freq")
  266. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  267. if "freq=5180 chan=36 sec_chan=0" not in ev:
  268. raise Exception("Unexpected DFS new freq: " + ev)
  269. ev = wait_dfs_event(hapd, None, 5)
  270. if "AP-ENABLED" not in ev:
  271. raise Exception("Unexpected DFS event")
  272. dev[0].connect("dfs", key_mgmt="NONE")
  273. finally:
  274. dev[0].request("DISCONNECT")
  275. if hapd:
  276. hapd.request("DISABLE")
  277. subprocess.call(['iw', 'reg', 'set', '00'])
  278. dev[0].flush_scan_cache()
  279. def test_dfs_radar_no_ht(dev, apdev):
  280. """DFS chanlist when radar is detected and no HT configured"""
  281. try:
  282. hapd = None
  283. hapd = start_dfs_ap(apdev[0], chanlist="36", ht=False,
  284. allow_failure=True)
  285. time.sleep(1)
  286. dfs_simulate_radar(hapd)
  287. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  288. if ev is None:
  289. raise Exception("Timeout on DFS aborted event")
  290. if "success=0 freq=5260" not in ev:
  291. raise Exception("Unexpected DFS aborted event contents: " + ev)
  292. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  293. if "freq=5260 ht_enabled=0" not in ev:
  294. raise Exception("Unexpected DFS radar detection freq: " + ev)
  295. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  296. if "freq=5180 chan=36 sec_chan=0" not in ev:
  297. raise Exception("Unexpected DFS new freq: " + ev)
  298. ev = wait_dfs_event(hapd, None, 5)
  299. if "AP-ENABLED" not in ev:
  300. raise Exception("Unexpected DFS event")
  301. dev[0].connect("dfs", key_mgmt="NONE")
  302. finally:
  303. dev[0].request("DISCONNECT")
  304. if hapd:
  305. hapd.request("DISABLE")
  306. subprocess.call(['iw', 'reg', 'set', '00'])
  307. dev[0].flush_scan_cache()
  308. def test_dfs_radar_ht40minus(dev, apdev):
  309. """DFS chanlist when radar is detected and HT40- configured"""
  310. try:
  311. hapd = None
  312. hapd = start_dfs_ap(apdev[0], chanlist="36", ht40minus=True,
  313. allow_failure=True)
  314. time.sleep(1)
  315. dfs_simulate_radar(hapd)
  316. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  317. if ev is None:
  318. raise Exception("Timeout on DFS aborted event")
  319. if "success=0 freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  320. raise Exception("Unexpected DFS aborted event contents: " + ev)
  321. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  322. if "freq=5280 ht_enabled=1 chan_offset=-1" not in ev:
  323. raise Exception("Unexpected DFS radar detection freq: " + ev)
  324. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  325. if "freq=5180 chan=36 sec_chan=1" not in ev:
  326. raise Exception("Unexpected DFS new freq: " + ev)
  327. ev = wait_dfs_event(hapd, None, 5)
  328. if "AP-ENABLED" not in ev:
  329. raise Exception("Unexpected DFS event")
  330. dev[0].connect("dfs", key_mgmt="NONE")
  331. finally:
  332. dev[0].request("DISCONNECT")
  333. if hapd:
  334. hapd.request("DISABLE")
  335. subprocess.call(['iw', 'reg', 'set', '00'])
  336. dev[0].flush_scan_cache()