Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pox.core import core
- from pox.lib.recoco import Timer
- import pox.openflow.libopenflow_01 as of
- import pox.lib.packet as pkt
- from pox.lib.util import dpidToStr
- log = core.getLogger()
- interval = 4
- ips = {}
- class Flow:
- def __init__(self, bytes, packets, duration):
- self.bytes = bytes
- self.packets = packets
- self.duration = duration
- def __str__(self):
- return "bytes: %s packets: %s duration: %s" % (self.bytes, self.packets, self.duration)
- class Entry:
- def __init__(self, src, dst, tp_src, tp_dst):
- self.src = src
- self.dst = dst
- self.tp_src = tp_src
- self.tp_dst = tp_dst
- self.stats = []
- self.printed = False
- def add_flow(self, flow):
- self.stats.append(flow)
- def add_more(self):
- return len(self.stats) < 2
- def __str__(self):
- s = "from: %s:%s to: %s:%s\n" % (self.src, self.tp_src, self.dst, self.tp_dst)
- for stat in self.stats:
- s += "\t\t"
- s += str(stat)
- s += "\n"
- return s
- def request_flow_stats():
- for c in core.openflow._connections:
- c.send(of.ofp_stats_request(body=of.ofp_flow_stats_request()))
- break
- def handle_flow_stats(event):
- for s in event.stats:
- from_to = (s.match.nw_src, s.match.nw_dst)
- entry = ips.get(from_to)
- if entry is None:
- entry = Entry(src=s.match.nw_src, dst=s.match.nw_dst, tp_src=s.match.tp_src, tp_dst=s.match.tp_dst)
- ips[from_to] = entry
- for s in event.stats:
- from_to = (s.match.nw_src, s.match.nw_dst)
- to_from = (s.match.nw_dst, s.match.nw_src)
- if ips.get(from_to) is not None and ips.get(to_from) is not None:
- entry = ips.get(from_to)
- if entry.add_more() is True:
- entry.add_flow(Flow(s.byte_count, s.packet_count, s.duration_sec))
- print_flow_obj()
- def print_flow_obj():
- for k, v in ips.items():
- if v.printed is False and v.add_more() is False:
- v.printed = True
- log.debug(v)
- def launch ():
- core.openflow.addListenerByName("FlowStatsReceived", handle_flow_stats)
- Timer(interval, request_flow_stats, recurring=True)
- log.debug("Packet collector running")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement