Statistics
| Branch: | Tag: | Revision:

mininet / examples / numberedports.py @ e0bf8ece

History | View | Annotate | Download (2.07 KB)

1
#!/usr/bin/python
2

    
3
"""
4
Create a network with 5 hosts, numbered 1-4 and 9. 
5
Validate that the port numbers match to the interface name,
6
and that the ovs ports match the mininet ports.
7
"""
8

    
9
from mininet.net import Mininet
10
from mininet.node import Controller
11
from mininet.log import setLogLevel, info, warn
12

    
13
def validatePort( switch, intf ):
14
    "Validate intf's OF port number"
15
    ofport = int( switch.cmd( 'ovs-vsctl get Interface', intf,
16
                          'ofport' ) )
17
    if ofport != switch.ports[ intf ]:
18
        warn( 'WARNING: ofport for', intf, 'is actually', ofport, '\n' )
19
        return 0
20
    else:
21
        return 1
22

    
23
def net():
24

    
25
    "Create a network with 5 hosts."
26

    
27
    net = Mininet( controller=Controller )
28

    
29
    info( '*** Adding controller\n' )
30
    net.addController( 'c0' )
31

    
32
    info( '*** Adding hosts\n' )
33
    h1 = net.addHost( 'h1', ip='10.0.0.1' )
34
    h2 = net.addHost( 'h2', ip='10.0.0.2' )
35
    h3 = net.addHost( 'h3', ip='10.0.0.3' )
36
    h4 = net.addHost( 'h4', ip='10.0.0.4' )
37
    h5 = net.addHost( 'h5', ip='10.0.0.5' )
38

    
39
    info( '*** Adding switch\n' )
40
    s1 = net.addSwitch( 's1' )
41

    
42
    info( '*** Creating links\n' )
43
    # host 1-4 connect to ports 1-4 on the switch
44
    net.addLink( h1, s1 )
45
    net.addLink( h2, s1 )
46
    net.addLink( h3, s1 )
47
    net.addLink( h4, s1 )
48
    net.addLink( h5, s1, port1 = 1, port2 = 9 ) # specify a different port to connect host 5 to on the switch.
49

    
50
    info( '*** Starting network\n' )
51
    net.start()
52

    
53
    # print the interfaces and their port numbers
54
    info( '\n*** printing and validating the ports running on each interface\n' )
55
    for intfs in s1.intfList():
56
        if not intfs.name == "lo":
57
            info( intfs, ': ', s1.ports[intfs], 
58
            '\n' )
59
            info ( 'Validating that', intfs, 'is actually on port', s1.ports[intfs], '... ' )
60
            if validatePort( s1, intfs ):
61
                info( 'Validated.\n' )
62
    print '\n'
63
        
64
    # test the network with pingall
65
    net.pingAll()
66
    print '\n'
67

    
68
    info( '*** Stopping network' )
69
    net.stop()
70

    
71
if __name__ == '__main__':
72
    setLogLevel( 'info' )
73
    net()
74