123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- #!/usr/bin/env python3
- import random # For simulation, not cryptography!
- import math
- import sys
- import logging
- import resource
- import network
- import dirauth
- import relay
- import client
- class Simulator:
- def __init__(self, relaytarget, clienttarget, statslogger):
- self.relaytarget = relaytarget
- self.clienttarget = clienttarget
- self.statslogger = statslogger
- # Some (for now) hard-coded parameters
- # The number of directory authorities
- numdirauths = 9
- # The fraction of relays that are fallback relays
- fracfallbackrelays = 0.05
- # Mean number of circuits created per client per epoch
- self.gamma = 8.9
- # Start some dirauths
- self.dirauthaddrs = []
- self.dirauths = []
- for i in range(numdirauths):
- dira = dirauth.DirAuth(i, numdirauths)
- self.dirauths.append(dira)
- self.dirauthaddrs.append(dira.netaddr)
- # Start some relays
- self.relays = []
- for i in range(self.relaytarget):
- # Relay bandwidths (at least the ones fast enough to get used)
- # in the live Tor network (as of Dec 2019) are well approximated
- # by (200000-(200000-25000)/3*log10(x)) where x is a
- # uniform integer in [1,2500]
- x = random.randint(1,2500)
- bw = int(200000-(200000-25000)/3*math.log10(x))
- self.relays.append(relay.Relay(self.dirauthaddrs, bw, 0))
- # The fallback relays are a hardcoded list of a small fraction
- # of the relays, used by clients for bootstrapping
- numfallbackrelays = int(self.relaytarget * fracfallbackrelays) + 1
- fallbackrelays = random.sample(self.relays, numfallbackrelays)
- for r in fallbackrelays:
- r.set_is_fallbackrelay()
- network.thenetwork.setfallbackrelays(fallbackrelays)
- # Tick the epoch to build the first consensus
- network.thenetwork.nextepoch()
- # Start some clients
- self.clients = []
- for i in range(clienttarget):
- self.clients.append(client.Client(self.dirauthaddrs))
- # Throw away all the performance statistics to this point
- for d in self.dirauths: d.perfstats.reset()
- for r in self.relays: r.perfstats.reset()
- for c in self.clients: c.perfstats.reset()
- # Tick the epoch to bootstrap the clients
- network.thenetwork.nextepoch()
- def one_epoch(self):
- """Simulate one epoch."""
- epoch = network.thenetwork.getepoch()
- # Each client will start a random number of circuits in a
- # Poisson distribution with mean gamma. To randomize the order
- # of the clients creating each circuit, we actually use a
- # Poisson distribution with mean (gamma*num_clients), and assign
- # each event to a uniformly random client. (This does in fact
- # give the required distribution.)
- numclients = len(self.clients)
- # simtime is the simulated time, measured in epochs (i.e.,
- # 0=start of this epoch; 1=end of this epoch)
- simtime = 0
- allcircs = []
- lastpercent = -1
- while simtime < 1.0:
- allcircs.append(
- random.choice(self.clients).channelmgr.new_circuit())
- simtime += random.expovariate(self.gamma * numclients)
- percent = int(100*simtime)
- if percent != lastpercent:
- logging.info("Creating circuits in epoch %s: %d%%",
- epoch, percent)
- lastpercent = percent
- # gather stats
- totsent = 0
- totrecv = 0
- dirasent = 0
- dirarecv = 0
- relaysent = 0
- relayrecv = 0
- clisent = 0
- clirecv = 0
- dirastats = network.PerfStatsStats()
- for d in self.dirauths:
- logging.debug("%s", d.perfstats)
- dirasent += d.perfstats.bytes_sent
- dirarecv += d.perfstats.bytes_received
- dirastats.accum(d.perfstats)
- totsent += dirasent
- totrecv += dirarecv
- relaystats = network.PerfStatsStats()
- relaybstats = network.PerfStatsStats()
- relaynbstats = network.PerfStatsStats()
- for r in self.relays:
- logging.debug("%s", r.perfstats)
- relaysent += r.perfstats.bytes_sent
- relayrecv += r.perfstats.bytes_received
- relaystats.accum(r.perfstats)
- if r.perfstats.is_bootstrapping:
- relaybstats.accum(r.perfstats)
- else:
- relaynbstats.accum(r.perfstats)
- totsent += relaysent
- totrecv += relayrecv
- clistats = network.PerfStatsStats()
- clibstats = network.PerfStatsStats()
- clinbstats = network.PerfStatsStats()
- for c in self.clients:
- logging.debug("%s", c.perfstats)
- clisent += c.perfstats.bytes_sent
- clirecv += c.perfstats.bytes_received
- clistats.accum(c.perfstats)
- if c.perfstats.is_bootstrapping:
- clibstats.accum(c.perfstats)
- else:
- clinbstats.accum(c.perfstats)
- totsent += clisent
- totrecv += clirecv
- self.statslogger.info("DirAuths sent=%s recv=%s" % (dirasent, dirarecv))
- self.statslogger.info("Relays sent=%s recv=%s" % (relaysent, relayrecv))
- self.statslogger.info("Client sent=%s recv=%s" % (clisent, clirecv))
- self.statslogger.info("Total sent=%s recv=%s" % (totsent, totrecv))
- numdirauths = len(self.dirauths)
- numrelays = len(self.relays)
- numclients = len(self.clients)
- self.statslogger.info("Dirauths %s", dirastats)
- self.statslogger.info("Relays %s", relaystats)
- self.statslogger.info("Relays(B) %s", relaybstats)
- self.statslogger.info("Relays(NB) %s", relaynbstats)
- self.statslogger.info("Clients %s", clistats)
- self.statslogger.info("Clients(B) %s", clibstats)
- self.statslogger.info("Clients(NB) %s", clinbstats)
- # Close circuits
- for c in allcircs:
- c.close()
- # Reset stats
- for d in self.dirauths: d.perfstats.reset()
- for r in self.relays: r.perfstats.reset()
- for c in self.clients: c.perfstats.reset()
- # Churn relays
- # Churn is controlled by three parameters:
- # newmean: the mean number of new arrivals per epoch
- # newstddev: the stddev number of new arrivals per epoch
- # oldprob: the probability any given existing one leaves per epoch
- # If target is the desired steady state number, then it should
- # be the case that target * oldprob = newmean. That way, if the
- # current number is below target, on average you add more than
- # you remove, and if the current number is above target, on
- # average you add fewer than you remove.
- # For relays, looking at all the consensuses for Nov and Dec
- # 2019, newmean is about 1.0% of the network size, and newstddev
- # is about 0.3% of the network size.
- relay_newmean = 0.010 * self.relaytarget
- relay_newstddev = 0.003 * self.relaytarget
- relay_oldprob = 0.010
- # Stop some of the (non-fallback) relays
- relays_terminating = []
- for r in self.relays:
- if not r.is_fallbackrelay and random.random() < relay_oldprob:
- relays_terminating.append(r)
- for r in relays_terminating:
- r.terminate()
- self.relays.remove(r)
- # Start some new relays
- relays_new = int(random.normalvariate(relay_newmean, relay_newstddev))
- if relays_new > 0:
- for i in range(relays_new):
- x = random.randint(1,2500)
- bw = int(200000-(200000-25000)/3*math.log10(x))
- self.relays.append(relay.Relay(self.dirauthaddrs, bw, 0))
- # TODO: churn clients
- # Tick the epoch
- network.thenetwork.nextepoch()
- if __name__ == '__main__':
- # Args: womode snipauthmode networkscale numepochs randseed
- if len(sys.argv) != 6:
- sys.stderr.write("Usage: womode snipauthmode networkscale numepochs randseed\n")
- sys.exit(1)
- womode = network.WOMode[sys.argv[1].upper()]
- snipauthmode = network.SNIPAuthMode[sys.argv[2].upper()]
- networkscale = float(sys.argv[3])
- numepochs = int(sys.argv[4])
- randseed = int(sys.argv[5])
- logfile = "%s_%s_%f_%s_%s.log" % (womode.name, snipauthmode.name,
- networkscale, numepochs, randseed)
- # Seed the PRNG. On Ubuntu 18.04, this in fact makes future calls
- # to (non-cryptographic) random numbers deterministic. On Ubuntu
- # 16.04, it does not.
- random.seed(randseed)
- loglevel = logging.INFO
- # Uncomment to see all the debug messages
- # loglevel = logging.DEBUG
- logging.basicConfig(level=loglevel,
- format="%(asctime)s:%(levelname)s:%(message)s")
- # The gathered statistics get logged separately
- statslogger = logging.getLogger("simulator")
- handler = logging.FileHandler(logfile)
- handler.setFormatter(logging.Formatter("%(asctime)s:%(message)s"))
- statslogger.addHandler(handler)
- statslogger.setLevel(logging.INFO)
- # Set the Walking Onions style to use
- network.thenetwork.set_wo_style(womode, snipauthmode)
- # The steady-state numbers of relays and clients
- relaytarget = math.ceil(6500 * networkscale)
- clienttarget = math.ceil(2500000 * networkscale)
- # Create the simulation
- simulator = Simulator(relaytarget, clienttarget, statslogger)
- for e in range(numepochs):
- statslogger.info("Starting epoch %s simulation", e+3)
- simulator.one_epoch()
- maxmemmib = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024
- statslogger.info("%d MiB used", maxmemmib)
|