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