sdn

 1 #!/usr/bin/env python
 2 
 3 #from mininet.cli import CLI
 4 #from mininet.link import Link
 5 #from mininet.net import Mininet
 6 #from mininet.node import RemoteController
 7 #from mininet.trm import makeTerm
 8 from mininet.topo import Topo
 9 class spanning_tree( Topo ):
10         def __init__( self ):
11                 Topo.__init__(self)
12                 host1 = self.addHost('h1')
13                 host2 = self.addHost('h2')
14                 host3 = self.addHost('h3')
15 
16                 switch1 = self.addSwitch( 's1' )
17                 switch2 = self.addSwitch( 's2' )
18                 switch3 = self.addSwitch( 's3' )
19 
20                 self.addLink(switch1, host1)
21                 self.addLink(switch2, host2)
22                 self.addLink(switch3, host3)
23 
24                 self.addLink(switch1, switch2)
25                 self.addLink(switch2, switch3)
26                 self.addLink(switch1, switch3)
27 
28 topos = {'mytopo': ( lambda:spanning_tree())}

 环路拓扑结构,可能造成广播风暴:

手动编写的拓扑

 1 #!/usr/bin/env python
 2 
 3 from mininet.cli import CLI
 4 from mininet.node import Node
 5 from mininet.link import Link
 6 from mininet.net import Mininet
 7 from mininet.node import RemoteController
 8 from mininet.term import makeTerm
 9 
10 
11 if '__main__' == __name__:
12     net = Mininet(controller=RemoteController)
13 
14     c0 = net.addController('c0')
15 
16     s1 = net.addSwitch('s1')
17     s2 = net.addSwitch('s2')
18     s3 = net.addSwitch('s3')
19 
20     h1 = net.addHost('h1')
21     h2 = net.addHost('h2')
22     h3 = net.addHost('h3')
23 
24     Link(s1, h1)
25     Link(s2, h2)
26     Link(s3, h3)
27 
28     Link(s1, s2)
29     Link(s2, s3)
30     Link(s1, s3)
31 
32     net.build()
33     c0.start()
34     s1.start([c0])
35     s2.start([c0])
36     s3.start([c0])
37 
38     net.terms.append(makeTerm(c0))
39     net.terms.append(makeTerm(s1))
40     net.terms.append(makeTerm(s2))
41     net.terms.append(makeTerm(s3))
42     net.terms.append(makeTerm(h1))
43     net.terms.append(makeTerm(h2))
44     net.terms.append(makeTerm(h3))
45 
46     CLI(net)
47     net.stop()
View Code

图像化生成的脚本:

#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call

