Statistics
| Branch: | Tag: | Revision:

mininet / examples / scratchnet.py @ 8a130dea

History | View | Annotate | Download (1.83 KB)

1 433503cf Bob Lantz
#!/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 696a619d Bob Lantz
but it exposes the configuration details and allows customization.
7 b1458654 Bob Lantz

8
For most tasks, the higher-level API will be preferable.
9 433503cf Bob Lantz
"""
10
11 efc9a01c Bob Lantz
from mininet.net import init
12 11782ae0 Bob Lantz
from mininet.node import Node, OVSKernelSwitch
13 efc9a01c Bob Lantz
from mininet.util import createLink
14 d93c6ef6 Bob Lantz
from mininet.log import setLogLevel, info
15 433503cf Bob Lantz
16 80be5642 Bob Lantz
def scratchNet( cname='controller', cargs='ptcp:' ):
17
    "Create network from scratch using kernel switch."
18 dc630c54 Bob Lantz
19
    info( "*** Creating nodes\n" )
20 efc9a01c Bob Lantz
    controller = Node( 'c0', inNamespace=False )
21
    switch = Node( 's0', inNamespace=False )
22
    h0 = Node( 'h0' )
23
    h1 = Node( 'h1' )
24 80be5642 Bob Lantz
25 dc630c54 Bob Lantz
    info( "*** Creating links\n" )
26 73a323f2 Bob Lantz
    createLink( node1=h0, node2=switch, port1=0, port2=0 )
27
    createLink( node1=h1, node2=switch, port1=0, port2=1 )
28 dc630c54 Bob Lantz
29
    info( "*** Configuring hosts\n" )
30 47e26cce Bob Lantz
    h0.setIP( h0.intfs[ 0 ], '192.168.123.1', 24 )
31
    h1.setIP( h1.intfs[ 0 ], '192.168.123.2', 24 )
32 dc630c54 Bob Lantz
    info( str( h0 ) + '\n' )
33
    info( str( h1 ) + '\n' )
34 80be5642 Bob Lantz
35 11782ae0 Bob Lantz
    info( "*** Starting network using Open vSwitch kernel datapath\n" )
36 dc630c54 Bob Lantz
    controller.cmd( cname + ' ' + cargs + '&' )
37 11782ae0 Bob Lantz
    switch.cmd( 'ovs-dpctl del-dp dp0' )
38
    switch.cmd( 'ovs-dpctl add-dp dp0' )
39 efc9a01c Bob Lantz
    for intf in switch.intfs.values():
40 11782ae0 Bob Lantz
        print switch.cmd( 'ovs-dpctl add-if dp0 ' + intf )
41
    print switch.cmd( 'ovs-openflowd dp0 tcp:127.0.0.1 &' )
42 80be5642 Bob Lantz
43 dc630c54 Bob Lantz
    info( "*** Running test\n" )
44
    h0.cmdPrint( 'ping -c1 ' + h1.IP() )
45 80be5642 Bob Lantz
46 dc630c54 Bob Lantz
    info( "*** Stopping network\n" )
47 80be5642 Bob Lantz
    controller.cmd( 'kill %' + cname )
48 11782ae0 Bob Lantz
    switch.cmd( 'ovs-dpctl del-dp dp0' )
49
    switch.cmd( 'kill %ovs-openflowd' )
50 47e26cce Bob Lantz
    switch.deleteIntfs()
51
    info( '\n' )
52 80be5642 Bob Lantz
53 433503cf Bob Lantz
if __name__ == '__main__':
54 d93c6ef6 Bob Lantz
    setLogLevel( 'info' )
55 dc630c54 Bob Lantz
    info( '*** Scratch network demo (kernel datapath)\n' )
56 11782ae0 Bob Lantz
    OVSKernelSwitch.setup()
57 dc630c54 Bob Lantz
    init()
58 efc9a01c Bob Lantz
    scratchNet()