Lab 6: A simple Controller

[mymininet8.py: Similar to mymininet6.py. The difference is that the command is placed in a controller.

#!/usr/bin/python

 

from mininet.net import Mininet

from mininet.node import Node

from mininet.link import TCLink

from mininet.log import  setLogLevel, info

from threading import Timer

from mininet.util import quietRun

from time import sleep

 

def myNet(cname='controller', cargs='-v ptcp:'):

    "Create network from scratch using Open vSwitch."

    info( "*** Creating nodes\n" )

    controller = Node( 'c0', inNamespace=False )

    switch = Node( 's0', inNamespace=False )

    h0 = Node( 'h0' )

    h1 = Node( 'h1' )

    h2 = Node( 'h2' )

 

    info( "*** Creating links\n" )

    linkopts0=dict(bw=100, delay='1ms', loss=0)

    TCLink( h0, switch, **linkopts0)

    TCLink( h1, switch, **linkopts0)

    TCLink( h2, switch, **linkopts0)

 

    info( "*** Configuring hosts\n" )

    h0.setIP( '192.168.123.1/24' )

    h1.setIP( '192.168.123.2/24' )

    h2.setIP( '192.168.123.3/24' )

       

    info( "*** Starting network using Open vSwitch\n" )

    switch.cmd( 'ovs-vsctl del-br dp0' )

    switch.cmd( 'ovs-vsctl add-br dp0' )

 

    controller.cmd( cname + ' ' + cargs + '&' )    

    for intf in switch.intfs.values():

        print intf

        print switch.cmd( 'ovs-vsctl add-port dp0 %s' % intf )

  

    # Note: controller and switch are in root namespace, and we

    # can connect via loopback interface

    switch.cmd( 'ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633' )

   

    info( '*** Waiting for switch to connect to controller' )

    while 'is_connected' not in quietRun( 'ovs-vsctl show' ):

        sleep( 1 )

        info( '.' )

    info( '\n' )

 

    #info( "*** Running test\n" )

    h0.cmdPrint( 'ping -c 3 ' + h1.IP() )

    h0.cmdPrint( 'ping -c 3 ' + h2.IP() )

    h2.cmdPrint( 'ping -c 3 ' + h1.IP() )

 

    info( "*** Stopping network\n" )

    controller.cmd( 'kill %' + cname )

    switch.cmd( 'ovs-vsctl del-br dp0' )

    switch.deleteIntfs()

    info( '\n' )

 

if __name__ == '__main__':

    setLogLevel( 'info' )

    info( '*** Scratch network demo (kernel datapath)\n' )

    Mininet.init()

    myNet()

 

[mypox1:put this file under /pox/ext folder]

# Copyright 2012 James McCauley

#

# This file is part of POX.

#

# POX is free software: you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation, either version 3 of the License, or

# (at your option) any later version.

#

# POX is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

# GNU General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with POX.  If not, see <http://www.gnu.org/licenses/>.

 

"""

Turns your complex OpenFlow switches into stupid hubs.

"""

 

from pox.core import core

import pox.openflow.libopenflow_01 as of

from pox.lib.util import dpidToStr

 

log = core.getLogger()

 

def _handle_ConnectionUp (event):

 

  msg = of.ofp_flow_mod()

  msg.priority =1

  msg.idle_timeout = 0

  msg.match.in_port =1

  msg.actions.append(of.ofp_action_output(port = of.OFPP_ALL))

  event.connection.send(msg)

 

  msg = of.ofp_flow_mod()

  msg.priority =1

  msg.idle_timeout = 0

  msg.match.in_port =2

  msg.actions.append(of.ofp_action_output(port = of.OFPP_ALL))

  event.connection.send(msg)

 

  msg = of.ofp_flow_mod()

  msg.priority =1

  msg.idle_timeout = 0

  msg.match.in_port =3

  msg.actions.append(of.ofp_action_output(port = of.OFPP_ALL))

  event.connection.send(msg)

 

  msg = of.ofp_flow_mod()

  msg.priority =10

  msg.idle_timeout = 0

  msg.hard_timeout = 0

  msg.match.dl_type = 0x0800

  msg.match.nw_dst = "192.168.123.3"

  msg.actions.append(of.ofp_action_output(port = 3))

  event.connection.send(msg)

 

  msg = of.ofp_flow_mod()

  msg.priority =10

  msg.idle_timeout = 0

  msg.hard_timeout = 0

  msg.match.dl_type = 0x0800

  msg.match.nw_dst = "192.168.123.2"

  msg.actions.append(of.ofp_action_output(port = 2))

  event.connection.send(msg)

 

  msg = of.ofp_flow_mod()

  msg.priority =10

  msg.idle_timeout = 0

  msg.hard_timeout = 0

  msg.match.dl_type = 0x0800

  msg.match.nw_dst = "192.168.123.1"

  msg.actions.append(of.ofp_action_output(port = 1))

  event.connection.send(msg)

 

def launch ():

  core.openflow.addListenerByName("ConnectionUp", _handle_ConnectionUp)

 

  log.info("mypox1")

 

[Execution]

a.      Start the controller first. (remember to put mypox1 under /pox/ext folder.)

 

b.      Then run the script.

 

 

Dr. Chih-Heng Ke (smallko@gmail.com)

Department of Computer Science and Information Engineering,

National Quemoy University, Kinmen, Taiwan.