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

一、基本要求

h1 ping h2、h2和h3的tcpdump抓包结果

使用 tcpdump 验证Hub模块

使用 tcpdump 验证Switch模块

L2_learning模块代码流程图

(二)进阶要求

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

生成拓扑

sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow10

SendFlowInSingle3代码

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):
        
        #设置数据包从端口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 = of.OFPP_ALL))#增加转发向所有端口的动作
        event.connection.send(msg)#通过send函数向交换机发送所设定的消息

        #设置数据包从端口2进,从端口1和端口3出
        msg = of.ofp_flow_mod()
        msg.priority = 1
        msg.match.in_port = 2
        msg.actions.append(of.ofp_action_output(port = of.OFPP_ALL))
        event.connection.send(msg)

        #设置数据包从端口3进,从端口1和端口2出
        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(SendFlowInSingle3)

2. 基于进阶1的代码,完成ODL实验的硬超时功能

SendPoxHardTimeOut代码

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
        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)

运行SendPoxHardTimeOut

./pox.py log.level --DEBUG forwarding.SendPoxHardTimeOut

3.总结

1.在pox目录下创建 SendFlowInSingle3.py 和 SendPoxHardTimeOut.py 文件时需要加 sudo ,否则无法创建成功.
2.

msg = of.ofp_flow_mod()  
        msg.priority = 3
        msg.match.in_port = 3 
        msg.hard_timeout = 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)

对于同一个端口的操作,通过msg.priority判断先后顺序。

posted @ 2022-10-20 10:59  nonowarn  阅读(11)  评论(0)    收藏  举报