forked from hip2b2/poxstuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_stats.py
81 lines (70 loc) · 2.81 KB
/
flow_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
# Copyright 2012 William Yu
# wyu@ateneo.edu
#
# This file is part of POX.
#
# POX is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# POX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with POX. If not, see <http://www.gnu.org/licenses/>.
#
"""
This is a demonstration file created to show how to obtain flow
and port statistics from OpenFlow 1.0-enabled switches. The flow
statistics handler contains a summary of web-only traffic.
"""
# standard includes
from pox.core import core
from pox.lib.util import dpidToStr
import pox.openflow.libopenflow_01 as of
# include as part of the betta branch
from pox.openflow.of_json import *
log = core.getLogger()
# handler for timer function that sends the requests to all the
# switches connected to the controller.
def _timer_func ():
for connection in core.openflow._connections.values():
connection.send(of.ofp_stats_request(body=of.ofp_flow_stats_request()))
connection.send(of.ofp_stats_request(body=of.ofp_port_stats_request()))
log.debug("Sent %i flow/port stats request(s)", len(core.openflow._connections))
# handler to display flow statistics received in JSON format
# structure of event.stats is defined by ofp_flow_stats()
def _handle_flowstats_received (event):
stats = flow_stats_to_list(event.stats)
log.debug("FlowStatsReceived from %s: %s",
dpidToStr(event.connection.dpid), stats)
# Get number of bytes/packets in flows for web traffic only
web_bytes = 0
web_flows = 0
web_packet = 0
for f in event.stats:
if f.match.tp_dst == 80 or f.match.tp_src == 80:
web_bytes += f.byte_count
web_packet += f.packet_count
web_flows += 1
log.info("Web traffic from %s: %s bytes (%s packets) over %s flows",
dpidToStr(event.connection.dpid), web_bytes, web_packet, web_flows)
# handler to display port statistics received in JSON format
def _handle_portstats_received (event):
stats = flow_stats_to_list(event.stats)
log.debug("PortStatsReceived from %s: %s",
dpidToStr(event.connection.dpid), stats)
# main functiont to launch the module
def launch ():
from pox.lib.recoco import Timer
# attach handsers to listners
core.openflow.addListenerByName("FlowStatsReceived",
_handle_flowstats_received)
core.openflow.addListenerByName("PortStatsReceived",
_handle_portstats_received)
# timer set to execute every five seconds
Timer(5, _timer_func, recurring=True)