diff --git a/examples/natnet.py b/examples/natnet.py index 9fcc4bfd52e8f20d334a8e474d4b0623c9faaa37..c2fe0077b64ee7b570e635d407560ff14331a6f3 100755 --- a/examples/natnet.py +++ b/examples/natnet.py @@ -20,7 +20,7 @@ from mininet.topo import Topo from mininet.net import Mininet -from mininet.node import NAT +from mininet.nodelib import NAT from mininet.log import setLogLevel from mininet.cli import CLI from mininet.util import irange diff --git a/mininet/net.py b/mininet/net.py index 42ab0a70447a93acac30f4e0352cf6035e671c7b..880780401847226905b183a4ddf1a791c3b70f0e 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -96,7 +96,8 @@ from mininet.cli import CLI from mininet.log import info, error, debug, output, warn -from mininet.node import Host, OVSKernelSwitch, DefaultController, Controller, NAT +from mininet.node import Host, OVSKernelSwitch, DefaultController, Controller +from mininet.nodelib import NAT from mininet.link import Link, Intf from mininet.util import quietRun, fixLimits, numCores, ensureRoot from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd diff --git a/mininet/node.py b/mininet/node.py index 4e50fad38a009a3740c918c0fc1b0cc282970b47..fec711795aac3dfb924c8bdf6544ce16b5a13141 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -41,8 +41,6 @@ arbitrary OpenFlow-compatible controller, and which is not created or managed by mininet. -TODO: NAT - Future enhancements: - Possibly make Node, Switch and Controller more abstract so that @@ -1362,71 +1360,3 @@ def DefaultController( name, order=[ Controller, OVSController ], **kwargs ): for controller in order: if controller.isAvailable(): return controller( name, **kwargs ) - -class NAT( Node ): - """NAT: Provides connectivity to external network""" - - 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 - inetIntf: interface for internet access - subnet: Mininet subnet (default 10.0/8)=""" - self.inetIntf = inetIntf - self.subnet = subnet - self.localIntf = localIntf - - def config( self, **params ): - super( NAT, self).config( **params ) - """Configure the NAT and iptables""" - - if not self.localIntf: - self.localIntf = self.defaultIntf() - - self.cmd( 'sysctl net.ipv4.ip_forward=0' ) - - # Flush any currently active rules - # TODO: is this safe? - self.cmd( 'iptables -F' ) - self.cmd( 'iptables -t nat -F' ) - - # Create default entries for unmatched traffic - self.cmd( 'iptables -P INPUT ACCEPT' ) - self.cmd( 'iptables -P OUTPUT ACCEPT' ) - self.cmd( 'iptables -P FORWARD DROP' ) - - # Configure NAT - 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' ) - - # Instruct the kernel to perform forwarding - self.cmd( 'sysctl net.ipv4.ip_forward=1' ) - - # Prevent network-manager from messing with our interface - # by specifying manual configuration in /etc/network/interfaces - intf = self.localIntf - cfile = '/etc/network/interfaces' - line = '\niface %s inet manual\n' % intf - config = open( cfile ).read() - if ( line ) not in config: - info( '*** Adding "' + line.strip() + '" to ' + cfile ) - with open( cfile, 'a' ) as f: - f.write( line ) - # Probably need to restart network-manager to be safe - - # hopefully this won't disconnect you - self.cmd( 'service network-manager restart' ) - - def terminate( self ): - """Stop NAT/forwarding between Mininet and external network""" - # Flush any currently active rules - # TODO: is this safe? - self.cmd( 'iptables -F' ) - self.cmd( 'iptables -t nat -F' ) - - # Instruct the kernel to stop forwarding - self.cmd( 'sysctl net.ipv4.ip_forward=0' ) - - super( NAT, self ).terminate() - diff --git a/mininet/nodelib.py b/mininet/nodelib.py index 2eb80465cd005c28c9a2aaa86aa920b2cc04c02a..1760c7b30b761cf143e1f04d71bc3b6714ea9ec4 100644 --- a/mininet/nodelib.py +++ b/mininet/nodelib.py @@ -1,15 +1,12 @@ """ Node Library for Mininet -This contains additional Node types which you may find to be useful +This contains additional Node types which you may find to be useful. """ -from mininet.net import Mininet -from mininet.topo import Topo -from mininet.node import Switch +from mininet.node import Node, Switch from mininet.log import setLogLevel, info - class LinuxBridge( Switch ): "Linux Bridge (with optional spanning tree)" @@ -49,3 +46,70 @@ def stop( self ): self.cmd( 'ifconfig', self, 'down' ) self.cmd( 'brctl delbr', self ) +class NAT( Node ): + """NAT: Provides connectivity to external network""" + + 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 + inetIntf: interface for internet access + subnet: Mininet subnet (default 10.0/8)=""" + self.inetIntf = inetIntf + self.subnet = subnet + self.localIntf = localIntf + + def config( self, **params ): + super( NAT, self).config( **params ) + """Configure the NAT and iptables""" + + if not self.localIntf: + self.localIntf = self.defaultIntf() + + self.cmd( 'sysctl net.ipv4.ip_forward=0' ) + + # Flush any currently active rules + # TODO: is this safe? + self.cmd( 'iptables -F' ) + self.cmd( 'iptables -t nat -F' ) + + # Create default entries for unmatched traffic + self.cmd( 'iptables -P INPUT ACCEPT' ) + self.cmd( 'iptables -P OUTPUT ACCEPT' ) + self.cmd( 'iptables -P FORWARD DROP' ) + + # Configure NAT + 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' ) + + # Instruct the kernel to perform forwarding + self.cmd( 'sysctl net.ipv4.ip_forward=1' ) + + # Prevent network-manager from messing with our interface + # by specifying manual configuration in /etc/network/interfaces + intf = self.localIntf + cfile = '/etc/network/interfaces' + line = '\niface %s inet manual\n' % intf + config = open( cfile ).read() + if ( line ) not in config: + info( '*** Adding "' + line.strip() + '" to ' + cfile ) + with open( cfile, 'a' ) as f: + f.write( line ) + # Probably need to restart network-manager to be safe - + # hopefully this won't disconnect you + self.cmd( 'service network-manager restart' ) + + def terminate( self ): + """Stop NAT/forwarding between Mininet and external network""" + # Flush any currently active rules + # TODO: is this safe? + self.cmd( 'iptables -F' ) + self.cmd( 'iptables -t nat -F' ) + + # Instruct the kernel to stop forwarding + self.cmd( 'sysctl net.ipv4.ip_forward=0' ) + + super( NAT, self ).terminate() +