diff --git a/bin/mn b/bin/mn index 0424d35..474a3c2 100755 --- a/bin/mn +++ b/bin/mn @@ -226,8 +226,6 @@ class MininetRunner( object ): start = time.time() topo = buildTopo( TOPOS, self.options.topo ) - if self.options.nat: - topo.addNAT() switch = customConstructor( SWITCHES, self.options.switch ) host = customConstructor( HOSTS, self.options.host ) controller = customConstructor( CONTROLLERS, self.options.controller ) @@ -255,6 +253,10 @@ class MininetRunner( object ): autoStaticArp=arp, autoPinCpus=pin, listenPort=listenPort ) + if self.options.nat: + nat = mn.addNAT() + mn.configHosts() + if self.options.pre: CLI( mn, script=self.options.pre ) diff --git a/examples/natnet.py b/examples/natnet.py index 718ca42..cb0c44c 100755 --- a/examples/natnet.py +++ b/examples/natnet.py @@ -56,7 +56,7 @@ class InternetTopo(Topo): defaultRoute='via %s' % localIP) self.addLink(host, switch) -def perfTest(): +def run(): "Create network and run simple performance test" topo = InternetTopo() net = Mininet(topo=topo) @@ -66,4 +66,4 @@ def perfTest(): if __name__ == '__main__': setLogLevel('info') - perfTest() \ No newline at end of file + run() \ No newline at end of file diff --git a/mininet/net.py b/mininet/net.py index f2f49b3..1912fba 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -227,14 +227,18 @@ class Mininet( object ): self.nameToNode[ name ] = controller_new return controller_new - # TODO: incomplete - def addNAT( self, name='nat0', connect=True, **params ): - nat = self.addHost( name, cls=NAT, **params ) + def addNAT( self, name='nat0', connect=True, inNamespace=False, **params ): + nat = self.addHost( name, cls=NAT, inNamespace=inNamespace, + subnet=self.ipBase, **params ) # find first switch and create link - print "******* &&&&&& net/addNAT" if connect: - #connect the nat to the first switch + # connect the nat to the first switch self.addLink( nat, self.switches[ 0 ] ) + # set the default route on hosts + natIP = nat.params[ 'ip' ].split('/')[ 0 ] + for host in self.hosts: + if host.inNamespace: + host.setDefaultRoute( 'via %s' % natIP ) return nat # BL: We now have four ways to look up nodes diff --git a/mininet/node.py b/mininet/node.py index d6e8f87..edc6de8 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -1253,7 +1253,7 @@ class NAT( Node ): inetIntf: interface for internet access subnet: Mininet subnet (default 10.0/8)=""" self.inetIntf = inetIntf - self.subnet = subnet #TODO: get subnet from Mininet directly + self.subnet = subnet self.localIntf = localIntf def config( self, **params ): @@ -1263,13 +1263,6 @@ class NAT( Node ): if not self.localIntf: self.localIntf = self.defaultIntf() - #------------------------- - print "inetIntf:", self.inetIntf - print "subnet:", self.subnet - # Identify the interface connecting to the mininet network - print "LocalIntf:", self.localIntf - #------------------------- - self.cmd( 'sysctl net.ipv4.ip_forward=0' ) # Flush any currently active rules