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

一、基础要求

1. 验证hub

h1 ping h2 与h1 ping h3

2. 验证switch

h1 ping h2 与h1 ping h3

3. L2_learning模板代码流程图

二、进阶要求

1. SendFlowInSingle3

1)代码

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

2)运行结果

2. SendPoxHardTime

1)代码

from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.openflow.of_json import *

def _handle_ConnectionUp(event):
    msg = of.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.openflow.addListenerByName("ConnectionUp", _handle_ConnectionUp)

2)运行结果

三、心得体会

在做基础部分还是比较得心应手,难度卡在了进阶部分2。我一开始以为只能同时运行,但没办法同时运行。看了下其他人博客,是先运行SendFlowInSingle3,同时执行ping操作,之后执行SendPoxHardTimeOut,停止运SendPoxHardTimeOut,再次运行运行SendFlowInSingle3。按照这种操着,很快得出了想要的结果。

posted @ 2022-10-12 11:52  Pantalone  阅读(34)  评论(0)    收藏  举报