42
Mobile Intelligent Network Lab Depart. Electronic Engineering Lab210 LAB 2: RYU Controller with Mininet 10. 30, 2020

LAB 2: RYU Controller with Mininet

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

LAB 2: RYU Controller with Mininet

10. 30, 2020

Page 2: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Outline

• 實驗目標與步驟- 瞭解 controller 與 switch 之間的關係,並且能夠控制封包的傳遞的方向。

• 實驗背景知識介紹- Ryu- REST API

• 實驗步驟1. 安裝 Ryu2. 連接 Ryu 及 Mininet3. 使用Postman利用 REST API 對controller下Flow

• 查核點一: 基於 REST API 設置 flow rule

Page 3: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

3

實驗背景知識介紹 1: Ryu

Page 4: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Ryu

• Ryu是來自日本NTT COMWARE 公司所開發的控制器,提供給SDN一個開發框架,並且支援許多API及協定。- 支援OpenFlow許多版本

- 使用python

Page 5: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

5

實驗背景知識介紹 2: REST API

Page 6: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

REST 設計風格

• Representational State Transfer (REST) 是 Roy Thomas Fielding 博士於 2000 年在博士論文中提出的一種全球資訊網軟體架構風格- 目的是便於不同軟體 / 程序在網絡(例如:網際網路)中互相傳遞信息。

• 在 REST 中的資源 (Resource) 代表整個網路上的資源。- 網路上提供了各式各樣的資源,而網路上的資源由 Uniform Resource Identifier

(URI,統一資源標識符) 來提供。

• Client 透過 URI 來獲取資源的具體象徵 (Representational),並且使應用程式轉變其狀態 (以 Browser 而言,取得 HTML、CSS、JavaScript … 來產生畫面)- 隨著不斷取得資源的具體象徵,Client 端不斷地改變其狀態,這樣不斷的反覆

(iterations) 過程就是所謂的 Representational State Transfer。

6

Page 7: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

REST API

• 符合REST設計風格的Web API稱為RESTful API。它對資源(Resource)有以下三個方面的定義:

• HTTP請求方法(Request Method)在RESTful API中的典型應用:

7

1. 直觀簡短的資源地址:URI,比如:http://example.com/resources/。

2. 傳輸的資源:Web服務接受與返回的網際網路媒體類型,比如:JSON,XML,YAML等。

3. 對資源的操作:Web服務在該資源上所支持的一系列請求方法(比如:POST,GET,PUT或DELETE等)。

Page 8: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

8

實驗步驟 1: 安裝 Ryu

Page 9: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

安裝Ryu

在安裝 Ryu 之前,有些套件需要先準備好:• 下載前在更新一次系統- sudo apt-get update- sudo apt-get upgrade

• 下載所需要的套件- sudo apt-get install python-pip- sudo easy_install –U pip

• Git clone取得Ryu的原始碼- sudo git clone git://github.com/faucetsdn/ryu.git

• 安裝ryu- cd ryu- pip install .

Page 10: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

安裝成功畫面

Page 11: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

安裝成功畫面

• 執行Ryu- ryu-manager

沒有跳出錯誤代表安裝成功

• 修正錯誤- 如果執行ryu-manager後會跳出

DistributionNotFound:”XX”的錯誤訊息

- 利用pip install XX的指令來安裝遺漏的套件

Page 12: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

12

實驗步驟二:連接 Ryu 及Mininet

Page 13: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Step1. 開啟 Ryu controller

• 首先先開一個終端機,並開啟Ryu controller- ryu-manager --verbose --observe-links

Page 14: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Mininet指令參數介紹

• sudo mn --topo=linear,3 --mac --switch ovs, protocols=<OF_VER> --controller=remote,

ip=<controller_ip>,port=<port> “--topo=linear,3”:製作一個範例拓樸,使用線型的方式,製作三個節點 “--mac”:mac 使用內建的方式編排 “--switch ovs”:使用 OpenVSwitch

“protocols =<OF_VER> ”:選擇 Openflow 的版本,預設為1.0

--controller=remote”:使用外部的 controller 來控制 Mininet

• sudo mn --topo=linear,3 --switch ovs, protocols=OpenFlow13 --controller=remote,ip=127.0.0.1

註:相對於 LAB 1,此處增加了 OpenVSwitch 的設置,包含 OpenFlow 版本設置與controller 控制方式

註:使用OpenFlow13, 可指定 controller ip=127.0.0.1

Page 15: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Step2. 開啟 Mininet

• 再開另一個新的終端機 (Terminal) 並執行 Mininet- sudo mn --topo=linear,3 --mac --controller=remote

連接成功

Page 16: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Step3. 測試 Mininet

• 使用 pingall 或是h1 ping h2的指令來檢查自己設定的拓樸

- 這時候會發現host之間無法重送封包,是因為目前Switch內的Flow Table是空的,沒有任何規則,所以我們需要藉由Ryu controller中的python檔或掛載APP才能使host之間正常傳送封包

Page 17: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Step4. 讓host之間可以傳送封包

• 重新開啟Ryu controller,並且利用內建的simple_switch_13.py- ryu-manager ryu/ryu/app/simple_switch_13.py

或是

- ryu-manager ryu.app.simple_switch_13

• Ryu controller重新啟動後,Mininet也需要重新啟動

Page 18: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Step4. 讓host之間可以傳送封包

• 再測試一次host之間的封包傳送- h1 ping h2

- simple_switch_13.py 能夠實驗簡單交換器的功能可以分派網路內封包的流向

可以處理不明封包

- 所以host之間可以正常的傳送封包

Page 19: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Step5. Ryu的圖形化介面

