Lab 3: Use “ovs-vsctl” command to directly control the open vswitch

 

[mymininet4.py: one switch and two hosts. When there is traffic coming in port 1, forward it to port 2, and vice versa]

 #!/usr/bin/python

 

"""

Build a simple network from scratch, using mininet primitives.

This is more complicated than using the higher-level classes,

but it exposes the configuration details and allows customization.

 

For most tasks, the higher-level API will be preferable.

"""

 

from mininet.net import Mininet

from mininet.node import Node

from mininet.link import Link

from mininet.log import setLogLevel, info

from mininet.util import quietRun

 

from time import sleep

 

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

    "Create network from scratch using Open vSwitch."

 

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

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

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

    h0 = Node( 'h0' )

    h1 = Node( 'h1' )

 

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

    Link( h0, switch0 )

    Link( h1, switch0 )

 

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

    h0.setIP( '192.168.123.1/24' )

    h1.setIP( '192.168.123.2/24' )

    info( str( h0 ) + '\n' )

    info( str( h1 ) + '\n' )

       

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

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

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

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

 

    for intf in switch0.intfs.values():

        print intf

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

 

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

    # can connect via loopback interface

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

    switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"' )

    switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"' )

 

    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 -c3 ' + h1.IP() )

    h1.cmdPrint( 'ping -c3 ' + h0.IP() )  

 

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

    controller.cmd( 'kill %' + cname )

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

    switch0.deleteIntfs()

    info( '\n' )

 

if __name__ == '__main__':

    setLogLevel( 'info' )

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

    Mininet.init()

    scratchNet()

 

[Execution]

 

 

[mymininet4_1.py: two switches and two hosts. Host 0 is connected to Switch 0 while Host 1 is connected to Switch 1. And switch 0 and switch 1 are connected.]

#!/usr/bin/python

 

"""

Build a simple network from scratch, using mininet primitives.

This is more complicated than using the higher-level classes,

but it exposes the configuration details and allows customization.

 

For most tasks, the higher-level API will be preferable.

"""

 

from mininet.net import Mininet

from mininet.node import Node

from mininet.link import Link

from mininet.log import setLogLevel, info

from mininet.util import quietRun

 

from time import sleep

 

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

    "Create network from scratch using Open vSwitch."

 

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

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

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

    switch1 = Node( 's1', inNamespace=False )

    h0 = Node( 'h0' )

    h1 = Node( 'h1' )

 

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

    Link( h0, switch0 )

    Link( h1, switch1 )

    Link( switch0, switch1 )

 

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

    h0.setIP( '192.168.123.1/24' )

    h1.setIP( '192.168.123.2/24' )

    info( str( h0 ) + '\n' )

    info( str( h1 ) + '\n' )

       

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

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

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

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

    switch1.cmd( 'ovs-vsctl del-br dp1' )

    switch1.cmd( 'ovs-vsctl add-br dp1' )

 

    for intf in switch0.intfs.values():

        print intf

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

 

    for intf in switch1.intfs.values():

        print intf

        print switch1.cmd( 'ovs-vsctl add-port dp1 %s' % intf )

 

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

    # can connect via loopback interface

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

    switch0.cmd( 'ovs-vsctl set-controller dp1 tcp:127.0.0.1:6633' )

    switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"' )

    switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"' )

    switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=1 actions=output:2\"' )

    switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=2 actions=output:1\"' )

 

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

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

        sleep( 1 )

        info( '.' )

    info( '\n' )

 

    print switch0.cmd( 'ovs-ofctl show dp0' )  

    print switch1.cmd( 'ovs-ofctl show dp1' )

 

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

    h0.cmdPrint( 'ping -c3 ' + h1.IP() )

    h1.cmdPrint( 'ping -c3 ' + h0.IP() )  

 

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

    controller.cmd( 'kill %' + cname )

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

    switch0.deleteIntfs()

    switch1.cmd( 'ovs-vsctl del-br dp1' )

    switch1.deleteIntfs()

    info( '\n' )

 

if __name__ == '__main__':

    setLogLevel( 'info' )

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

    Mininet.init()

    scratchNet()

 

[Execution]

 

 

[mymininet4_3.py: Almost the same as mymininet4_1.py. The main difference is that when creating links, we set the bandwidth, delay, and loss rate parameters]

#!/usr/bin/python

 

from mininet.net import Mininet

from mininet.node import Node

from mininet.link import TCLink

from mininet.log import  setLogLevel, info

 

def myNet():

    "Create network from scratch using Open vSwitch."

 

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

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

    switch1 = Node( 's1', inNamespace=False )

    h0 = Node( 'h0' )

    h1 = Node( 'h1' )

 

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

    linkopts0=dict(bw=10)

    linkopts1=dict(bw=10, delay='5ms', loss=10)

    TCLink( h0, switch0, **linkopts0)

    TCLink( h1, switch1,**linkopts0)

    TCLink( switch0, switch1,**linkopts1)

 

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

    h0.setIP( '192.168.123.1/24' )

    h1.setIP( '192.168.123.2/24' )

    info( str( h0 ) + '\n' )

    info( str( h1 ) + '\n' )

       

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

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

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

    switch1.cmd( 'ovs-vsctl del-br dp1' )

    switch1.cmd( 'ovs-vsctl add-br dp1' )

 

    for intf in switch0.intfs.values():

        print intf

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

 

    for intf in switch1.intfs.values():

        print intf

        print switch1.cmd( 'ovs-vsctl add-port dp1 %s' % intf )

 

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

    # can connect via loopback interface

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

    switch0.cmd( 'ovs-vsctl set-controller dp1 tcp:127.0.0.1:6633' )

    switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"' )

    switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"' )

    switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=1 actions=output:2\"' )

    switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=2 actions=output:1\"' )

 

    print switch0.cmd( 'ovs-ofctl show dp0' )      

    print switch1.cmd( 'ovs-ofctl show dp1' )

 

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

    h0.cmdPrint( 'ping -c10 ' + h1.IP() )

    h1.cmdPrint( 'ping -c10 ' + h0.IP() )  

 

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

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

    switch0.deleteIntfs()

    switch1.cmd( 'ovs-vsctl del-br dp1' )

    switch1.deleteIntfs()

    info( '\n' )

 

if __name__ == '__main__':

    setLogLevel( 'info' )

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

    Mininet.init()

    myNet()

 

[Execution]

 

 

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

Department of Computer Science and Information Engineering,

National Quemoy University, Kinmen, Taiwan.