Pyretic: Flow Monitor

 

[Topology]

  

h1---s1---h2

 

[mymonitor2.py]

from pyretic.lib.corelib import *

from pyretic.lib.std import *

from pyretic.lib.query import *

from pyretic.modules.mac_learner import *

from collections import defaultdict

 

from datetime import datetime

import time

 

#thr[srcip][dstip]

byte=defaultdict(lambda:defaultdict(lambda:None))

clock=defaultdict(lambda:defaultdict(lambda:None))

thr=defaultdict(lambda:defaultdict(lambda:None))

 

def byte_count_printer(n):

  if len(n)!=0:

    print time.time()

    for i, j in n.items():

      if byte[str(i).split(" ")[2]][str(i).split(" ")[4]]>0:

        thr[str(i).split(" ")[2]][str(i).split(" ")[4]] = (j - byte[str(i).split(" ")[2]][str(i).split(" ")[4]]) * 8.0 / (time.time()-clock[str(i).split(" ")[2]][str(i).split(" ")[4]])

        print str(i).split(" ")[2], "->", str(i).split(" ")[4], " =", thr[str(i).split(" ")[2]][str(i).split(" ")[4]], "bps"

      byte[str(i).split(" ")[2]][str(i).split(" ")[4]]=j

      clock[str(i).split(" ")[2]][str(i).split(" ")[4]]=time.time()

      print " ----------------------------------------------------"

 

def byte_counts():

  #every 3 seconds to count bytes

  q = count_bytes(3,['srcip','dstip'])

  q.register_callback(byte_count_printer)

  return q      

 

def main():

  return (mac_learner() + byte_counts())

 

 

[Execution]

 

Ping Test to measure the throughput. (From below figures, we can see that there are two flows. One is from h1 to h2, and the other is from h2 to h1).

 

[Topology]

 

h1--s1--s2--h2

 

If we want to measure the throughput of flow from h1 to h2 at switch 2, we can simply modify the code.

 

[mymonitor2_1.py]

from pyretic.lib.corelib import *

from pyretic.lib.std import *

from pyretic.lib.query import *

from pyretic.modules.mac_learner import *

from collections import defaultdict

 

from datetime import datetime

import time

 

ip1 = IPAddr('10.0.0.1')

ip2 = IPAddr('10.0.0.2')

 

#thr[srcip][dstip]

byte=defaultdict(lambda:defaultdict(lambda:None))

clock=defaultdict(lambda:defaultdict(lambda:None))

thr=defaultdict(lambda:defaultdict(lambda:None))

 

def byte_count_printer(n):

  if len(n)!=0:

    print time.time()

    for i, j in n.items():

      if byte[str(i).split(" ")[2]][str(i).split(" ")[4]]>0:

        thr[str(i).split(" ")[2]][str(i).split(" ")[4]] = (j - byte[str(i).split(" ")[2]][str(i).split(" ")[4]]) * 8.0 / (time.time()-clock[str(i).split(" ")[2]][str(i).split(" ")[4]])

        print str(i).split(" ")[2], "->", str(i).split(" ")[4], " =", thr[str(i).split(" ")[2]][str(i).split(" ")[4]], "bps"

      byte[str(i).split(" ")[2]][str(i).split(" ")[4]]=j

      clock[str(i).split(" ")[2]][str(i).split(" ")[4]]=time.time()

      print " ----------------------------------------------------"

 

 

def byte_counts():

  q = count_bytes(3,['srcip','dstip'])

  q.register_callback(byte_count_printer)

  return q      

 

def main():

  return (mac_learner() + (match(dstip=ip2, switch=2) >> byte_counts()))

 

 

[Execution]

 

Ping Test to measure the throughput. (We can see that only the flow throughput from h1 to h2 is shown.)

 

Dr. Chih-Heng Ke

Department of Computer Science and Information Engineering, National Quemoy University, Kinmen, Taiwan

Email: smallko@gmail.com / smallko@hotmail.com