test_wmediumd.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. # wmediumd sanity checks
  2. # Copyright (c) 2015, Intel Deutschland GmbH
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import tempfile, os, subprocess, errno, hwsim_utils, time
  7. from utils import HwsimSkip
  8. from wpasupplicant import WpaSupplicant
  9. from tshark import run_tshark
  10. from test_ap_open import _test_ap_open
  11. from test_wpas_mesh import check_mesh_support, check_mesh_group_added
  12. from test_wpas_mesh import check_mesh_peer_connected, add_open_mesh_network
  13. from test_wpas_mesh import check_mesh_group_removed
  14. class LocalVariables:
  15. revs = []
  16. CFG = """
  17. ifaces :
  18. {
  19. ids = ["%s", "%s" ];
  20. links = (
  21. (0, 1, 30)
  22. );
  23. };
  24. """
  25. CFG2 = """
  26. ifaces :
  27. {
  28. ids = ["%s", "%s", "%s"];
  29. };
  30. model:
  31. {
  32. type = "prob";
  33. links = (
  34. (0, 1, 0.000000),
  35. (0, 2, 0.000000),
  36. (1, 2, 1.000000)
  37. );
  38. };
  39. """
  40. CFG3 = """
  41. ifaces :
  42. {
  43. ids = ["%s", "%s", "%s", "%s", "%s" ];
  44. };
  45. model:
  46. {
  47. type = "prob";
  48. default_prob = 1.0;
  49. links = (
  50. (0, 1, 0.000000),
  51. (1, 2, 0.000000),
  52. (2, 3, 0.000000),
  53. (3, 4, 0.000000)
  54. );
  55. };
  56. """
  57. def get_wmediumd_version():
  58. if len(LocalVariables.revs) > 0:
  59. return LocalVariables.revs;
  60. try:
  61. verstr = subprocess.check_output(['wmediumd', '-V'])
  62. except OSError, e:
  63. if e.errno == errno.ENOENT:
  64. raise HwsimSkip('wmediumd not available')
  65. raise
  66. vernum = verstr.split(' ')[1][1:]
  67. LocalVariables.revs = vernum.split('.')
  68. for i in range(0, len(LocalVariables.revs)):
  69. LocalVariables.revs[i] = int(LocalVariables.revs[i])
  70. while len(LocalVariables.revs) < 3:
  71. LocalVariables.revs += [0]
  72. return LocalVariables.revs;
  73. def require_wmediumd_version(major, minor, patch):
  74. revs = get_wmediumd_version()
  75. if revs[0] < major or revs[1] < minor or revs[2] < patch:
  76. raise HwsimSkip('wmediumd v%s.%s.%s is too old for this test' %
  77. (revs[0], revs[1], revs[2]))
  78. def output_wmediumd_log(p, params, data):
  79. log_file = open(os.path.abspath(os.path.join(params['logdir'],
  80. 'wmediumd.log')), 'a')
  81. log_file.write(data)
  82. log_file.close()
  83. def start_wmediumd(fn, params):
  84. try:
  85. p = subprocess.Popen(['wmediumd', '-c', fn],
  86. stdout=subprocess.PIPE,
  87. stderr=subprocess.STDOUT)
  88. except OSError, e:
  89. if e.errno == errno.ENOENT:
  90. raise HwsimSkip('wmediumd not available')
  91. raise
  92. logs = ''
  93. while True:
  94. line = p.stdout.readline()
  95. if not line:
  96. output_wmediumd_log(p, params, logs)
  97. raise Exception('wmediumd was terminated unexpectedly')
  98. if line.find('REGISTER SENT!') > -1:
  99. break
  100. logs += line
  101. return p
  102. def stop_wmediumd(p, params):
  103. p.terminate()
  104. p.wait()
  105. stdoutdata, stderrdata = p.communicate()
  106. output_wmediumd_log(p, params, stdoutdata)
  107. def test_wmediumd_simple(dev, apdev, params):
  108. """test a simple wmediumd configuration"""
  109. fd, fn = tempfile.mkstemp()
  110. try:
  111. f = os.fdopen(fd, 'w')
  112. f.write(CFG % (apdev[0]['bssid'], dev[0].own_addr()))
  113. f.close()
  114. p = start_wmediumd(fn, params)
  115. try:
  116. _test_ap_open(dev, apdev)
  117. finally:
  118. stop_wmediumd(p, params)
  119. # test that releasing hwsim works correctly
  120. _test_ap_open(dev, apdev)
  121. finally:
  122. os.unlink(fn)
  123. def test_wmediumd_path_simple(dev, apdev, params):
  124. """test a mesh path"""
  125. # 0 and 1 is connected
  126. # 0 and 2 is connected
  127. # 1 and 2 is not connected
  128. # 1 --- 0 --- 2
  129. # | |
  130. # +-----X-----+
  131. # This tests if 1 and 2 can communicate each other via 0.
  132. require_wmediumd_version(0, 3, 1)
  133. fd, fn = tempfile.mkstemp()
  134. try:
  135. f = os.fdopen(fd, 'w')
  136. f.write(CFG2 % (dev[0].own_addr(), dev[1].own_addr(),
  137. dev[2].own_addr()))
  138. f.close()
  139. p = start_wmediumd(fn, params)
  140. try:
  141. _test_wmediumd_path_simple(dev, apdev)
  142. finally:
  143. stop_wmediumd(p, params)
  144. finally:
  145. os.unlink(fn)
  146. def _test_wmediumd_path_simple(dev, apdev):
  147. for i in range(0, 3):
  148. check_mesh_support(dev[i])
  149. add_open_mesh_network(dev[i], freq="2462", basic_rates="60 120 240")
  150. # Check for mesh joined
  151. for i in range(0, 3):
  152. check_mesh_group_added(dev[i])
  153. state = dev[i].get_status_field("wpa_state")
  154. if state != "COMPLETED":
  155. raise Exception("Unexpected wpa_state on dev" + str(i) + ": " + state)
  156. mode = dev[i].get_status_field("mode")
  157. if mode != "mesh":
  158. raise Exception("Unexpected mode: " + mode)
  159. # Check for peer connected
  160. check_mesh_peer_connected(dev[0])
  161. check_mesh_peer_connected(dev[0])
  162. check_mesh_peer_connected(dev[1])
  163. check_mesh_peer_connected(dev[2])
  164. # Test connectivity 1->2 and 2->1
  165. hwsim_utils.test_connectivity(dev[1], dev[2])
  166. # Check mpath table on 0
  167. res, data = dev[0].cmd_execute(['iw', dev[0].ifname, 'mpath', 'dump'])
  168. if res != 0:
  169. raise Exception("iw command failed on dev0")
  170. if data.find(dev[1].own_addr() + ' ' + dev[1].own_addr()) == -1 or \
  171. data.find(dev[2].own_addr() + ' ' + dev[2].own_addr()) == -1:
  172. raise Exception("mpath not found on dev0:\n" + data)
  173. if data.find(dev[0].own_addr()) > -1:
  174. raise Exception("invalid mpath found on dev0:\n" + data)
  175. # Check mpath table on 1
  176. res, data = dev[1].cmd_execute(['iw', dev[1].ifname, 'mpath', 'dump'])
  177. if res != 0:
  178. raise Exception("iw command failed on dev1")
  179. if data.find(dev[0].own_addr() + ' ' + dev[0].own_addr()) == -1 or \
  180. data.find(dev[2].own_addr() + ' ' + dev[0].own_addr()) == -1:
  181. raise Exception("mpath not found on dev1:\n" + data)
  182. if data.find(dev[2].own_addr() + ' ' + dev[2].own_addr()) > -1 or \
  183. data.find(dev[1].own_addr()) > -1:
  184. raise Exception("invalid mpath found on dev1:\n" + data)
  185. # Check mpath table on 2
  186. res, data = dev[2].cmd_execute(['iw', dev[2].ifname, 'mpath', 'dump'])
  187. if res != 0:
  188. raise Exception("iw command failed on dev2")
  189. if data.find(dev[0].own_addr() + ' ' + dev[0].own_addr()) == -1 or \
  190. data.find(dev[1].own_addr() + ' ' + dev[0].own_addr()) == -1:
  191. raise Exception("mpath not found on dev2:\n" + data)
  192. if data.find(dev[1].own_addr() + ' ' + dev[1].own_addr()) > -1 or \
  193. data.find(dev[2].own_addr()) > -1:
  194. raise Exception("invalid mpath found on dev2:\n" + data)
  195. # remove mesh groups
  196. for i in range(0, 3):
  197. dev[i].mesh_group_remove()
  198. check_mesh_group_removed(dev[i])
  199. dev[i].dump_monitor()
  200. def test_wmediumd_path_ttl(dev, apdev, params):
  201. """Mesh path request TTL"""
  202. # 0 --- 1 --- 2 --- 3 --- 4
  203. # Test the TTL of mesh path request.
  204. # If the TTL is shorter than path, the mesh path request should be dropped.
  205. require_wmediumd_version(0, 3, 1)
  206. local_dev = []
  207. for i in range(0, 3):
  208. local_dev.append(dev[i])
  209. for i in range(5, 7):
  210. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  211. wpas.interface_add("wlan" + str(i))
  212. check_mesh_support(wpas)
  213. temp_dev = wpas.request("MESH_INTERFACE_ADD ifname=mesh" + str(i))
  214. if "FAIL" in temp_dev:
  215. raise Exception("MESH_INTERFACE_ADD failed")
  216. local_dev.append(WpaSupplicant(ifname=temp_dev))
  217. fd, fn = tempfile.mkstemp()
  218. try:
  219. f = os.fdopen(fd, 'w')
  220. f.write(CFG3 % (local_dev[0].own_addr(), local_dev[1].own_addr(),
  221. local_dev[2].own_addr(), local_dev[3].own_addr(),
  222. local_dev[4].own_addr()))
  223. f.close()
  224. p = start_wmediumd(fn, params)
  225. try:
  226. _test_wmediumd_path_ttl(local_dev, True)
  227. _test_wmediumd_path_ttl(local_dev, False)
  228. finally:
  229. stop_wmediumd(p, params)
  230. finally:
  231. os.unlink(fn)
  232. for i in range(5, 7):
  233. wpas.interface_remove("wlan" + str(i))
  234. def _test_wmediumd_path_ttl(dev, ok):
  235. for i in range(0, 5):
  236. check_mesh_support(dev[i])
  237. add_open_mesh_network(dev[i], freq="2462", basic_rates="60 120 240")
  238. # Check for mesh joined
  239. for i in range(0, 5):
  240. check_mesh_group_added(dev[i])
  241. state = dev[i].get_status_field("wpa_state")
  242. if state != "COMPLETED":
  243. raise Exception("Unexpected wpa_state on dev" + str(i) + ": " + state)
  244. mode = dev[i].get_status_field("mode")
  245. if mode != "mesh":
  246. raise Exception("Unexpected mode: " + mode)
  247. # set mesh path request ttl
  248. subprocess.check_call([ "iw", "dev", dev[0].ifname, "set", "mesh_param",
  249. "mesh_element_ttl=" + ("4" if ok else "3") ])
  250. # Check for peer connected
  251. for i in range(0, 5):
  252. check_mesh_peer_connected(dev[i])
  253. for i in range(1, 4):
  254. check_mesh_peer_connected(dev[i])
  255. # Test connectivity 0->4 and 0->4
  256. hwsim_utils.test_connectivity(dev[0], dev[4], success_expected=ok)
  257. # Check mpath table on 0
  258. res, data = dev[0].cmd_execute(['iw', dev[0].ifname, 'mpath', 'dump'])
  259. if res != 0:
  260. raise Exception("iw command failed on dev0")
  261. if ok:
  262. if data.find(dev[1].own_addr() + ' ' + dev[1].own_addr()) == -1 or \
  263. data.find(dev[4].own_addr() + ' ' + dev[1].own_addr()) == -1:
  264. raise Exception("mpath not found on dev0:\n" + data)
  265. else:
  266. if data.find(dev[1].own_addr() + ' ' + dev[1].own_addr()) == -1 or \
  267. data.find(dev[4].own_addr() + ' 00:00:00:00:00:00') == -1:
  268. raise Exception("mpath not found on dev0:\n" + data)
  269. if data.find(dev[0].own_addr()) > -1 or \
  270. data.find(dev[2].own_addr()) > -1 or \
  271. data.find(dev[3].own_addr()) > -1:
  272. raise Exception("invalid mpath found on dev0:\n" + data)
  273. # remove mesh groups
  274. for i in range(0, 3):
  275. dev[i].mesh_group_remove()
  276. check_mesh_group_removed(dev[i])
  277. dev[i].dump_monitor()
  278. def test_wmediumd_path_rann(dev, apdev, params):
  279. """Mesh path with RANN"""
  280. # 0 and 1 is connected
  281. # 0 and 2 is connected
  282. # 1 and 2 is not connected
  283. # 2 is mesh root and RANN enabled
  284. # 1 --- 0 --- 2
  285. # | |
  286. # +-----X-----+
  287. # This tests if 1 and 2 can communicate each other via 0.
  288. require_wmediumd_version(0, 3, 1)
  289. fd, fn = tempfile.mkstemp()
  290. try:
  291. f = os.fdopen(fd, 'w')
  292. f.write(CFG2 % (dev[0].own_addr(), dev[1].own_addr(),
  293. dev[2].own_addr()))
  294. f.close()
  295. p = start_wmediumd(fn, params)
  296. try:
  297. _test_wmediumd_path_rann(dev, apdev)
  298. finally:
  299. stop_wmediumd(p, params)
  300. finally:
  301. os.unlink(fn)
  302. capfile = os.path.join(params['logdir'], "hwsim0.pcapng")
  303. # check Root STA address in root announcement element
  304. filt = "wlan.fc.type_subtype == 0x000d && " + \
  305. "wlan_mgt.fixed.mesh_action == 0x01 && " + \
  306. "wlan_mgt.tag.number == 126"
  307. out = run_tshark(capfile, filt, [ "wlan.rann.root_sta" ])
  308. if out is None:
  309. raise Exception("No captured data found\n")
  310. if out.find(dev[2].own_addr()) == -1 or \
  311. out.find(dev[0].own_addr()) > -1 or \
  312. out.find(dev[1].own_addr()) > -1:
  313. raise Exception("RANN should be sent by dev2 only:\n" + out)
  314. # check RANN interval is in range
  315. filt = "wlan.sa == 02:00:00:00:02:00 && " + \
  316. "wlan.fc.type_subtype == 0x000d && " + \
  317. "wlan_mgt.fixed.mesh_action == 0x01 && " + \
  318. "wlan_mgt.tag.number == 126"
  319. out = run_tshark(capfile, filt, [ "frame.time_relative" ])
  320. if out is None:
  321. raise Exception("No captured data found\n")
  322. lines = out.splitlines()
  323. prev = float(lines[len(lines) - 1])
  324. for i in reversed(range(1, len(lines) - 1)):
  325. now = float(lines[i])
  326. if prev - now < 1.0 or 3.0 < prev - now:
  327. raise Exception("RANN interval " + str(prev - now) +
  328. "(sec) should be close to 2.0(sec)\n")
  329. prev = now
  330. # check no one uses broadcast path request
  331. filt = "wlan.da == ff:ff:ff:ff:ff:ff && " + \
  332. "wlan.fc.type_subtype == 0x000d && " + \
  333. "wlan_mgt.fixed.mesh_action == 0x01 && " + \
  334. "wlan_mgt.tag.number == 130"
  335. out = run_tshark(capfile, filt, [ "wlan.sa", "wlan.da" ])
  336. if out is None:
  337. raise Exception("No captured data found\n")
  338. if len(out) > 0:
  339. raise Exception("invalid broadcast path requests\n" + out)
  340. def _test_wmediumd_path_rann(dev, apdev):
  341. for i in range(0, 3):
  342. check_mesh_support(dev[i])
  343. add_open_mesh_network(dev[i], freq="2462", basic_rates="60 120 240")
  344. # Check for mesh joined
  345. for i in range(0, 3):
  346. check_mesh_group_added(dev[i])
  347. state = dev[i].get_status_field("wpa_state")
  348. if state != "COMPLETED":
  349. raise Exception("Unexpected wpa_state on dev" + str(i) + ": " + state)
  350. mode = dev[i].get_status_field("mode")
  351. if mode != "mesh":
  352. raise Exception("Unexpected mode: " + mode)
  353. # set node 2 as RANN supported root
  354. subprocess.check_call(["iw", "dev", dev[0].ifname, "set", "mesh_param",
  355. "mesh_hwmp_rootmode=0"])
  356. subprocess.check_call(["iw", "dev", dev[1].ifname, "set", "mesh_param",
  357. "mesh_hwmp_rootmode=0"])
  358. subprocess.check_call(["iw", "dev", dev[2].ifname, "set", "mesh_param",
  359. "mesh_hwmp_rootmode=4"])
  360. subprocess.check_call(["iw", "dev", dev[2].ifname, "set", "mesh_param",
  361. "mesh_hwmp_rann_interval=2000"])
  362. # Check for peer connected
  363. check_mesh_peer_connected(dev[0])
  364. check_mesh_peer_connected(dev[0])
  365. check_mesh_peer_connected(dev[1])
  366. check_mesh_peer_connected(dev[2])
  367. # Wait for RANN frame
  368. time.sleep(10)
  369. # Test connectivity 1->2 and 2->1
  370. hwsim_utils.test_connectivity(dev[1], dev[2])
  371. # Check mpath table on 0
  372. res, data = dev[0].cmd_execute(['iw', dev[0].ifname, 'mpath', 'dump'])
  373. if res != 0:
  374. raise Exception("iw command failed on dev0")
  375. if data.find(dev[1].own_addr() + ' ' + dev[1].own_addr()) == -1 or \
  376. data.find(dev[2].own_addr() + ' ' + dev[2].own_addr()) == -1:
  377. raise Exception("mpath not found on dev0:\n" + data)
  378. if data.find(dev[0].own_addr()) > -1:
  379. raise Exception("invalid mpath found on dev0:\n" + data)
  380. # Check mpath table on 1
  381. res, data = dev[1].cmd_execute(['iw', dev[1].ifname, 'mpath', 'dump'])
  382. if res != 0:
  383. raise Exception("iw command failed on dev1")
  384. if data.find(dev[0].own_addr() + ' ' + dev[0].own_addr()) == -1 or \
  385. data.find(dev[2].own_addr() + ' ' + dev[0].own_addr()) == -1:
  386. raise Exception("mpath not found on dev1:\n" + data)
  387. if data.find(dev[2].own_addr() + ' ' + dev[2].own_addr()) > -1 or \
  388. data.find(dev[1].own_addr()) > -1:
  389. raise Exception("invalid mpath found on dev1:\n" + data)
  390. # Check mpath table on 2
  391. res, data = dev[2].cmd_execute(['iw', dev[2].ifname, 'mpath', 'dump'])
  392. if res != 0:
  393. raise Exception("iw command failed on dev2")
  394. if data.find(dev[0].own_addr() + ' ' + dev[0].own_addr()) == -1 or \
  395. data.find(dev[1].own_addr() + ' ' + dev[0].own_addr()) == -1:
  396. raise Exception("mpath not found on dev2:\n" + data)
  397. if data.find(dev[1].own_addr() + ' ' + dev[1].own_addr()) > -1 or \
  398. data.find(dev[2].own_addr()) > -1:
  399. raise Exception("invalid mpath found on dev2:\n" + data)
  400. # remove mesh groups
  401. for i in range(0, 3):
  402. dev[i].mesh_group_remove()
  403. check_mesh_group_removed(dev[i])
  404. dev[i].dump_monitor()