Revision 4015e066 mininet/nodelib.py

View differences:

mininet/nodelib.py
1 1
"""
2 2
Node Library for Mininet
3 3

  
4
This contains additional Node types which you may find to be useful
4
This contains additional Node types which you may find to be useful.
5 5
"""
6 6

  
7
from mininet.net import Mininet
8
from mininet.topo import Topo
9
from mininet.node import Switch
7
from mininet.node import Node, Switch
10 8
from mininet.log import setLogLevel, info
11 9

  
12

  
13 10
class LinuxBridge( Switch ):
14 11
    "Linux Bridge (with optional spanning tree)"
15 12

  
......
49 46
        self.cmd( 'ifconfig', self, 'down' )
50 47
        self.cmd( 'brctl delbr', self )
51 48

  
49
class NAT( Node ):
50
    """NAT: Provides connectivity to external network"""
51

  
52
    def __init__( self, name, inetIntf='eth0', subnet='10.0/8', localIntf=None, **params):
53
        super( NAT, self ).__init__( name, **params )
54

  
55
        """Start NAT/forwarding between Mininet and external network
56
        inetIntf: interface for internet access
57
        subnet: Mininet subnet (default 10.0/8)="""
58
        self.inetIntf = inetIntf
59
        self.subnet = subnet
60
        self.localIntf = localIntf
61

  
62
    def config( self, **params ):
63
        super( NAT, self).config( **params )
64
        """Configure the NAT and iptables"""
65

  
66
        if not self.localIntf:
67
            self.localIntf =  self.defaultIntf()
68

  
69
        self.cmd( 'sysctl net.ipv4.ip_forward=0' )
70

  
71
        # Flush any currently active rules
72
        # TODO: is this safe?
73
        self.cmd( 'iptables -F' )
74
        self.cmd( 'iptables -t nat -F' )
75

  
76
        # Create default entries for unmatched traffic
77
        self.cmd( 'iptables -P INPUT ACCEPT' )
78
        self.cmd( 'iptables -P OUTPUT ACCEPT' )
79
        self.cmd( 'iptables -P FORWARD DROP' )
80

  
81
        # Configure NAT
82
        self.cmd( 'iptables -I FORWARD -i', self.localIntf, '-d', self.subnet, '-j DROP' )
83
        self.cmd( 'iptables -A FORWARD -i', self.localIntf, '-s', self.subnet, '-j ACCEPT' )
84
        self.cmd( 'iptables -A FORWARD -i', self.inetIntf, '-d', self.subnet, '-j ACCEPT' )
85
        self.cmd( 'iptables -t nat -A POSTROUTING -o ', self.inetIntf, '-j MASQUERADE' )
86

  
87
        # Instruct the kernel to perform forwarding
88
        self.cmd( 'sysctl net.ipv4.ip_forward=1' )
89

  
90
        # Prevent network-manager from messing with our interface
91
        # by specifying manual configuration in /etc/network/interfaces
92
        intf = self.localIntf
93
        cfile = '/etc/network/interfaces'
94
        line = '\niface %s inet manual\n' % intf
95
        config = open( cfile ).read()
96
        if ( line ) not in config:
97
            info( '*** Adding "' + line.strip() + '" to ' + cfile )
98
            with open( cfile, 'a' ) as f:
99
                f.write( line )
100
        # Probably need to restart network-manager to be safe -
101
        # hopefully this won't disconnect you
102
        self.cmd( 'service network-manager restart' )
103

  
104
    def terminate( self ):
105
        """Stop NAT/forwarding between Mininet and external network"""
106
        # Flush any currently active rules
107
        # TODO: is this safe?
108
        self.cmd( 'iptables -F' )
109
        self.cmd( 'iptables -t nat -F' )
110

  
111
        # Instruct the kernel to stop forwarding
112
        self.cmd( 'sysctl net.ipv4.ip_forward=0' )
113

  
114
        super( NAT, self ).terminate()
115

  

Also available in: Unified diff