目錄表
Mininet基本演練
0x00 前言
接續上一篇[Mininet] 安裝教學,本篇翻譯整理官方網站資訊,講述Mininet指令
主要分成
- Mininet的基本指令
- Mininet進階選項
- Mininet CLI指令
- Python API範例
- 附錄:使用遠端 Controller
0x01 Mininet的基本指令
首先,Mininet VM的指令列有三種提示符號
$
在Shell中執行Linux指令mininet>
在Mininet’s CLI中執行Mininet指令#
在root Shell中執行Linux指令
顯示Mininet參數選項
$ sudo mn -h
使用Wireshark
Mininet VM中已經包含wireshark,而封包分析在網路中是很重要的工作,開啟wireshark在背景執行
$ sudo wireshark &
在wireshark的過濾器欄位輸入 of
過濾OpenFlow封包
網卡部分在Capture>Interface>選擇lo0
如果執行Wireshark時出現 $DISPLAY not set或cannot open display錯誤
使用最小拓樸並進入Mininet CLI
$ sudo mn
mininet的預設拓樸是 mininal
,這個拓樸包含一個OpenFlow kernel switch,連接兩個host,加上一個OpenFlow reference controller
- 顯示Mininet CLI commands:
mininet> help
- 顯示nodes
mininet> nodes
- 顯示連結
mininet> net
- 印出所有nodes的資訊
mininet> dump
如果在Mininet CLI中command的開頭是host, switch 或 controller name,則後頭的指令是獨立在該node上執行
- 對host1下ifconfig -a指令
mininet> h1 ifconfig -a
namespace 概念
透過 Linux namespace ,我們其實可以將各個 hosts, switches, controller, 隔離在不同網路,但一般除非是要配置成 multiple-controller network,不然不太需要,可以參考 –innamespace
指令
而在 mininet 中,除了 network 是虛擬化的之外,其他如 process 仍是共用沒有以 namespace 區隔
所以透過指令
mininet> h1 ps -a
跟
mininet> h2 ps -a
看到的結果其實都是以一樣的,已 root(執行 mininet 前有 sudo) 身份看見的 ps -a
測試兩個hosts的連線
mininet> h1 ping h2
All-pairs Ping
mininet> pingall
Run a simple web server and client
在 mininet host 中不只可以跑 ping,也可以執行 bash 或是 job control 指令 (&, jobs, kill, etc)
底下範例透過 python 開一個簡易的 web server,透過 h2 向 h1 送出請求,之後關閉 web server
mininet> h1 python -m SimpleHTTPServer 80 & mininet> h2 wget -O - h1 ... mininet> h1 kill %python
離開 CLI
mininet> exit
CleanUp
如果Mininet因為某些原因crash了,使用CleanUp
$ sudo mn -c
0x02 Mininet進階選項
執行測試
- 即便不進入 mininet CLI, 也可使用 CLI 中 pingall 指令
$ sudo mn --test pingpair
- 測試 h1/h2 間流量效能
$ sudo mn --test iperf
改變拓墣大小和型態
- 使用 –topo 這個參數
- single 是單台 switch 連接多台 host
- linear 是一台 host 接一台 switch,switch 再串成線狀
$ sudo mn --test pingall --topo single,3 $ sudo mn --test pingall --topo linear,4
Link variations
- Mininet 2.0 後允許我們對 link 下參數
- 這邊我們將頻寬設為 10Mbits, 每條 link 則 delay 10ms,在 ping 測試中應該可見 round trip time (RTT) 約為 40ms 左右
$ sudo mn --link tc,bw=10,delay=10ms mininet> iperf ... mininet> h1 ping -c10 h2
Adjustable Verbosity
- 使用
-v <option>
我們可以設定輸出資訊的 level- info
- debug
- warning
- output
$ sudo mn -v debug
Custom Topologies
- 透過 python API 我們可以在 mininet 寫自定義的拓墣
- 透過
–custom
參數指定 py 檔,–topo
參數後面要根據 py 中最後一行的名稱
$ sudo mn --custom ~/mininet/custom/topo-2sw-2host.py --topo mytopo --test pingall
- custom/topo-2sw-2host.py
"""Custom topology example Two directly connected switches plus a host for each switch: host --- switch --- switch --- host Adding the 'topos' dict with a key/value pair to generate our newly defined topology enables one to pass in '--topo=mytopo' from the command line. """ from mininet.topo import Topo class MyTopo( Topo ): "Simple topology example." def __init__( self ): "Create custom topo." # Initialize topology Topo.__init__( self ) # Add hosts and switches leftHost = self.addHost( 'h1' ) rightHost = self.addHost( 'h2' ) leftSwitch = self.addSwitch( 's3' ) rightSwitch = self.addSwitch( 's4' ) # Add links self.addLink( leftHost, leftSwitch ) self.addLink( leftSwitch, rightSwitch ) self.addLink( rightSwitch, rightHost ) topos = { 'mytopo': ( lambda: MyTopo() ) }
ID = MAC
- 預設上,每次啟動 mininet 產生的虛擬機都會分配到不同 MAC,但便於 debug,在 mininet 中我們可以使用
–mac
參數將簡單的 ID 轉成 MAC - 在 mininet 底下 Linux OS 中,host 的 mac 其實還是隨機的,這是透過 OpenFlow 可以把 MAC assign 給 data port 而實現的
Before: $ sudo mn ... mininet> h1 ifconfig h1-eth0 Link encap:Ethernet HWaddr f6:9d:5a:7f:41:42 inet addr:10.0.0.1 Bcast:10.255.255.255 Mask:255.0.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:6 errors:0 dropped:0 overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:392 (392.0 B) TX bytes:392 (392.0 B) mininet> exit After: $ sudo mn --mac ... mininet> h1 ifconfig h1-eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:01 inet addr:10.0.0.1 Bcast:10.255.255.255 Mask:255.0.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) mininet> exit
Xterm Display
- 透過 xterm 可以幫助我們做更複雜的 debug
$ sudo mn -x
- 在 switch 的 xterm 上我們可以使用下面指令 dump-flows
- 一開始會沒有 flow,輸出為空,在 h1 的 xterm 下 ping 10.0.0.2,再重新 dump-flows 一次就會有了
# dpctl dump-flows tcp:127.0.0.1:6634
Other Switch Types
- 使用
–switch
參數,我們可以將原本的 kernel-space switch 改為 user-space switch
$ sudo mn --switch user --test iperf
- 也可以使用 OpenvSwitch,TCP 頻寬應該會跟原來的 OpenFlow kernel module 差不多甚至更快
$ sudo mn --switch ovsk --test iperf
Mininet Benchmark
- 如果只是要記錄使用 topo 的時間戳的話可用
$ sudo mn --test none
0x03 Mininet CLI指令
Display Options
- 啟動 mininet
$ sudo mn
- 顯示選項
mininet> help
Python Interpreter
如果 mininet 中第一個字是 py
,則後面敘述會以 python 執行
- hello world
mininet> py 'hello ' + 'world'
- 印出可存取的區域變數
mininet> py locals()
- 印出某 node 可取得的方法或屬性
py dir(s1)
- 讀取 on-line documentation 得知一個 node 可取用的變數,按 q 離開
mininet> py help(h1)
- 取得 IP
mininet> py h1.IP()
Link Up/Down
mininet> link s1 h1 down mininet> link s1 h1 up
Xterm display
mininet> xterm h1 h2
0x04 Python API範例
SSH daemon per host
- 帳號密碼好像不是 mininet,登不進去…
$ sudo ~/mininet/examples/sshd.py
$ ssh 10.0.0.1 $ ping 10.0.0.2 ... $ exit
$ exit
0x05 附錄
Using a Remote Controller
$ sudo mn --controller=remote,ip=[controller IP],port=[controller listening port]