SDN實驗---Mininet實驗(模擬多數據中心帶寬實驗)


補充:NameError: name 'buffer' is not defined

>>> import sys
>>> if sys.version_info > (3,):
...     buffer = memoryview
>>> b = buffer('yay!'.encode())
>>> len(b)
4
因為在Python3中buffer已經被memoryview取代了,buffer在Python2中使用,所以我們可以在文件中加入
import sys
if sys.version_info > (3,):
   buffer = memoryview

一:Mininet模擬多數據中心流量帶寬實驗

(一)案例目的

(二)為什么使用Mininet模擬數據中心--應用價值

 

Mininet最常用的場景就是數據中心。因為Mininet可以模擬出很復雜的網絡拓撲,而不需要硬件的支持,就可以搭建出不同的數據中心的拓撲。
可以為真正的數據中心網絡的搭建起到模擬預測實驗作用,為真實的數據中心的成本帶來一定的節省。

二:數據中心網絡拓撲

(一)數據中心網絡拓撲結構

存在線路冗余(多條鏈路可達),容錯能力強-----胖樹拓撲

(二)實現網絡拓撲---按照結構實現,代碼不唯一

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import RemoteController
from mininet.link import TCLink
from mininet.util import dumpNodeConnections

class MyTopo(Topo):

    def __init__(self):
        super(MyTopo,self).__init__()

        #Marking the number of switch for per level
        L1 = 2;    
        L2 = L1*2
        L3 = L2

        #Starting create the switch
        c = []    #core switch
        a = []    #aggregate switch
        e = []    #edge switch

        #notice: switch label is a special data structure
        for i in range(L1):
            c_sw = self.addSwitch('c{}'.format(i+1))    #label from 1 to n,not start with 0
            c.append(c_sw)

        for i in range(L2):
            a_sw = self.addSwitch('a{}'.format(L1+i+1))
            a.append(a_sw)

        for i in range(L3):
            e_sw = self.addSwitch('e{}'.format(L1+L2+i+1))
            e.append(e_sw)

        #Starting create the link between switchs
        #first the first level and second level link
        for i in range(L1):
            c_sw = c[i]
            for j in range(L2):
                self.addLink(c_sw,a[j])

        #second the second level and third level link
        for i in range(L2):
            self.addLink(a[i],e[i])
            if not i%2:
                self.addLink(a[i],e[i+1])
            else:
                self.addLink(a[i],e[i-1])

        #Starting create the host and create link between switchs and hosts
        for i in range(L3):
            for j in range(2):
                hs = self.addHost('h{}'.format(i*2+j+1))
                self.addLink(e[i],hs)



topos = {"mytopo":(lambda:MyTopo())}

(三)使用Mininet測試

sudo mn --custom ./data_center_topo.py --topo=mytopo --controller=remote

三:流量模擬

(一)為什么需要流量模擬

  

(二)流量隨機模型在Mininet中的應用 

 

 四:自定義命令拓展實現---為流量模擬做准備

(一)修改net.py

    def iperf_single( self, hosts=None, udpBw='10M',period=60,port=5001):
        """Run iperf between two hosts using UDP.
           hosts: list of hosts; if None, uses first and last hosts
           returns: results two-element array of server and client speeds
        """
 if not hosts: return else: assert len(hosts) == 2  #這段代碼我們要求一定要有兩個參數,即下面的client,和server client, server = hosts filename = client.name[1:]+'.out' output('*** Iperf:testing bandwidth between ') output('%s and %s\n'%(client.name,server.name))  #這里在Mininet交互界面顯示提示信息 iperfArgs = 'iperf -u ' bwArgs = '-b '+udpBw+' '  #設置命令和參數,這是要在client和server上執行的 print("***start server***") server.cmd(iperfArgs+'-s -i 1'+' > /home/njzy/temp_log/'+filename+'&')  #服務器端執行指令,並且將返回的信息存放在文件中
                          #注意:對應我們存放日志的目錄,一定是真實存在的
        print("***start client***")  client.cmd(iperfArgs+'-t '+str(period)+' -c '+server.IP()+' '+bwArgs  #客戶端執行指令,並且將返回的信息存放在文件中 +' > /home/njzy/temp_log/'+'client'+filename+'&')

    def iperfMulti(self,bw,period=60):
        base_port = 5001 server_list = [] client_list = [h for h in self.hosts] host_list = [] host_list = [h for h in self.hosts]  #收集所有主機信息

        cli_outs = []
        ser_outs = []

 _len = len(host_list) for i in xrange(0,_len):  #按照主機數目進行循環,每次選擇一台主機,作為客戶端 client = host_list[i] server = client while (server==client):  #如果客戶端和服務端是同一台主機,那么我們隨機從主機中國選擇一台新的主機作為服務端,直到找到一台與客戶端不同的主機,用來做服務端 server = random.choice(host_list)

            server_list.append(server)
            self.iperf_single(hosts=[client,server],udpBw=bw,period=period,port=base_port)  #客戶端和服務端進行帶寬測試
            sleep(.05)
            base_port += 1  #更換端口號,做到隨機

        sleep(period)
        print("test has done")  #結束,打印提示信息

(二)修改cli.py將iperfmulti命令在CLI類中注冊

    def do_iperfmulti( self, line ):
        """
        Multi iperf UDP test between nodes
        """
        args = line.split()
        if len(args) == 1:
            udpBw = args[0]
            self.mn.iperfMulti(udpBw)
        elif len(args) == 2:
            udpBw = args[0]
            period = args[1]
            self.mn.iperfMulti(udpBw,float(period))
        else:
            error( 'invalid number of args: iperfMulti udpBw period\n '+
                    'udpBw examples:1M 120\n' )

 

(三) 在mininet/bin/mn文件中加入iperfmulti可執行命令

TESTS = { name: True
          for name in ( 'pingall', 'pingpair', 'iperf', 'iperfudp','iperfmulti' ) }
# Map to alternate spellings of Mininet() methods
ALTSPELLING = { 'pingall': 'pingAll', 'pingpair': 'pingPair',
                'iperfudp': 'iperfUdp','iperfmulti': 'iperfMulti' }

(四)重新編譯mininet---因為我們修改了mininet內核文件

因為我們已經安裝OpenFlow協議和openvswitch,所以不需要再加3V

sudo mn
iperfmulti  后面會自動補全

五:進行網絡測試

(一)開始Ryu---為了防止廣播風暴,使用生成樹協議《重點注意協議文件選擇》

ryu-manager simple_switch_stp_13.py     注意:是使用simple_switch_stp_13協議,不要使用simple_switch_stp文件,不然會出問題

(二)Mininet啟動網絡拓撲 

sudo mn --custom ./data_center_topo.py --topo=mytopo --controller=remote

(三)使用iperf命令,進行TCP帶寬測試

注意:在測試之前先多ping幾次<h1 ping h2>(找到可以ping通),使得網絡拓撲結構提前存在控制器中,不然容易直接退出

iperf h1 h2

iperf h1 h3

 

iperf h1 h5

(四)使用iperfmulti命令,進行UDP帶寬測試

iperfmulti 0.025M

查看流量日志

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM