Statistics
| Branch: | Tag: | Revision:

mininet / examples / scratchnet.py @ dc630c54

History | View | Annotate | Download (1.62 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 433503cf Bob Lantz
"""
8
9 efc9a01c Bob Lantz
import logging
10
11
from mininet.net import init
12
from mininet.node import Node
13
from mininet.util import createLink
14 dc630c54 Bob Lantz
from mininet.log import lg, info
15 433503cf Bob Lantz
16
def scratchNet( cname='controller', cargs='ptcp:'):
17 dc630c54 Bob Lantz
18
    info( "*** Creating nodes\n" )
19 efc9a01c Bob Lantz
    controller = Node( 'c0', inNamespace=False )
20
    switch = Node( 's0', inNamespace=False )
21
    h0 = Node( 'h0' )
22
    h1 = Node( 'h1' )
23 dc630c54 Bob Lantz
    
24
    info( "*** Creating links\n" )
25 efc9a01c Bob Lantz
    createLink( node1=h0, port1=0, node2=switch, port2=0 )
26
    createLink( node1=h1, port1=0, node2=switch, port2=1 )
27 dc630c54 Bob Lantz
28
    info( "*** Configuring hosts\n" )
29 efc9a01c Bob Lantz
    h0.setIP( h0.intfs[ 0 ], '192.168.123.1', '/24' )
30
    h1.setIP( h1.intfs[ 0 ], '192.168.123.2', '/24' )
31 dc630c54 Bob Lantz
    info( str( h0 ) + '\n' )
32
    info( str( h1 ) + '\n' )
33
    
34
    info( "*** Starting network using kernel datapath\n" )
35
    controller.cmd( cname + ' ' + cargs + '&' )
36
    switch.cmd( 'dpctl deldp nl:0' )
37
    switch.cmd( 'dpctl adddp nl:0' )
38 efc9a01c Bob Lantz
    for intf in switch.intfs.values():
39 dc630c54 Bob Lantz
      switch.cmd( 'dpctl addif nl:0 ' + intf )
40
    switch.cmd( 'ofprotocol nl:0 tcp:localhost &')
41
    
42
    info( "*** Running test\n" )
43
    h0.cmdPrint( 'ping -c1 ' + h1.IP() )
44
    
45
    info( "*** Stopping network\n" )
46
    controller.cmd( 'kill %' + cname)
47
    switch.cmd( 'dpctl deldp nl:0' )
48
    switch.cmd( 'kill %ofprotocol' )
49 433503cf Bob Lantz
   
50
if __name__ == '__main__':
51 dc630c54 Bob Lantz
    info( '*** Scratch network demo (kernel datapath)\n' )
52
    init()
53
    lg.setLogLevel( 'info' )
54 efc9a01c Bob Lantz
    scratchNet()