Statistics
| Branch: | Tag: | Revision:

mininet / examples / numberedports.py @ 18aab5b7

History | View | Annotate | Download (2.27 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 testPortNumbering():
24

    
25
    """Test port numbering:
26
       Create a network with 5 hosts (using Mininet's
27
       mid-level API) and check that implicit and
28
       explicit port numbering works as expected."""
29

    
30
    net = Mininet( controller=Controller )
31

    
32
    info( '*** Adding controller\n' )
33
    net.addController( 'c0' )
34

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

    
42
    info( '*** Adding switch\n' )
43
    s1 = net.addSwitch( 's1' )
44

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

    
54
    info( '*** Starting network\n' )
55
    net.start()
56

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

    
70
    # test the network with pingall
71
    net.pingAll()
72
    print '\n'
73

    
74
    info( '*** Stopping network' )
75
    net.stop()
76

    
77
if __name__ == '__main__':
78
    setLogLevel( 'info' )
79
    testPortNumbering()