Statistics
| Branch: | Tag: | Revision:

mininet / examples / scratchnet.py @ e113f8ed

History | View | Annotate | Download (1.98 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 8e3699ec Bob Lantz
from mininet.net import Mininet
12
from mininet.node import Node
13 a49c85a6 Bob Lantz
from mininet.link import Link
14 d93c6ef6 Bob Lantz
from mininet.log import setLogLevel, info
15 a49c85a6 Bob Lantz
from mininet.util import quietRun
16 433503cf Bob Lantz
17 a49c85a6 Bob Lantz
from time import sleep
18
19
def scratchNet( cname='controller', cargs='-v ptcp:' ):
20 8e3699ec Bob Lantz
    "Create network from scratch using Open vSwitch."
21 dc630c54 Bob Lantz
22
    info( "*** Creating nodes\n" )
23 efc9a01c Bob Lantz
    controller = Node( 'c0', inNamespace=False )
24
    switch = Node( 's0', inNamespace=False )
25
    h0 = Node( 'h0' )
26
    h1 = Node( 'h1' )
27 80be5642 Bob Lantz
28 dc630c54 Bob Lantz
    info( "*** Creating links\n" )
29 a49c85a6 Bob Lantz
    Link( h0, switch )
30
    Link( h1, switch )
31 dc630c54 Bob Lantz
32
    info( "*** Configuring hosts\n" )
33 a49c85a6 Bob Lantz
    h0.setIP( '192.168.123.1/24' )
34
    h1.setIP( '192.168.123.2/24' )
35 dc630c54 Bob Lantz
    info( str( h0 ) + '\n' )
36
    info( str( h1 ) + '\n' )
37 80be5642 Bob Lantz
38 a49c85a6 Bob Lantz
    info( "*** Starting network using Open vSwitch\n" )
39 dc630c54 Bob Lantz
    controller.cmd( cname + ' ' + cargs + '&' )
40 a49c85a6 Bob Lantz
    switch.cmd( 'ovs-vsctl del-br dp0' )
41
    switch.cmd( 'ovs-vsctl add-br dp0' )
42 efc9a01c Bob Lantz
    for intf in switch.intfs.values():
43 a49c85a6 Bob Lantz
        print switch.cmd( 'ovs-vsctl add-port dp0 %s' % intf )
44
45
    # Note: controller and switch are in root namespace, and we
46
    # can connect via loopback interface
47
    switch.cmd( 'ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633' )
48
49
    info( '*** Waiting for switch to connect to controller' )
50
    while 'is_connected' not in quietRun( 'ovs-vsctl show' ):
51
        sleep( 1 )
52
        info( '.' )
53
    info( '\n' )
54 80be5642 Bob Lantz
55 dc630c54 Bob Lantz
    info( "*** Running test\n" )
56
    h0.cmdPrint( 'ping -c1 ' + h1.IP() )
57 80be5642 Bob Lantz
58 dc630c54 Bob Lantz
    info( "*** Stopping network\n" )
59 80be5642 Bob Lantz
    controller.cmd( 'kill %' + cname )
60 a49c85a6 Bob Lantz
    switch.cmd( 'ovs-vsctl del-br dp0' )
61 47e26cce Bob Lantz
    switch.deleteIntfs()
62
    info( '\n' )
63 80be5642 Bob Lantz
64 433503cf Bob Lantz
if __name__ == '__main__':
65 d93c6ef6 Bob Lantz
    setLogLevel( 'info' )
66 dc630c54 Bob Lantz
    info( '*** Scratch network demo (kernel datapath)\n' )
67 8e3699ec Bob Lantz
    Mininet.init()
68 efc9a01c Bob Lantz
    scratchNet()