本文实验环境:
1.VirtualBox
2.SDN Hub tutorial VM 64bit
学习目标:
1.熟悉SDN Hub tutorial
2.学习POX提供的样例代码:tutorial_l2.hub.py
SDN Hub tutorial就不多介绍,官方的介绍如下:
All-in-one SDN App Development Starter VM
翻译成中文就是一款集所有SDN相关APP的VM 镜像,方便我这样的初学者进行学习.
SDN Hub tutorial安装很简单,到官网下载最新的版本,导入到VirtualBox或者VMware这样的虚拟机软件.
一、下面是本次实验的步骤了:
1.安装SDN Hub tutorial
2.在命令行通过mininet创建一个网络,该网络具备三台主机,一台交换机和一个控制器。
具体命令如下:
$ sudo mn --topo single,3 --mac --controller remote --switch ovsk
sudo ovs-vsctl set bridge s1 protocols=OpenFlow10
3.启动POX控制器,并使用tutorial_l2.hub.py这个模块,这个模块是用Python实现的集线器
cd /home/ubuntu/pox && ./pox.py log.level --DEBUG forwarding.tutorial_l2_hub
4.最后在mininet命令端输入h1 ping h2,可以看到h1能够ping通h2.
二.结果理解
1.tutorial_l2.hub.py程序
from pox.core import core
import pox.openflow.libopenflow_01 as of
class L2Hub (object):
def __init__ (self, connection):
# Keep track of the connection to the switch so that we can
# send it messages!
self.connection = connection
# This binds all our event listener
connection.addListeners(self)
# Handles packet in messages from the switch.
# 处理PackIn消息
def _handle_PacketIn (self, event):
packet = event.parsed # This is the parsed packet data.
packet_in = event.ofp # The actual ofp_packet_in message.
#生成packet_out消息
msg = of.ofp_packet_out()
msg.buffer_id = event.ofp.buffer_id
msg.in_port = packet_in.in_port
# Add an action to send to the specified port
#讲所有来到交换机的数据包都从其它端口转发出去
action = of.ofp_action_output(port = of.OFPP_FLOOD)
msg.actions.append(action)
# Send message to switch
self.connection.send(msg)
def launch ():
def start_switch (event):
#实例化一个Hub
L2Hub(event.connection)
#添加监听
core.openflow.addListenerByName("ConnectionUp", start_switch)
2.数据包交互时序,以h1 ping h2为例