moving NAT to nodelib
This commit is contained in:
+1
-1
@@ -20,7 +20,7 @@ natnet.py: Example network with NATs
|
|||||||
|
|
||||||
from mininet.topo import Topo
|
from mininet.topo import Topo
|
||||||
from mininet.net import Mininet
|
from mininet.net import Mininet
|
||||||
from mininet.node import NAT
|
from mininet.nodelib import NAT
|
||||||
from mininet.log import setLogLevel
|
from mininet.log import setLogLevel
|
||||||
from mininet.cli import CLI
|
from mininet.cli import CLI
|
||||||
from mininet.util import irange
|
from mininet.util import irange
|
||||||
|
|||||||
+2
-1
@@ -96,7 +96,8 @@ from itertools import chain, groupby
|
|||||||
|
|
||||||
from mininet.cli import CLI
|
from mininet.cli import CLI
|
||||||
from mininet.log import info, error, debug, output, warn
|
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.link import Link, Intf
|
||||||
from mininet.util import quietRun, fixLimits, numCores, ensureRoot
|
from mininet.util import quietRun, fixLimits, numCores, ensureRoot
|
||||||
from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd
|
from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd
|
||||||
|
|||||||
@@ -41,8 +41,6 @@ RemoteController: a remote controller node, which may use any
|
|||||||
arbitrary OpenFlow-compatible controller, and which is not
|
arbitrary OpenFlow-compatible controller, and which is not
|
||||||
created or managed by mininet.
|
created or managed by mininet.
|
||||||
|
|
||||||
TODO: NAT
|
|
||||||
|
|
||||||
Future enhancements:
|
Future enhancements:
|
||||||
|
|
||||||
- Possibly make Node, Switch and Controller more abstract so that
|
- 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:
|
for controller in order:
|
||||||
if controller.isAvailable():
|
if controller.isAvailable():
|
||||||
return controller( name, **kwargs )
|
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()
|
|
||||||
|
|
||||||
|
|||||||
+69
-5
@@ -1,15 +1,12 @@
|
|||||||
"""
|
"""
|
||||||
Node Library for Mininet
|
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.node import Node, Switch
|
||||||
from mininet.topo import Topo
|
|
||||||
from mininet.node import Switch
|
|
||||||
from mininet.log import setLogLevel, info
|
from mininet.log import setLogLevel, info
|
||||||
|
|
||||||
|
|
||||||
class LinuxBridge( Switch ):
|
class LinuxBridge( Switch ):
|
||||||
"Linux Bridge (with optional spanning tree)"
|
"Linux Bridge (with optional spanning tree)"
|
||||||
|
|
||||||
@@ -49,3 +46,70 @@ class LinuxBridge( Switch ):
|
|||||||
self.cmd( 'ifconfig', self, 'down' )
|
self.cmd( 'ifconfig', self, 'down' )
|
||||||
self.cmd( 'brctl delbr', self )
|
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()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user