Mininet基礎筆記

Part 1: 基礎

  • $表明Linux普通環境
  • mininet>表示MIninet環境
  • #表明LinuxRoot環境

啓動參數

$ sudo mn -h          //顯示啓動mininet時的參數

結果如下:

  • -h, –help

    show this help message and exit
    
  • –switch=SWITCH

    default|ivs|lxbr|ovs|ovsbr|ovsk|user[,param=value...]
    ovs=OVSSwitch default=OVSSwitch ovsk=OVSSwitch
    lxbr=LinuxBridge user=UserSwitch ivs=IVSSwitch
    ovsbr=OVSBridge
    
  • –host=HOST

    cfs|proc|rt[,param=value...]
    rt=CPULimitedHost{'sched': 'rt'} proc=Host
    cfs=CPULimitedHost{'sched': 'cfs'}
    
  • –controller=CONTROLLER

    default|none|nox|ovsc|ref|remote|ryu[,param=value...]
    ovsc=OVSController none=NullController
    remote=RemoteController default=DefaultController
    nox=NOX ryu=Ryu ref=Controller
    
  • –link=LINK

    default|ovs|tc|tcu[,param=value...] default=Link
    ovs=OVSLink tcu=TCULink tc=TCLink
    
  • –topo=TOPO

    linear|minimal|reversed|single|torus|tree[,param=value
    ...] linear=LinearTopo torus=TorusTopo tree=TreeTopo
    single=SingleSwitchTopo
    reversed=SingleSwitchReversedTopo minimal=MinimalTopo
    
  • -c, –clean

    clean and exit
    
  • –custom=CUSTOM

    read custom classes or params from .py file(s)
    
  • –test=TESTS

    cli|build|pingall|pingpair|iperf|all|iperfudp|none|pin
    gpair|iperfudp|pingall|iperfUDP
    
  • -x, –xterms

    spawn xterms for each node
    
  • -i IPBASE, –ipbase=IPBASE

    base IP address for hosts
    
  • –mac

    automatically set host MACs
    
  • –arp

    set all-pairs ARP entries
    
  • -v VERBOSITY, –verbosity=VERBOSITY

    info|warning|critical|error|debug|output
    
  • –innamespace

    sw and ctrl in namespace?
    
  • –listenport=LISTENPORT

    base port for passive switch listening
    
  • –nolistenport

    don't use passive listening port
    
  • –pre=PRE

    CLI script to run before tests
    
  • –post=POST

    CLI script to run after tests
    
  • –pin

    pin hosts to CPU cores (requires --host cfs or --host rt)
    
  • –nat

    [option=val...] adds a NAT to the topology that
    connects Mininet hosts to the physical network.
    Warning: This may route any traffic on the machine
    that uses Mininet's IP subnet into the Mininet
    network. If you need to change Mininet's IP subnet,
    see the --ipbase option.
    
  • –version

    prints the version and exits
    
  • –cluster=server1,server2…

    run on multiple servers (experimental!)
    
  • –placement=block|random

    node placement for --cluster (experimental!)
    

啓動wireshark

$ wireshark &

Mininet虛擬機內置的Wireshark可以解析openflow協議的數據包。在wireshark的過濾文本框中輸入“of”並應用,即可只顯示openflow的數據包。抓包接口選擇“lo”,並開始抓包。

與主機、交換機交互

$ sudo mn

默認啓動拓撲爲minimal的網絡:主機h1和h2,交換機s1,控制器。這四項各爲一個進程,其中控制器可以是遠程的,通過controller參數設置。

Commands Meanings
mininet>help 顯示mininet裏的命令
miminet>nodes 顯示所有節點
mininet>net 顯示所有鏈接
mininet>dump 顯示所有節點內信息

當mininet中的第一個參數是節點時,表示該命令是在該節點上運行,例如:

mininet>h1 ifconfig

測試連通性

mininet>h1 ping -c 3 h2  or  mininet>pingall

關閉mininet

$ sudo mn -c

退出mininet並清理內存。

Part 2:高級啓動選項

改變topo大小和類型

$ sudo mn --topo linear,4           //線性topo,4個主機

鏈接多樣化

$ sudo mn --link tc,bw=10,delay=10ms      //每條鏈接時延爲10ms

調整冗餘度

$ sudo mn -v debug          //冗餘級別爲debug

自定義拓撲結構

#topo-2sw-2host.py
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() ) }
$ sudo mn --custom topo-2sw-2host.py --topo mytopo --test pingall

命名空間

默認情況下,主機各自在自己的命名空間,而控制器和交換機則在root空間。將交換機放在自己的命名空間,可以用–innamespace

$ sudo mn --innamespace --switch user

Mininet命令行命令

$ mininet> help

Python解釋器

如果以py開頭,則會用python執行。

mininet>py locals()             #顯示可獲取的局部變量
mininet>py dir(s1)              #顯示一個節點的方法和屬性
mininet>py help(h1)             #獲取一個基點的方法的在線文檔(按q退出)
mininet>py h1.IP()              #求變量的值

顯示xterm

mininet>xterm h1,s1 

python API

構建topo

API description
Topo Mininet 拓撲結構的基類
.build(n) 脫坡類中需要重寫的函數
.addSwitch(string) 向拓撲結構中添加交換價
.addHost(String) 向拓撲結構中添加主機
.addLink(string1,string2) 添加鏈接
Mininet 創建和管理網絡的主類
.start() 啓動網絡
.pingAll() 測試網絡連通性
.stop() 終止網絡
.hosts 網絡中的所有節點
dumpNodeConnections() 顯示一組節點的connection
setLogLevel 顯示顯示級別,’info’、’debug’、’output’等,推薦’info’

設置性能參數

API description
addHost(name,cpu=f) 添加佔用f的CPU資源的主機
addLink(node1,node2,bw,delay,max_queue_size,loss) 在node1和node2間添加帶寬、時延、隊列長度和丟包率的鏈接
net.get(name) 通過名字獲取節點

在主機中運行程序

h1 = net.get('h1')
result = h1.cmd('ifconfig')
print result

主機配置方法

API description
.IP() 返回IP地址
.MAC() 返回MAC地址
.setARP() 向主機的ARP緩存中添加靜態ARP項
.setIP() 設置主機IP
.setMAC() 設置主機MAC()

以上方法中,若不聲明具體接口,則使用主機默認接口。

CLI接口

CLI即可可以顯示窗口,運行各種命令。

from mininet.topo import SingleSwitchTopo
from mininet.net import Mininet
from mininet.cli import CLI

net = Mininet(SingleSwitchTopo(2))
net.start()
CLI(net)
net.stop()

自定義文件

sudo mn --custom mytopo.py --topo mytopo,3

需要在mytopo.py文件中定義字典,且必須滿足如下要求:

option dict name key: value
–topo topos ‘short name’: Topo constructor
–switch switches ‘short name’: Switch constructor
–host hosts ‘short name’: Host constructor
–controller controllers ‘short name’: Controller constructor
–link links ‘short name’: Link constructor
–test test ‘short name’: test function to call with Mininet object
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章