下面兩段代碼可以在后面給我加上注釋嗎。。 小白看不懂 救命大佬們!!!
控制器代碼:
#coding:utf-8
from ryu.base import app_manager
from ryu.controller import mac_to_port
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.mac import haddr_to_bin
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types
from ryu.lib import mac
from ryu.topology.api import get_switch, get_link
from ryu.app.wsgi import ControllerBase
from ryu.topology import event, switches
from collections import defaultdict
#switches
switches = []
#mymac[srcmac]->(switch, port)
mymac={ }
#adjacency map [sw1][sw2]->port from sw1 to sw2
adjacency=defaultdict(lambda:defaultdict(lambda:None))
def minimum_distance(distance, Q):
min = float('Inf')
node = 0
for v in Q:
if distance[v] < min:
min = distance[v]
node = v
return 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
distance = {}
previous = {}
for dpid in switches:
distance[dpid] = float('Inf')
previous[dpid] = None
distance[src]=0
Q=set(switches)
print "Q=", Q
while len(Q)>0:
u = minimum_distance(distance, Q)
Q.remove(u)
for p in switches:
if adjacency[u][p]!=None:
w = 1
if distance[u] + w < distance[p]:
distance[p] = distance[u] + w
previous[p] = u
r=[]
p=dst
r.append(p)
q=previous[p]
while q is not None:
if q == src:
r.append(q)
break
p=q
r.append(p)
q=previous[p]
r.reverse()
if src=https://bbs.csdn.net/topics/=dst:
path=[src]
else:
path=r
# Adding the ports
r = []
in_port = first_port
for s1,s2 in zip(path[:-1],path[1:]):
out_port = adjacency[s1][s2]
r.append((s1,in_port,out_port))
in_port = adjacency[s2][s1]
r.append((dst,in_port,final_port))
return r
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=[]
# Function that lists all 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,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(z[0]))
#print ""+str(z)
print "源mac:", src_mac, " 目的mac: ", dst_mac, "最短路徑:", 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,"->", 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)
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 "連接交換機:"+ str(ev.msg.datapath.id)
datapath = ev.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
match = parser.OFPMatch()
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,
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)
def _packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
eth = pkt.get_protocol(ethernet.ethernet)
#print "eth.ethertype=", eth.ethertype
#avodi broadcast from LLDP
if eth.ethertype==35020:
return
dst = eth.dst
src = eth.src
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
if src not in mymac.keys():
mymac[src]=( dpid, in_port)
#print "mymac=", mymac
if dst in mymac.keys():
p = get_path(mymac[src][0], mymac[dst][0], mymac[src][1], mymac[dst][1])
print p
self.install_path(p, ev, src, dst)
out_port = p[0][2]
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
# install a flow to avoid packet_in next time
if out_port != ofproto.OFPP_FLOOD:
match = parser.OFPMatch(in_port=in_port, eth_src=https://bbs.csdn.net/topics/src, eth_dst=dst)
data=https://bbs.csdn.net/topics/None
if msg.buffer_id==ofproto.OFP_NO_BUFFER:
data=https://bbs.csdn.net/topics/msg.data
out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port,actions=actions, data=https://bbs.csdn.net/topics/data)
datapath.send_msg(out)
# EventSwitchEnter triggers get_topology_data method
@set_ev_cls(event.EventSwitchEnter)
def get_topology_data(self, ev):
global switches
#List of switch objects from topology
switch_list = get_switch(self.topology_api_app, None)
switches=[switch.dp.id for switch in switch_list]
self.datapath_list=[switch.dp for switch in switch_list]
#print "self.datapath_list=", self.datapath_list
#print "switches=", switches
#List of link objects from topology
links_list = get_link(self.topology_api_app, None)
mylinks=[(link.src.dpid,link.dst.dpid,link.src.port_no,link.dst.port_no) for link in links_list]
#print mylinks
# for s1,s2,port1,port2 in mylinks:
# adjacency[s1][s2]=port1
# adjacency[s2][s1]=port2
@set_ev_cls(event.EventSwitchLeave, MAIN_DISPATCHER)
def switch_leave_handler(self, ev):
print "switch_leave_handler:"
switch = ev.switch.dp.id
print switch
if switch in switches:
switches.remove(switch)
del adjacency[switch]
@set_ev_cls(event.EventLinkAdd, MAIN_DISPATCHER)
def link_add_handler(self, ev):
s1 = ev.link.src
s2 = ev.link.dst
adjacency[s1.dpid][s2.dpid] = s1.port_no
adjacency[s2.dpid][s1.dpid] = s2.port_no
print 'a',adjacency
print "---"
拓撲代碼:
#!/usr/bin/python
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call
from mininet.link import TCLink
from mininet.topo import Topo
from mininet.util import dumpNodeConnections
class CustomTree(Topo):
"Creating the switches and hosts, then building the tree"
def build(self):
#Adding Hosts - Client PC & Client Server
h1 = self.addHost('h1',ip='10.0.0.1',cls= None,defaultRoute=None)
h2 = self.addHost('h2',ip='10.0.0.2',cls= None,defaultRoute=None)
#Adding Switches
info("Adding Switches\n")
s1 = self.addSwitch('s1',datapath=None,protocols=None)
s2 = self.addSwitch('s2',datapath=None,protocols=None)
s3 = self.addSwitch('s3',datapath=None,protocols=None)
s4 = self.addSwitch('s4',datapath=None,protocols=None)
s5 = self.addSwitch('s5',datapath=None,protocols=None)
s6 = self.addSwitch('s6',datapath=None,protocols=None)
s7 = self.addSwitch('s7',datapath=None,protocols=None)
info("Adding Links\n")
#Adding bandwidth attribute
self.addLink(s1,s2,cls=TCLink,bw=10)
self.addLink(s2,s3,cls=TCLink,bw=10)
self.addLink(s3,s7,cls=TCLink,bw=10)
self.addLink(s4,s5,cls=TCLink,bw=10)
self.addLink(s5,s6,cls=TCLink,bw=10)
self.addLink(s6,s7,cls=TCLink,bw=10)
self.addLink(s4,s1,cls=TCLink,bw=10)
info("Adding Host Links\n")
#Adding Host Links
self.addLink(h1,s1)
self.addLink(h2,s7)
def myProgram():
info("Topology Python Script...\n")
net = Mininet(topo=CustomTree(), build=False, ipBase='10.0.0.0/8',autoStaticArp=True,link=TCLink)
info("Adding the RemoteController\n")
c0 = net.addController('c0',ip='127.0.0.1',protocol='tcp',controller=RemoteController, port = 6633)
info("Adding Hosts\n")
info("Starting network")
#net.build()
net.start()
info('*** Starting switches\n')
CLI(net)
net.stop()
if __name__ == '__main__':
setLogLevel('info')
myProgram()
由衷感謝大佬的幫助!!!!!!
uj5u.com熱心網友回復:
是用RYU控制器做的路由配置 用的Dijkstra演算法轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/278214.html
標籤:數據結構與算法
上一篇:qt程式實作控制電腦螢屏亮度問題
