实验5:开源控制器实践——POX

一、基本要求:

阅读Hub模块代码,使用 tcpdump 验证Hub模块

h1 ping h2、h2和h3的tcpdump抓包结果截图如下

L2_Learning代码流程图如下

使用 tcpdump 验证Switch模块

h1 ping h2抓包结果:

h1 ping h3抓包结果

二、进阶要求

重新搭建(一)的拓扑,此时交换机内无流表规则,拓扑内主机互不相通;编写Python程序自定义一个POX模块SendFlowInSingle3,并且将拓扑连接至SendFlowInSingle3(默认端口6633),实现向s1发送流表规则使得所有主机两两互通。
SendFlowInSingle3.py代码如下

import pox.openflow.libopenflow_01 as of

class SendFlowInSingle3(object):
    def __init__(self):
        core.openflow.addListeners(self)
    def _handle_ConnectionUp(self, event):
        msg = of.ofp_flow_mod()  # 使用ofp_flow_mod()方法向交换机下发流表
        msg.priority = 1
        msg.match.in_port = 1  # 使数据包进入端口1
        msg.actions.append(of.ofp_action_output(port=2))  # 从端口2转发出去
        msg.actions.append(of.ofp_action_output(port=3))  # 从端口3转发出去
        event.connection.send(msg)

        msg = of.ofp_flow_mod()  # 使用ofp_flow_mod()方法向交换机下发流表
        msg.priority = 1
        msg.match.in_port = 2  # 使数据包进入端口2
        msg.actions.append(of.ofp_action_output(port=1))  # 从端口1转发出去
        msg.actions.append(of.ofp_action_output(port=3))  # 从端口3转发出去
        event.connection.send(msg)

        msg = of.ofp_flow_mod()  # 使用ofp_flow_mod()方法向交换机下发流表
        msg.priority = 1
        msg.match.in_port = 3  # 使数据包进入端口3
        msg.actions.append(of.ofp_action_output(port=1))  # 从端口1转发出去
        msg.actions.append(of.ofp_action_output(port=2))  # 从端口2转发出去
        event.connection.send(msg)
def launch():
    core.registerNew(SendFlowInSingle3)

运行SendFlowInSingle3后能够ping通

流表项截图

基于进阶1的代码,完成ODL实验的硬超时功能。
SendPoxHardTimeOut.py代码如下

from pox.core import core
import pox.openflow.libopenflow_01 as of
 
class SendFlowInSingle3(object):
    def __init__(self):
        core.openflow.addListeners(self)
    def _handle_ConnectionUp(self, event):
        msg = of.ofp_flow_mod()  # 使用ofp_flow_mod()方法向交换机下发流表
        msg.priority = 1
        msg.match.in_port = 1  
        msg.actions.append(of.ofp_action_output(port=2))  
       # msg.actions.append(of.ofp_action_output(port=3)) 
        event.connection.send(msg)
 
        msg = of.ofp_flow_mod() 
        msg.priority = 1
        msg.match.in_port = 2  
        msg.actions.append(of.ofp_action_output(port=1))  
        msg.actions.append(of.ofp_action_output(port=3))  
        event.connection.send(msg)
 
        msg = of.ofp_flow_mod()  
        msg.priority = 1
        msg.match.in_port = 3  
       # msg.actions.append(of.ofp_action_output(port=1))  
        msg.actions.append(of.ofp_action_output(port=2))  
        event.connection.send(msg)
 
def launch():
    core.registerNew(SendFlowInSingle3)

实现截图如下

三、个人总结

本次实验难度较前两次实验有所提升。在基本要求中,我在了解了tcpdump的使用方法后顺利地进行了实验,对Hub和Switch模块的验证过程是很顺利的。但在进阶要求的第二项中,我发现清除流表后重新生成拓扑怎么也ping不通,通过搜索引擎查询以后得知此现象属正常。在运行SendPoxHardTimeOut代码后,h1 ping h3数据包的收发中断,若不及时重新运行SendFlowInSingle3,则会因为超时太久报错。
此外,在绘制switch模块的流程图时,我原先是用iPad手绘的,发觉着实有些不好看,就现学了流程图绘画软件BoardMix,画出来果然好看了很多,又掌握了一项小技能。
posted @ 2022-10-11 17:33  Chandicx  阅读(23)  评论(0)    收藏  举报