verify.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import time
  2. import chutney
  3. def run_test(network):
  4. wait_time = network._dfltEnv['bootstrap_time']
  5. start_time = time.time()
  6. end_time = start_time + wait_time
  7. print("Verifying data transmission: (retrying for up to %d seconds)"
  8. % wait_time)
  9. status = False
  10. # Keep on retrying the verify until it succeeds or times out
  11. while not status and time.time() < end_time:
  12. # TrafficTester connections time out after ~3 seconds
  13. # a TrafficTester times out after ~10 seconds if no data is being sent
  14. status = _verify_traffic(network)
  15. # Avoid madly spewing output if we fail immediately each time
  16. if not status:
  17. time.sleep(5)
  18. print("Transmission: %s" % ("Success" if status else "Failure"))
  19. if not status:
  20. print("Set CHUTNEY_DEBUG to diagnose.")
  21. return status
  22. def _verify_traffic(network):
  23. """Verify (parts of) the network by sending traffic through it
  24. and verify what is received."""
  25. # TODO: IPv6 SOCKSPorts, SOCKSPorts with IPv6Traffic, and IPv6 Exits
  26. LISTEN_ADDR = network._dfltEnv['ip']
  27. LISTEN_PORT = 4747 # FIXME: Do better! Note the default exit policy.
  28. # HSs must have a HiddenServiceDir with
  29. # "HiddenServicePort <HS_PORT> <CHUTNEY_LISTEN_ADDRESS>:<LISTEN_PORT>"
  30. # TODO: Test <CHUTNEY_LISTEN_ADDRESS_V6>:<LISTEN_PORT>
  31. HS_PORT = 5858
  32. # The amount of data to send between each source-sink pair,
  33. # each time the source connects.
  34. # We create a source-sink pair for each (bridge) client to an exit,
  35. # and a source-sink pair for a (bridge) client to each hidden service
  36. DATALEN = network._dfltEnv['data_bytes']
  37. # Print a dot each time a sink verifies this much data
  38. DOTDATALEN = 5 * 1024 * 1024 # Octets.
  39. TIMEOUT = 3 # Seconds.
  40. # Calculate the amount of random data we should use
  41. randomlen = _calculate_randomlen(DATALEN)
  42. reps = _calculate_reps(DATALEN, randomlen)
  43. connection_count = network._dfltEnv['connection_count']
  44. # sanity check
  45. if reps == 0:
  46. DATALEN = 0
  47. # Get the random data
  48. if randomlen > 0:
  49. # print a dot after every DOTDATALEN data is verified, rounding up
  50. dot_reps = _calculate_reps(DOTDATALEN, randomlen)
  51. # make sure we get at least one dot per transmission
  52. dot_reps = min(reps, dot_reps)
  53. with open('/dev/urandom', 'r') as randfp:
  54. tmpdata = randfp.read(randomlen)
  55. else:
  56. dot_reps = 0
  57. tmpdata = {}
  58. # now make the connections
  59. bind_to = (LISTEN_ADDR, LISTEN_PORT)
  60. tt = chutney.Traffic.TrafficTester(bind_to, tmpdata, TIMEOUT, reps,
  61. dot_reps)
  62. client_list = filter(lambda n:
  63. n._env['tag'] == 'c' or n._env['tag'] == 'bc',
  64. network._nodes)
  65. exit_list = filter(lambda n:
  66. ('exit' in n._env.keys()) and n._env['exit'] == 1,
  67. network._nodes)
  68. hs_list = filter(lambda n:
  69. n._env['tag'] == 'h',
  70. network._nodes)
  71. if len(client_list) == 0:
  72. print(" Unable to verify network: no client nodes available")
  73. return False
  74. if len(exit_list) == 0 and len(hs_list) == 0:
  75. print(" Unable to verify network: no exit/hs nodes available")
  76. print(" Exit nodes must be declared 'relay=1, exit=1'")
  77. print(" HS nodes must be declared 'tag=\"hs\"'")
  78. return False
  79. print("Connecting:")
  80. # the number of tor nodes in paths which will send DATALEN data
  81. # if a node is used in two paths, we count it twice
  82. # this is a lower bound, as cannabilised circuits are one node longer
  83. total_path_node_count = 0
  84. total_path_node_count += _configure_exits(tt, bind_to, tmpdata, reps,
  85. client_list, exit_list,
  86. LISTEN_ADDR, LISTEN_PORT,
  87. connection_count)
  88. total_path_node_count += _configure_hs(tt, tmpdata, reps, client_list,
  89. hs_list, HS_PORT, LISTEN_ADDR,
  90. LISTEN_PORT, connection_count,
  91. network._dfltEnv['hs_multi_client'])
  92. print("Transmitting Data:")
  93. start_time = time.time()
  94. status = tt.run()
  95. end_time = time.time()
  96. # if we fail, don't report the bandwidth
  97. if not status:
  98. return status
  99. # otherwise, report bandwidth used, if sufficient data was transmitted
  100. _report_bandwidth(DATALEN, total_path_node_count, start_time, end_time)
  101. return status
  102. # In order to performance test a tor network, we need to transmit
  103. # several hundred megabytes of data or more. Passing around this
  104. # much data in Python has its own performance impacts, so we provide
  105. # a smaller amount of random data instead, and repeat it to DATALEN
  106. def _calculate_randomlen(datalen):
  107. MAX_RANDOMLEN = 128 * 1024 # Octets.
  108. if datalen > MAX_RANDOMLEN:
  109. return MAX_RANDOMLEN
  110. else:
  111. return datalen
  112. def _calculate_reps(datalen, replen):
  113. # sanity checks
  114. if datalen == 0 or replen == 0:
  115. return 0
  116. # effectively rounds datalen up to the nearest replen
  117. if replen < datalen:
  118. return (datalen + replen - 1) / replen
  119. else:
  120. return 1
  121. # if there are any exits, each client / bridge client transmits
  122. # via 4 nodes (including the client) to an arbitrary exit
  123. # Each client binds directly to <CHUTNEY_LISTEN_ADDRESS>:LISTEN_PORT
  124. # via an Exit relay
  125. def _configure_exits(tt, bind_to, tmpdata, reps, client_list, exit_list,
  126. LISTEN_ADDR, LISTEN_PORT, connection_count):
  127. CLIENT_EXIT_PATH_NODES = 4
  128. exit_path_node_count = 0
  129. if len(exit_list) > 0:
  130. exit_path_node_count += (len(client_list) *
  131. CLIENT_EXIT_PATH_NODES *
  132. connection_count)
  133. for op in client_list:
  134. print(" Exit to %s:%d via client %s:%s"
  135. % (LISTEN_ADDR, LISTEN_PORT,
  136. 'localhost', op._env['socksport']))
  137. for _ in range(connection_count):
  138. proxy = ('localhost', int(op._env['socksport']))
  139. tt.add(chutney.Traffic.Source(tt, bind_to, tmpdata, proxy,
  140. reps))
  141. return exit_path_node_count
  142. # The HS redirects .onion connections made to hs_hostname:HS_PORT
  143. # to the Traffic Tester's CHUTNEY_LISTEN_ADDRESS:LISTEN_PORT
  144. # an arbitrary client / bridge client transmits via 8 nodes
  145. # (including the client and hs) to each hidden service
  146. # Instead of binding directly to LISTEN_PORT via an Exit relay,
  147. # we bind to hs_hostname:HS_PORT via a hidden service connection
  148. def _configure_hs(tt, tmpdata, reps, client_list, hs_list, HS_PORT,
  149. LISTEN_ADDR, LISTEN_PORT, connection_count, hs_multi_client):
  150. CLIENT_HS_PATH_NODES = 8
  151. hs_path_node_count = (len(hs_list) * CLIENT_HS_PATH_NODES *
  152. connection_count)
  153. # Each client in hs_client_list connects to each hs
  154. if hs_multi_client:
  155. hs_client_list = client_list
  156. hs_path_node_count *= len(client_list)
  157. else:
  158. # only use the first client in the list
  159. hs_client_list = client_list[:1]
  160. # Setup the connections from each client in hs_client_list to each hs
  161. for hs in hs_list:
  162. hs_bind_to = (hs._env['hs_hostname'], HS_PORT)
  163. for client in hs_client_list:
  164. print(" HS to %s:%d (%s:%d) via client %s:%s"
  165. % (hs._env['hs_hostname'], HS_PORT,
  166. LISTEN_ADDR, LISTEN_PORT,
  167. 'localhost', client._env['socksport']))
  168. for _ in range(connection_count):
  169. proxy = ('localhost', int(client._env['socksport']))
  170. tt.add(chutney.Traffic.Source(tt, hs_bind_to, tmpdata,
  171. proxy, reps))
  172. return hs_path_node_count
  173. # calculate the single stream bandwidth and overall tor bandwidth
  174. # the single stream bandwidth is the bandwidth of the
  175. # slowest stream of all the simultaneously transmitted streams
  176. # the overall bandwidth estimates the simultaneous bandwidth between
  177. # all tor nodes over all simultaneous streams, assuming:
  178. # * minimum path lengths (no cannibalized circuits)
  179. # * unlimited network bandwidth (that is, localhost)
  180. # * tor performance is CPU-limited
  181. # This be used to estimate the bandwidth capacity of a CPU-bound
  182. # tor relay running on this machine
  183. def _report_bandwidth(data_length, total_path_node_count, start_time,
  184. end_time):
  185. # otherwise, if we sent at least 5 MB cumulative total, and
  186. # it took us at least a second to send, report bandwidth
  187. MIN_BWDATA = 5 * 1024 * 1024 # Octets.
  188. MIN_ELAPSED_TIME = 1.0 # Seconds.
  189. cumulative_data_sent = total_path_node_count * data_length
  190. elapsed_time = end_time - start_time
  191. if (cumulative_data_sent >= MIN_BWDATA and
  192. elapsed_time >= MIN_ELAPSED_TIME):
  193. # Report megabytes per second
  194. BWDIVISOR = 1024*1024
  195. single_stream_bandwidth = (data_length / elapsed_time / BWDIVISOR)
  196. overall_bandwidth = (cumulative_data_sent / elapsed_time /
  197. BWDIVISOR)
  198. print("Single Stream Bandwidth: %.2f MBytes/s"
  199. % single_stream_bandwidth)
  200. print("Overall tor Bandwidth: %.2f MBytes/s"
  201. % overall_bandwidth)