Ejemplo n.º 1
0
def add_sketch(args):
  import iptables
  global sketch_list, nfqueue
  # allocate an id to a new sketch
  sketch_id = len(sketch_list)
  s = Sketch()
  # add the new sketch to the global sketch list
  sketch_list.append(s)
  # install a rule to iptable, which sends targeted packets to nf queue
  construct_msg = {}
  construct_msg['interface'] = args['interface']
  construct_msg['target'] = 'NFQUEUE'
  construct_msg['queue number'] = str(sketch_id)
  if 'proto' in args:
    construct_msg['proto'] = args['proto']
  if 'src' in args:
    construct_msg['src'] = args['src']
  if 'dst' in args:
    construct_msg['dst'] = args['dst']
  print iptables.install_rules(construct_msg)
  # bind the queue number with the sketch 
  queue_num = sketch_id
  print 'try to add sketch with local sketch id: ' + str(sketch_id)
  try:
    nfqueue.bind(queue_num, sketch_list[sketch_id].iterate_counters)
    print 'done with local sketch id: ' + str(sketch_id)
    thread.start_new_thread(nfqueue.run,())
  except Exception as x:
    print 'add sketch failed'
    print x
  return sketch_id
Ejemplo n.º 2
0
  def do_POST(self):
    global leaf_agent
    self.query_string = self.rfile.read(int(self.headers['Content-Length']))  
    self.args = dict(cgi.parse_qsl(self.query_string))
    status_code = 400
    response = "error: message not parsed"
    if 'type' in self.args:
      msg_type = self.args['type']
    else:
      self.send_response(400)
      self.end_headers()
      self.wfile.write("No message type indicated.")
      return

    if msg_type == 'est_req':
      thread.start_new_thread(do_bw_estimate,(self.args,))
      response = "Started Bandwidth Estimation"
    if msg_type == 'rcv_req':
      response = receive_probe(self.args)

    if msg_type == 'query bw':
      r = iperf.query_bw(self.args)
      status_code = r['status_code']
      response = r['data']
    if msg_type == 'query jitter':
      r = iperf.query_jitter(self.args)
      status_code = r['status_code']
      response = r['data']
    if msg_type == 'config iperf server':
      if 'server_mode' in self.args:
        mode = self.args['server_mode']
        if iperf.start_server(mode) == 0:
          status_code = 200
          response = "iperf server started"
        else:
          status_code = 500
          response = "error: server failed"
      else:
        status_code = 400
        response = "bad requst: missing server mode"
    # handle traffic monitoring messages
    if msg_type == 'config counter':
      status_code = 200
      # send out configuration message to leaf agents
      for i,addr in enumerate(leaf_agent):
        url = 'http://' + str(addr) + ':8000'
        r = requests.post(url,data=self.args)
        if r.status_code != 200:
          status_code = r.status_code
      response = iptables.install_rules(self.args)  
    if msg_type == 'query counter':
      # recursive querying leaf agents
      r = recursive_query(self.args)
      status_code = r['status_code']
      response = r['data']
    if msg_type == 'config sketch':
      # send out configuration message to leaf agents
      for i,addr in enumerate(leaf_agent):
        thread.start_new_thread(send_to_leaf,(addr,self.args))
      r = send_to_monitor(self.args)
      status_code = r.status_code
      response = r.text
    if msg_type == 'config sketch counter':
      # send out configuration message to leaf agents
      for i,addr in enumerate(leaf_agent):
        # to config global counters on distributed end-hosts, may need to look up sketch id on each end-host, although in experiments they are the same
        thread.start_new_thread(send_to_leaf,(addr,self.args))
      r = send_to_monitor(self.args)
      status_code = r.status_code
      response = r.text
      
    if msg_type == 'query sketch':
      r= recursive_query_sketch(self.args)
      status_code = r['status_code']
      response = r['data']
    if (msg_type == 'query heavy hitters') or (msg_type == 'query real time counter'):
      r = recursive_query_heavy_hitter(self.args)
      status_code = r['status_code']
      response = r['data']
    if msg_type == 'add leaf agent':
      if 'agent_addr' in self.args:
        leaf_agent.append(self.args['agent_addr'])
        status_code = 200
        response = 'leaf agent added'
    if msg_type == 'query leaf agents':
      status_code = 200
      response = leaf_agent
    
    if status_code != 200:
      print 'error: ' + str(response)

    self.send_response(status_code)
    self.end_headers()
    self.wfile.write(str(response))
    return