Revision 3f2355a3

View differences:

mininet/net.py
112 112
                  build=True, xterms=False, cleanup=False, ipBase='10.0.0.0/8',
113 113
                  inNamespace=False,
114 114
                  autoSetMacs=False, autoStaticArp=False, autoPinCpus=False,
115
                  listenPort=None,
116
                  gateway=None ):
115
                  listenPort=None ):
117 116
        """Create Mininet object.
118 117
           topo: Topo (topology) object or None
119 118
           switch: default Switch class
......
130 129
           autoStaticArp: set all-pairs static MAC addrs?
131 130
           autoPinCpus: pin hosts to (real) cores (requires CPULimitedHost)?
132 131
           listenPort: base listening port to open; will be incremented for
133
               each additional switch in the net if inNamespace=False
134
           gateway: node that provides connectivity to the Internet"""
132
               each additional switch in the net if inNamespace=False"""
135 133
        self.topo = topo
136 134
        self.switch = switch
137 135
        self.host = host
......
150 148
        self.numCores = numCores()
151 149
        self.nextCore = 0  # next core for pinning hosts to CPUs
152 150
        self.listenPort = listenPort
153
        self.gateway = gateway
154 151

  
155 152
        self.hosts = []
156 153
        self.switches = []
......
166 163
        if topo and build:
167 164
            self.build()
168 165

  
169
    # list of Node subclasses that provide gateway service
170
    gateways = [ NAT ]
171

  
172 166
    def addHost( self, name, cls=None, **params ):
173 167
        """Add host.
174 168
           name: name of host to add
......
189 183
        defaults.update( params )
190 184
        # TODO: clean this up
191 185
        if params.get( 'isNAT', False ):
186
            print "***** &&&&&& !!!! nat nat nat"
192 187
            cls = NAT
193 188
        if not cls:
194 189
            cls = self.host
195 190
        h = cls( name, **defaults )
196 191
        self.hosts.append( h )
197 192
        self.nameToNode[ name ] = h
198
        if cls in self.gateways:
199
            self.gateway = h
200 193
        return h
201 194

  
202 195
    def addSwitch( self, name, cls=None, **params ):
......
242 235
    def addNAT( self, name='nat0', connect=True, **params ):
243 236
        nat = self.addHost( name, cls=NAT, **params )
244 237
        # find first switch and create link
245
        print "net/addNAT"
238
        print "******* &&&&&& net/addNAT"
246 239
        if connect:
247 240
            #connect the nat to the first switch
248 241
            self.addLink( nat, self.switches[ 0 ] )
......
326 319
            host.cmd( 'ifconfig lo up' )
327 320
        info( '\n' )
328 321

  
322
    ''' TODO: remove this!
329 323
    def configGateway( self ):
330 324
        """Add gateway routes to all hosts if the networks has a gateway."""
331 325
        if self.gateway:
......
338 332
                else:
339 333
                    # Don't mess with hosts in the root namespace
340 334
                    pass
335
    '''
341 336

  
342 337
    def buildFromTopo( self, topo=None ):
343 338
        """Build mininet from a topology object
......
397 392
            self.startTerms()
398 393
        if self.autoStaticArp:
399 394
            self.staticArp()
400
        self.configGateway()
395
        # TODO: remove this
396
        #self.configGateway()
401 397
        self.built = True
402 398

  
403 399
    def startTerms( self ):
mininet/node.py
1246 1246
class NAT( Node ):
1247 1247
    """NAT: Provides connectivity to external network"""
1248 1248

  
1249
    def __init__( self, name, inetIntf='eth0', subnet='10.0/8', **params):
1249
    def __init__( self, name, inetIntf='eth0', subnet='10.0/8', localIntf=None, **params):
1250 1250
        super( NAT, self ).__init__( name, **params )
1251 1251

  
1252 1252
        """Start NAT/forwarding between Mininet and external network
......
1254 1254
        subnet: Mininet subnet (default 10.0/8)="""
1255 1255
        self.inetIntf = inetIntf
1256 1256
        self.subnet = subnet #TODO: get subnet from Mininet directly
1257
        self.localIntf = localIntf
1257 1258

  
1258
    def config( self, inetIntf='eth0', subnet='10.0/8', **params ):
1259
    def config( self, **params ):
1259 1260
        super( NAT, self).config( **params )
1260 1261
        """Configure the NAT and iptables"""
1261 1262

  
1263
        if not self.localIntf:
1264
            self.localIntf =  self.defaultIntf()
1265

  
1266
        #-------------------------
1267
        print "inetIntf:", self.inetIntf
1268
        print "subnet:", self.subnet
1262 1269
        # Identify the interface connecting to the mininet network
1263
        localIntf =  self.defaultIntf()
1270
        print "LocalIntf:", self.localIntf
1271
        #-------------------------
1272

  
1264 1273
        self.cmd( 'sysctl net.ipv4.ip_forward=0' )
1265 1274

  
1266 1275
        # Flush any currently active rules
......
1274 1283
        self.cmd( 'iptables -P FORWARD DROP' )
1275 1284

  
1276 1285
        # Configure NAT
1277
        self.cmd( 'iptables -I FORWARD -i', localIntf, '-d', self.subnet, '-j DROP' )
1278
        self.cmd( 'iptables -A FORWARD -i', localIntf, '-s', self.subnet, '-j ACCEPT' )
1286
        self.cmd( 'iptables -I FORWARD -i', self.localIntf, '-d', self.subnet, '-j DROP' )
1287
        self.cmd( 'iptables -A FORWARD -i', self.localIntf, '-s', self.subnet, '-j ACCEPT' )
1279 1288
        self.cmd( 'iptables -A FORWARD -i', self.inetIntf, '-d', self.subnet, '-j ACCEPT' )
1280 1289
        self.cmd( 'iptables -t nat -A POSTROUTING -o ', self.inetIntf, '-j MASQUERADE' )
1281 1290

  
......
1284 1293

  
1285 1294
        # Prevent network-manager from messing with our interface
1286 1295
        # by specifying manual configuration in /etc/network/interfaces
1287
        intf = localIntf
1296
        intf = self.localIntf
1288 1297
        cfile = '/etc/network/interfaces'
1289 1298
        line = '\niface %s inet manual\n' % intf
1290 1299
        config = open( cfile ).read()
mininet/topo.py
12 12
'''
13 13

  
14 14
from mininet.util import irange, natural, naturalSeq
15
from mininet.node import NAT
15 16

  
16 17
class MultiGraph( object ):
17 18
    "Utility class to track nodes and edges - replaces networkx.Graph"
......
89 90
        result = self.addNode(name, isSwitch=True, **opts)
90 91
        return result
91 92

  
92
    def addNAT(self, name='nat', connect=True, **opts):
93
    def addNAT(self, name='nat', connect=True, inNamespace=False, **opts):
93 94
        """Convenience method: Add NAT to graph.
94 95
           name: NAT name
95 96
           connect: True will automatically connect to the first switch"""
96
        nat = self.addNode(name, isNAT=True, inNamespace=False)
97
        #nat = self.addNode(name, isNAT=True, inNamespace=False)
98
        nat = self.addNode(name, cls=NAT, inNamespace=inNamespace, hosts=self.hosts(), **opts)
97 99
        if connect:
98 100
            # connect the NAT to the first switch
99 101
            self.addLink(name, self.switches()[ 0 ])

Also available in: Unified diff