The following two pieces of code can be annotated in the back to me,, look not to understand small white help bosses!!!!!!
The controller code:
# coding: utf-8
The from ryu. Base import app_manager
The from ryu. Controller import mac_to_port
The from ryu. Controller import ofp_event
The from ryu. Controller. Handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
The from ryu. Controller. Handler import set_ev_cls
The from ryu. Ofproto import ofproto_v1_3
The from ryu. Lib. MAC import haddr_to_bin
The from ryu. Lib. Packet import packet
The from ryu. Lib. Packet import Ethernet
The from ryu. Lib. Packet import ether_types
The from ryu. Lib import MAC
The from ryu. Topology. API import get_switch, get_link
The from ryu. App. Wsgi import ControllerBase
The from ryu. Topology import event, switches
The from the collections import defaultdict
# switches
Switches=[]
# mymac [srcmac] - & gt; (the switch port)
Mymac={}
# adjacency map [sw1] [sw2] - & gt; The port from sw1 to sw2
Adjacency=defaultdict (lambda: defaultdict (lambda: None))
Def minimum_distance (short, Q) :
Min=float (' Inf ')
The node=0
For v in Q:
If short [v]
The node=v
Return the node
Def get_path (SRC, DST, first_port final_port) :
# Dijkstra 's implementation
# print "get_path is called, SRC=" https://bbs.csdn.net/topics/, SRC, "DST, DST=", "first_port=", first_port, "final_port final_port=",
Short={}
Previous={}
For dpid switches in:
Short [dpid]=float (' Inf ')
Previous [dpid]=None
Short/SRC=0
Q=set (switches)
Print "Q=", Q
While len (Q) & gt; 0:
U=minimum_distance (short, Q)
Q.r emove (u)
For p in switches:
If adjacency [u] [p]!=None:
W=1
If short [u] + w & lt; Short [p] :
Short [p]=short [u] + w
The previous [p]=u
R=[]
P=DST
Of state Richard armitage ppend (p)
Q=previous [p]
While q is not None:
If the q==SRC:
Of state Richard armitage ppend (q)
Break
P=q
Of state Richard armitage ppend (p)
Q=previous [p]
R.r everse ()
If src=https://bbs.csdn.net/topics/=dst:
Path=(SRC)
The else:
Path=r
# Adding the ports
R=[]
In_port=first_port
For s1, s2 in zip (path [: 1], the path [1]) :
Out_port=adjacency/s1/s2
Of state Richard armitage ppend ((s1, in_port out_port))
In_port=adjacency (s2) (s1)
Of state Richard armitage ppend ((DST, in_port final_port))
Return r
The class ProjectController (app_manager RyuApp) :
OFP_VERSIONS=[ofproto_v1_3 OFP_VERSION]
Def __init__ (self, * args, * * kwargs) :
Super (ProjectController, self) __init__ (* args, * * kwargs)
Self. Mac_to_port={}
Self. Topology_api_app=self
Self. Datapath_list=[]
# the Function that lists all the attributes in the given object
Def ls (self, obj) :
"\ n". Join ([x for x in dir (obj) if x [0]!="_"])
Def add_flow (self, datapath, in_port, DST, actions) :
Ofproto=datapath. Ofproto
Parser=datapath. Ofproto_parser
Match=datapath. Ofproto_parser. OFPMatch (in_port=in_port, eth_dst=DST)
Inst=[parser OFPInstructionActions (ofproto OFPIT_APPLY_ACTIONS, actions)]
Mod=datapath. Ofproto_parser. OFPFlowMod (datapath=datapath, match=match, cookie=0, the command=ofproto. OFPFC_ADD, idle_timeout=0, hard_timeout=0, priority=ofproto. OFP_DEFAULT_PRIORITY, instructions=inst)
Datapath. Send_msg (mod)
Def install_path (self, p, ev, src_mac dst_mac) :
Shortest_path_route=[]
For z in p:
Shortest_path_route. Append (STR [0]) (z)
# print "" + STR (z)
MAC: print "source", src_mac, "destination MAC:" dst_mac, "the shortest path:", shortest_path_route
MSG=ev. MSG
Datapath=MSG. Datapath
Ofproto=datapath. Ofproto
Parser=datapath. Ofproto_parser
For sw, in_port out_port in p:
# print src_mac, "- & gt;" , dst_mac, "via", sw, "in_port=" in_port, "out_port out_port=",
Match=parser. OFPMatch (in_port=in_port eth_src=https://bbs.csdn.net/topics/src_mac, eth_dst=dst_mac)
The actions=[parser OFPActionOutput (out_port)]
Datapath=self. Datapath_list [int (sw) - 1)
Inst=[parser OFPInstructionActions (ofproto OFPIT_APPLY_ACTIONS, actions)]
Mod=datapath. Ofproto_parser. OFPFlowMod (
Datapath=datapath, match=match, idle_timeout=0, hard_timeout=0, priority=1, instructions=inst)
Datapath. Send_msg (mod)
@ set_ev_cls (ofp_event EventOFPSwitchFeatures CONFIG_DISPATCHER)
Def switch_features_handler (self, ev) :
Print "connection switch:" + STR (ev) MSG. Datapath. Id)
Datapath=ev. MSG. Datapath
Ofproto=datapath. Ofproto
Parser=datapath. Ofproto_parser
Match=parser. OFPMatch ()
The actions=[parser OFPActionOutput (ofproto. OFPP_CONTROLLER, ofproto. OFPCML_NO_BUFFER)]
Inst=[parser OFPInstructionActions (ofproto OFPIT_APPLY_ACTIONS, actions)]
Mod=datapath. Ofproto_parser. OFPFlowMod (
Datapath=datapath, match=match, cookie=0,
The command=ofproto OFPFC_ADD, idle_timeout=0, hard_timeout=0, priority=0, instructions=inst)
Datapath. Send_msg (mod)
@ set_ev_cls (ofp_event EventOFPPacketIn MAIN_DISPATCHER)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull