user3663154
user3663154

Reputation: 41

Getting stats from ryu controller

I'm trying obtain stats in ryu (with python). But when I run mi code only configures the switch, and doesn't make anything else. I've just started with python progamming and I can't find the mistake.

class SimpleSwitch(app_manager.RyuApp):

def __init__(self, *args, **kwargs):
    super(SimpleSwitch, self).__init__(*args, **kwargs)
    self.mac_to_port = {}

def send_desc_stats_requests(self, datapath):
    ofp_parser = datapath.ofproto_parser

    req = ofp_parser.OFPDescStatsRequest(datapath,0)
    datapath.send_msg(req)

    @set_ev_cls(ofp_event.EventOFPDescStatsReply, MAIN_DISPATCHER)
def desc_stats_reply_handler(self,body):    
    ofp = msg.datapath.ofproto
    body = ev.msg.body

    self.logger.info('OFPDescStatsReply received: '
    'mfr_desc=%d hw_desc=%s sw_desc=%s '
    'serial_num=%s dp_desc=%s ',
    body.mfr_desc, body.hw_desc, body.sw_desc,
    body.serial_num, body.dp_desc)

def add_flow(self, datapath, in_port, dst, actions):
    ofproto = datapath.ofproto

    match = datapath.ofproto_parser.OFPMatch(
        in_port=in_port, dl_dst=haddr_to_bin(dst))

    mod = datapath.ofproto_parser.OFPFlowMod(
        datapath=datapath, match=match, cookie=0,
        command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
        priority=ofproto.OFP_DEFAULT_PRIORITY,
        flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)

    datapath.send_msg(mod)

@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
    msg = ev.msg
    datapath = msg.datapath
    ofproto = datapath.ofproto

    pkt = packet.Packet(msg.data)
    eth = pkt.get_protocol(ethernet.ethernet)

    dst = eth.dst
    src = eth.src

    dpid = datapath.id
    self.mac_to_port.setdefault(dpid, {})

    self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)


    self.desc_stats_reply_handler(self,body)

    # learn a mac address to avoid FLOOD next time.
    self.mac_to_port[dpid][src] = msg.in_port

    if dst in self.mac_to_port[dpid]:
        out_port = self.mac_to_port[dpid][dst]
    else:
        out_port = ofproto.OFPP_FLOOD

    actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]

    # install a flow to avoid packet_in next time
    if out_port != ofproto.OFPP_FLOOD:
        self.add_flow(datapath, msg.in_port, dst, actions)

    out = datapath.ofproto_parser.OFPPacketOut(
        datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
        actions=actions)
    datapath.send_msg(out)

@set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
def multipart_reply_handler(self, ev):
    msg = ev.msg
    reason = msg.reason
    port_no = msg.desc.port_no

    ofproto = msg.datapath.ofproto
    if reason == ofproto.OFPPR_ADD:
        self.logger.info("port added %s", port_no)
    elif reason == ofproto.OFPPR_DELETE:
        self.logger.info("port deleted %s", port_no)
    elif reason == ofproto.OFPPR_MODIFY:
        self.logger.info("port modified %s", port_no)
    else:
        self.logger.info("Illeagal port state %s %s", port_no, reason)

Thanks.

Upvotes: 4

Views: 1817

Answers (2)

shivram
shivram

Reputation: 469

Vishlesh Gave a good answer.

But there are some error present in your code.

def desc_stats_reply_handler(self,ev):    
    body = ev.msg.body
    self.logger.info('OFPDescStatsReply received: '
    'mfr_desc=%d hw_desc=%s sw_desc=%s '
    'serial_num=%s dp_desc=%s ',
    body.mfr_desc, body.hw_desc, body.sw_desc,
    body.serial_num, body.dp_desc)

Upvotes: 0

Vishlesh Patel
Vishlesh Patel

Reputation: 168

In your code, you have created a method named send_desc_stats_requests(self, datapath) to request for the stats. That method is requesting stats from the switch.

Instead of calling self.desc_stats_reply_handler(self,body) in packet in handler, you should call self.desc_stats_request(datapath). So, here you should request for stats from switch, so that switch will reply the stats description and when ryu controller will receive that reply message, controller will run self.desc_stats_reply_handler(self,body) as it has @set_ev_cls(ofp_event.EventOFPDescStatsReply, MAIN_DISPATCHER)

In the short, call self.desc_stats_request(datapath) instead of calling reply_handler

Upvotes: 2

Related Questions