Skip to content

Instantly share code, notes, and snippets.

@roghnin
Created April 11, 2016 08:01
Show Gist options
  • Save roghnin/6e43c2a3441bdb7c3556269c31976caa to your computer and use it in GitHub Desktop.
Save roghnin/6e43c2a3441bdb7c3556269c31976caa to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
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
from mininet.node import Host
from mininet.node import Switch
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
#Init Topo
Topo.__init__(self)
def createTopo(self):
logger.debug("Start create Core Layer Swich")
self.createCoreLayerSwitch(self.iCoreLayerSwitch)
logger.debug("Start create Agg Layer Swich ")
self.createAggLayerSwitch(self.iAggLayerSwitch)
logger.debug("Start create Edge Layer Swich ")
self.createEdgeLayerSwitch(self.iEdgeLayerSwitch)
logger.debug("Start create Host")
self.createHost(self.iHost)
"""
Create Switch and Host
"""
def createCoreLayerSwitch(self, NUMBER):
logger.debug("Create Core Layer")
for x in range(1, NUMBER+1):
PREFIX = "s10"
if x >= int(10):
PREFIX = "s1"
self.CoreSwitchList.append(self.addSwitch(PREFIX + str(x)))
def createAggLayerSwitch(self, NUMBER):
logger.debug( "Create Agg Layer")
for x in range(1, NUMBER+1):
PREFIX = "s20"
if x >= int(10):
PREFIX = "s2"
self.AggSwitchList.append(self.addSwitch(PREFIX + str(x)))
def createEdgeLayerSwitch(self, NUMBER):
logger.debug("Create Edge Layer")
for x in range(1, NUMBER+1):
PREFIX = "s30"
if x >= int(10):
PREFIX = "s3"
self.EdgeSwitchList.append(self.addSwitch(PREFIX + str(x)))
def createHost(self, NUMBER):
logger.debug("Create Host")
for x in range(1, NUMBER+1):
PREFIX = "h0"
if x >= int(10):
PREFIX = "h"
self.HostList.append(self.addHost(PREFIX + str(x)))
"""
Create Link
"""
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)
self.addLink(self.CoreSwitchList[1], self.AggSwitchList[x], bw=1000)
for x in range(1, self.iAggLayerSwitch, 2):
self.addLink(self.CoreSwitchList[2], self.AggSwitchList[x], bw=1000)
self.addLink(self.CoreSwitchList[3], self.AggSwitchList[x], bw=1000)
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):
## limit = 2 * x + 1
self.addLink(self.EdgeSwitchList[x], self.HostList[2 * x])
self.addLink(self.EdgeSwitchList[x], self.HostList[2 * x + 1])
def enableSTP():
"""
//HATE: Dirty Code
"""
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])
#iperf Server
h1000.popen('iperf -s -u -i 1 > iperf_server_differentPod_result', shell=True)
#iperf Server
h1015.popen('iperf -s -u -i 1 > iperf_server_samePod_result', shell=True)
#iperf Client
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 = "127.0.0.1"
CONTROLLER_PORT = 6653
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)
#h4001=Host('4001').MAC()
#print "h4001:",h4001
#s3001=net.get('3001')
#print s3001.MAC()
#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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment