参考资料:
https://www.cnblogs.com/zzqsss/p/11924685.html
问答环节
描述官方教程实现了一个什么样的交换机功能?
Ryu是一个基于组件的软件定义的网络框架。Ryu为软件组件提供了定义良好的API,使开发人员可以轻松创建新的网络管理和控制应用程序。Ryu支持各种用于管理网络设备的协议,如OpenFlow、Netconf、OF config等。
控制器设定交换机支持什么版本的OpenFlow?
关于OpenFlow,Ryu完全支持1.0、1.2、1.3、1.4、1.5和Nicira扩展。
控制器设定了交换机如何处理数据包?
RYU环境配置
sudo apt-get install python3-pip
git clone https://github.com/osrg/ryu.git
cd ryu
sudo pip3 install -r tools/pip-requires -i https://pypi.tuna.tsinghua.edu.cn/simple
sudo python3 setup.py install
代码环节
根据官方教程和提供的示例代码,将SelfLearning.py补充完整:
SelfLearning.py
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_0
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
class SimpleSwitch(app_manager.RyuApp):
# TODO define OpenFlow 1.0 version for the switch
# add your code here
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
self.mac_to_port = {}
def add_flow(self, datapath, in_port, dst, src, actions):
ofproto = datapath.ofproto
match = datapath.ofproto_parser.OFPMatch(
in_port=in_port,
dl_dst=haddr_to_bin(dst), dl_src=haddr_to_bin(src))
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,
flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
# TODO send modified message out
# add your code here
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
pkt = packet.Packet(msg.data)
eth = pkt.get_protocol(ethernet.ethernet)
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
# ignore lldp packet
return
if eth.ethertype == ether_types.ETH_TYPE_IPV6:
# ignore ipv6 packet
return
dst = eth.dst
src = eth.src
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
self.logger.info("packet in DPID:%s MAC_SRC:%s MAC_DST:%s IN_PORT:%s", dpid, src, dst, msg.in_port)
# learn a mac address to avoid FLOOD next time.
self.mac_to_port[dpid][src] = msg.in_port
if dst in self.mac_to_port[dpid]:
out_port = self.mac_to_port[dpid][dst]
else:
out_port = ofproto.OFPP_FLOOD
# TODO define the action for output
# add your code here
actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
# install a flow to avoid packet_in next time
if out_port != ofproto.OFPP_FLOOD:
self.logger.info("add flow s:DPID:%s Match:[ MAC_SRC:%s MAC_DST:%s IN_PORT:%s ], Action:[OUT_PUT:%s] ", dpid, src, dst, msg.in_port, out_port)
self.add_flow(datapath, msg.in_port, dst, src, actions)
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
# TODO define the OpenFlow Packet Out
# add your code here
out = datapath.ofproto_parser.OFPPacketOut(
datapath=datapath,
buffer_id=msg.buffer_id,
in_port=msg.in_port,
actions=actions, data=data)
datapath.send_msg(out)
print ("PACKET_OUT...")
建立拓扑
使用图形编辑器进行拓扑构建:
导出PY文件并创建拓扑:
验证功能
查看下发流表:
打开wireshark:
sudo wireshark
选择ANY的接口
执行ryu-manager:
ryu-manager PY程序名.py
在mininet的程序中执行:
h1 ping h2
在wireshark中查看报文:
心得体会
经过本次实验感觉还是非常蒙圈的,因为我接触Python的内容还是比较少的
我实在看不太懂源码的意义,所以是参考了其他同学的代码才实现了这个结果
果然还是需要进一步加强学习代码的热情度呀