mininet 實戰

1. 安裝

  • Debian Package

    • sudo apt-get install mininet
  • Build source and install

    git clone git://github.com/mininet/mininet
    mininet/util/install.sh -a
    

2. 啟動

  • 在沒有任何參數下,產生的虛擬環境為兩個host (h1,h2),一個switch (s1),並將h1,h2連接s1
Topology
Image 1 - Topology
  • 啟動sudo mn
mn
Image 2 - mn
  • mininet是基於OVS的應用軟體,當沒有設定OVS Controller時,他會退回OVS Bridge模式(不依賴Controller,預設所有node都是可以連通的)

    OVS:全名Open vSwitch,類似VM,主要是虛擬switch,支援OpenFlow

3. 測試

  • 顯示所有節點nodes
nodes
Image 3 - nodes
  • 顯示所有節點的連接關係net
net
Image 4 - net
  • 察看所有節點的資訊:dump
dump
Image 5 - dump
  • h1 ping h2 一個封包h1 ping -c 1 h2
ping
Image 6 - ping
  • 叫出兩個host的命令視窗xterm h1 h2
xterm
Image 7 - xterm

4. Mininet Python API

Topo: Mininet拓撲結構的base class

build(): The method to override in your topology class. Constructor parameters (n) will be passed through to it automatically by Topo.init().

addSwitch(): 給拓撲結構中增加一個switch,返回switch名字

addHost(): 增加host,返回host名字

addLink(): 增加一個雙向link,返回link的key

Mininet: 建立和管理網路的main class

start(): 開啟網路

pingAll(): 通過host之間的互ping測試連通性

stop(): 關閉網絡

net.hosts: 網路中的所有主機

dumpNodeConnections(): 所有的連接關係

setLogLevel( 'info' | 'debug' | 'output' ): set Mininet's default output level; 'info' is recommended as it provides useful information.

5. 寫一個Python範例

5.1. 目標

  • 啟動50個hosts,作為VM server farm
  • 啟動1個controller
  • 啟動1個switch
Server Farm
Image 8 - Server Farm

5.2. Code

example

#!/usr/bin/python import re from mininet.net import Mininet from mininet.node import Controller from mininet.cli import CLI from mininet.link import Intf from mininet.log import setLogLevel, info, error from mininet.util import quietRun from mininet.node import OVSController def checkIntf( intf ): "Make sure intf exists and is not configured." if ( ' %s:' % intf ) not in quietRun( 'ip link show' ): error( 'Error:', intf, 'does not exist!\n' ) exit( 1 ) ips = re.findall( r'\d+\.\d+\.\d+\.\d+', quietRun( 'ifconfig ' + intf ) ) if ips: error( 'Error:', intf, 'has an IP address and is probably in use!\n' ) exit( 1 ) def myNetwork(): net = Mininet( topo=None, build=False) info( '*** Adding controller\n' ) net.addController(name='c0',controller = OVSController) info( '*** Add switches\n') s1 = net.addSwitch('s1') max_hosts = 50 newIntf = 'enp3s0' host_list = {} info( '*** Add hosts\n') for i in xrange(1,max_hosts+1): host_list[i] = net.addHost('h'+str(i)) info( '*** Add links between ',host_list[i],' and s1 \r') net.addLink(host_list[i], s1) info( '*** Checking the interface ', newIntf, '\n' ) checkIntf( newIntf ) switch = net.switches[ 0 ] info( '*** Adding', newIntf, 'to switch', switch.name, '\n' ) brintf = Intf( newIntf, node=switch ) info( '*** Starting network\n') net.start() CLI(net) net.stop() if __name__ == '__main__': setLogLevel( 'info' ) myNetwork()

5.3. 執行

  • sudo python example.py
example
Image 9 - example

5.4. Q&A

執行出現Cannot find required executable ovs-controller

  • 你沒有安裝ovs-controller
    • sudo apt-get install openvswitch-testcontroller
  • ovs-controller是舊名稱,所以你需要複製一下
    • sudo cp /usr/bin/ovs-testcontroller /usr/bin/ovs-controller

執行出現

Exception: Please shut down the controller which is running on port 6653:
Active Internet connections (servers and established)
tcp        0      0 0.0.0.0:6653            0.0.0.0:*               LISTEN      30215/ovs-testcontr

  • 你需要kill正在背景執行的ovs-testcontroller
    1. sudo netstat -lptu
    2. sudo service ovs-testcontroller stop

6. Mininet建立網路的三種方式

6.1. 底層API

  • Node & Link
h1 = Host( 'h1' )
h2 = Host( 'h2' )
s1 = OVSSwitch( 's1', inNamespace=False )
c0 = Controller( 'c0', inNamespace=False )
Link( h1, s1 )
Link( h2, s1 )
h1.setIP( '10.1/8' )
h2.setIP( '10.2/8' )
c0.start()
s1.start( [ c0 ] )
print h1.cmd( 'ping -c1', h2.IP() )
s1.stop()
c0.stop()

6.2. 中層API

  • net Object
net = Mininet()
h1 = net.addHost( 'h1' )
h2 = net.addHost( 'h2' )
s1 = net.addSwitch( 's1' )
c0 = net.addController( 'c0' )
net.addLink( h1, s1 )
net.addLink( h2, s1 )
net.start()
print h1.cmd( 'ping -c1', h2.IP() )
CLI( net )
net.stop()

6.3. 上層API

  • Topology Object
class SingleSwitchTopo( Topo ):
    "Single Switch Topology"
    def __init__( self, count=1, **params ):
        Topo.__init__( self, **params )
        hosts = [ self.addHost( 'h%d' % i ) for i in range( 1, count + 1 ) ]
        s1 = self.addSwitch( 's1' )
        for h in hosts:
            self.addLink( h, s1 )                                                                          

net = Mininet( topo=SingleSwitchTopo( 3 ) )
net.start()
CLI( net )
net.stop()
© 2017 Trashman all right reserved,powered by Gitbook修訂時間: 2024-10-14 03:41:00

results matching ""

    No results matching ""