Hi
I want to see the source and destination address of each flow passing through
the switches. Thus I have used the following code. I got the flow monitoring
part of that code from
<https://github.com/osrg/ryu-book/blob/master/en/source/sources/simple_monitor.py>
The problem is that the flowstatsReply part is not working. I have specified
the problematic part of the code.
It will be so kind if you kindly show the error?
Thanks
-Tanvir
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
from operator import attrgetter
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.lib.pack_utils import msg_pack_into
from ryu import utils
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER,
DEAD_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import (ofproto_v1_3, ether)
from ryu.lib import hub
from ryu.topology import event
from ryu.lib.packet import (packet, ethernet, arp, icmp, ipv4)
from ryu.lib.packet import ether_types
from ryu.app.wsgi import ControllerBase, WSGIApplication, route
from webob import Response
import os
import time
import logging
class SimpleSW(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
_CONTEXTS = {'WSGI':WSGIApplication}
def __init__(self, *args, **kwargs):
super(SimpleSW, self).__init__(*args, **kwargs)
self.mac_to_port = {} # mac to port table
self.ip_to_mac = {} # ip to mac
self.datapaths = {}
self.monitor_thread = hub.spawn(self._monitor)
# add the number of monitor
# a function that will send flows from switch to switch
def add_flow(self, datapath, command, priority, match, inst):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
flow_add = parser.OFPFlowMod(datapath=datapath, command=command,
buffer_id=ofproto.OFP_NO_BUFFER, priority=priority, match=match,
instructions=inst)
datapath.send_msg(flow_add)
# the packet handled by the controller
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
dpid = datapath.id
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
dst = eth.dst
src = eth.src
# self.logger.info("packet in %s %s %s %s %s", datapath, dpid, src, dst,
in_port)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
datapath = ev.msg.datapath
dpid = datapath.id
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
match = parser.OFPMatch()
actions =
[parser.OFPActionSetField(parser.OFPMatchField.make(ofproto.OXM_OF_IP_DSCP,
11)), parser.OFPActionOutput(2)]
priority = 100 # add a variable
command=ofproto.OFPFC_ADD
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
self.add_flow(datapath, command, priority, match, inst)
@set_ev_cls(ofp_event.EventOFPStateChange, [MAIN_DISPATCHER, DEAD_DISPATCHER])
def _state_change_handler(self, ev):
datapath = ev.datapath
if ev.state == MAIN_DISPATCHER:
if not datapath.id in self.datapaths:
self.logger.debug('register datapath: %016x', datapath.id)
self.datapaths[datapath.id] = datapath
elif ev.state == DEAD_DISPATCHER:
if datapath.id in self.datapaths:
self.logger.debug('deregister datapath: %016x', datapath.id)
del self.datapaths[datapath.id]
def _monitor(self):
while True:
for dp in self.datapaths.values():
self._request_stats(dp)
hub.sleep(3)
def _request_stats(self, datapath):
self.logger.debug('send stats request: %016x', datapath.id)
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
req = parser.OFPFlowStatsRequest(datapath)
datapath.send_msg(req)
req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY)
datapath.send_msg(req)
@set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
def _flow_stats_reply_handler(self, ev):
body = ev.msg.body
self.logger.info('datapath '
'in-port eth-dst '
'out-port packets bytes')
self.logger.info('---------------- '
'-------- ----------------- '
'-------- -------- --------')
## *** problem is at here.......................
START.............................
for stat in sorted([flow for flow in body], key=lambda flow:
(flow.match['in_port'], flow.match['eth_dst'])):
self.logger.info('%016x %8x %17s %8x %8d %8d',
ev.msg.datapath.id,
stat.match['in_port'], stat.match['eth_dst'],
stat.instructions[0].actions[0].port,
stat.packet_count, stat.byte_count)
## *** problem is at
here.......................END.............................
@set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
def _port_stats_reply_handler(self, ev):
body = ev.msg.body
self.logger.info('datapath port '
'rx-pkts rx-bytes rx-error '
'tx-pkts tx-bytes tx-error')
self.logger.info('---------------- -------- '
'-------- -------- -------- '
'-------- -------- --------')
for stat in sorted(body, key=attrgetter('port_no')):
self.logger.info('%016x %8x %8d %8d %8d %8d %8d %8d',
ev.msg.datapath.id, stat.port_no,
stat.rx_packets, stat.rx_bytes, stat.rx_errors,
stat.tx_packets, stat.tx_bytes, stat.tx_errors)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. http://sdm.link/zohodev2dev
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel