1. 程式人生 > 實用技巧 >軟體定義網路實驗二:Mininet 實驗——拓撲的命令指令碼生成

軟體定義網路實驗二:Mininet 實驗——拓撲的命令指令碼生成

軟體定義網路實驗二:Mininet 實驗——拓撲的命令指令碼生成

一、實驗目的

掌握 Mininet 的自定義拓撲生成方法:命令列建立、Python 指令碼編寫

二、實驗任務

通過使用命令列建立、Python 指令碼編寫生成拓撲,熟悉 Mininet 的基本功能。

三、實驗步驟

1.實驗環境

  • 安裝了Ubuntu 16.04.6 Desktop amd64的虛擬機器

2. 實驗過程

  • (1)針對特定拓撲的命令列快速建立

    • //最小拓撲,1 臺交換機下掛 2 臺主機
      $ sudo mn --topo minimal
    • // 簡單拓撲,1 臺交換機下掛 n 臺主機,此處 n=3,n=2 即為最小拓撲
      $ sudo mn --topo single,3
    • // 線性拓撲,交換機連成一線,每臺交換機下掛 1 臺主機,此處有 3 臺交換機 3 臺主機
      $ sudo mn --topo linear,3
    • // 樹形拓撲,基於深度 depth 和扇出 fanout,此處均為 2
      $ sudo mn --topo tree, fanout=2,depth=2
  • (2)通用情形的 Python 指令碼自定義建立

    • 此種方法需要具備 Python 的程式設計能力。
    • 本題所構建線性拓撲結構如下圖

python程式碼如下:

# coding=UTF-8
from mininet.net import Mininet
from mininet.node import CPULimitedHost
from mininet.link import TCLink
net = Mininet(host=CPULimitedHost,link=TCLink) # 如不限制性能,引數為空
# 建立網路節點
c0 = net.addController()
h1 = net.addHost('h1',cpu=0.5)
h2 = net.addHost('h2',cpu=0.5)
h3 = net.addHost('h3',cpu=0.5)
s1 = net.addSwitch('s1')
s2 = net.addSwitch('s2')
s3 = net.addSwitch('s3')
# 建立節點間的鏈路
net.addLink(h1,s1,bw=10,delay='5ms',max_queue_size=1000,loss=0,use_htb=True)
net.addLink(s1,s2)
net.addLink(h2,s2,bw=10,delay='5ms',max_queue_size=1000,loss=0,use_htb=True)
net.addLink(s2,s3)
net.addLink(h3,s3,bw=10,delay='5ms',max_queue_size=1000,loss=0,use_htb=True)
# 配置主機 ip
h1.setIP('10.0.0.1',24)
h2.setIP('10.0.0.2',24)
h3.setIP('10.0.0.3',24)
net.start()
net.pingAll()
net.stop()
  • 執行命令:

    • $ nano mytopo.py // 複製 Python 程式碼到 py 檔案中
    • $ sudo python mytopo.py // 執行 py 文
  • 結果如下:

  • 之後修改之前的 Python 程式,使之可用 iPerf 測試網路拓撲中的指定主機之間的頻寬。

python程式碼如下:

# coding=UTF-8
#!/usr/bin/python
from mininet.net import Mininet
from mininet.node import CPULimitedHost
from mininet.link import TCLink
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel
def IperfTest():
    net = Mininet(host=CPULimitedHost, link=TCLink)
    c0 = net.addController()
    h1 = net.addHost('h1',cpu=0.5)
    h2 = net.addHost('h2',cpu=0.5)
    h3 = net.addHost('h3',cpu=0.5)
    s1 = net.addSwitch('s1')
    s2 = net.addSwitch('s2')
    s3 = net.addSwitch('s3')
    net.addLink(h1,s1, bw=10,delay='5ms',max_queue_size=1000,loss=0,use_htb=True)
    net.addLink(s1,s2)
    net.addLink(h2,s2, bw=10,delay='5ms',max_queue_size=1000,loss=0,use_htb=True)
    net.addLink(s2,s3)
    net.addLink(h3,s3, bw=10,delay='5ms',max_queue_size=1000,loss=0,use_htb=True)
    h1.setIP('10.0.0.1',24)
    h2.setIP('10.0.0.2',24)
    h3.setIP('10.0.0.3',24)
    net.start()
    print "Dumping host connections"
    dumpNodeConnections(net.hosts)
    print "Testing network connectivity"
    net.pingAll()
    print "Testing bandwidth"
    h1,h2,h3 = net.get('h1','h2','h3')
    net.iperf((h1,h2))
    net.iperf((h2,h3))
    net.iperf((h1,h3))
    net.stop()
if __name__=='__main__':
    setLogLevel('info') #print the log when Configuring hosts,starting switches and controller
    IperfTest()
  • 執行命令:
    • $ nano IperfTest.py // 複製 Python 程式碼到 py 檔案中
    • $ sudo python IperfTest.py // 執行 py 檔案
  • 結果如下:

四、實驗心得

  • 注意程式碼的空格縮排,不然會報錯。