Statistics
| Branch: | Tag: | Revision:

mininet / examples / scratchnetuser.py @ 9bfc7c77

History | View | Annotate | Download (2.4 KB)

1 98332cb6 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 98332cb6 Bob Lantz

8 b1458654 Bob Lantz
For most tasks, the higher-level API will be preferable.
9

10 80be5642 Bob Lantz
This version uses the user datapath and an explicit control network.
11 98332cb6 Bob Lantz
"""
12
13 8e3699ec Bob Lantz
from mininet.net import Mininet
14 dc630c54 Bob Lantz
from mininet.node import Node
15 a49c85a6 Bob Lantz
from mininet.link import Link
16 fe414722 Bob Lantz
from mininet.log import setLogLevel, info
17 98332cb6 Bob Lantz
18 a49c85a6 Bob Lantz
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 47e26cce Bob Lantz
def scratchNetUser( cname='controller', cargs='ptcp:' ):
24 80be5642 Bob Lantz
    "Create network from scratch using user switch."
25
26 dc630c54 Bob Lantz
    # 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 a49c85a6 Bob Lantz
    cintf, sintf = linkIntfs( controller, switch )
37
    h0intf, sintf1 = linkIntfs( h0, switch )
38
    h1intf, sintf2 = linkIntfs( h1, switch )
39 dc630c54 Bob Lantz
40
    info( '*** Configuring control network\n' )
41 bf208cde Brandon Heller
    controller.setIP( '10.0.123.1/24', intf=cintf )
42
    switch.setIP( '10.0.123.2/24', intf=sintf)
43 80be5642 Bob Lantz
44 dc630c54 Bob Lantz
    info( '*** Configuring hosts\n' )
45 bf208cde Brandon Heller
    h0.setIP( '192.168.123.1/24', intf=h0intf )
46
    h1.setIP( '192.168.123.2/24', intf=h1intf )
47 80be5642 Bob Lantz
48 dc630c54 Bob Lantz
    info( '*** Network state:\n' )
49
    for node in controller, switch, h0, h1:
50
        info( str( node ) + '\n' )
51 80be5642 Bob Lantz
52 dc630c54 Bob Lantz
    info( '*** Starting controller and user datapath\n' )
53
    controller.cmd( cname + ' ' + cargs + '&' )
54
    switch.cmd( 'ifconfig lo 127.0.0.1' )
55 14ff3ad3 Bob Lantz
    intfs = [ str( i ) for i in sintf1, sintf2 ]
56 dc630c54 Bob Lantz
    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 47e26cce Bob Lantz
    switch.deleteIntfs()
67
    info( '\n' )
68 80be5642 Bob Lantz
69 98332cb6 Bob Lantz
if __name__ == '__main__':
70 fe414722 Bob Lantz
    setLogLevel( 'info' )
71 dc630c54 Bob Lantz
    info( '*** Scratch network demo (user datapath)\n' )
72 8e3699ec Bob Lantz
    Mininet.init()
73 dc630c54 Bob Lantz
    scratchNetUser()