目錄表

Mininet基本演練

0x00 前言

接續上一篇[Mininet] 安裝教學,本篇翻譯整理官方網站資訊,講述Mininet指令

主要分成

  1. Mininet的基本指令
  2. Mininet進階選項
  3. Mininet CLI指令
  4. Python API範例
  5. 附錄:使用遠端 Controller

0x01 Mininet的基本指令

首先,Mininet VM的指令列有三種提示符號

顯示Mininet參數選項

  $ sudo mn -h

使用Wireshark

Mininet VM中已經包含wireshark,而封包分析在網路中是很重要的工作,開啟wireshark在背景執行

  $ sudo wireshark &

在wireshark的過濾器欄位輸入 of 過濾OpenFlow封包

網卡部分在Capture>Interface>選擇lo0

如果執行Wireshark時出現 $DISPLAY not set或cannot open display錯誤

請參考SSH X11 Forwarding,或是Run GUI in Mininet VM console

使用最小拓樸並進入Mininet CLI

  $ sudo mn

mininet的預設拓樸是 mininal,這個拓樸包含一個OpenFlow kernel switch,連接兩個host,加上一個OpenFlow reference controller

  mininet> help
  mininet> nodes
  mininet> net
  mininet> dump

如果在Mininet CLI中command的開頭是host, switch 或 controller name,則後頭的指令是獨立在該node上執行

  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進階選項

執行測試

$ sudo mn --test pingpair
$ sudo mn --test iperf

改變拓墣大小和型態

$ sudo mn --test pingall --topo single,3
$ sudo mn --test pingall --topo linear,4

Link variations

$ sudo mn --link tc,bw=10,delay=10ms
 mininet> iperf
 ...
 mininet> h1 ping -c10 h2

Adjustable Verbosity

$ sudo mn -v debug

Custom Topologies

$ 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

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

$ sudo mn -x
# dpctl dump-flows tcp:127.0.0.1:6634

Other Switch Types

$ sudo mn --switch user --test iperf
$ sudo mn --switch ovsk --test iperf

Mininet Benchmark

$ sudo mn --test none

0x03 Mininet CLI指令

Display Options

$ sudo mn
mininet> help

Python Interpreter

如果 mininet 中第一個字是 py,則後面敘述會以 python 執行

mininet> py 'hello ' + 'world'
mininet> py locals()
py dir(s1)
mininet> py help(h1)
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

$ 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]

0x06 參考資料