源码:(附注释)
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 # 要自己装,装1.11版本
# 来源:https://blog.csdn.net/qq_37041925/article/details/84838848
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() # 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) # 使用dijkstra算法
self.paths[src][dst] = path
path = self.paths[src][dst]
next_hop = path[path.index(dpid) + 1] # 即把path列表里存的下一跳取出来,并不是做计算
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,因为有packet_in,就必须对应有packet_out
out = ofp_parser.OFPPacketOut(
datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions)
datapath.send_msg(out)
新增加了2个方法:get_topology和get_out_port.用来得到拓扑图和最终的转发端口。