In: Computer Science
In this assignment you will start with Python code that builds a 2 host network connected by a legacy router. You will modify it such that the two hosts can send packets to each other. It requires that you understand subnet addressing and the function of a router. The network built using Miniedit on a Mininet virtual machine: python mininet/examples/miniedit.py. We need to also make 6 static routes to make the program work.
This is the work that I have so far!!
#!/usr/bin/python
from mininet.net import Mininet
from mininet.node import Controller,
RemoteController,OVSController
from mininet.node import Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call
def myNetwork():
net = Mininet( topo=None, build=False,ipBase='10.0.0.0/24')
info( '*** Adding controller\n' )
c0=net.addController(name='c0',controller=Controller,
protocol='tcp',port=6633)
s1 = net.addSwitch('s1', cls=OVSKernelSwitch)
s2 = net.addSwitch('s2', cls=OVSKernelSwitch)
info( '*** Add switches\n')
r5 = net.addHost('r5', cls=Node, ip=' 10.0.1.1/24',
defaultRoute='via 192.168.56.1')
r5.cmd('sysctl -w net.ipv4.ip_forward=1')
r4 = net.addHost('r4', cls=Node, ip=' 10.0.2.1/24',
defaultRoute='via 192.168.56.2')
r4.cmd('sysctl -w net.ipv4.ip_forward=1')
r3 = net.addHost('r3', cls=Node, ip=' 10.0.3.1/24',
defaultRoute='via 192.168.56.3')
r3.cmd('sysctl -w net.ipv4.ip_forward=1')
info( '*** Add hosts\n')
h1 = net.addHost('h1', cls=Host, ip='10.0.0.1',defaultRoute='via
10.0.3.1')
h2 = net.addHost('h2', cls=Host, ip='10.0.0.2',defaultRoute='via
10.0.1.1')
info( '*** Add links\n')
net.addLink(h1, s1)
net.addLink(h2, s2)
net.addLink(s2, r5, intfName2='r5-eth1', params2={ 'ip' :
'10.0.1.1/24'})
net.addLink(s1, r3, intfName2='r3-eth1', params2={ 'ip' :
'10.0.3.1/24'})
net.addLink(r3, r4, intfName1='r3-eth2', params1={ 'ip' :
'192.168.56.3'}, intfName1='r4-eth2',
params0={'192.168.56.2'})
net.addLink(r4, r5, intfName1='r4-eth1', params1={ 'ip' :
'10.0.2.1/24'}, intfName2='r5-eth2', params2={'192.168.56.1'})
info( '*** Starting network\n')
net.build()
info( '*** Starting controllers\n')
for controller in net.controllers:
controller.start()
info( '*** Starting switches\n')
net.get('s2').start([c0])
net.get('s1').start([c0])
info( '*** Post configure switches and hosts\n')
CLI(net)
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
myNetwork()
inuxrouter.py: Example network with Linux IP router | |
This example converts a Node into a router using IP forwarding | |
already built into Linux. | |
The example topology creates a router and three IP subnets: | |
- 192.168.1.0/24 (r0-eth1, IP: 192.168.1.1) | |
- 172.16.0.0/12 (r0-eth2, IP: 172.16.0.1) | |
- 10.0.0.0/8 (r0-eth3, IP: 10.0.0.1) | |
Each subnet consists of a single host connected to | |
a single switch: | |
r0-eth1 - s1-eth1 - h1-eth0 (IP: 192.168.1.100) | |
r0-eth2 - s2-eth1 - h2-eth0 (IP: 172.16.0.100) | |
r0-eth3 - s3-eth1 - h3-eth0 (IP: 10.0.0.100) | |
The example relies on default routing entries that are | |
automatically created for each router interface, as well | |
as 'defaultRoute' parameters for the host interfaces. | |
Additional routes may be added to the router or hosts by | |
executing 'ip route' or 'route' commands on the router or hosts. | |
""" | |
from mininet.topo import Topo | |
from mininet.net import Mininet | |
from mininet.node import Node | |
from mininet.log import setLogLevel, info | |
from mininet.cli import CLI | |
class LinuxRouter( Node ): | |
"A Node with IP forwarding enabled." | |
def config( self, **params ): | |
super( LinuxRouter, self).config( **params ) | |
# Enable forwarding on the router | |
self.cmd( 'sysctl net.ipv4.ip_forward=1' ) | |
def terminate( self ): | |
self.cmd( 'sysctl net.ipv4.ip_forward=0' ) | |
super( LinuxRouter, self ).terminate() | |
class NetworkTopo( Topo ): | |
"A LinuxRouter connecting three IP subnets" | |
def build( self, **_opts ): | |
defaultIP = '192.168.1.1/24' # IP address for r0-eth1 | |
router = self.addNode( 'r0', cls=LinuxRouter, ip=defaultIP ) | |
s1, s2, s3 = [ self.addSwitch( s ) for s in ( 's1', 's2', 's3' ) ] | |
self.addLink( s1, router, intfName2='r0-eth1', | |
params2={ 'ip' : defaultIP } ) # for clarity | |
self.addLink( s2, router, intfName2='r0-eth2', | |
params2={ 'ip' : '172.16.0.1/12' } ) | |
self.addLink( s3, router, intfName2='r0-eth3', | |
params2={ 'ip' : '10.0.0.1/8' } ) | |
h1 = self.addHost( 'h1', ip='192.168.1.100/24', | |
defaultRoute='via 192.168.1.1' ) | |
h2 = self.addHost( 'h2', ip='172.16.0.100/12', | |
defaultRoute='via 172.16.0.1' ) | |
h3 = self.addHost( 'h3', ip='10.0.0.100/8', | |
defaultRoute='via 10.0.0.1' ) | |
for h, s in [ (h1, s1), (h2, s2), (h3, s3) ]: | |
self.addLink( h, s ) | |
def run(): | |
"Test linux router" | |
topo = NetworkTopo() | |
net = Mininet( topo=topo ) # controller is used by s1-s3 | |
net.start() | |
info( '*** Routing Table on Router:\n' ) | |
info( net[ 'r0' ].cmd( 'route' ) ) | |
CLI( net ) | |
net.stop() | |
if __name__ == '__main__': | |
setLogLevel( 'info' ) | |
run() |