Skip to content

Instantly share code, notes, and snippets.

@pichuang
Created June 9, 2014 01:43
Show Gist options
  • Save pichuang/1cf0e3a022f4b51503f2 to your computer and use it in GitHub Desktop.
Save pichuang/1cf0e3a022f4b51503f2 to your computer and use it in GitHub Desktop.
Programming Assignment 2: Using Mininet and Mininet Python API
#!/usr/bin/env python
'''
Coursera:
- Software Defined Networking (SDN) course
-- Programming Assignment 2
Professor: Nick Feamster
Teaching Assistant: Arpit Gupta, Muhammad Shahbaz
'''
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.util import irange,dumpNodeConnections
from mininet.log import setLogLevel
from CustomTopo import *
class CustomTopo(Topo):
"Simple Data Center Topology"
"linkopts - (1:core, 2:aggregation, 3: edge) parameters"
"fanout - number of child switch per parent switch"
setLogLevel("info")
def __init__(self, linkopts1, linkopts2, linkopts3, fanout=2, **opts):
#logger.info("Initialize topology and default options")
Topo.__init__(self, **opts)
self.fanout = fanout
core_sw = self.addSwitch('c1')
#logger.debug("create core switch: " + core_sw)
for i in irange(1, fanout):
#logger.debug("create aggregation switch: " + str(i))
aggregation_sw = self.addSwitch('a%s' %i)
self.addLink(core_sw, aggregation_sw, **linkopts1)
for j in irange(1, fanout):
#logger.debug("create edge switch: " + str(j))
edge_sw_number = fanout * (i-1) + j
edge_sw = self.addSwitch('e%s' %edge_sw_number)
self.addLink(aggregation_sw, edge_sw, **linkopts2)
for k in irange(1, fanout):
#logger.debug("create host: " + str(k))
host_number = fanout * (edge_sw_number - 1 )+ k
host = self.addHost('h%s' %host_number)
self.addLink(edge_sw, host, **linkopts3)
topos = { 'custom': ( lambda: CustomTopo() ) }
'''
def SimpleTest():
"Set up link parameters"
print "a. Setting link parameters"
"--- core to aggregation switches"
linkopts1 = {'bw':50, 'delay':'5ms'}
"--- aggregation to edge switches"
linkopts2 = {'bw':30, 'delay':'10ms'}
"--- edge switches to hosts"
linkopts3 = {'bw':10, 'delay':'15ms'}
"Creating network and run simple performance test"
print "b. Creating Custom Topology"
topo = CustomTopo(linkopts1, linkopts2, linkopts3, fanout=3)
print "c. Firing up Mininet"
net = Mininet(topo=topo, link=TCLink)
net.start()
dumpNodeConnections(net.hosts)
h1 = net.get('h1')
h27 = net.get('h27')
print "d. Starting Test"
# Start pings
outputString = h1.cmd('ping', '-c6', h27.IP())
print "e. Stopping Mininet"
net.stop()
if __name__ == '__main__':
#logging.basicConfig(level=logging.DEBUG)
#logger = logging.getLogger("sdn2")
SimpleTest()
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment