carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 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 Mininet |
| 12 | from mininet.node import Node |
| 13 | from mininet.link import Link |
| 14 | from mininet.log import setLogLevel, info |
| 15 | from mininet.util import quietRun |
| 16 | |
| 17 | from time import sleep |
| 18 | |
| 19 | def scratchNet( cname='controller', cargs='-v ptcp:' ): |
| 20 | "Create network from scratch using Open vSwitch." |
| 21 | |
| 22 | info( "*** Creating nodes\n" ) |
| 23 | controller = Node( 'c0', inNamespace=False ) |
| 24 | switch = Node( 's0', inNamespace=False ) |
| 25 | h0 = Node( 'h0' ) |
| 26 | h1 = Node( 'h1' ) |
| 27 | |
| 28 | info( "*** Creating links\n" ) |
| 29 | Link( h0, switch ) |
| 30 | Link( h1, switch ) |
| 31 | |
| 32 | info( "*** Configuring hosts\n" ) |
| 33 | h0.setIP( '192.168.123.1/24' ) |
| 34 | h1.setIP( '192.168.123.2/24' ) |
| 35 | info( str( h0 ) + '\n' ) |
| 36 | info( str( h1 ) + '\n' ) |
| 37 | |
| 38 | info( "*** Starting network using Open vSwitch\n" ) |
| 39 | controller.cmd( cname + ' ' + cargs + '&' ) |
| 40 | switch.cmd( 'ovs-vsctl del-br dp0' ) |
| 41 | switch.cmd( 'ovs-vsctl add-br dp0' ) |
| 42 | for intf in switch.intfs.values(): |
| 43 | 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 | |
| 55 | info( "*** Running test\n" ) |
| 56 | h0.cmdPrint( 'ping -c1 ' + h1.IP() ) |
| 57 | |
| 58 | info( "*** Stopping network\n" ) |
| 59 | controller.cmd( 'kill %' + cname ) |
| 60 | switch.cmd( 'ovs-vsctl del-br dp0' ) |
| 61 | switch.deleteIntfs() |
| 62 | info( '\n' ) |
| 63 | |
| 64 | if __name__ == '__main__': |
| 65 | setLogLevel( 'info' ) |
| 66 | info( '*** Scratch network demo (kernel datapath)\n' ) |
| 67 | Mininet.init() |
| 68 | scratchNet() |