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

查看流量日誌

相關文章
相關標籤/搜索