基于DFS算法的Ryu+Mininet应用
利用DFS算法,实现Ryu应用,并在Mininet上完成相关验证
Ryu与Mininet相关安装与配置详见:
https://blog.csdn.net/haimianxiaojie/article/details/50705288
关于本文内所有完整代码详见:
https://github.com/PPPerry/Ryu_projects中的DFS部分
实现内容如下:
-
在Mininet上搭建一个20个节点网络(拓扑给定),每个网络节点下挂一个主机;
按照如图所示的拓扑,编写mininet的拓扑代码,各个交换机与主机的序号均相同。
完整的拓扑代码如下:#!/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 OVSKernelSwitch, 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 def myNetwork(): net = Mininet( topo=None, controller=None, ipBase='10.0.0.0/8') ## specify clearly to use a remote controller instead of the default one c=RemoteController('c','0.0.0.0',6633) net.addController(c) info( '*** Add switches\n') s2 = net.addSwitch('s2', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s11 = net.addSwitch('s11', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s3 = net.addSwitch('s3', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s8 = net.addSwitch('s8', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s4 = net.addSwitch('s4', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s6 = net.addSwitch('s6', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s7 = net.addSwitch('s7', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s5 = net.addSwitch('s5', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s10 = net.addSwitch('s10', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s9 = net.addSwitch('s9', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s1 = net.addSwitch('s1', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s12 = net.addSwitch('s12', cls=OVSKernelSwitch,protocols=['OpenFlow13']) s13 = net.addSwitch('s13', cls=OVSKernelSwitch,protocols=['OpenFlow13']) info( '*** Add hosts\n') h3 = net.addHost('h3', cls=Host, ip='10.0.0.3', defaultRoute=None,mac='00:00:00:00:00:03') h10 = net.addHost('h10', cls=Host, ip='10.0.0.10', defaultRoute=None,mac='00:00:00:00:00:10') h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None,mac='00:00:00:00:00:02') h5 = net.addHost('h5', cls=Host, ip='10.0.0.5', defaultRoute=None,mac='00:00:00:00:00:05') h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None,mac='00:00:00:00:00:01') h11 = net.addHost('h11', cls=Host, ip='10.0.0.11', defaultRoute=None,mac='00:00:00:00:00:11') h6 = net.addHost('h6', cls=Host, ip='10.0.0.6', defaultRoute=None,mac='00:00:00:00:00:06') h4 = net.addHost('h4', cls=Host, ip='10.0.0.4', defaultRoute=None,mac='00:00:00:00:00:04') h7 = net.addHost('h7', cls=Host, ip='10.0.0.7', defaultRoute=None,mac='00:00:00:00:00:07') h13 = net.addHost('h13', cls=Host, ip='10.0.0.13', defaultRoute=None,mac='00:00:00:00:00:13') h9 = net.addHost('h9', cls=Host, ip='10.0.0.9', defaultRoute=None,mac='00:00:00:00:00:09') h12 = net.addHost('h12', cls=Host, ip='10.0.0.12', defaultRoute=None,mac='00:00:00:00:00:12') h8 = net.addHost('h8', cls=Host, ip='10.0.0.8', defaultRoute=None,mac='00:00:00:00:00:08') info( '*** Add links\n') net.addLink(h1, s1) net.addLink(h2, s2) net.addLink(h3, s3) net.addLink(h4, s4) net.addLink(h5, s5) net.addLink(h6, s6) net.addLink(h7, s7) net.addLink(h8, s8) net.addLink(h9, s9) net.addLink(h10, s10) net.addLink(h11, s11) net.addLink(h12, s12) net.addLink(h13, s13) net.addLink(s2, s1) net.addLink(s2, s3) net.addLink(s3, s4) net.addLink(s2, s5) net.addLink(s5, s7) net.addLink(s5, s6) net.addLink(s5, s8) net.addLink(s5, s9) net.addLink(s8, s9) net.addLink(s9, s11) net.addLink(s9, s10) net.addLink(s10, s11) net.addLink(s3, s10) net.addLink(s3, s13) net.addLink(s10, s13) net.addLink(s11, s12) net.addLink(s12, s13) ## start switch for i in range(1,7): net.get("s{}".format(i)).start([]) info( '*** Starting network\n') net.start() #into the interactive mode CLI(net) net.stop() if __name__ == '__main__': setLogLevel( 'info' ) myNetwork()
其中,控制器c被设置为RemoteController,方便下一步与Ryu进行连接。
-
使用Ryu连接Mininet中的交换机;
使用Ryu连接mininet后,结果如下:Ryu读出了所有的交换机,并发现了34个连接。
-
并将拓扑读出来进行可视化展示;
利用mininet自带的miniedit.py,我们可以直观的实现mininet的静态可视化,如图所示: -
在Ryu上实现深度优先遍历算法,并找出任意两个主机间的最短路和最长路;
使用DFS可以找到任意两个交换机之间的所有路径,从而通过遍历,很容易找到任意两个主机(交换机)之间的最短路和最长路。
实现DFS的递归算法如下:def findpath(self,src_sw,dst_sw,sign,onepath,allpaths): if src_sw==dst_sw: #print(onepath) allpaths.append(onepath.copy()) #print(allpaths) else: for u in self.switches: if (self.get_adjacent(src_sw,u) is not None)and(sign[u]!=1): sign[u]=1 onepath.append(u) self.findpath(u,dst_sw,sign,onepath,allpaths) onepath.remove(u) sign[u]=0
返回值为一个二维列表,里面存储了从源交换机到目的交换机的所有路径(用队列存储)
-
使用最长路来配置任意两个主机间的通信连接
再得到所有路径之后,通过遍历找到最长路和最短路并打印,再将最长路的路径存储到record中,进行下一步的路径配置def shortest_path(self,src_sw,dst_sw,first_port,last_port): self.logger.info("topo calculate the shortest path from ---{}-{}-------{}-{}".format(first_port,src_sw,dst_sw,last_port)) self.logger.debug("there is {} swithes".format(len(self.switches))) sign={} for s in self.switches: sign[s]=0 sign[src_sw]=1 onepath=[] onepath.append(src_sw) allpaths=[] self.findpath(src_sw,dst_sw,sign,onepath,allpaths) #print(allpaths) print("paths num is: {}".format(len(allpaths))) print("all paths:") sp=allpaths[0] lp=allpaths[0] for i in allpaths: if(len(i)>len(lp)): lp=i if(len(i)<len(sp)): sp=i print(i) print("the shortest path is: ") print(sp) print("the longest path is: ") print(lp) if src_sw==dst_sw: path=[src_sw] else: path=lp record=[] inport=first_port # s1 s2; s2:s3, sn-1 sn for s1,s2 in zip(path[:-1],path[1:]): # s1--outport-->s2 outport,_=self.get_adjacent(s1,s2) record.append((s1,inport,outport)) inport,_=self.get_adjacent(s2,s1) record.append((dst_sw,inport,last_port)) #we find a path # (s1,inport,outport)->(s2,inport,outport)->...->(dest_switch,inport,outport) return record
返回值record为最长路(一维列表),传到路径配置中
实现效果如图:
以h1 ping -c 1 h9为例:(拓扑序号命名见前面的拓扑图)
打印出h9到h1所有路径和最长路、最短路,并配置最长路:
以pingall为例:
所有的交换机均ping通,无掉包,且均按最长路径配置
输出所有的路径: -
将配置通的业务在可视化平台上进行展示
利用networkx,将拓扑图与最长路径(配置的路径)进行可视化展示:
以h1 ping -c 1 h9为例:(拓扑序号命名见前面的拓扑图)
图片样式是networkx随机生成的,但拓扑是不会变的,即从h1到h9的路径就如图中红色的路径所示。
实现networkx的部分代码如下:#draw def draw_graph(graph,path): # extract nodes from graph nodes = set([n1 for n1, n2 in graph] + [n2 for n1, n2 in graph]) # create networkx graph G=nx.Graph() # add nodes for node in nodes: G.add_node(node) # add edges G.add_edges_from(graph,color='b') G.add_edges_from(path,color='r') #G.add_edges_from(new) # draw graph edges = G.edges() colors = [G[u][v]['color'] for u,v in edges] nx.draw(G,with_labels=True,edges=edges,edge_color=colors) # show graph plt.show()
推荐阅读