Skip to content

Instantly share code, notes, and snippets.

@lantz
Last active December 27, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lantz/7287376 to your computer and use it in GitHub Desktop.
Save lantz/7287376 to your computer and use it in GitHub Desktop.
In Mininet (up to 2.1.0 at least) you cannot have multiple Mininet instances running without running into namespace conflicts for the nodes and port conflicts for local controllers. This example demonstrates an easy way to specify a unique prefix for the nodes and a unique port for the controller, thus allowing multiple Mininet instances to coex…
#!/usr/bin/python
"""
Example of adding a node prefix to avoid namespace conflicts
(and changing the controller port to avoid port conflicts.)
If we use UserSwitch, then we can run in a Mininet host (!)
"""
from mininet.net import Mininet
from mininet.topo import Topo, LinearTopo
from mininet.node import Controller, UserSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel
from sys import argv, exit
from functools import partial
class PrefixTopoMixin( Topo ):
"Mix-in for adding node prefixes"
prefix = ''
def addNode( self, name, **opts):
name = self.prefix + name
return super( PrefixTopoMixin, self).addNode( name, **opts )
class PrefixLinearTopo( PrefixTopoMixin, LinearTopo ):
"Linear topo with node prefixes"
pass
try:
prefix = argv[ 1 ]
port = int( argv[ 2 ] )
except:
print 'usage: %s <node-prefix> <controller-port>' % argv[ 0 ]
exit( 1 )
setLogLevel( 'info' )
PrefixLinearTopo.prefix = prefix
net = Mininet( topo=PrefixLinearTopo( 3 ), switch=UserSwitch,
controller=partial( Controller, port=port ) )
net.start()
CLI( net )
net.stop()
@lantz
Copy link
Author

lantz commented Nov 3, 2013

This is just off the top of my head - it could certainly be improved in a variety of ways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment