从ryu控制器获取统计数据

时间:2014-05-22 04:13:15

标签: python mininet

我试图在ryu中获取统计数据(使用python)。 但是当我运行mi代码时只配置交换机,并且没有其他任何东西。我刚从python程序开始,我无法找到错误。

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)

感谢。

2 个答案:

答案 0 :(得分:2)

在您的代码中,您创建了一个名为send_desc_stats_requests(self,datapath)的方法来请求统计信息。该方法是从交换机请求统计信息。

不要在处理程序中的数据包中调用self.desc_stats_reply_handler(self,body),而应该调用self.desc_stats_request(datapath)。所以,在这里你应该从switch请求stats,以便切换将回复统计信息描述,当ryu控制器收到该回复消息时,控制器将运行self.desc_stats_reply_handler(self,body),因为它具有     @set_ev_cls(ofp_event.EventOFPDescStatsReply,MAIN_DISPATCHER)

简而言之,调用self.desc_stats_request(datapath)而不是调用reply_handler

答案 1 :(得分:0)

Vishlesh给出了一个很好的答案。

但是您的代码中存在一些错误。

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)