实验5:开源控制器实践--POX
(一)基本要求
1.hub下h1 ping h2
2.l2_learning下h1 ping h2
3.l2_learrning流程图
(二)进阶
创建Pox下发流表模块,代码:
from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.lib.util import dpidToStr
log = core.getLogger()
def _handle_ConnectionUp(event):
msg = of.ofp_flow_mod()
msg.actions.append(of.ofp_action_output(port=of.OFPP_FLOOD))
event.connection.send(msg)
log.info("Hubifying %s", dpidToStr(event.dpid))
def _handle_PacketIn(event):
msg = of.ofp_packet_out()
msg.data = event.ofp
msg.actions.append(of.ofp_action_output(port=of.OFPP_FLOOD))
event.connection.send(msg)
event.connection.send(of.ofp_flow_mod(action=of.ofp_action_output(port=1),
priority=42,
match=of.ofp_match(dl_type=0x800, nw_dst="10.0.0.1", tp_dst=80)))
event.connection.send(of.ofp_flow_mod(action=of.ofp_action_output(port=2),
priority=42,
match=of.ofp_match(dl_type=0x800, nw_dst="10.0.0.2", tp_dst=80)))
event.connection.send(of.ofp_flow_mod(action=of.ofp_action_output(port=3),
priority=42,
match=of.ofp_match(dl_type=0x800, nw_dst="10.0.0.3", tp_dst=80)))
def launch(reactive=False):
if reactive:
core.openflow.addListenerByName("PacketIn", _handle_PacketIn)
log.info("Reactive hub running.")
else:
core.openflow.addListenerByName("ConnectionUp", _handle_ConnectionUp)
log.info("Proactive hub running.")
运行结果
(三)总结
·在基本要求这边,刚开始以为顺畅的做完,结果hub和l2_learning做出来的抓包结果没有差别,才发现原来启动pox的命令错了,张炜龙同学的博文里有特别提到这个点。还有如果做了多次的话,要注意修改端口号,或者关闭相应的进程,否则会显示端口号被占用。
·进阶的话,本来是想写一整个代码,结果运行了跑不出来。看了张炜龙同学的博文我又有了思路,我往hub.py的_handle_PacketIn中添加了下发流表的代码就大功告成了。
·以上都不是最让人感到无奈的!l2_learning代码流程的话在英文注释里有说明,无奈的是第一次试着用markdown做流程图,结果因为空格语法错误找了好久,在typora做出来的图代码贴过来居然没反应。大晚上的,等待救赎中。。。(解决了,把typora上的图放大截图贴上来)