SDN实验---Ryu的应用开发(四)基于跳数的最短路径转发原理
一:实现最短跳数转发

(一)原理

推文:迪杰斯特拉算法和弗洛伊德算法
二:代码实现
(一)全部代码
from ryu.base import app_manager
from ryu.controller.handler import set_ev_cls
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER,CONFIG_DISPATCHER
from ryu.lib.packet import packet,ethernet
from ryu.topology import event
from ryu.topology.api import get_switch,get_link
from ryu.ofproto import ofproto_v1_3 import networkx as nx class MyShortestForwarding(app_manager.RyuApp):
'''
class to achive shortest path to forward, based on minimum hop count
'''
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] def __init__(self,*args,**kwargs):
super(MyShortestForwarding,self).__init__(*args,**kwargs) #set data structor for topo construction
self.network = nx.DiGraph() #store the dj graph
self.paths = {} #store the shortest path
self.topology_api_app = self @set_ev_cls(ofp_event.EventOFPSwitchFeatures,CONFIG_DISPATCHER)
def switch_features_handler(self,ev):
'''
manage the initial link between switch and controller
'''
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser match = ofp_parser.OFPMatch() #for all packet first arrive, match it successful, send it ro controller
actions = [ofp_parser.OFPActionOutput(
ofproto.OFPP_CONTROLLER,ofproto.OFPCML_NO_BUFFER
)] self.add_flow(datapath, , match, actions) def add_flow(self,datapath,priority,match,actions):
'''
fulfil the function to add flow entry to switch
'''
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(ofp_event.EventOFPPacketIn,MAIN_DISPATCHER)
def packet_in_handler(self,ev):
'''
manage the packet which comes from switch
'''
#first get event infomation
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser in_port = msg.match['in_port']
dpid = datapath.id #second get ethernet protocol message
pkt = packet.Packet(msg.data)
eth_pkt = pkt.get_protocol(ethernet.ethernet) eth_src = eth_pkt.src #note: mac info willn`t change in network
eth_dst = eth_pkt.dst out_port = self.get_out_port(datapath,eth_src,eth_dst,in_port)
actions = [ofp_parser.OFPActionOutput(out_port)] if out_port != ofproto.OFPP_FLOOD:
match = ofp_parser.OFPMatch(in_port=in_port,eth_dst=eth_dst)
self.add_flow(datapath,,match,actions) out = ofp_parser.OFPPacketOut(
datapath=datapath,buffer_id=msg.buffer_id,in_port=in_port,
actions=actions,data=msg.data
) datapath.send_msg(out) @set_ev_cls(event.EventSwitchEnter,[CONFIG_DISPATCHER,MAIN_DISPATCHER]) #event is not from openflow protocol, is come from switchs` state changed, just like: link to controller at the first time or send packet to controller
def get_topology(self,ev):
'''
get network topo construction, save info in the dict
''' #store nodes info into the Graph
switch_list = get_switch(self.topology_api_app,None) #------------need to get info,by debug
switches = [switch.dp.id for switch in switch_list]
self.network.add_nodes_from(switches) #store links info into the Graph
link_list = get_link(self.topology_api_app,None)
#port_no, in_port ---------------need to debug, get diffirent from both
links = [(link.src.dpid,link.dst.dpid,{'attr_dict':{'port':link.dst.port_no}}) for link in link_list] #add edge, need src,dst,weigtht
self.network.add_edges_from(links) links = [(link.dst.dpid,link.src.dpid,{'attr_dict':{'port':link.dst.port_no}}) for link in link_list]
self.network.add_edges_from(links) def get_out_port(self,datapath,src,dst,in_port):
'''
datapath: is current datapath info
src,dst: both are the host info
in_port: is current datapath in_port
'''
dpid = datapath.id #the first :Doesn`t find src host at graph
if src not in self.network:
self.network.add_node(src)
self.network.add_edge(dpid, src, attr_dict={'port':in_port})
self.network.add_edge(src, dpid)
self.paths.setdefault(src, {}) #second: search the shortest path, from src to dst host
if dst in self.network:
if dst not in self.paths[src]: #if not cache src to dst path,then to find it
path = nx.shortest_path(self.network,src,dst)
self.paths[src][dst]=path path = self.paths[src][dst]
next_hop = path[path.index(dpid)+]
#print("1ooooooooooooooooooo")
#print(self.network[dpid][next_hop])
out_port = self.network[dpid][next_hop]['attr_dict']['port']
#print("2ooooooooooooooooooo")
#print(out_port) #get path info
#print("6666666666 find dst")
print(path)
else:
out_port = datapath.ofproto.OFPP_FLOOD #By flood, to find dst, when dst get packet, dst will send a new back,the graph will record dst info
#print("8888888888 not find dst")
return out_port
(二)注意:由于各种版本的不同,导致我们使用函数的时候可能有所不同,需要我们自己去调试
1.安装networkx模块,若是下载太慢或者出错,换国内源:在末尾加上-i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install networkx
pip3 install multiqc
2.出现self.network.add_edge(dpid, src, {'port':in_port})使用时,会出现参数太多
self.network.add_edge(dpid, src, attr_dict={'port':in_port}) attr_dict是对我们提供的扩展成熟
3.学会代码调试和思考
三:代码讲解
(一)不变代码:实现初始连接处理和公共函数--下发流表
@set_ev_cls(ofp_event.EventOFPSwitchFeatures,CONFIG_DISPATCHER)
def switch_features_handler(self,ev):
'''
manage the initial link between switch and controller
'''
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser match = ofp_parser.OFPMatch() #for all packet first arrive, match it successful, send it ro controller
actions = [ofp_parser.OFPActionOutput(
ofproto.OFPP_CONTROLLER,ofproto.OFPCML_NO_BUFFER
)] self.add_flow(datapath, , match, actions) def add_flow(self,datapath,priority,match,actions):
'''
fulfil the function to add flow entry to switch
'''
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)
(二)实现获取网络拓扑结构
class MyShortestForwarding(app_manager.RyuApp):
'''
class to achive shortest path to forward, based on minimum hop count
'''
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] def __init__(self,*args,**kwargs):
super(MyShortestForwarding,self).__init__(*args,**kwargs) #set data structor for topo construction
self.network = nx.DiGraph() #store the dj graph 设置图结构存储信息
self.paths = {} #store the shortest path
self.topology_api_app = self @set_ev_cls(event.EventSwitchEnter,[CONFIG_DISPATCHER,MAIN_DISPATCHER]) #event is not from openflow protocol, is come from switchs` state changed, just like: link to controller at the first time or send packet to controller
def get_topology(self,ev):
'''
get network topo construction, save info in the dict 由于监听交换机信息进入,触发,注意事件不在flow协议类,在from ryu.topology import event中
''' #store nodes info into the Graph
switch_list = get_switch(self.topology_api_app,None) #------------need to get info,by debug
switches = [switch.dp.id for switch in switch_list]
self.network.add_nodes_from(switches) #store links info into the Graph
link_list = get_link(self.topology_api_app,None)
#port_no, in_port ---------------need to debug, get diffirent from both
links = [(link.src.dpid,link.dst.dpid,{'attr_dict':{'port':link.dst.port_no}}) for link in link_list] #add edge, need src,dst,weigtht
self.network.add_edges_from(links) links = [(link.dst.dpid,link.src.dpid,{'attr_dict':{'port':link.dst.port_no}}) for link in link_list]
self.network.add_edges_from(links)
补充:event.EventSwitchEnter---由于监听交换机信息进入,触发,注意事件不在flow协议类,在from ryu.topology import event中
(三)实现下一跳端口获取(根据图获取最短路径,从中获取信息)
def get_out_port(self,datapath,src,dst,in_port):
'''
datapath: is current datapath info
src,dst: both are the host info
in_port: is current datapath in_port
'''
dpid = datapath.id #the first :Doesn`t find src host at graph
if src not in self.network: #根据src主机是否在网络中,决定是否新添加进入
self.network.add_node(src)
self.network.add_edge(dpid, src, attr_dict={'port':in_port})
self.network.add_edge(src, dpid)
self.paths.setdefault(src, {}) #设置数据结构:用于保存每个源主机到各个目的主机的最短路径{src1:{dst1:[],dst2:[],dst3:[]....},src2:{dst1:[],dst2:[],dst3:[]....},} #second: search the shortest path, from src to dst host
if dst in self.network:
if dst not in self.paths[src]: #if not cache src to dst path,then to find it
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] #根据数据结构获取下一跳datapath信息
#print("1ooooooooooooooooooo")
#print(self.network[dpid][next_hop])
out_port = self.network[dpid][next_hop]['attr_dict']['port'] #根据该id和下一跳id去获取出端口,进行数据转发
#print("2ooooooooooooooooooo")
#print(out_port) #get path info
#print("6666666666 find dst")
print(path)
else: #否则是泛洪处理
out_port = datapath.ofproto.OFPP_FLOOD #By flood, to find dst, when dst get packet, dst will send a new back,the graph will record dst info
#print("8888888888 not find dst")
return out_port
(四)实现包接收,计算最短路径,按照最短路径进行动作下发《重点》
@set_ev_cls(ofp_event.EventOFPPacketIn,MAIN_DISPATCHER)
def packet_in_handler(self,ev):
'''
manage the packet which comes from switch
'''
#first get event infomation
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser in_port = msg.match['in_port']
dpid = datapath.id #second get ethernet protocol message
pkt = packet.Packet(msg.data)
eth_pkt = pkt.get_protocol(ethernet.ethernet) eth_src = eth_pkt.src #note: mac info willn`t change in network
eth_dst = eth_pkt.dst out_port = self.get_out_port(datapath,eth_src,eth_dst,in_port) #这里进行获取下一跳端口
actions = [ofp_parser.OFPActionOutput(out_port)] if out_port != ofproto.OFPP_FLOOD:
match = ofp_parser.OFPMatch(in_port=in_port,eth_dst=eth_dst)
self.add_flow(datapath,,match,actions) out = ofp_parser.OFPPacketOut(
datapath=datapath,buffer_id=msg.buffer_id,in_port=in_port,
actions=actions,data=msg.data
) datapath.send_msg(out)
三:实验演示:注意开启顺序,否则可能导致其他错误
(一)启动Ryu控制器 observe-links命令会导致控制器在运行期间 会不间断地发送LLDP数据包进行链路探测
ryu-manager my_shortest_forward.py --observe-links --verbose

(二)启动Mininet
sudo mn --topo=tree,, --controller=remote --mac


(三)Ryu查看信息

SDN实验---Ryu的应用开发(四)基于跳数的最短路径转发原理的更多相关文章
- SDN实验---Ryu的应用开发(二)Learning Switch
一:自学习交换机(二层MAC交换机)的编程思路 (一)明确问题 如何实现软件定义的自学习交换机? (二)设计解决方案 通过控制器来实现自学习交换算法,然后指导数据平面实现交换机操作 (三)确定具体的技 ...
- SDN实验---Ryu的应用开发(三)流量监控
一:实现流量监控 (一)流量监控原理 其中控制器向交换机周期下发获取统计消息,请求交换机消息------是主动下发过程 流速公式:是(t1时刻的流量-t0时刻的流量)/(t1-t0) 剩余带宽公式:链 ...
- SDN实验---Ryu的应用开发(四)北向接口RESTAPI
一:推文 软件定义网络基础---REST API概述 软件定义网络基础---REST API的设计规范 二:掌握Ryu基本RESTAPI使用方法 (一)Ryu的RESTAPI (二) REST应用样例 ...
- SDN实验---Ryu的应用开发(一)Hub实现
补充: (一)Ubuntu下安装Notepadqq 背景:为什么安装Notepadqq Notepad++ 不仅有语法高亮度显示,也有语法折叠功能,并且支持宏以及扩充基本功能的外挂模组.但是可惜的是N ...
- SDN实验---Ryu的安装
一:Ryu是主流SDN开源控制器之一 (一)推文(摘录自) https://ryu.readthedocs.io/en/latest/ https://www.sdnlab.com/1785.html ...
- SDN实验---Ryu的源码分析
一:安装Pycharm https://www.cnblogs.com/huozf/p/9304396.html(有可取之处) https://www.jetbrains.com/idea/buy/# ...
- SDN实验---Mininet实验(玩转流表)
一:实验目的 (一)案例目的 (二)实验内容 (三)网络拓扑结构 二:OpenFlow流表实验准备 (一)使用Python设置网络拓扑 --- tree_topo.py from mininet.to ...
- 构建一个基本的前端自动化开发环境 —— 基于 Gulp 的前端集成解决方案(四)
通过前面几节的准备工作,对于 npm / node / gulp 应该已经有了基本的认识,本节主要介绍如何构建一个基本的前端自动化开发环境. 下面将逐步构建一个可以自动编译 sass 文件.压缩 ja ...
- 实验四实验报告————Android基础开发
实验四实验报告----Android基础开发 任务一 关于R类 关于apk文件 实验成果 任务二 活动声明周期 实验成果 任务三 关于PendingIntent类 实验成果 任务四 关于布局 实验成果 ...
随机推荐
- Docker版zabbix
1. docker-compose docker-compose :容器自带的编排工作,可以通过yaml编排文件,将容器要启动的命令写入文件,然后再利用docker-compose run file. ...
- 接口自动化--读取Excel操作(openpyxl)
上次我们已经将requests库封装成我们想要的样子了,我们的接口自动化已经完成了最开始的一步了,接下来我们需要完成我们相应的其他模块的封装,下面简单介绍下我们在接口自动化需要用到的模块吧在接口自动化 ...
- python正则表达式(5)--findall、finditer方法
findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...
- Vue 实例暴露了一些有用的实例属性与方法。这些属性与方法都有前缀 $,以便与代理的 data 属性区分
var data = { a: 1 } var vm = new Vue({ el: '#example', data: data }) vm.$data === data // -> true ...
- OpenCV应用(4)雄迈相机网络取图
第一种办法 单张图获取 网页打开#//http://192.168.1.82/webcapture.jpg?command=snap&channel= 1 网页打开直接抓一张图 python ...
- 【BZOJ1095】【ZJOI2007】捉迷藏
前言 好恶心的一道题,代码写了2.5h,调试调了5h+,局部重构了n遍. 题意 一棵树上的节点有黑白两色,初始为黑,支持修改颜色,查询最远黑点对.$n<=10^5,m<=5*10^5$ 题 ...
- podium micro-frontends 简单试用
以下是一个简单的podium 试用,包含了layout 以及podlets,使用docker 运行 podium 主要包含了两大部分 podlets 片段服务 layouts 片段组合服务 环境准备 ...
- 8.8poc包问题
对于8.8的包的问题:zabbix server设备重启后 zabbix server,mariadb,zabbix agent启动不了.是因为在7代的centos中在主机重启后.自动删除了/var/ ...
- Reconstructing Cloud-Contaminated Multispectral Images With Contextualized Autoencoder Neural Networks(自编码机重建云污染区)
1.逐像元输入输出与邻域输入输出,邻域处理是先flatten,再unflatten 2.用MDL方法(最小描述长度)寻找自编码机最佳隐藏层数 3.多目标优化方法寻找MDL方法的超参数,平衡MDL方法两 ...
- bzoj 3585 mex - 线段树 - 分块 - 莫队算法
Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. 从第三行开始,每行一个询问 ...