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

一、基本要求

(1)验证Hub模块

1.h1 ping h2的tcpdump抓包结果截图

2.h1 ping h3的tcpdump抓包结果截图

(2)验证Switch模块

1.h1 ping h2的tcpdump抓包结果截图

2.h1 ping h3的tcpdump抓包结果截图

(3)L2_learning模块代码流程图

二、进阶要求

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

代码

点击查看代码
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.基于进阶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()  # 使用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(SendPoxHardTimeOut)

三、心得体会

本次实验基础部分较简单,就是阅读L2_learning模块代码并画出程序流程图这一步阅读较吃力,进阶部分较难,特别是代码部分的理解和编写。
实验中遇到一下问题:
1.验证完hub模块后要验证switc模块时报错端口被占用,一开始直接重启虚拟机解决,之后发现这个问题出现的比较多,每次都重启太麻烦了,经过搜索后使用lsof -i [端口号] 查找端口对应进程号,再使用kill -s 9 [进程号] 强行杀死进程。
2.将拓扑连接至SendFlowInSingle3后还是ping不通,重启解决了。
通过此次实验,我了解了POX 控制器的工作原理,通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法。

posted @ 2022-10-12 13:04  gt123  阅读(27)  评论(0编辑  收藏  举报