def myNetwork():

    net = Mininet( topo=None,
                   build=False,
                   ipBase='10.0.0.0/8')

    info( '*** Adding controller\n' )
    c0=net.addController(name='c0',
                      controller=Controller,
                      protocol='tcp',
                      port=6633)

    info( '*** Add switches\n')
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch)
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch)
    s3 = net.addSwitch('s3', cls=OVSKernelSwitch)

    info( '*** Add hosts\n')
    h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None)
    h3 = net.addHost('h3', cls=Host, ip='10.0.0.3', defaultRoute=None)

    info( '*** Add links\n')
    net.addLink(h1, s1)
    net.addLink(s1, s2)
    net.addLink(s1, s3)
    net.addLink(s2, s3)
    net.addLink(h2, s2)
    net.addLink(s3, h3)

    info( '*** Starting network\n')
    net.build()
    info( '*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    info( '*** Starting switches\n')
    net.get('s2').start([c0])
    net.get('s1').start([c0])
    net.get('s3').start([c0])

    info( '*** Post configure switches and hosts\n')

    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()
View Code

 

根据miniedit绘制拓扑生成的脚本:

#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call

def myNetwork():

    net = Mininet( topo=None,
                   build=False,
                   ipBase='10.0.0.0/8')

    info( '*** Adding controller\n' )
    c0=net.addController(name='c0',
                      controller=Controller,
                      protocol='tcp',
                      port=6633)

    info( '*** Add switches\n')
    s10 = net.addSwitch('s10', cls=OVSKernelSwitch)
    s3 = net.addSwitch('s3', cls=OVSKernelSwitch)
    s14 = net.addSwitch('s14', cls=OVSKernelSwitch)
    s19 = net.addSwitch('s19', cls=OVSKernelSwitch)
    s4 = net.addSwitch('s4', cls=OVSKernelSwitch)
    s20 = net.addSwitch('s20', cls=OVSKernelSwitch)
    s9 = net.addSwitch('s9', cls=OVSKernelSwitch)
    s11 = net.addSwitch('s11', cls=OVSKernelSwitch)
    s12 = net.addSwitch('s12', cls=OVSKernelSwitch)
    s15 = net.addSwitch('s15', cls=OVSKernelSwitch)
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch)
    s6 = net.addSwitch('s6', cls=OVSKernelSwitch)
    s5 = net.addSwitch('s5', cls=OVSKernelSwitch)
    s16 = net.addSwitch('s16', cls=OVSKernelSwitch)
    s17 = net.addSwitch('s17', cls=OVSKernelSwitch)
    s7 = net.addSwitch('s7', cls=OVSKernelSwitch)
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch)
    s18 = net.addSwitch('s18', cls=OVSKernelSwitch)
    s8 = net.addSwitch('s8', cls=OVSKernelSwitch)
    s13 = net.addSwitch('s13', cls=OVSKernelSwitch)

    info( '*** Add hosts\n')
    h15 = net.addHost('h15', cls=Host, ip='10.0.0.15', defaultRoute=None)
    h6 = net.addHost('h6', cls=Host, ip='10.0.0.6', defaultRoute=None)
    h16 = net.addHost('h16', cls=Host, ip='10.0.0.16', defaultRoute=None)
    h10 = net.addHost('h10', cls=Host, ip='10.0.0.10', defaultRoute=None)
    h7 = net.addHost('h7', cls=Host, ip='10.0.0.7', defaultRoute=None)
    h8 = net.addHost('h8', cls=Host, ip='10.0.0.8', defaultRoute=None)
    h14 = net.addHost('h14', cls=Host, ip='10.0.0.14', defaultRoute=None)
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None)
    h9 = net.addHost('h9', cls=Host, ip='10.0.0.9', defaultRoute=None)
    h4 = net.addHost('h4', cls=Host, ip='10.0.0.4', defaultRoute=None)
    h12 = net.addHost('h12', cls=Host, ip='10.0.0.12', defaultRoute=None)
    h5 = net.addHost('h5', cls=Host, ip='10.0.0.5', defaultRoute=None)
    h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)
    h11 = net.addHost('h11', cls=Host, ip='10.0.0.11', defaultRoute=None)
    h3 = net.addHost('h3', cls=Host, ip='10.0.0.3', defaultRoute=None)
    h13 = net.addHost('h13', cls=Host, ip='10.0.0.13', defaultRoute=None)

    info( '*** Add links\n')
    net.addLink(s1, s5)
    net.addLink(s1, s7)
    net.addLink(s1, s9)
    net.addLink(s1, s11)
    net.addLink(s2, s5)
    net.addLink(s2, s7)
    net.addLink(s2, s9)
    net.addLink(s2, s11)
    net.addLink(s3, s6)
    net.addLink(s3, s8)
    net.addLink(s3, s10)
    net.addLink(s3, s12)
    net.addLink(s4, s6)
    net.addLink(s4, s8)
    net.addLink(s4, s10)
    net.addLink(s4, s12)
    net.addLink(s5, s13)
    net.addLink(s6, s14)
    net.addLink(s6, s13)
    net.addLink(s5, s14)
    net.addLink(s7, s15)
    net.addLink(s8, s16)
    net.addLink(s8, s15)
    net.addLink(s7, s16)
    net.addLink(s9, s17)
    net.addLink(s10, s18)
    net.addLink(s10, s17)
    net.addLink(s9, s18)
    net.addLink(s11, s19)
    net.addLink(s12, s20)
    net.addLink(s12, s19)
    net.addLink(s11, s20)
    net.addLink(s13, h1)
    net.addLink(s13, h2)
    net.addLink(s14, h3)
    net.addLink(s14, h4)
    net.addLink(s15, h5)
    net.addLink(s15, h6)
    net.addLink(s16, h7)
    net.addLink(s16, h8)
    net.addLink(s17, h9)
    net.addLink(s17, h10)
    net.addLink(s18, h11)
    net.addLink(s18, h12)
    net.addLink(s19, h13)
    net.addLink(s19, h14)
    net.addLink(s20, h16)
    net.addLink(s20, h15)

    info( '*** Starting network\n')
    net.build()
    info( '*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    info( '*** Starting switches\n')
    net.get('s10').start([])
    net.get('s3').start([c0])
    net.get('s14').start([])
    net.get('s19').start([])
    net.get('s4').start([c0])
    net.get('s20').start([])
    net.get('s9').start([])
    net.get('s11').start([])
    net.get('s12').start([])
    net.get('s15').start([])
    net.get('s1').start([c0])
    net.get('s6').start([])
    net.get('s5').start([])
    net.get('s16').start([])
    net.get('s17').start([])
    net.get('s7').start([])
    net.get('s2').start([c0])
    net.get('s18').start([])
    net.get('s8').start([])
    net.get('s13').start([])

    info( '*** Post configure switches and hosts\n')

    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()
View Code

手动编写的fat-tree拓扑结构:

#!/usr/bin/env python

from mininet.cli import CLI
from mininet.node import Node
from mininet.link import Link
from mininet.net import Mininet
from mininet.node import RemoteController
from mininet.term import makeTerm


if '__main__' == __name__:
    net = Mininet(controller=RemoteController)

    c0 = net.addController('c0')

    s1 = net.addSwitch('s1')
    s2 = net.addSwitch('s2')
    s3 = net.addSwitch('s3')
    s4 = net.addSwitch('s4')
    s5 = net.addSwitch('s5')
    s6 = net.addSwitch('s6')
    s7 = net.addSwitch('s7')
    s8 = net.addSwitch('s8')
    s9 = net.addSwitch('s9')
    s10 = net.addSwitch('s10')
    s11 = net.addSwitch('s11')
    s12 = net.addSwitch('s12')
    s13 = net.addSwitch('s13')
    s14 = net.addSwitch('s14')
    s15 = net.addSwitch('s15')
    s16 = net.addSwitch('s16')
    s17 = net.addSwitch('s17')
    s18 = net.addSwitch('s18')
    s19 = net.addSwitch('s19')
    s20 = net.addSwitch('s20')

    h1 = net.addHost('h1')
    h2 = net.addHost('h2')
    h3 = net.addHost('h3')
    h4 = net.addHost('h4')
    h5 = net.addHost('h5')
    h6 = net.addHost('h6')
    h7 = net.addHost('h7')
    h8 = net.addHost('h8')
    h9 = net.addHost('h9')
    h10 = net.addHost('h10')
    h11 = net.addHost('h11')
    h12 = net.addHost('h12')
    h13 = net.addHost('h13')
    h14 = net.addHost('h14')
    h15 = net.addHost('h15')
    h16 = net.addHost('h16')

    Link(s1, s5)
    Link(s1, s7)
    Link(s1, s9)
    Link(s1, s11)
    Link(s2, s5)
    Link(s2, s7)
    Link(s2, s9)
    Link(s2, s11)
    Link(s3, s6)
    Link(s3, s8)
    Link(s3, s10)
    Link(s3, s12)
    Link(s2, s11)
    Link(s4, s6)
    Link(s4, s8)
    Link(s4, s10)
    Link(s4, s12)
    Link(s5, s13)
    Link(s5, s14)
    Link(s6, s13)
    Link(s6, s14)
    Link(s7, s15)
    Link(s7, s16)
    Link(s8, s15)
    Link(s8, s16)
    Link(s9, s17)
    Link(s9, s18)
    Link(s10, s17)
    Link(s10, s18)
    Link(s11, s19)
    Link(s11, s20)
    Link(s12, s19)
    Link(s12, s20)
    s = [s13, s14, s15, s16, s17, s18, s19, s20]
    h = [h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12, h13, h14, h15, h16]
    j = 0
    count = 0
    for i in range(0, len(s)):
        count = 0
        while count < 2:
            Link(s[i], h[j])
            j += 1
            count += 1

    net.build()
    c0.start()
    s1.start([c0])
    s2.start([c0])
    s3.start([c0])
    s4.start([c0])

 #   net.terms.append(makeTerm(c0))
 #   net.terms.append(makeTerm(s1))
 #   net.terms.append(makeTerm(s2))
 #   net.terms.append(makeTerm(s3))
 #   net.terms.append(makeTerm(h1))
 #   net.terms.append(makeTerm(h2))
 #   net.terms.append(makeTerm(h3))

    CLI(net)
    net.stop()
View Code

 开源fat-tree拓扑结构:

from mininet.net import Mininet
from mininet.node import Controller, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import Link, Intf, TCLink
from mininet.topo import Topo
from mininet.util import dumpNodeConnections
import logging
import os

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger( __name__ )

class HugeTopo(Topo):
    logger.debug("Class HugeTopo")
    CoreSwitchList = []
    AggSwitchList = []
    EdgeSwitchList = []
    HostList = []
    iNUMBER = 0

    def __init__(self):
        logger.debug("Class HugeTopo init")
        iNUMBER = 4
        
        self.iNUMBER = iNUMBER
        self.iCoreLayerSwitch = iNUMBER
        self.iAggLayerSwitch = iNUMBER * 2
        self.iEdgeLayerSwitch = iNUMBER * 2
        self.iHost = self.iEdgeLayerSwitch * 2

        Topo.__init__(self)

    def createTopo(self):
        logger.debug("Start create Core Layer Switch")
        self.createCoreLayerSwitch(self.iCoreLayerSwitch)
        logger.debug("Start create Agg Layer Switch")
        self.createAggLayerSwitch(self.iAggLayerSwitch)
        logger.debug("Start create Edge Layer Switch")
        self.createEdgeLayerSwitch(self.iEdgeLayerSwitch)
        logger.debug("Satrt create Host")
        self.createHost(self.iHost)

    def createCoreLayerSwitch(self, NUMBER):
        logger.debug("Create Core Layer")
        for x in range(1, NUMBER+1):
            PERFIX = "100"
            if x >= int(10):
                PERFIX = "10"
            self.CoreSwitchList.append(self.addSwitch(PERFIX + str(x)))
    
    def createAggLayerSwitch(self, NUMBER):
                logger.debug("Create Agg Layer")
                for x in range(1, NUMBER+1):
                        PERFIX = "200"
                        if x >= int(10):
                                PERFIX = "20"
                        self.AggSwitchList.append(self.addSwitch(PERFIX + str(x)))

    def createEdgeLayerSwitch(self, NUMBER):
                logger.debug("Create Edge Layer")
                for x in range(1, NUMBER+1):
                        PERFIX = "300"
                        if x >= int(10):
                                PERFIX = "30"
                        self.EdgeSwitchList.append(self.addSwitch(PERFIX + str(x)))
    def createHost(self, NUMBER):
                logger.debug("Create Host")
                for x in range(1, NUMBER+1):
                        PERFIX = "400"
                        if x >= int(10):
                                PERFIX = "40"
                        self.HostList.append(self.addHost(PERFIX + str(x)))
    
    def createLink(self):
        logger.debug("Create Core to Agg")
        for x in range(0, self.iAggLayerSwitch, 2):
            self.addLink(self.CoreSwitchList[0], self.AggSwitchList[x], bw=1000, loss=5)
            self.addLink(self.CoreSwitchList[1], self.AggSwitchList[x], bw=1000, loss=5)

        for x in range(1, self.iAggLayerSwitch, 2):
                        self.addLink(self.CoreSwitchList[2], self.AggSwitchList[x], bw=1000, loss=5)
                        self.addLink(self.CoreSwitchList[3], self.AggSwitchList[x], bw=1000, loss=5)

        logger.debug("Create Agg to Edge")
                for x in range(0, self.iAggLayerSwitch, 2):
                        self.addLink(self.AggSwitchList[x], self.EdgeSwitchList[x], bw=100)
                        self.addLink(self.AggSwitchList[x], self.EdgeSwitchList[x+1], bw=100)
            self.addLink(self.AggSwitchList[x+1], self.EdgeSwitchList[x], bw=100)
                        self.addLink(self.AggSwitchList[x+1], self.EdgeSwitchList[x+1], bw=100)
        
        logger.debug("Create Edge to Host")
                for x in range(0, self.iEdgeLayerSwitch):
                        self.addLink(self.EdgeSwitchList[x], self.HostList[2*x])
                        self.addLink(self.EdgeSwitchList[x], self.HostList[2*x+1])

def enableSTP():
    for x in range(1, 5):
        cmd = "ovs-vsctl set Bridge %s stp_enable=true" % ("100" + str(x))
        os.system(cmd)
        print cmd
    for x in range(1, 9):
        cmd = "ovs-vsctl set Bridge %s stp_enable=true" % ("200" + str(x))
                os.system(cmd)
                print cmd
        cmd = "ovs-vsctl set Bridge %s stp_enable=true" % ("300" + str(x))
                os.system(cmd)
                print cmd

def iperfTest(net, topo):
    logger.debug("Start iperfTest:")
    h1000, h1015, h1016 = net.get(topo.HostList[0], topo.HostList[14], topo.HostList[15])
        
    h1000.popen('iperf -s -u -i 1 > iperf_server_differentPod_result', shell=True)
    h1015.popen('iperf -s -u -i 1 > iperf_server_samePod_result', shell=True)
    h1016.cmdPrint('iperf -c' + h1000.IP() + ' -u -t 10 -i 1 -b 100m')
    h1016.cmdPrint('iperf -c' + h1015.IP() + ' -u -t 10 -i 1 -b 100m')

def pingTest(net):
    logger.debug("Start Test all network")
    net.pingAll()

def createTopo():
    logging.debug("LV1 Create HugeTopo")
    topo = HugeTopo()
    topo.createTopo()
    topo.createLink()

    logging.debug("LV1 Start Mininet")
    CONTROLLER_IP = "192.168.116.128"
    CONTROLLER_PORT = 6633
    net = Mininet(topo=topo, link=TCLink, controller=None)
    net.addController('controller', controller=RemoteController,ip=CONTROLLER_IP, port=CONTROLLER_PORT)
    net.start()

    logger.debug("LV1 dumpNode")
    enableSTP()
    dumpNodeConnections(net.hosts)
        
    pingTest(net)
    iperfTest(net, topo)
        
    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel('info')
    if os.getuid() != 0:
        logger.debug("You are not root")
    elif os.getuid() == 0:
        createTopo()
View Code

 

posted @ 2016-10-26 17:01  CoolRandy  阅读(490)  评论(0编辑  收藏  举报