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

一、实验目的
1、能够理解 POX 控制器的工作原理;
2、通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;
3、能够运用 POX控制器编写自定义网络应用程序,进一步熟悉POX控制器流表下发的方法。

二、实验环境
Ubuntu 20.04 Desktop amd64

三、实验要求
(一)基本要求
1、搭建下图所示SDN拓扑,协议使用Open Flow 1.0,控制器使用部署于本地的POX(默认监听6633端口)

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

2、阅读Hub模块代码,使用 tcpdump 验证Hub模块

  • 使用pox控制器

*在CLI界面下打开h1、h2和h3终端
xterm h1 h2 h3

  • h2和h3使用开启抓包(抓取eth0端口)
tcpdump -nn -i h2-eth0 #抓取h2-eth0端口的数据包
tcpdump -nn -i h3-eth0 #抓取h3-eth0端口的数据包

  • h1 ping h2

  • h1 ping h3

结论:无论h1 ping h2 还是 h1 ping h3 ,h2和h3均能抓到包,即验证了Hub模块的作用:将数据包广播转发。

3、阅读L2_learning模块代码,画出程序流程图,使用 tcpdump 验证Switch模块。

  • 先清除sudo mn --clean

  • 再重新创建topo
    sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow10

  • 运行L2_learning模块
    ./pox.py log.level --DEBUG forwarding.l2_learning

  • 在CLI界面下打开h1、h2和h3终端,h1,h2和h3使用开启抓包(抓取eth0端口)

在CLI界面下打开h1、h2和h3终端
xterm h1 h2 h3
h1,h2和h3使用开启抓包(抓取eth0端口)
tcpdump -nn -i h1-eth0 #抓取h1-eth0端口的数据包
tcpdump -nn -i h2-eth0 #抓取h2-eth0端口的数据包
tcpdump -nn -i h3-eth0 #抓取h3-eth0端口的数据包
  • h1 ping h2

  • h1 ping h3

结论:h1 ping h2 和h1 ping h3,只有相应主机可以抓到包,即验证了Switch模块的作用:让OpenFlow交换机实现L2自学习

  • 程序流程图

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


  • 重新构建topo,并测试连通性

  • 运行pox模块,并重新pingall

  • 查看流表项
    sudo ovs-ofctl -O OpenFlow10 dump-flows s1

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

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

  • 基于进阶1的代码,完成ODL实验的硬超时功能。(先运行SendFlowSingle3,先通再断再恢复)

  • 基于进阶1的代码,完成ODL实验的硬超时功能。(直接运行SendPoxHardTimeOut,先断后通,能验证流表项生效即可)

(三)个人感想
1、此次实验的基础要求为验证性实验,难度较小。进阶要求难度较大,pox使用指南是全英文的看得有点晕,理解起来很困难,边百度翻译边找关键词ofp-flow-mod,看了指南里面的一些example,加上我前前后后查了不少资料以及各大网站课程,算是写出个不像脚本的脚本,非常好,执行脚本一堆错,人都麻了,我真的不想写代码,因为对我来说真的太难啦!!!麻归麻,还是要继续排错,可能是对代码不敏感,导致我脚本排错花了很长时间!
2、问题及解决
1)
问题:执行POX模块出现的错误

解决:将SendFlowInSingle3.py文件放到pox目录下

2)
问题:执行POX模块出现的错误

解决:缩进问题,tab与空格不可同时混用,肉眼很难看得出来

3)
问题:执行POX模块出现的错误

解决:写法不一样,两者均可,但是总体写法不太一样

def launch ():
  core.registerNew(SendFlowInSingle3)

改为

def launch():
   core.openflow.addListenerByName("ConnectionUp", SendFlowInSingle3)

举例子看区别:

  • registerNew()方法注册一个类(组件),若该组件有_core_name属性,则注册到core的组件名称为_core_name的值,否则就是类名称本身
点击查看代码
#!/usr/bin/python

from pox.core import core


class MyComponent(object):
    _core_name = "square"

    def __init__(self, an_arg):
        self.arg = an_arg
        print "MyComponent instance registered with arg:", self.arg

    def message(self):
        print "MyComponent with arg:", self.arg


def launch():
    core.registerNew(MyComponent, "chosen")
    core.square.message() #注意此处是用组件名称‘square’调用组件的函数
  • 能触发事件的类的实例.addListenerByName("事件类名", 事件处理函数):事件类可以不在作用域内,仅传递一个类名字符串
    例子如SendFlowInSingle3.py

4)
问题:实验中开启pox时遇到如下情况

解决:重启虚拟机后解决

5)
问题:实现硬超时功能时出现如下情况

解决:先运行SendFlowSingle3后,要按Ctrl+alt+c退出,再次运行SendPoxHardTimeOut。(我原先直接CTRL+z,快捷键使用不当导致)

posted @ 2022-10-09 14:59  SinaLee  阅读(187)  评论(0)    收藏  举报