Statistics
| Branch: | Tag: | Revision:

mininet / examples / scratchnet.py @ 8a130dea

History | View | Annotate | Download (1.83 KB)

1
#!/usr/bin/python
2

    
3
"""
4
Build a simple network from scratch, using mininet primitives.
5
This is more complicated than using the higher-level classes,
6
but it exposes the configuration details and allows customization.
7

8
For most tasks, the higher-level API will be preferable.
9
"""
10

    
11
from mininet.net import init
12
from mininet.node import Node, OVSKernelSwitch
13
from mininet.util import createLink
14
from mininet.log import setLogLevel, info
15

    
16
def scratchNet( cname='controller', cargs='ptcp:' ):
17
    "Create network from scratch using kernel switch."
18

    
19
    info( "*** Creating nodes\n" )
20
    controller = Node( 'c0', inNamespace=False )
21
    switch = Node( 's0', inNamespace=False )
22
    h0 = Node( 'h0' )
23
    h1 = Node( 'h1' )
24

    
25
    info( "*** Creating links\n" )
26
    createLink( node1=h0, node2=switch, port1=0, port2=0 )
27
    createLink( node1=h1, node2=switch, port1=0, port2=1 )
28

    
29
    info( "*** Configuring hosts\n" )
30
    h0.setIP( h0.intfs[ 0 ], '192.168.123.1', 24 )
31
    h1.setIP( h1.intfs[ 0 ], '192.168.123.2', 24 )
32
    info( str( h0 ) + '\n' )
33
    info( str( h1 ) + '\n' )
34

    
35
    info( "*** Starting network using Open vSwitch kernel datapath\n" )
36
    controller.cmd( cname + ' ' + cargs + '&' )
37
    switch.cmd( 'ovs-dpctl del-dp dp0' )
38
    switch.cmd( 'ovs-dpctl add-dp dp0' )
39
    for intf in switch.intfs.values():
40
        print switch.cmd( 'ovs-dpctl add-if dp0 ' + intf )
41
    print switch.cmd( 'ovs-openflowd dp0 tcp:127.0.0.1 &' )
42

    
43
    info( "*** Running test\n" )
44
    h0.cmdPrint( 'ping -c1 ' + h1.IP() )
45

    
46
    info( "*** Stopping network\n" )
47
    controller.cmd( 'kill %' + cname )
48
    switch.cmd( 'ovs-dpctl del-dp dp0' )
49
    switch.cmd( 'kill %ovs-openflowd' )
50
    switch.deleteIntfs()
51
    info( '\n' )
52

    
53
if __name__ == '__main__':
54
    setLogLevel( 'info' )
55
    info( '*** Scratch network demo (kernel datapath)\n' )
56
    OVSKernelSwitch.setup()
57
    init()
58
    scratchNet()