102101616许之烨
实验5︰开源控制器实践——POX
⼀、阅读Hub模块代码,使⽤tcpdump 验证Hub模块
h1 ping h2的tcpdump的抓包截图(Hub模块)
![在这里插入图片描述](https://img-blog.csdnimg.cn/1e1b2fb54ac3495aafe608a38241108e.png)
h1 ping h3
![在这里插入图片描述](https://img-blog.csdnimg.cn/2fe73ddb5cd24e90a79f3638b0cbe302.png)
根据上⽅截图可以发现,不管h1 ping h2还是h3,h2和h3都能接收到数据包,符合Hub模块的作⽤:在每个交换机上安装泛洪通配符规则,将数据包⼴播转发,此时交换机等效于集线器。
⼆、阅读L2_learning模块代码,使⽤tcpdump 验证Switch模块。
h1 ping h2
![在这里插入图片描述](https://img-blog.csdnimg.cn/cabafa24ce2e46bdabe09fb4b1bed96c.png)
h1 ping h3
根据上方截图可以发现,当h1 ping相应主机时,只有相应主机可以接收到数据包,原因是Switch模块的作用:让OpenFlow交换机实现L2自学习
实验6:开源控制器实践——RYU
⼀、搭建下图所⽰SDN拓扑,协议使⽤Open Flow 1.0,并连接Ryu控制器,通过Ryu的图形界⾯查看⽹络拓扑。
![在这里插入图片描述](https://img-blog.csdnimg.cn/ab0212af38704fa6a7849a25dae19e3b.png)
⼆、阅读Ryu⽂档的The First Application⼀节,运⾏当中的L2Switch,h1ping h2或h3,在⽬标主机使⽤ tcpdump 验证L2Switch,分析L2Switch和POX的Hub模块有何不同。
L2Switch.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
class L2Switch(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(L2Switch, self).__init__(*args, **kwargs)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
msg = ev.msg
dp = msg.datapath
ofp = dp.ofproto
ofp_parser = dp.ofproto_parser
actions = [ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)]
data = None
if msg.buffer_id == ofp.OFP_NO_BUFFER:
data = msg.data
out = ofp_parser.OFPPacketOut(
datapath=dp, buffer_id=msg.buffer_id, in_port=msg.in_port,
actions=actions, data = data)
dp.send_msg(out)
重新构建拓扑,并对h2、h3节点进抓包
h1 ping h2
![在这里插入图片描述](https://img-blog.csdnimg.cn/a5d1b5e586d04defa2a0a38ec30e94aa.png)
h1 ping h3
![在这里插入图片描述](https://img-blog.csdnimg.cn/41147dbdb09b433ea11e2c58087daf12.png)
结论:RYU的L2Switch模块和POX的Hub模块都采洪泛转发,但不同之处在于可以在pox的Hub模块运时查看流表,在ryu的L2Switch模块运时查看到流表。
三、编程修改L2Switch.py,另存为L2xxxxxxxxx.py,使之和POX的Hub模块的变得⼀致
102101616.py
from ryu.base import app_manager
from ryu.ofproto import ofproto_v1_3
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
class hub(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(hub, self).__init__(*args, **kwargs)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_feathers_handler(self, ev):
datapath = ev.msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
# install flow table-miss flow entry
match = ofp_parser.OFPMatch()
actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
# 1\OUTPUT PORT, 2\BUFF IN SWITCH?
self.add_flow(datapath, 0, match, actions)
def add_flow(self, datapath, priority, match, actions):
# 1\ datapath for the switch, 2\priority for flow entry, 3\match field,
4\action for packet
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
# install flow
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):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
ofp_parser = datapath.ofproto_parser
in_port = msg.match['in_port'] # get in port of the packet
# add a flow entry for the packet
match = ofp_parser.OFPMatch()
actions = [ofp_parser.OFPActionOutput(ofproto.OFPP_FLOOD)]
self.add_flow(datapath, 1, match, actions)
# to output the current packet. for install rules only output later
packets
out = ofp_parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
in_port=in_port, actions=actions)
# buffer id: locate the buffered packet
datapath.send_msg(out)
结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/c17bc428408e4832854322d7262236a3.png)
四、编程实现和OpenDaylight实验的样的硬超时功能
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]
def __init__(self, *args, **kwargs):
super(SimpleSwitch13, self).__init__(*args, **kwargs)
self.mac_to_port = {}
@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()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions)
def add_flow(self, datapath, priority, match, actions, buffer_id=None,
hard_timeout=0):
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, hard_timeout=hard_timeout)
else:
mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
match=match, instructions=inst,
hard_timeout=hard_timeout)
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 |