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

1.基础要求:
h1 ping h2

h1 ping h3

L2_learning模块代码流程图:

2.进阶要求(1):

SendFlowInSingle3.py

from pox.core import core
import pox.openflow.libopenflow_01 as of # POX convention
from pox.openflow.of_json import *

def SendFlowInSingle3(event):
msg = of.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) #send flowmod to the switch.

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.openflow.addListenerByName("ConnectionUp", SendFlowInSingle3)

进阶要求(2):

SendPoxHardTimeOut.py

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)

3.个人总结:

这次实验的基础难度较小,而进阶要求难度较大。
在基础实验中明白了switch模块的功能是让Openflow交换机实现自学习,可以看出交换机对数据包进行了学习,实现了从相应的端口发出,只有目的主机可以抓取到报文。而hub模块是在每个交换机上将数据包以广播形式转发。

由于缩进问题,在运行的时候出现,缩进解决后就可以运行了

posted @ 2022-10-20 10:48  听弦  阅读(24)  评论(0编辑  收藏  举报