本文實驗環境:
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爲例