實驗6:開源控制器實踐——RYU
一、實驗目的
- 能夠獨立部署RYU控制器;
- 能夠理解RYU控制器實現軟件定義的集線器原理;
- 能夠理解RYU控制器實現軟件定義的交換機原理。
二、實驗環境
- 下載虛擬機軟件Oracle VisualBox或VMware;
- 在虛擬機中安裝Ubuntu 20.04 Desktop amd64,並完整安裝Mininet;
三、實驗要求
(一)基本要求
- 完成Ryu控制器的安裝。
- 使用ryu --version 命令查看安裝的ryu控制器版本
- 搭建下圖所示SDN拓撲,協議使用Open Flow 1.0,並連接Ryu控制器。
- 搭建如上圖所示拓撲,並開啟ryu控制器
- 通過Ryu的圖形界面查看網絡拓撲。
- 在火狐瀏覽器打開ryu圖形界面,查看拓撲結構
- 閱讀Ryu文檔的The First Application一節,運行並使用 tcpdump 驗證L2Switch,分析和POX的Hub模塊有何不同。
- L2Switch模塊代碼展示
- 將L2Switch模塊py文件保存到合適的文件目錄下
- 構建拓撲,運行L2Switch模塊,同時開啟h2和h3終端,使用tcpdump進行數據包監聽
- h1 ping h2(觀察到h2,h3都接收到數據包)
- h1 ping h3(觀察到h2,h3都接收到數據包)
- h1 ping h2(觀察到h2,h3都接收到數據包)
- 通過dpctl dump-flows命令檢查ryu的L2Switch模塊和pox的Hub模塊的區別
- 在連接ryu控制器,啟動L2Switch模塊后查看下發流表
- 在連接pox控制器,啟動Hub模塊后查看下發流表
- 在連接ryu控制器,啟動L2Switch模塊后查看下發流表
RYU的L2Switch模塊和POX的Hub模塊都采用洪泛轉發,但不同之處在於:
可以在pox的Hub模塊運行時查看流表,而無法在ryu的L2Switch模塊運行時查看到流表
(二)進階要求
- 閱讀Ryu關於simple_switch.py和simple_switch_1x.py的實現,以simple_switch_13.py為例,完成其代碼的注釋工作,並回答下列問題:
- 代碼注釋
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#導入需要使用的相應包
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types
class SimpleSwitch13(app_manager.RyuApp):
#指定OpenFlow 1.3版本
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleSwitch13, self).__init__(*args, **kwargs)
#self.mac_to_port是mac地址映射到轉發端口的字典。
self.mac_to_port = {}
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
# ev.msg 是用來存儲對應事件的 OpenFlow 消息類別實體
datapath = ev.msg.datapath
# ofproto表示使用的OpenFlow版本所對應的ryu.ofproto.ofproto_v1_3
ofproto = datapath.ofproto
# 使用對應版本的ryu.ofproto.ofproto_v1_3_parser來解析協議
parser = datapath.ofproto_parser
# install table-miss flow entry
#
# We specify NO BUFFER to max_len of the output action due to
# OVS bug. At this moment, if we specify a lesser number, e.g.,
# 128, OVS will send Packet-In with invalid buffer_id and
# truncated packet data. In that case, we cannot output packets
# correctly. The bug has been fixed in OVS v2.1.0.
match = parser.OFPMatch()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
# priority = 0表示優先級最低,即若所有流表都匹配不到時,才會把數據包發送到controller
self.add_flow(datapath, 0, match, actions)
# 執行 add_flow() 方法以發送 Flow Mod 消息
def add_flow(self, datapath, priority, match, actions, buffer_id=None):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
actions)]
if buffer_id:
mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
priority=priority, match=match,
instructions=inst)
else:
mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
match=match, instructions=inst)
datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
# 處理PacketIn事件
def _packet_in_handler(self, ev):
# If you hit this you might want to increase
# the "miss_send_length" of your switch
if ev.msg.msg_len < ev.msg.total_len:
self.logger.debug("packet truncated: only %s of %s bytes",
ev.msg.msg_len, ev.msg.total_len)
#從事件類里取出一些參數
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
#接受到了lldp包,就直接丟棄
# ignore lldp packet
return
dst = eth.dst
src = eth.src
dpid = format(datapath.id, "d").zfill(16)
self.mac_to_port.setdefault(dpid, {})
self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
#進行自學習,盡可能避免在下一次洪泛
# learn a mac address to avoid FLOOD next time.
#dpid是交換機的id,src是數據包的源mac地址,in_port是交換機接受到包的端口
self.mac_to_port[dpid][src] = in_port
#檢驗目的地址是否已經學習
if dst in self.mac_to_port[dpid]:
#如果已經學習到,則向交換機下發流表,並讓交換機向相應端口轉發包
out_port = self.mac_to_port[dpid][dst]
else:
#如果還沒有學習到,則無法下發流表,讓交換機洪泛轉發包。
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
# install a flow to avoid packet_in next time
if out_port != ofproto.OFPP_FLOOD:
match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src)
# verify if we have a valid buffer_id, if yes avoid to send both
# flow_mod & packet_out
if msg.buffer_id != ofproto.OFP_NO_BUFFER:
#buffer_id不為None,控制器只需下發流表的命令,交換機增加了流表項后,位於緩沖區的數據包會自動轉發出去。
self.add_flow(datapath, 1, match, actions, msg.buffer_id)
return
else:
#buffer_id為None,那么控制器不僅要更改交換機的流表項,
self.add_flow(datapath, 1, match, actions)
#還要把數據包的信息傳給交換機,讓交換機把數據包轉發出去。
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
in_port=in_port, actions=actions, data=data)
datapath.send_msg(out)
a) 代碼當中的mac_to_port的作用是什么?
答:mac_to_port是mac地址映射到轉發端口的字典,可用於交換機自學習。
b) simple_switch和simple_switch_13在dpid的輸出上有何不同?
答:差別在於:simple_switch直接輸出dpid,而simple_switch_13則在dpid前端填充0直至滿16位
#simple_switch.py
dpid = datapath.id
#################################
#simple_switch_13.py
dpid = format(datapath.id, "d").zfill(16)
c) 相比simple_switch,simple_switch_13增加的switch_feature_handler實現了什么功能?
答:實現了交換機以特性應答消息來響應特性請求的功能。
d) simple_switch_13是如何實現流規則下發的?
答:在接收到packetin事件后,首先獲取包學習,交換機信息,以太網信息,協議信息等。若以太網類型是LLDP類型,則不予處理。如果不是,則獲取源端口的目的端口和交換機id,先學習源地址對應的交換機的入端口,再查看是否已經學習目的mac地址,如果沒有則進行洪泛轉發。如果學習過該mac地址,則查看是否有buffer_id,如果有的話,則在添加流表信息時加上buffer_id,向交換機發送流表。
e) switch_features_handler和_packet_in_handler兩個事件在發送流規則的優先級上有何不同?
答:switch_features_handler下發流表的優先級比_packet_in_handler的優先級高。
實驗心得
- 在本次實驗中,通過閱讀RYU文檔並查看相關模塊的源代碼,了解了RYU控制器的工作原理,並比較了RYU的L2Switch模塊與POX的Hub模塊的異同。本次的實驗基礎部分難度較低,最開始安裝RYU時,基本上根據老師的實驗指導書一步一步來,即可順利完成,同時實驗操作與前兩次操作ODL和POX控制器差不多,因此能較為快速地完成對應步驟,而進階部分則難度較大,尤其在閱讀源碼部分進度較慢。不過,雖然閱讀源碼的過程有些痛苦,但在過程中,查閱相關材料,結合源碼進行閱讀,也使得我對RYU的控制機制有了更為形象和深入的認識。
- 在實驗過程中遇到的問題及解決
- 在一開始安裝RYU控制器時,碰到了如下的報錯。上網查找原因並詢問身邊同學后,發現可能是網絡連接有問題,沒法一下子將東西完全下載下來,從而導致超時報錯。之后重新嘗試鍵入命令,在網絡連接暢通的情況下順利完成下載。
- 在完成進階要求的源碼注釋過程中,明顯感覺到困難。原因有幾點:首先,一開始對RYU的工作原理並沒有十分了解;同時,源碼中使用了許多RYU中定義的數據結構,閱讀時根本沒有一點的基礎概念。基於此,我使用搜索引擎,查找了RYU使用的相應數據結構,並了解了其中各個參數的基本含義,再重新閱讀源碼,同時,在網上查找RYU工作原理的說明材料,才最終成功完成源碼的注釋。
- 在一開始安裝RYU控制器時,碰到了如下的報錯。上網查找原因並詢問身邊同學后,發現可能是網絡連接有問題,沒法一下子將東西完全下載下來,從而導致超時報錯。之后重新嘗試鍵入命令,在網絡連接暢通的情況下順利完成下載。