Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. from pox.core import core
  2. from pox.lib.recoco import Timer
  3. import pox.openflow.libopenflow_01 as of
  4. import pox.lib.packet as pkt
  5. from pox.lib.util import dpidToStr
  6.  
  7. log = core.getLogger()
  8.  
  9. interval = 4
  10.  
  11. ips = {}
  12.  
  13. class Flow:
  14. def __init__(self, bytes, packets, duration):
  15. self.bytes = bytes
  16. self.packets = packets
  17. self.duration = duration
  18. def __str__(self):
  19. return "bytes: %s packets: %s duration: %s" % (self.bytes, self.packets, self.duration)
  20.  
  21. class Entry:
  22. def __init__(self, src, dst, tp_src, tp_dst):
  23. self.src = src
  24. self.dst = dst
  25. self.tp_src = tp_src
  26. self.tp_dst = tp_dst
  27. self.stats = []
  28. self.printed = False
  29.  
  30. def add_flow(self, flow):
  31. self.stats.append(flow)
  32.  
  33. def add_more(self):
  34. return len(self.stats) < 2
  35.  
  36. def __str__(self):
  37. s = "from: %s:%s to: %s:%s\n" % (self.src, self.tp_src, self.dst, self.tp_dst)
  38. for stat in self.stats:
  39. s += "\t\t"
  40. s += str(stat)
  41. s += "\n"
  42. return s
  43.  
  44. def request_flow_stats():
  45. for c in core.openflow._connections:
  46. c.send(of.ofp_stats_request(body=of.ofp_flow_stats_request()))
  47. break
  48.  
  49. def handle_flow_stats(event):
  50. for s in event.stats:
  51. from_to = (s.match.nw_src, s.match.nw_dst)
  52. entry = ips.get(from_to)
  53. if entry is None:
  54. entry = Entry(src=s.match.nw_src, dst=s.match.nw_dst, tp_src=s.match.tp_src, tp_dst=s.match.tp_dst)
  55. ips[from_to] = entry
  56.  
  57. for s in event.stats:
  58. from_to = (s.match.nw_src, s.match.nw_dst)
  59. to_from = (s.match.nw_dst, s.match.nw_src)
  60. if ips.get(from_to) is not None and ips.get(to_from) is not None:
  61. entry = ips.get(from_to)
  62. if entry.add_more() is True:
  63. entry.add_flow(Flow(s.byte_count, s.packet_count, s.duration_sec))
  64.  
  65. print_flow_obj()
  66.  
  67. def print_flow_obj():
  68. for k, v in ips.items():
  69. if v.printed is False and v.add_more() is False:
  70. v.printed = True
  71. log.debug(v)
  72.  
  73. def launch ():
  74. core.openflow.addListenerByName("FlowStatsReceived", handle_flow_stats)
  75. Timer(interval, request_flow_stats, recurring=True)
  76. log.debug("Packet collector running")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement