本文主要是介绍Ryu中基于时延的最短路径转发算法(SPF),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
1.主机发现
2.链路时延测量[2]
3. ryu/topology/Switches.py修改
3.1 PortData类
3.2 lldp_packet_in_handler()
3.3 重新编译安装Ryu
4.获取lldp_delay
5.图存储与最短路径计算
参考文献
-
环路广播风暴问题
对于存在环路的网络拓扑结构,首先需要解决ARP包的洪泛问题,一种解决思路是:Ryu可以将交换机的端口信息记录下来,发现全局的拓扑信息。当控制器收到一个未学习的ARP Request时,直接发给所有交换机连接主机的那些端口,从而减少了在交换机与交换机间的洪泛[1]。
1.主机发现
Ryu通过LLDP报文发现拓扑中的交换机,主机发现则需要主机主动发包
from ryu.base import app_manager
from ryu.ofproto import ofproto_v1_3
from ryu.controller.handler import set_ev_cls
from ryu.controller.handler import MAIN_DISPATCHER, CONFIG_DISPATCHER
from ryu.controller import ofp_event
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib import hub
from ryu.topology.api import get_all_host, get_all_link, get_all_switchclass NetworkAwareness(app_manager.RyuApp):OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]def __init__(self, *args, **kwargs):super(NetworkAwareness, self).__init__(*args, **kwargs)self.dpid_mac_port = {}self.topo_thread = hub.spawn(self._get_topology)def add_flow(self, datapath, priority, match, actions):dp = datapathofp = dp.ofprotoparser = dp.ofproto_parserinst = [parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions)]mod = parser.OFPFlowMod(datapath=dp, priority=priority, match=match, instructions=inst)dp.send_msg(mod)@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)def switch_features_handler(self, ev):msg = ev.msgdp = msg.datapathofp = dp.ofprotoparser = dp.ofproto_parsermatch = parser.OFPMatch()actions = [parser.OFPActionOutput(ofp.OFPP_CONTROLLER, ofp.OFPCML_NO_BUFFER)]self.add_flow(dp, 0, match, actions)def _get_topology(self):while True:self.logger.info('\n\n\n')hosts = get_all_host(self)switches = get_all_switch(self)links = get_all_link(self)self.logger.info('hosts:')for hosts in hosts:self.logger.info(hosts.to_dict())self.logger.info('switches:')for switch in switches:self.logger.info(switch.to_dict())self.logger.info('links:')for link in links:self.logger.info(link.to_dict())hub.sleep(2)
2.链路时延测量[2]
参考:基于Ryu和OpenFlow协议的网络时延、带宽等状态测量。
交换机A到交换机B的链路时延为(Tab+Tba-Ta-Tb)/2,Tab为控制器经交换机A到B再返回控制器的时延,Tba为控制器经交换机B到A再返回控制器的时延,Ta、Tb分别为控制器到交换机A和B的往返时延。
3. ryu/topology/Switches.py修改
3.1 PortData类
PortData记录交换机的端口信息,增加self.delay属性记录Tab和Tba(后用lldp_delay统一表示),self.timestamp为LLDP包在发送时被打上的时间戳。
class PortData(object):def __init__(self, is_down, lldp_data):super(PortData, self).__init__()self.is_down = is_downself.lldp_data = lldp_dataself.timestamp = Noneself.sent = 0self.delay = 0
3.2 lldp_packet_in_handler()
lldp_packet_in_handler()负责处理接收到的LLDP包,lldp_delay=收到LLDP报文的时间戳-发送时的时间戳。
由于LLDP报文被设计为经一跳后转给控制器,因此可将lldp_delay存入发送LLDP包对应的交换机端口。
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)def lldp_packet_in_handler(self, ev):# add receive timestamprecv_timestamp = time.time()if not self.link_discovery:returnmsg = ev.msgtry:src_dpid, src_port_no = LLDPPacket.lldp_parse(msg.data)except LLDPPacket.LLDPUnknownFormat:# This handler can receive all the packets which can be# not-LLDP packet. Ignore it silentlyreturn# calc the delay of lldp packetfor port, port_data in self.ports.items():if src_dpid == port.dpid and src_port_no == port.port_no:send_timestamp = port_data.timestampif send_timestamp:port_data.delay = recv_timestamp - send_timestamp
3.3 重新编译安装Ryu
在Ryu安装目录下,找到setup.py文件,执行以下代码段:
sudo python setup.py install
4.获取lldp_delay
利用lookup_service_brick获取到正在运行的switches
的实例,
from ryu.base.app_manager import lookup_service_brick...@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)def packet_in_hander(self, ev):msg = ev.msgdpid = msg.datapath.idtry:src_dpid, src_port_no = LLDPPacket.lldp_parse(msg.data)if self.switches is None:self.switches = lookup_service_brick('switches')for port in self.switches.ports.keys():if src_dpid == port.dpid and src_port_no == port.port_no:lldp_delay[(src_dpid, dpid)] = self.switches.ports[port].delayexcept:return
5.图存储与最短路径计算
对于图的存储及最短路径算法,可使用现有的库,如networkx。
参考文献
1.软件定义网络入门学习笔记5-实现最短路径转发的ryu控制器app. https://blog.csdn.net/xiajx98/article/details/92800026.
2.基于Ryu和OpenFlow协议的网络时延、带宽等状态测量.https://blog.csdn.net/smiling_sweety/article/details/116647972.
这篇关于Ryu中基于时延的最短路径转发算法(SPF)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!