4

我正在尝试获取 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)

谢谢。

4

2 回答 2

2

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

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

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

于 2015-04-18T07:04:46.263 回答
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)
于 2015-07-16T23:01:29.150 回答