Tuesday, October 22, 2013

Build your network topology in Mininet

In this post i will discuss about building a network topology without using default topology objects which come with Mininet.

Import all the modules as below.


#!/usr/bin/python
from mininet.net import Mininet
from mininet.node import Controller
from mininet.node import RemoteController
from mininet.cli import CLI
from mininet.link import TCLink
from mininet.log import setLogLevel, info 



Define a function myNet() which creates all the nodes and links them. The Mininet class returns a empty network object to which we add hosts, switches and controller. RemoteController class returns a controller object which runs outside Mininet. Assign the controller into our net object.

def myNet():

    "Create an empty network"
    net = Mininet( link=TCLink )

    "Create a RemoteController and add to network"
    info( '*** Adding RemoteController\n' )
    c = RemoteController('c', ip='127.0.0.1', port=6666)
    net.controllers = [ c ]


net.addHost() creates host nodes and adds to the network. net.addSwitch adds the openflow switch to the network
 
    "Create hosts and add to network"
    info( '*** Adding hosts\n' )
    h1 = net.addHost( 'h1', ip='10.0.0.1' )
    h2 = net.addHost( 'h2', ip='10.0.0.2' )
    h3 = net.addHost( 'h3', ip='10.0.0.3' )

    "Create switch and to network"
    info( '*** Adding switch\n' )
    s3 = net.addSwitch( 's3' )
    s4 = net.addSwitch( 's4' )

Connect the nodes by adding links between them as below. Then net.build() call will build the topology and net.start() will start all the nodes. CLI(net) will run the cli mode awaiting for user inputs.

    info( '*** Creating links\n' )
    h1.linkTo( s3 )
    h2.linkTo( s3 )
    s3.linkTo( s4 )
    h3.linkTo( s4 )

    info( '*** Starting network\n')
    net.build()
    net.start()

    info( '*** Running CLI\n' )
    CLI( net )

    info( '*** Stopping network' )
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNet()

Results :

*** Adding RemoteController
Unable to contact the remote controller at 127.0.0.1:6666
*** Adding hosts
*** Adding switch
*** Creating links
*** Starting network
*** Configuring hosts
h1 h2 h3
*** Starting controller
*** Starting 2 switches
s3 s4
*** Running CLI
*** Starting CLI:
mininet> dump
<RemoteController c: 127.0.0.1:6666 pid=5371>
<OVSSwitch s3: lo:127.0.0.1,s3-eth1:None,s3-eth2:None,s3-eth3:None pid=5383>
<OVSSwitch s4: lo:127.0.0.1,s4-eth1:None,s4-eth2:None pid=5388>
<Host h1: h1-eth0:10.0.0.1 pid=5378>
<Host h2: h2-eth0:10.0.0.2 pid=5379>
<Host h3: h3-eth0:10.0.0.3 pid=5380>
mininet>

No comments :

Post a Comment