test_dfs.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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" ]
  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):
  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", "dfs")
  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. hapd.enable()
  40. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  41. if "DFS-CAC-START" not in ev:
  42. raise Exception("Unexpected DFS event")
  43. state = hapd.get_status_field("state")
  44. if state != "DFS":
  45. if allow_failure:
  46. logger.info("Interface state not DFS: " + state)
  47. return None
  48. raise Exception("Unexpected interface state: " + state)
  49. return hapd
  50. def test_dfs(dev, apdev):
  51. """DFS CAC functionality on clear channel"""
  52. try:
  53. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  54. if hapd is None:
  55. if not os.path.exists("dfs"):
  56. return "skip"
  57. raise Exception("Failed to start DFS AP")
  58. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  59. if "success=1" not in ev:
  60. raise Exception("CAC failed")
  61. if "freq=5260" not in ev:
  62. raise Exception("Unexpected DFS freq result")
  63. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  64. if not ev:
  65. raise Exception("AP setup timed out")
  66. state = hapd.get_status_field("state")
  67. if state != "ENABLED":
  68. raise Exception("Unexpected interface state")
  69. freq = hapd.get_status_field("freq")
  70. if freq != "5260":
  71. raise Exception("Unexpected frequency")
  72. dev[0].connect("dfs", key_mgmt="NONE")
  73. hwsim_utils.test_connectivity(dev[0], hapd)
  74. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  75. ev = hapd.wait_event(["DFS-RADAR-DETECTED"], timeout=10)
  76. if ev is None:
  77. raise Exception("DFS-RADAR-DETECTED event not reported")
  78. if "freq=5260" not in ev:
  79. raise Exception("Incorrect frequency in radar detected event: " + ev);
  80. ev = hapd.wait_event(["DFS-NEW-CHANNEL"], timeout=70)
  81. if ev is None:
  82. raise Exception("DFS-NEW-CHANNEL event not reported")
  83. if "freq=5260" in ev:
  84. raise Exception("Channel did not change after radar was detected");
  85. ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=70)
  86. if ev is None:
  87. raise Exception("AP-CSA-FINISHED event not reported")
  88. if "freq=5260" in ev:
  89. raise Exception("Channel did not change after radar was detected(2)");
  90. time.sleep(1)
  91. hwsim_utils.test_connectivity(dev[0], hapd)
  92. finally:
  93. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  94. def test_dfs_radar(dev, apdev):
  95. """DFS CAC functionality with radar detected"""
  96. try:
  97. hapd = start_dfs_ap(apdev[0])
  98. if hapd is None:
  99. if not os.path.exists("dfs"):
  100. return "skip"
  101. raise Exception("Failed to start DFS AP")
  102. time.sleep(1)
  103. phyname = hapd.get_driver_status_field("phyname")
  104. radar_file = '/sys/kernel/debug/ieee80211/' + phyname + '/hwsim/dfs_simulate_radar'
  105. cmd = subprocess.Popen(["sudo", "tee", radar_file],
  106. stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  107. cmd.stdin.write("1")
  108. cmd.stdin.close()
  109. cmd.stdout.read()
  110. cmd.stdout.close()
  111. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
  112. if ev is None:
  113. raise Exception("Timeout on DFS aborted event")
  114. if "success=0 freq=5260" not in ev:
  115. raise Exception("Unexpected DFS aborted event contents: " + ev)
  116. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
  117. if "freq=5260" not in ev:
  118. raise Exception("Unexpected DFS radar detection freq")
  119. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  120. if "freq=5260" in ev:
  121. raise Exception("Unexpected DFS new freq")
  122. ev = wait_dfs_event(hapd, None, 5)
  123. if "AP-ENABLED" in ev:
  124. logger.info("Started AP on non-DFS channel")
  125. else:
  126. logger.info("Trying to start AP on another DFS channel")
  127. if "DFS-CAC-START" not in ev:
  128. raise Exception("Unexpected DFS event")
  129. if "freq=5260" in ev:
  130. raise Exception("Unexpected DFS CAC freq")
  131. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  132. if "success=1" not in ev:
  133. raise Exception("CAC failed")
  134. if "freq=5260" in ev:
  135. raise Exception("Unexpected DFS freq result - radar channel")
  136. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  137. if not ev:
  138. raise Exception("AP setup timed out")
  139. state = hapd.get_status_field("state")
  140. if state != "ENABLED":
  141. raise Exception("Unexpected interface state")
  142. freq = hapd.get_status_field("freq")
  143. if freq == "5260":
  144. raise Exception("Unexpected frequency: " + freq)
  145. dev[0].connect("dfs", key_mgmt="NONE")
  146. finally:
  147. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  148. def test_dfs_radar_on_non_dfs_channel(dev, apdev):
  149. """DFS radar detection test code on non-DFS channel"""
  150. params = { "ssid": "radar" }
  151. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  152. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  153. hapd.request("RADAR DETECTED freq=2412 ht_enabled=1 chan_width=1")