Revision b1ec912d mininet/nodelib.py

View differences:

mininet/nodelib.py
33 33
        else:
34 34
            return True
35 35

  
36
    def start( self, controllers ):
36
    def start( self, _controllers ):
37 37
        "Start Linux bridge"
38 38
        self.cmd( 'ifconfig', self, 'down' )
39 39
        self.cmd( 'brctl delbr', self )
......
62 62

  
63 63

  
64 64
class NAT( Node ):
65
    """NAT: Provides connectivity to external network"""
65
    "NAT: Provides connectivity to external network"
66 66

  
67
    def __init__( self, name, inetIntf=None, subnet='10.0/8', localIntf=None, **params):
67
    def __init__( self, name, inetIntf=None, subnet='10.0/8',
68
                  localIntf=None, **params):
69
        """Start NAT/forwarding between Mininet and external network
70
           inetIntf: interface for internet access
71
           subnet: Mininet subnet (default 10.0/8)="""
68 72
        super( NAT, self ).__init__( name, **params )
69 73

  
70
        """Start NAT/forwarding between Mininet and external network
71
        inetIntf: interface for internet access
72
        subnet: Mininet subnet (default 10.0/8)="""
73 74
        self.inetIntf = inetIntf if inetIntf else self.getGatewayIntf()
74 75
        self.subnet = subnet
75 76
        self.localIntf = localIntf
76 77

  
77 78
    def config( self, **params ):
78
        super( NAT, self).config( **params )
79 79
        """Configure the NAT and iptables"""
80
        super( NAT, self).config( **params )
80 81

  
81 82
        if not self.localIntf:
82 83
            self.localIntf =  self.defaultIntf()
......
94 95
        self.cmd( 'iptables -P FORWARD DROP' )
95 96

  
96 97
        # Configure NAT
97
        self.cmd( 'iptables -I FORWARD -i', self.localIntf, '-d', self.subnet, '-j DROP' )
98
        self.cmd( 'iptables -A FORWARD -i', self.localIntf, '-s', self.subnet, '-j ACCEPT' )
99
        self.cmd( 'iptables -A FORWARD -i', self.inetIntf, '-d', self.subnet, '-j ACCEPT' )
100
        self.cmd( 'iptables -t nat -A POSTROUTING -o ', self.inetIntf, '-s', self.subnet, '-j MASQUERADE' )
98
        self.cmd( 'iptables -I FORWARD',
99
                  '-i', self.localIntf, '-d', self.subnet, '-j DROP' )
100
        self.cmd( 'iptables -A FORWARD',
101
                  '-i', self.localIntf, '-s', self.subnet, '-j ACCEPT' )
102
        self.cmd( 'iptables -A FORWARD',
103
                  '-i', self.inetIntf, '-d', self.subnet, '-j ACCEPT' )
104
        self.cmd( 'iptables -t nat -A POSTROUTING',
105
                  '-o', self.inetIntf, '-s', self.subnet, '-j MASQUERADE' )
101 106

  
102 107
        # Instruct the kernel to perform forwarding
103 108
        self.cmd( 'sysctl net.ipv4.ip_forward=1' )
......
116 121
        # hopefully this won't disconnect you
117 122
        self.cmd( 'service network-manager restart' )
118 123

  
119
    def getGatewayIntf( self ):
124
    def getGatewayIntf( self, fallback='eth0' ):
125
        """Return gateway interface name
126
           fallback: default device to fall back to"""
120 127
        routes = self.cmd( 'ip route show' )
121
        match = re.search('default via \S+ dev (\S+)', routes )
128
        match = re.search( r'default via \S+ dev (\S+)', routes )
122 129
        if match:
123 130
            return match.group( 1 )
124 131
        else:
125
            warn( 'There is no default route set. Using eth0 as gateway interface...\n' )
126
            return 'eth0'
132
            warn( 'There is no default route set.',
133
                  'Using', fallback, 'as gateway interface...\n' )
134
            return fallback
127 135

  
128 136
    def terminate( self ):
129 137
        """Stop NAT/forwarding between Mininet and external network"""
......
136 144
        self.cmd( 'sysctl net.ipv4.ip_forward=0' )
137 145

  
138 146
        super( NAT, self ).terminate()
139

  

Also available in: Unified diff