diff --git a/mininet/net.py b/mininet/net.py index 87f7b47..64174fc 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -112,8 +112,7 @@ class Mininet( object ): build=True, xterms=False, cleanup=False, ipBase='10.0.0.0/8', inNamespace=False, autoSetMacs=False, autoStaticArp=False, autoPinCpus=False, - listenPort=None, - gateway=None ): + listenPort=None ): """Create Mininet object. topo: Topo (topology) object or None switch: default Switch class @@ -130,8 +129,7 @@ class Mininet( object ): autoStaticArp: set all-pairs static MAC addrs? autoPinCpus: pin hosts to (real) cores (requires CPULimitedHost)? listenPort: base listening port to open; will be incremented for - each additional switch in the net if inNamespace=False - gateway: node that provides connectivity to the Internet""" + each additional switch in the net if inNamespace=False""" self.topo = topo self.switch = switch self.host = host @@ -150,7 +148,6 @@ class Mininet( object ): self.numCores = numCores() self.nextCore = 0 # next core for pinning hosts to CPUs self.listenPort = listenPort - self.gateway = gateway self.hosts = [] self.switches = [] @@ -166,9 +163,6 @@ class Mininet( object ): if topo and build: self.build() - # list of Node subclasses that provide gateway service - gateways = [ NAT ] - def addHost( self, name, cls=None, **params ): """Add host. name: name of host to add @@ -189,14 +183,13 @@ class Mininet( object ): defaults.update( params ) # TODO: clean this up if params.get( 'isNAT', False ): + print "***** &&&&&& !!!! nat nat nat" cls = NAT if not cls: cls = self.host h = cls( name, **defaults ) self.hosts.append( h ) self.nameToNode[ name ] = h - if cls in self.gateways: - self.gateway = h return h def addSwitch( self, name, cls=None, **params ): @@ -242,7 +235,7 @@ class Mininet( object ): def addNAT( self, name='nat0', connect=True, **params ): nat = self.addHost( name, cls=NAT, **params ) # find first switch and create link - print "net/addNAT" + print "******* &&&&&& net/addNAT" if connect: #connect the nat to the first switch self.addLink( nat, self.switches[ 0 ] ) @@ -326,6 +319,7 @@ class Mininet( object ): host.cmd( 'ifconfig lo up' ) info( '\n' ) + ''' TODO: remove this! def configGateway( self ): """Add gateway routes to all hosts if the networks has a gateway.""" if self.gateway: @@ -338,6 +332,7 @@ class Mininet( object ): else: # Don't mess with hosts in the root namespace pass + ''' def buildFromTopo( self, topo=None ): """Build mininet from a topology object @@ -397,7 +392,8 @@ class Mininet( object ): self.startTerms() if self.autoStaticArp: self.staticArp() - self.configGateway() + # TODO: remove this + #self.configGateway() self.built = True def startTerms( self ): diff --git a/mininet/node.py b/mininet/node.py index f572993..d6e8f87 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -1246,7 +1246,7 @@ class RemoteController( Controller ): class NAT( Node ): """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 ) """Start NAT/forwarding between Mininet and external network @@ -1254,13 +1254,22 @@ class NAT( Node ): subnet: Mininet subnet (default 10.0/8)=""" self.inetIntf = inetIntf 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 ) """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 - localIntf = self.defaultIntf() + print "LocalIntf:", self.localIntf + #------------------------- + self.cmd( 'sysctl net.ipv4.ip_forward=0' ) # Flush any currently active rules @@ -1274,8 +1283,8 @@ class NAT( Node ): self.cmd( 'iptables -P FORWARD DROP' ) # Configure NAT - self.cmd( 'iptables -I FORWARD -i', localIntf, '-d', self.subnet, '-j DROP' ) - self.cmd( 'iptables -A FORWARD -i', localIntf, '-s', self.subnet, '-j ACCEPT' ) + self.cmd( 'iptables -I FORWARD -i', self.localIntf, '-d', self.subnet, '-j DROP' ) + 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 -t nat -A POSTROUTING -o ', self.inetIntf, '-j MASQUERADE' ) @@ -1284,7 +1293,7 @@ class NAT( Node ): # Prevent network-manager from messing with our interface # by specifying manual configuration in /etc/network/interfaces - intf = localIntf + intf = self.localIntf cfile = '/etc/network/interfaces' line = '\niface %s inet manual\n' % intf config = open( cfile ).read() diff --git a/mininet/topo.py b/mininet/topo.py index 9de6eff..4ae447c 100644 --- a/mininet/topo.py +++ b/mininet/topo.py @@ -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.node import NAT class MultiGraph( object ): "Utility class to track nodes and edges - replaces networkx.Graph" @@ -89,11 +90,12 @@ class Topo(object): result = self.addNode(name, isSwitch=True, **opts) 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. name: NAT name 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: # connect the NAT to the first switch self.addLink(name, self.switches()[ 0 ])