实验6:开源控制器实践——Ryu
一、完成Ryu控制器的安装。
二、 搭建下图所示SDN拓扑,协议使用Open Flow 1.0,并连接Ryu控制器。
-
搭建拓扑
sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow10 -
连接Ryu控制器
ryu-manager ryu/ryu/app/gui_topology/gui_topology.py --observe-links
三、通过Ryu的图形界面查看网络拓扑。
四、阅读Ryu文档的The First Application一节,运行并使用 tcpdump 验证L2Switch,分析和POX的 Hub模块有何不同。
-
h1 ping h2
-
h1 ping h3
-
与POX的Hub模块区别
L2Switch的实现是于Hub类似集线器的广播形式发送ICMP报文,h1 ping h2/h3时,h1发送给h2/h3的ICMP报文,h3/h2也会收到。
进阶要求
一、阅读Ryu关于simple_switch.py和simple_switch_1x.py的实现,以simple_switch_13.py为例, 完成其代码的注释工作,并回答下列问题:
- simple_switch_13.py代码及注释:
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#引入所需使用的包
from ryu.base import app_manager
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.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types
class SimpleSwitch13(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] #标明OpenFlow版本,这里为1.3版本。
def __init__(self, *args, **kwargs):
super(SimpleSwitch13, self).__init__(*args, **kwargs)
self.mac_to_port = {} #self.mac_to_port保存mac地址到转发端口。
#对EventOFPSwitchFeatures事件的处理
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
datapath = ev.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
# install table-miss flow entry
# We specify NO BUFFER to max_len of the output action due to
# OVS bug. At this moment, if we specify a lesser number, e.g.,
# 128, OVS will send Packet-In with invalid buffer_id and
# truncated packet data. In that case, we cannot output packets
# correctly. The bug has been fixed in OVS v2.1.0.
match = parser.OFPMatch() #match指流表项匹配
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions) #add_flow是添加流表项的函数,本函数的目的即为下发流表。
#add_flow()为增加流表项函数;
#datapath,priority,match,actions,buffer_id为参数;
#此流表项匹配成功后应立即执行所规定的动作。
def add_flow(self, datapath, priority, match, actions, buffer_id=None):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
actions)]
if buffer_id:
mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
priority=priority, match=match,
instructions=inst)
else:
mod = 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):
# If you hit this you might want to increase
# the "miss_send_length" of your switch
if ev.msg.msg_len < ev.msg.total_len: #传输出错
self.logger.debug("packet truncated: only %s of %s bytes",
ev.msg.msg_len, ev.msg.total_len)
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_protocols(ethernet.ethernet)[0]
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
# ignore lldp packet
return
dst = eth.dst
src = eth.src #用字典分析数据包
dpid = format(datapath.id, "d").zfill(16)
self.mac_to_port.setdefault(dpid, {})
self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
# learn a mac address to avoid FLOOD next time.
self.mac_to_port[dpid][src] = in_port #交换机自学习
if dst in self.mac_to_port[dpid]: #如表中有出端口信息,指示出端口
out_port = self.mac_to_port[dpid][dst]
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_dst=dst, eth_src=src)
# verify if we have a valid buffer_id, if yes avoid to send both
# flow_mod & packet_out
if msg.buffer_id != ofproto.OFP_NO_BUFFER:
self.add_flow(datapath, 1, match, actions, msg.buffer_id)
return
else:
self.add_flow(datapath, 1, match, actions)
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
#发送Packet_out数据包 带上交换机发来的数据包的信息
out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
in_port=in_port, actions=actions, data=data)
#发流表
datapath.send_msg(out)
a) 代码当中的mac_to_port的作用是什么?
答:保存mac地址到交换机端口的映射,为交换机自学习功能提供数据结构进行mac端口的存储。
b) simple_switch和simple_switch_13在dpid的输出上有何不同?
答:simple_switch_13会在前端加上0填充至16位,simple_switch则直接输出dpid。
c) 相比simple_switch,simple_switch_13增加的switch_feature_handler实现了什么功能?
答:交换机以特性应答消息响应特性请求。
d) simple_switch_13是如何实现流规则下发的?
答:通过构造流表项,判断其中参数和信息来实现流规则下发。
e) switch_features_handler和_packet_in_handler两个事件在发送流规则的优先级上有何不同?
答:前者发送的流规则优先级更高(0)。
实验总结
个人总结与想法
实验难度适中,最开始安装Ryu时很快安装完成,但不懂怎么检测浪费时间反复装了几遍。除进阶外的其他任务和第五次相比,操作不能说一摸一样只能说完全相同。进阶这块的代码注释确实不愧进阶之名,耗时颇多。阅读代码时的问题和困难都有所借鉴才完成,个个都是大佬,又能说会道,互联网实在是太精彩啦。
困难与解决方法
1.检测Ryu安装就直接查看版本命令就行了(ryu --version)
2.开始建立简拓扑无法ping通,后来发现链接ryu之后就可以了
3.进阶任务的代码注释个别不理解,发现有时候个别单词英译中一下就可能会有一点思路