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 | This version uses the user datapath and an explicit control network. |
| 11 | """ |
| 12 | |
| 13 | from mininet.net import Mininet |
| 14 | from mininet.node import Node |
| 15 | from mininet.link import Link |
| 16 | from mininet.log import setLogLevel, info |
| 17 | |
| 18 | def linkIntfs( node1, node2 ): |
| 19 | "Create link from node1 to node2 and return intfs" |
| 20 | link = Link( node1, node2 ) |
| 21 | return link.intf1, link.intf2 |
| 22 | |
| 23 | def scratchNetUser( cname='controller', cargs='ptcp:' ): |
| 24 | "Create network from scratch using user switch." |
| 25 | |
| 26 | # It's not strictly necessary for the controller and switches |
| 27 | # to be in separate namespaces. For performance, they probably |
| 28 | # should be in the root namespace. However, it's interesting to |
| 29 | # see how they could work even if they are in separate namespaces. |
| 30 | |
| 31 | info( '*** Creating Network\n' ) |
| 32 | controller = Node( 'c0' ) |
| 33 | switch = Node( 's0') |
| 34 | h0 = Node( 'h0' ) |
| 35 | h1 = Node( 'h1' ) |
| 36 | cintf, sintf = linkIntfs( controller, switch ) |
| 37 | h0intf, sintf1 = linkIntfs( h0, switch ) |
| 38 | h1intf, sintf2 = linkIntfs( h1, switch ) |
| 39 | |
| 40 | info( '*** Configuring control network\n' ) |
| 41 | controller.setIP( '10.0.123.1/24', intf=cintf ) |
| 42 | switch.setIP( '10.0.123.2/24', intf=sintf) |
| 43 | |
| 44 | info( '*** Configuring hosts\n' ) |
| 45 | h0.setIP( '192.168.123.1/24', intf=h0intf ) |
| 46 | h1.setIP( '192.168.123.2/24', intf=h1intf ) |
| 47 | |
| 48 | info( '*** Network state:\n' ) |
| 49 | for node in controller, switch, h0, h1: |
| 50 | info( str( node ) + '\n' ) |
| 51 | |
| 52 | info( '*** Starting controller and user datapath\n' ) |
| 53 | controller.cmd( cname + ' ' + cargs + '&' ) |
| 54 | switch.cmd( 'ifconfig lo 127.0.0.1' ) |
| 55 | intfs = [ str( i ) for i in sintf1, sintf2 ] |
| 56 | switch.cmd( 'ofdatapath -i ' + ','.join( intfs ) + ' ptcp: &' ) |
| 57 | switch.cmd( 'ofprotocol tcp:' + controller.IP() + ' tcp:localhost &' ) |
| 58 | |
| 59 | info( '*** Running test\n' ) |
| 60 | h0.cmdPrint( 'ping -c1 ' + h1.IP() ) |
| 61 | |
| 62 | info( '*** Stopping network\n' ) |
| 63 | controller.cmd( 'kill %' + cname ) |
| 64 | switch.cmd( 'kill %ofdatapath' ) |
| 65 | switch.cmd( 'kill %ofprotocol' ) |
| 66 | switch.deleteIntfs() |
| 67 | info( '\n' ) |
| 68 | |
| 69 | if __name__ == '__main__': |
| 70 | setLogLevel( 'info' ) |
| 71 | info( '*** Scratch network demo (user datapath)\n' ) |
| 72 | Mininet.init() |
| 73 | scratchNetUser() |