Skip to content

Instantly share code, notes, and snippets.

@John-Lin
Last active January 19, 2022 01:14
Show Gist options
  • Save John-Lin/961156c1c6dcac545b41 to your computer and use it in GitHub Desktop.
Save John-Lin/961156c1c6dcac545b41 to your computer and use it in GitHub Desktop.
Mininet Simple Topology
#!/usr/bin/python
from mininet.topo import Topo
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel
from mininet.node import RemoteController
REMOTE_CONTROLLER_IP = "127.0.0.1"
def simpleTest():
# Create and test a simple network
topo = SingleLoopTopo()
net = Mininet(topo=topo,
controller=None,
autoStaticArp=True)
net.addController("c0",
controller=RemoteController,
ip=REMOTE_CONTROLLER_IP,
port=6633)
net.start()
print "Dumping host connections"
dumpNodeConnections(net.hosts)
print "Testing network connectivity"
net.pingAll()
net.stop()
class SingleLoopTopo(Topo):
# Single switch connected to n hosts
def __init__(self, **opts):
# Initialize topology and default optioe
Topo.__init__(self, **opts)
switches = []
hosts = []
# create switches
for s in range(3):
switches.append(self.addSwitch('s%s' % (s + 1), protocols='OpenFlow13'))
# create hosts
for h in range(4):
hosts.append(self.addHost('h%s' % (h + 1)))
self.addLink(hosts[0], switches[1])
self.addLink(hosts[1], switches[1])
self.addLink(hosts[2], switches[2])
self.addLink(hosts[3], switches[2])
self.addLink(switches[0], switches[1])
self.addLink(switches[0], switches[2])
self.addLink(switches[1], switches[2])
if __name__ == '__main__':
# Tell mininet to print useful information
setLogLevel('info')
simpleTest()
topo = SingleLoopTopo()
net = Mininet(topo=topo,
controller=None,
autoStaticArp=True)
net.addController("c0",
controller=RemoteController,
ip=REMOTE_CONTROLLER_IP,
port=6633)
net.start()
CLI(net)
net.stop()
#!/usr/bin/python
from mininet.topo import Topo
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel
from mininet.node import RemoteController
# Traffic Control
from mininet.link import TCLink
REMOTE_CONTROLLER_IP = "127.0.0.1"
def simpleTest():
# Create and test a simple network
topo = SingleSwitchTopo(n=4)
net = Mininet(topo=topo, controller=RemoteController, autoStaticArp=True)
net.addController("c0", controller=RemoteController,
ip=REMOTE_CONTROLLER_IP, port=6633)
net.start()
print "Dumping host connections"
dumpNodeConnections(net.hosts)
print "Testing network connectivity"
net.pingAll()
net.stop()
def perfTest():
# Create network and run simple performance test
topo = SingleSwitchTopo(n=4)
net = Mininet(topo=topo, link=TCLink,
controller=RemoteController,
autoStaticArp=True)
net.addController("c0",
controller=RemoteController,
ip=REMOTE_CONTROLLER_IP,
port=6633)
net.start()
print "Dumping host connections"
dumpNodeConnections(net.hosts)
print "Testing network connectivity"
# net.pingAll()
print "Testing bandwidth between h1 and h4"
h1, h4 = net.get('h1', 'h4')
net.iperf((h1, h4), l4Type='UDP')
net.stop()
class SingleSwitchTopo(Topo):
# Single switch connected to n hosts
def __init__(self, n=2, **opts):
# Initialize topology and default options
Topo.__init__(self, **opts)
switch = self.addSwitch('s1', protocols='OpenFlow13')
# Python's range(N) generates 0..N-1
for h in range(n):
host = self.addHost('h%s' % (h + 1))
# self.addLink(host, switch, bw=10, delay='5ms', loss=10)
self.addLink(host, switch, bw=10)
# topos = {'singleswitchtopo': (lambda: SingleSwitchTopo(n=4))}
if __name__ == '__main__':
# Tell mininet to print useful information
setLogLevel('info')
# simpleTest()
# perfTest()
topo = SingleSwitchTopo(n=4)
net = Mininet(topo=topo, link=TCLink,
controller=None,
autoStaticArp=True)
net.addController("c0",
controller=RemoteController,
ip=REMOTE_CONTROLLER_IP,
port=6633)
net.start()
print "Dumping host connections"
dumpNodeConnections(net.hosts)
CLI(net)
net.stop()
# sudo mn --custom ./single_switch.py --topo singleswitchtopo --controller=remote,ip=127.0.0.1
"""Custom topology example
Two directly connected switches plus a host for each switch:
host --- switch --- switch --- host
Adding the 'topos' dict with a key/value pair to generate our newly defined
topology enables one to pass in '--topo=mytopo' from the command line.
"""
from mininet.topo import Topo
class MyTopo(Topo):
def __init__(self):
# Initialize topology
Topo.__init__(self)
# Add hosts and switches
leftHost = self.addHost('h1')
rightHost = self.addHost('h2')
leftSwitch = self.addSwitch('s3')
rightSwitch = self.addSwitch('s4')
# Add links
self.addLink(leftHost, leftSwitch)
self.addLink(leftSwitch, rightSwitch)
self.addLink(rightSwitch, rightHost)
topos = {'mytopo': (lambda: MyTopo())}
# sudo mn --custom ~/mininet/custom/topo-2sw-2host.py --topo mytopo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment