| 126 | == Esempio con routing == |
| 127 | |
| 128 | {{{#!python |
| 129 | from mininet.topo import Topo |
| 130 | from mininet.net import Mininet |
| 131 | from mininet.link import TCLink |
| 132 | from mininet.cli import CLI |
| 133 | |
| 134 | class MyTopo(Topo): |
| 135 | def __init__(self): |
| 136 | Topo.__init__(self) |
| 137 | s1 = self.addSwitch('s1') |
| 138 | s2 = self.addSwitch('s2') |
| 139 | |
| 140 | h = [] |
| 141 | for i in xrange(4): |
| 142 | h.append(self.addHost('h' + str(i+1))) |
| 143 | |
| 144 | self.addLink(s1, h[0]) |
| 145 | self.addLink(s1, h[1]) |
| 146 | self.addLink(s2, h[2]) |
| 147 | self.addLink(s2, h[3]) |
| 148 | |
| 149 | # Gateway node |
| 150 | gw = self.addHost('h5') |
| 151 | self.addLink(s1, gw) |
| 152 | self.addLink(s2, gw) |
| 153 | |
| 154 | |
| 155 | if __name__ == '__main__': |
| 156 | net = Mininet(topo=MyTopo(), link=TCLink) |
| 157 | h1, h2 = net.get('h1','h2') |
| 158 | h1.setIP('192.168.1.10/24') |
| 159 | h2.setIP('192.168.1.20/24') |
| 160 | |
| 161 | |
| 162 | h3, h4 = net.get('h3','h4') |
| 163 | h3.setIP('192.168.2.100/24') |
| 164 | h4.setIP('192.168.2.200/24') |
| 165 | |
| 166 | for h in net.hosts: |
| 167 | if h.name != 'h5': # gateway |
| 168 | h.setDefaultRoute(intf=h.intf()) |
| 169 | |
| 170 | gw = net.get('h5') |
| 171 | gw.setIP('192.168.1.1/24', intf='h5-eth0') |
| 172 | gw.setIP('192.168.2.1/24', intf='h5-eth1') |
| 173 | h1.cmd('ip route add 192.168.2.0/24 via 192.168.1.1') |
| 174 | h2.cmd('ip route add 192.168.2.0/24 via 192.168.1.1') |
| 175 | h3.cmd('ip route add 192.168.1.0/24 via 192.168.2.1') |
| 176 | h4.cmd('ip route add 192.168.1.0/24 via 192.168.2.1') |
| 177 | |
| 178 | gw.setHostRoute('192.168.1.0', intf='h5-eth0') |
| 179 | gw.setHostRoute('192.168.2.0', intf='h5-eth1') |
| 180 | gw.cmd('sysctl -w net.ipv4.ip_forward=1') |
| 181 | |
| 182 | net.start() |
| 183 | CLI(net) |
| 184 | net.stop() |
| 185 | }}} |