monitor.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Monitor support
  2. # Copyright (c) 2016, Tieto Corporation
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import time
  7. from remotehost import Host
  8. import config
  9. import rutils
  10. import re
  11. import traceback
  12. import logging
  13. logger = logging.getLogger()
  14. import hostapd
  15. # standalone monitor with multi iface support
  16. def create(devices, setup_params, refs, duts, monitors):
  17. mons = []
  18. mhosts = []
  19. hosts = duts + refs
  20. # choose only standalone monitors
  21. for monitor in monitors:
  22. if monitor not in hosts and monitor != "all":
  23. mons.append(monitor)
  24. for mon in mons:
  25. dev = config.get_device(devices, mon)
  26. if dev is None:
  27. continue
  28. host = Host(host = dev['hostname'],
  29. ifname = dev['ifname'],
  30. port = dev['port'],
  31. name = dev['name'])
  32. try:
  33. host.execute(["iw", "reg", "set", setup_params['country']])
  34. rutils.setup_hw_host(host, setup_params, True)
  35. except:
  36. pass
  37. mhosts.append(host)
  38. return mhosts
  39. def destroy(devices, hosts):
  40. for host in hosts:
  41. stop(host)
  42. for monitor in host.monitors:
  43. host.execute(["ifconfig", monitor, "down"])
  44. def setup(host, monitor_params):
  45. if host is None:
  46. return
  47. ifaces = re.split('; | |, ', host.ifname)
  48. count = 0
  49. for param in monitor_params:
  50. try:
  51. iface = ifaces[count]
  52. except:
  53. logger.debug(traceback.format_exc())
  54. break
  55. host.execute(["ifconfig", iface, " down"])
  56. host.execute(["iw", iface, "set type monitor"])
  57. host.execute(["ifconfig", iface, "up"])
  58. status, buf = host.execute(["iw", iface, "set", "freq", param['freq'],
  59. param['bw'], param['center_freq1'],
  60. param['center_freq2']])
  61. if status != 0:
  62. logger.debug("Could not setup monitor interface: " + buf)
  63. continue
  64. host.monitors.append(iface)
  65. count = count + 1
  66. def run(host, setup_params):
  67. monitor_res = []
  68. log_monitor = ""
  69. if host is None:
  70. return None
  71. if len(host.monitors) == 0:
  72. return None
  73. try:
  74. log_dir = setup_params['log_dir']
  75. tc_name = setup_params['tc_name']
  76. except:
  77. return None
  78. tshark = "tshark"
  79. for monitor in host.monitors:
  80. host.execute(["ifconfig", monitor, "up"])
  81. tshark = tshark + " -i " + monitor
  82. log_monitor = log_monitor + "_" + monitor
  83. log = log_dir + tc_name + "_" + host.name + log_monitor + ".pcap"
  84. host.add_log(log)
  85. thread = host.execute_run([tshark, "-w", log], monitor_res)
  86. host.thread = thread
  87. def stop(host):
  88. if host is None:
  89. return
  90. if len(host.monitors) == 0:
  91. return
  92. if host.thread is None:
  93. return
  94. host.execute(["killall", "-s", "INT", "tshark"])
  95. host.wait_execute_complete(host.thread, 5)
  96. if host.thread.isAlive():
  97. raise Exception("tshark still alive")
  98. host.thread = None
  99. # Add monitor to existing interface
  100. def add(host, monitors):
  101. if host is None:
  102. return
  103. for monitor in monitors:
  104. if monitor != "all" and monitor != host.name:
  105. continue
  106. mon = "mon_" + host.ifname
  107. status, buf = host.execute(["iw", host.ifname, "interface", "add", mon,
  108. "type", "monitor"])
  109. if status == 0:
  110. host.monitors.append(mon)
  111. host.execute(["ifconfig", mon, "up"])
  112. else:
  113. logger.debug("Could not add monitor for " + host.name)
  114. def remove(host):
  115. stop(host)
  116. for monitor in host.monitors:
  117. host.execute(["iw", monitor, "del"])
  118. host.monitors.remove(monitor)
  119. # get monitor params from hostapd/wpa_supplicant
  120. def get_monitor_params(wpa, is_p2p=False):
  121. if is_p2p:
  122. get_status_field_f = wpa.get_group_status_field
  123. else:
  124. get_status_field_f = wpa.get_status_field
  125. freq = get_status_field_f("freq")
  126. bw = "20"
  127. center_freq1=""
  128. center_freq2=""
  129. vht_oper_chwidth = get_status_field_f("vht_oper_chwidth")
  130. secondary_channel = get_status_field_f("secondary_channel")
  131. vht_oper_centr_freq_seg0_idx = get_status_field_f("vht_oper_centr_freq_seg0_idx")
  132. vht_oper_centr_freq_seg1_idx = get_status_field_f("vht_oper_centr_freq_seg1_idx")
  133. if vht_oper_chwidth == "0" or vht_oper_chwidth is None:
  134. if secondary_channel == "1":
  135. bw = "40"
  136. center_freq1 = str(int(freq) + 10)
  137. elif secondary_channel == "-1":
  138. center_freq1 = str(int(freq) - 10)
  139. else:
  140. pass
  141. elif vht_oper_chwidth == "1":
  142. bw = "80"
  143. center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
  144. elif vht_oper_chwidth == "2":
  145. bw = "160"
  146. center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
  147. elif vht_oper_chwidth == "3":
  148. bw = "80+80"
  149. center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
  150. center_freq2 = str(int(vht_oper_centr_freq_seg1_idx) * 5 + 5000)
  151. else:
  152. pass
  153. monitor_params = { "freq" : freq,
  154. "bw" : bw,
  155. "center_freq1" : center_freq1,
  156. "center_freq2" : center_freq2 }
  157. return monitor_params