Update and rename MantenerFIFO to MantenerFIFO.md
[vsorcdistro/.git] / mininet / examples / natnet.py
1 #!/usr/bin/python
2
3 """
4 natnet.py: Example network with NATs
5
6
7            h0
8            |
9            s0
10            |
11     ----------------
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 from mininet.nodelib import NAT
24 from mininet.log import setLogLevel
25 from mininet.cli import CLI
26 from mininet.util import irange
27
28 class InternetTopo(Topo):
29     "Single switch connected to n hosts."
30     def build(self, n=2, **_kwargs ):
31         # set up inet switch
32         inetSwitch = self.addSwitch('s0')
33         # add inet host
34         inetHost = self.addHost('h0')
35         self.addLink(inetSwitch, inetHost)
36
37         # add local nets
38         for i in irange(1, n):
39             inetIntf = 'nat%d-eth0' % i
40             localIntf = 'nat%d-eth1' % i
41             localIP = '192.168.%d.1' % i
42             localSubnet = '192.168.%d.0/24' % i
43             natParams = { 'ip' : '%s/24' % localIP }
44             # add NAT to topology
45             nat = self.addNode('nat%d' % i, cls=NAT, subnet=localSubnet,
46                                inetIntf=inetIntf, localIntf=localIntf)
47             switch = self.addSwitch('s%d' % i)
48             # connect NAT to inet and local switches
49             self.addLink(nat, inetSwitch, intfName1=inetIntf)
50             self.addLink(nat, switch, intfName1=localIntf, params1=natParams)
51             # add host and connect to local switch
52             host = self.addHost('h%d' % i,
53                                 ip='192.168.%d.100/24' % i,
54                                 defaultRoute='via %s' % localIP)
55             self.addLink(host, switch)
56
57 def run():
58     "Create network and run the CLI"
59     topo = InternetTopo()
60     net = Mininet(topo=topo)
61     net.start()
62     CLI(net)
63     net.stop()
64
65 if __name__ == '__main__':
66     setLogLevel('info')
67     run()