Statistics
| Branch: | Tag: | Revision:

mininet / examples / natnet.py @ 4015e066

History | View | Annotate | Download (1.91 KB)

1 555d10de Brian O'Connor
#!/usr/bin/python
2
3
"""
4
natnet.py: Example network with NATs
5

6

7
           h0
8
           |
9
           s0
10 cee62eb2 Brian O'Connor
           |
11 555d10de Brian O'Connor
    ----------------
12
    |              |
13
   nat1           nat2
14
    |              |
15
   s1              s2
16
    |              |
17
   h1              h2 
18

19
"""
20
21
from mininet.topo import Topo
22
from mininet.net import Mininet
23 4015e066 Brian O'Connor
from mininet.nodelib import NAT
24 555d10de Brian O'Connor
from mininet.log import setLogLevel
25
from mininet.cli import CLI
26
from mininet.util import irange
27
28
class InternetTopo(Topo):
29 cee62eb2 Brian O'Connor
    "Single switch connected to n hosts."
30 555d10de Brian O'Connor
    def __init__(self, n=2, h=1, **opts):
31
        Topo.__init__(self, **opts)
32
33
        # set up inet switch
34
        inetSwitch = self.addSwitch('s0')
35
        # add inet host
36
        inetHost = self.addHost('h0')
37
        self.addLink(inetSwitch, inetHost)
38
39
        # add local nets
40
        for i in irange(1, n):
41
            inetIntf = 'nat%d-eth0' % i
42
            localIntf = 'nat%d-eth1' % i
43
            localIP = '192.168.%d.1' % i
44
            localSubnet = '192.168.%d.0/24' % i
45
            natParams = { 'ip' : '%s/24' % localIP }
46
            # add NAT to topology
47
            nat = self.addNode('nat%d' % i, cls=NAT, subnet=localSubnet, 
48
                               inetIntf=inetIntf, localIntf=localIntf)
49
            switch = self.addSwitch('s%d' % i)
50
            # connect NAT to inet and local switches
51
            self.addLink(nat, inetSwitch, intfName1=inetIntf)
52
            self.addLink(nat, switch, intfName1=localIntf, params1=natParams)
53
            # add host and connect to local switch
54
            host = self.addHost('h%d' % i, 
55
                                ip='192.168.%d.100/24' % i, 
56
                                defaultRoute='via %s' % localIP)
57
            self.addLink(host, switch)
58
59 ffeb16eb Brian O'Connor
def run():
60 cee62eb2 Brian O'Connor
    "Create network and run the CLI"
61 555d10de Brian O'Connor
    topo = InternetTopo()
62
    net = Mininet(topo=topo)
63
    net.start()
64
    CLI(net)
65
    net.stop()
66
67
if __name__ == '__main__':
68
    setLogLevel('info')
69 ffeb16eb Brian O'Connor
    run()