undoing gateway in net and removing addNAT helpers

This commit is contained in:
Brian O'Connor
2013-10-03 15:15:20 -07:00
parent bceb298edb
commit 3f2355a36a
3 changed files with 27 additions and 20 deletions
+8 -12
View File
@@ -112,8 +112,7 @@ class Mininet( object ):
build=True, xterms=False, cleanup=False, ipBase='10.0.0.0/8', build=True, xterms=False, cleanup=False, ipBase='10.0.0.0/8',
inNamespace=False, inNamespace=False,
autoSetMacs=False, autoStaticArp=False, autoPinCpus=False, autoSetMacs=False, autoStaticArp=False, autoPinCpus=False,
listenPort=None, listenPort=None ):
gateway=None ):
"""Create Mininet object. """Create Mininet object.
topo: Topo (topology) object or None topo: Topo (topology) object or None
switch: default Switch class switch: default Switch class
@@ -130,8 +129,7 @@ class Mininet( object ):
autoStaticArp: set all-pairs static MAC addrs? autoStaticArp: set all-pairs static MAC addrs?
autoPinCpus: pin hosts to (real) cores (requires CPULimitedHost)? autoPinCpus: pin hosts to (real) cores (requires CPULimitedHost)?
listenPort: base listening port to open; will be incremented for listenPort: base listening port to open; will be incremented for
each additional switch in the net if inNamespace=False each additional switch in the net if inNamespace=False"""
gateway: node that provides connectivity to the Internet"""
self.topo = topo self.topo = topo
self.switch = switch self.switch = switch
self.host = host self.host = host
@@ -150,7 +148,6 @@ class Mininet( object ):
self.numCores = numCores() self.numCores = numCores()
self.nextCore = 0 # next core for pinning hosts to CPUs self.nextCore = 0 # next core for pinning hosts to CPUs
self.listenPort = listenPort self.listenPort = listenPort
self.gateway = gateway
self.hosts = [] self.hosts = []
self.switches = [] self.switches = []
@@ -166,9 +163,6 @@ class Mininet( object ):
if topo and build: if topo and build:
self.build() self.build()
# list of Node subclasses that provide gateway service
gateways = [ NAT ]
def addHost( self, name, cls=None, **params ): def addHost( self, name, cls=None, **params ):
"""Add host. """Add host.
name: name of host to add name: name of host to add
@@ -189,14 +183,13 @@ class Mininet( object ):
defaults.update( params ) defaults.update( params )
# TODO: clean this up # TODO: clean this up
if params.get( 'isNAT', False ): if params.get( 'isNAT', False ):
print "***** &&&&&& !!!! nat nat nat"
cls = NAT cls = NAT
if not cls: if not cls:
cls = self.host cls = self.host
h = cls( name, **defaults ) h = cls( name, **defaults )
self.hosts.append( h ) self.hosts.append( h )
self.nameToNode[ name ] = h self.nameToNode[ name ] = h
if cls in self.gateways:
self.gateway = h
return h return h
def addSwitch( self, name, cls=None, **params ): def addSwitch( self, name, cls=None, **params ):
@@ -242,7 +235,7 @@ class Mininet( object ):
def addNAT( self, name='nat0', connect=True, **params ): def addNAT( self, name='nat0', connect=True, **params ):
nat = self.addHost( name, cls=NAT, **params ) nat = self.addHost( name, cls=NAT, **params )
# find first switch and create link # find first switch and create link
print "net/addNAT" print "******* &&&&&& net/addNAT"
if connect: if connect:
#connect the nat to the first switch #connect the nat to the first switch
self.addLink( nat, self.switches[ 0 ] ) self.addLink( nat, self.switches[ 0 ] )
@@ -326,6 +319,7 @@ class Mininet( object ):
host.cmd( 'ifconfig lo up' ) host.cmd( 'ifconfig lo up' )
info( '\n' ) info( '\n' )
''' TODO: remove this!
def configGateway( self ): def configGateway( self ):
"""Add gateway routes to all hosts if the networks has a gateway.""" """Add gateway routes to all hosts if the networks has a gateway."""
if self.gateway: if self.gateway:
@@ -338,6 +332,7 @@ class Mininet( object ):
else: else:
# Don't mess with hosts in the root namespace # Don't mess with hosts in the root namespace
pass pass
'''
def buildFromTopo( self, topo=None ): def buildFromTopo( self, topo=None ):
"""Build mininet from a topology object """Build mininet from a topology object
@@ -397,7 +392,8 @@ class Mininet( object ):
self.startTerms() self.startTerms()
if self.autoStaticArp: if self.autoStaticArp:
self.staticArp() self.staticArp()
self.configGateway() # TODO: remove this
#self.configGateway()
self.built = True self.built = True
def startTerms( self ): def startTerms( self ):
+15 -6
View File
@@ -1246,7 +1246,7 @@ class RemoteController( Controller ):
class NAT( Node ): class NAT( Node ):
"""NAT: Provides connectivity to external network""" """NAT: Provides connectivity to external network"""
def __init__( self, name, inetIntf='eth0', subnet='10.0/8', **params): def __init__( self, name, inetIntf='eth0', subnet='10.0/8', localIntf=None, **params):
super( NAT, self ).__init__( name, **params ) super( NAT, self ).__init__( name, **params )
"""Start NAT/forwarding between Mininet and external network """Start NAT/forwarding between Mininet and external network
@@ -1254,13 +1254,22 @@ class NAT( Node ):
subnet: Mininet subnet (default 10.0/8)=""" subnet: Mininet subnet (default 10.0/8)="""
self.inetIntf = inetIntf self.inetIntf = inetIntf
self.subnet = subnet #TODO: get subnet from Mininet directly self.subnet = subnet #TODO: get subnet from Mininet directly
self.localIntf = localIntf
def config( self, inetIntf='eth0', subnet='10.0/8', **params ): def config( self, **params ):
super( NAT, self).config( **params ) super( NAT, self).config( **params )
"""Configure the NAT and iptables""" """Configure the NAT and iptables"""
if not self.localIntf:
self.localIntf = self.defaultIntf()
#-------------------------
print "inetIntf:", self.inetIntf
print "subnet:", self.subnet
# Identify the interface connecting to the mininet network # Identify the interface connecting to the mininet network
localIntf = self.defaultIntf() print "LocalIntf:", self.localIntf
#-------------------------
self.cmd( 'sysctl net.ipv4.ip_forward=0' ) self.cmd( 'sysctl net.ipv4.ip_forward=0' )
# Flush any currently active rules # Flush any currently active rules
@@ -1274,8 +1283,8 @@ class NAT( Node ):
self.cmd( 'iptables -P FORWARD DROP' ) self.cmd( 'iptables -P FORWARD DROP' )
# Configure NAT # Configure NAT
self.cmd( 'iptables -I FORWARD -i', localIntf, '-d', self.subnet, '-j DROP' ) self.cmd( 'iptables -I FORWARD -i', self.localIntf, '-d', self.subnet, '-j DROP' )
self.cmd( 'iptables -A FORWARD -i', localIntf, '-s', self.subnet, '-j ACCEPT' ) self.cmd( 'iptables -A FORWARD -i', self.localIntf, '-s', self.subnet, '-j ACCEPT' )
self.cmd( 'iptables -A FORWARD -i', self.inetIntf, '-d', self.subnet, '-j ACCEPT' ) self.cmd( 'iptables -A FORWARD -i', self.inetIntf, '-d', self.subnet, '-j ACCEPT' )
self.cmd( 'iptables -t nat -A POSTROUTING -o ', self.inetIntf, '-j MASQUERADE' ) self.cmd( 'iptables -t nat -A POSTROUTING -o ', self.inetIntf, '-j MASQUERADE' )
@@ -1284,7 +1293,7 @@ class NAT( Node ):
# Prevent network-manager from messing with our interface # Prevent network-manager from messing with our interface
# by specifying manual configuration in /etc/network/interfaces # by specifying manual configuration in /etc/network/interfaces
intf = localIntf intf = self.localIntf
cfile = '/etc/network/interfaces' cfile = '/etc/network/interfaces'
line = '\niface %s inet manual\n' % intf line = '\niface %s inet manual\n' % intf
config = open( cfile ).read() config = open( cfile ).read()
+4 -2
View File
@@ -12,6 +12,7 @@ setup for testing, and can even be emulated with the Mininet package.
''' '''
from mininet.util import irange, natural, naturalSeq from mininet.util import irange, natural, naturalSeq
from mininet.node import NAT
class MultiGraph( object ): class MultiGraph( object ):
"Utility class to track nodes and edges - replaces networkx.Graph" "Utility class to track nodes and edges - replaces networkx.Graph"
@@ -89,11 +90,12 @@ class Topo(object):
result = self.addNode(name, isSwitch=True, **opts) result = self.addNode(name, isSwitch=True, **opts)
return result return result
def addNAT(self, name='nat', connect=True, **opts): def addNAT(self, name='nat', connect=True, inNamespace=False, **opts):
"""Convenience method: Add NAT to graph. """Convenience method: Add NAT to graph.
name: NAT name name: NAT name
connect: True will automatically connect to the first switch""" connect: True will automatically connect to the first switch"""
nat = self.addNode(name, isNAT=True, inNamespace=False) #nat = self.addNode(name, isNAT=True, inNamespace=False)
nat = self.addNode(name, cls=NAT, inNamespace=inNamespace, hosts=self.hosts(), **opts)
if connect: if connect:
# connect the NAT to the first switch # connect the NAT to the first switch
self.addLink(name, self.switches()[ 0 ]) self.addLink(name, self.switches()[ 0 ])