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

1.使用命令创建拓扑:
sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow10

a)使用 tcpdump 验证Hub模块,h1 ping h2、h2和h3的tcpdump抓包结果截图

使用 tcpdump 验证Switch模块,h1 ping h2、h2和h3的tcpdump抓包结果截图

L2_learning模块代码流程图

进阶要求
a)相关代码

编写Python程序自定义一个POX模块SendFlowInSingle3,并且将拓扑连接至SendFlowInSingle3(默认端口6633),实现向s1发送流表规则使得所有主机两两互通。

点击查看代码
from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.openflow.of_json import *
from pox.lib.addresses import IPAddr

log = core.getLogger()

class SendFlowInSingle3(object):
    def __init__(self):
    	core.openflow.addListeners(self)

    def _handle_ConnectionUp(self, event):
#设置数据包从端口1进,从端口2和端口3出
        msg = of.ofp_flow_mod() # 使用ofp_flow_mod()的方法向交换机来下发流表
        msg.priority = 1 #设置msg的优先级
        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) #通过send函数向交换机发送所设定的消息
        log.debug("Connection %s" % (event.connection,))
#设置数据包从端口2进,从端口1和端口3出
        msg = of.ofp_flow_mod() # 使用ofp_flow_mod()的方法向交换机下发流表
        msg.priority = 1 #设置msg的优先级
        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)#通过send函数向交换机发送所设定的消息
        log.debug("Connection %s" % (event.connection,))
#设置数据包从端口3进,从端口1和端口2出
        msg = of.ofp_flow_mod()# 使用ofp_flow_mod()的方法向交换机下发流表
        msg.priority = 1 #设置msg的优先级
        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)#通过send函数向交换机发送所设定的消息
        log.debug("Connection %s" % (event.connection,))


def launch():
    core.registerNew(SendFlowInSingle3) #注册SendFlowInSingle3的组件

重新搭建(一)的拓扑,此时交换机内无流表规则,拓扑内主机互不相通
![](https://img2022.cnblogs.com/blog/2965917/202210/2965917-20221012224243134-1889863762.png

编写Python程序自定义一个POX模块SendFlowInSingle3,并且将拓扑连接至SendFlowInSingle3(默认端口6633)
运行文件下发流表

,实现向s1发送流表规则使得所有主机两两互通。
s

ovs-ofctl交换机流表项截图

基于进阶1的代码,完成ODL实验的硬超时功能。
先连接

再中断

点击查看代码
from pox.core import core
import pox.openflow.libopenflow_01 as of

class SendPoxHardTimeOut(object):
    def __init__(self):
        core.openflow.addListeners(self)
    def _handle_ConnectionUp(self, event):


        msg = of.ofp_flow_mod()
        msg.priority = 3
        msg.match.in_port = 1
        msg.hard_timeout = 10  #硬超时10秒
        event.connection.send(msg)

        msg = of.ofp_flow_mod()
        msg.priority = 1
        msg.match.in_port = 1
        msg.actions.append(of.ofp_action_output(port = of.OFPP_ALL))
        event.connection.send(msg)


        msg = of.ofp_flow_mod()
msg.priority = 3
        msg.match.in_port = 3
        msg.hard_timeout = 10  #硬超时10秒
        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 = of.OFPP_ALL))
        event.connection.send(msg)

def launch():
    core.registerNew(SendPoxHardTimeOut)




个人总结:
本次实验难度适中,主要通过老师提供的pdf来学习POX控制器的使用方法,并且能运用pox控制器编写自定义网络应用程序,进一步熟悉了pox控制器是如何进行流表的下发。
建立好新的拓扑以后,对比hub模块和Switch模块,观察h1 ping h2 h3的不同之处,可以更好的了解下发流表的过程,也巩固了OpenFlow的原理,进阶实验主要是通过建立好拓扑,将其流表删除后,通过python代码来使其链路重新通,需要注意的点是在hub模块中,在h1 ping h2 h3 前需要先tcpdump -nn -i h2-etho tcpdump -nn -i h3-etho 然后再在h1中使用h1 ping h2 此时链路则会重新联通,只需在mininet 中重新pingall即可

posted @ 2022-10-12 23:11  212002125王昊  阅读(41)  评论(0编辑  收藏  举报