• 重新啟動ryu controller,利用gui_topology.py提供圖形化介面- ryu-manager --observe-links ryu/ryu/app/simple_switch_13.py

ryu/ryu/app/gui_topology/gui_topology.py

• 重新啟動Mininet,開啟瀏覽器網址輸入:- 127.0.0.1:8080

可以看到switches連結的方式

Page 20: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Step5. Ryu的圖形化介面

• 在Mininet中讓 h1 ping h2,可以在Ryu controller圖形化介面中點選switch1跟switch2看到資訊

dl_dst:目的地的MAC

dl_src:來源的MAC

Page 21: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

21

實驗步驟三:使用Postman利用REST API 對controller下Flow

Page 22: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

下載Postman

• 下載Postman

Linux 64-bit

Page 23: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

開啟Postman

• 開啟解壓縮之後的資料夾,啟動Postman

Page 24: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

Postman

• 我們利用Postman來對Ryu controller下REST API

Page 25: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

常用REST API介紹

• REST API- Ryu controller有提供可以利用REST API對它做下指令的動作,可以用來新增Flow、查詢Switch…等功能。

• Ryu controller常用的REST API:

- 更多REST API指令可以參考https://ryu.readthedocs.io/en/latest/app/ofctl_rest.html

URI HTTP Method 說明

/stats/switches GET 取得所有連接到controller的Switch

/stats/flow/<DPID> GET 取得指定Switch中的Flow

/stats/flowentry/add POST 新增一條Flow

/stats/flowentry/delete POST 刪除指定的Switch中符合條件的Flow

/stats/flowentry/clear/<DPID> DELETE 刪除指定Switch裡的所有Flow

Page 26: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

常用REST API介紹

• 取得所有連接到controller的Switch

Page 27: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

常用REST API介紹

• 取得指定Switch中的Flow

Page 28: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

常用REST API介紹

• 新增一條Flow

• 刪除指定的Switch中符合條件的Flow

新增想要加入的Flow

指定條件,只要符合條件的Flow都會被刪除

Page 29: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

常用REST API介紹

• 刪除指定Switch裡的所有Flow

- 出現OK才代表指令有下成功

- 失敗

Page 30: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

開啟 Ryu

• 基礎功能結束後,要開始來替 Mininet 的拓樸建置 Flow rule了- ryu-manager --verbose --observe-links ryu.app.ofctl_rest

- 要使用REST API的話需要掛載ryu.app.ofctl_rest

Page 31: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

開啟 Mininet

• 用mininet開啟線性拓樸- sudo mn --mac --controller=remote,ip=127.0.0.1

1個Switch,2個Host

- h1 ping h2

- 一開始沒有任何規則,所以h1 ping不到 h2。

Page 32: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

REST API 範例

• 利用Postman新增Flow- http://<ControllerIP>:8080/stats/flowentry/add

• 第一條:當有來源為port1的封包時,把它從port2送出去

dpid →指定交換機match→抓到封包有這些條件就執行actions

in_port:1 →代表從port1來的封包都會執行actions

actions →match成功的封包就會執行以下動作type:OUTPUT →設定動作為送出封包port:2 →從port2送出

Page 33: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

REST API 範例

• 封包的傳送需要雙向都打通,所以還要在下第二條規則:當有來源為port2的封包時,把它從port1送出去

Page 34: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

REST API 範例

• 這時候 h1就能ping到 h2

原本無法傳送封包

Postman下完規則後封包可以正常傳送

Page 35: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

REST API 範例

• Ryu controller可以看到剛剛下的指令

• Mininet也可以透過指令看到交換機的Flow- sh ovs-ofctl dump-flows s1

Page 36: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

REST API 範例

• 也可以利用Postman查看Flow- http://<ControllerIP>: 8080/stats/flow/1

• 此API可以把指定的交換機的Flow清空- http://<ControllerIP>:8080/stats/flowentry

/clear/<DPID>

Page 37: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

作業一:REST API

• Mininet開啟一個topo為linear,3的拓樸

• 第一題:利用REST API下規則,讓h1,h2能互ping,但是ping不到h3(pingall截圖)。

- 提示:上面範例只對Switch1下規則,h1 h2要互ping需要打通所有封包會經過的地方。

H1

S1 S2 S3

H3H2

Port:2

Port:1 Port:1Port:1

Port:2

Port:2

Port:3

Page 38: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

作業一:REST API

• GET Switch 1跟Switch 2的Flow,儲存成json檔。

• 第二題:將兩份jason檔上傳gitlab。

Page 39: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

作業二

• 使用python檔來下規則

• 到Gitlab下載檔案

• 修改檔案內容使 h1 ping得到 h2- 要下的規則為:

- 第一條:如果是port1進來的封包則由port2送出

- 第二條:自行想像

• Mininet開啟一個topo為linear,3的拓樸

• 最後把修改完的python檔以分支的方法上傳至Gitlab

Page 40: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

作業二

• 新增規則的範例如下

match及actions的方式與REST API的範例類似

這邊actions的部分只需填入要從哪裡輸出

在此處新增規則

Page 41: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210

作業三(加分題)

• Mininet開啟lab1的topo- sudo mn --custom mininet/custom/topo-3sw-3host.py --topo=mytopo --

mac --controller=remote,ip=127.0.0.1

• Ryu controller下規則使host之間能ping通

Host1

(h1)

Host2

(h2)

OVS

(s1)

OVS

(s2)

OVS

(s3)

Host3

(h3)

Page 42: LAB 2: RYU Controller with Mininet

Mobile Intelligent Network LabDepart. Electronic Engineering

Lab210Lab210