问题记录:RYU基于跳数的最短路径转发运行时报错

代码如下:

from ryu.base import app_manager
from ryu.ofproto import ofproto_v1_3, ofproto_v1_3_parser
from ryu.controller.handler import set_ev_cls
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller import ofp_event
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.topology import event
from ryu.topology.api import get_switch, get_link
import networkx as nx


class ExampleShortestForwarding(app_manager.RyuApp):
    """docstring for ClassName"""
    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]

    def __init__(self, *args, **kwargs):
        super(ExampleShortestForwarding, self).__init__(*args, **kwargs)
        self.topology_api_app = self
        self.network = nx.DiGraph()
        self.paths = {}

    # handle switch features in packets

    @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
    def switch_features_handler(self, ev):
        datapath = ev.msg.datapath
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser

        # install a table-miss flow entry for each datapath
        match = ofp_parser.OFPMatch()
        actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
                                              ofproto.OFPCML_NO_BUFFER)]
        self.add_flow(datapath, 0, match, actions)

    # install flow entry
    def add_flow(self, datapath, priority, match, actions):
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser

        inst = [ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                                 actions)]
        mod = ofp_parser.OFPFlowMod(
            datapath=datapath, priority=priority, match=match, instructions=inst)
        datapath.send_msg(mod)

    @set_ev_cls(event.EventSwitchEnter, [CONFIG_DISPATCHER, MAIN_DISPATCHER])
    def get_topology(self, ev):
        # get nodes
        switch_list = get_switch(self.topology_api_app, None)
        switches = [switch.dp.id for switch in switch_list]  # del self
        self.network.add_nodes_from(switches)

        # get links
        links_list = get_link(self.topology_api_app, None)
        links = [(link.src.dpid, link.dst.dpid, {
                  'port': link.src.port_no}) for link in links_list]
        self.network.add_edges_from(links)

        # get reverse links
        links = [(link.dst.dpid, link.src.dpid, {
                  'port': link.dst.port_no}) for link in links_list]
  # too many unpacket
        self.network.add_edges_from(links)
    # get out_port by using networkx's Dijkstra algorithm.

    def get_out_port(self, datapath, src, dst, in_port):
        dpid = datapath.id
        # add links between host and access  switch
        if src not in self.network:
            self.network.add_node(src)
            self.network.add_edge(dpid, src, port=in_port)
            self.network.add_edge(src, dpid)
            self.paths.setdefault(src, {})

        # search dst's shortest path.
        if dst in self.network:
            if dst not in self.paths[src]:
                path = nx.shortest_path(self.network, src, dst)
                self.paths[src][dst] = path

            path = self.paths[src][dst]
            next_hop = path[path.index(dpid) + 1]
            out_port = self.network[dpid][next_hop]['port']
            print("path: ", path)
        else:
            out_port = datapath.ofproto.OFPP_FLOOD
        return out_port

    # handle packets in msg

    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
    def packet_in_handler(self, ev):
        # get topology info.
        msg = ev.msg
        datapath = msg.datapath
        ofproto = datapath.ofproto
        ofp_parser = datapath.ofproto_parser

        pkt = packet.Packet(msg.data)
        eth = pkt.get_protocol(ethernet.ethernet)
        in_port = msg.match["in_port"]

        # get out_port
        out_port = self.get_out_port(datapath, eth.src, eth.dst, in_port)
        actions = [ofp_parser.OFPActionOutput(out_port)]

        # installl flow entries
        if out_port != ofproto.OFPP_FLOOD:
            match = ofp_parser.OFPMatch(in_port=in_port, eth_dst=eth.dst)
            self.add_flow(datapath, 1, match, actions)
        # send packet_out msg to datapath
        out = ofp_parser.OFPPacketOut(
            datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions)
        datapath.send_msg(out)

1、报错一

解释:参考:https://github.com/mateuszk87/PcapViz/issues/5

lib'networkx'已于2017年9月更新为2.0版(https://pypi.python.org/pypi/networkx/),下载的版本使用'pip install'。并且一些功能的参数已经改变。例如,DiGraph中add_edges()的参数已从'def add_edge(self,u,v,attr_dict = None,** attr)'更改为'def add_edge(self,u,v,** attr)'。因此,当我编译源代码时,会出现一些错误,例如'TypeError:add_edge()正好需要3个参数(4个给定)'。

解决:方法是重新安装旧版本的networkx:

pip uninstall networkx
pip install networkx==1.11

2、在运行树形图:dy@mn-wifi:~$ sudo mn --topo=tree,3,3 --mac --controller=remote 

出现ping不同的现象。参考:https://blog.csdn.net/qq_37041925/article/details/84838848的代码之后,发现这行代码:

self.network.add_edge(dpid, src, {'port'=in_port})更改为:

self.network.add_edge(dpid, src, port=in_port)之后,就能ping通了。为什么这样没有去深究,因为这关系到networkx这个库。

你可能感兴趣的:(ryu)