Revision 212399fe examples/mobility.py

View differences:

examples/mobility.py
12 12
manually flush the switch flow tables!
13 13

  
14 14
Good luck!
15

  
16
to-do:
17

  
18
- think about wifi/hub behavior
19
- think about clearing last hop - why doesn't that work?
15 20
"""
16 21

  
17 22
from mininet.net import Mininet
18
from mininet.node import OVSSwitch, Controller
23
from mininet.node import OVSSwitch
19 24
from mininet.topo import LinearTopo
20
from mininet.cli import CLI
21
from mininet.util import dumpNetConnections
22
from mininet.log import output
23
from time import sleep
25
from mininet.util import quietRun
26
from mininet.log import output, warn
24 27

  
28
from random import randint
29
from re import findall
25 30

  
26 31
class MobilitySwitch( OVSSwitch ):
27 32
    "Switch that can reattach and rename interfaces"
28 33

  
34
    @classmethod
35
    def setup( cls ):
36
        "Call our parent method and determine OVS version"
37
        OVSSwitch.setup()
38
        info = quietRun( 'ovs-vsctl --version' )
39
        cls.OVSVersion =  float( findall( '\d+\.\d+', info )[ 0 ] )
40

  
29 41
    def delIntf( self, intf ):
30 42
        "Remove (and detach) an interface"
31 43
        port = self.ports[ intf ]
......
33 45
        del self.intfs[ port ]
34 46
        del self.nameToIntf[ intf.name ]
35 47

  
36
    def addIntf( self, intf, rename=True, **kwargs ):
48
    def addIntf( self, intf, rename=False, **kwargs ):
37 49
        "Add (and reparent) an interface"
38 50
        OVSSwitch.addIntf( self, intf, **kwargs )
39 51
        intf.node = self
40 52
        if rename:
41 53
            self.renameIntf( intf )
42 54

  
55
    def attach( self, intf ):
56
        "Attach an interface and set its port"
57
        port = self.ports[ intf ]
58
        if port:
59
            if MobilitySwitch.OVSVersion >= 1.10:
60
                self.cmd( 'ovs-vsctl add-port', self, intf,
61
                          '-- set Interface', intf,
62
                          'ofport_request=%s' % port )
63
            else:
64
                self.cmd( 'ovs-vsctl add-port', self, intf )
65
            self.validatePort( intf )
66

  
67
    def validatePort( self, intf ):
68
        "Validate intf's OF port number"
69
        ofport = int( self.cmd( 'ovs-vsctl get Interface', intf,
70
                              'ofport' ) )
71
        if ofport != self.ports[ intf ]:
72
            warn( 'WARNING: ofport for', intf, 'is', ofport,
73
                   'but we wanted', self.ports[ intf ], '\n' )
74

  
43 75
    def renameIntf( self, intf, newname='' ):
44 76
        "Rename an interface (to its canonical name)"
45 77
        intf.ifconfig( 'down' )
......
51 83
        self.nameToIntf[ intf.name ] = intf
52 84
        intf.ifconfig( 'up' )
53 85

  
54
    def validatePort( self, intf ):
55
        "Validate intf's OF port number"
56
        ofport = int( intf.cmd( 'ovs-vsctl get Interface', intf,
57
                              'ofport' ) )
58
        assert ofport == self.ports[ intf ]
59

  
60
    def moveIntf( self, intf, switch, rename=True ):
86
    def moveIntf( self, intf, switch, port=None, rename=True ):
61 87
        "Move one of our interfaces to another switch"
62 88
        self.detach( intf )
63 89
        self.delIntf( intf )
64
        switch.addIntf( intf, rename=True )
90
        switch.addIntf( intf, port=port, rename=True )
65 91
        switch.attach( intf )
66
        switch.validatePort( intf )
67 92

  
68 93

  
69 94
def printConnections( switches ):
......
79 104
        output( '\n' )
80 105

  
81 106

  
82
def moveHost( host, oldSwitch, newSwitch ):
107
def moveHost( host, oldSwitch, newSwitch, newPort=None ):
83 108
    "Move a host from old switch to new switch"
84 109
    hintf, sintf = host.connectionsTo( oldSwitch )[ 0 ]
85
    oldSwitch.moveIntf( sintf, newSwitch )
110
    oldSwitch.moveIntf( sintf, newSwitch, port=newPort )
86 111
    return hintf, sintf
87 112

  
88 113

  
......
90 115
    "A simple test of mobility"
91 116
    print '* Simple mobility test'
92 117
    net = Mininet( topo=LinearTopo( 3 ), switch=MobilitySwitch )
93
    net.start()
94 118
    print '* Starting network:'
119
    net.start()
95 120
    printConnections( net.switches )
121
    if MobilitySwitch.OVSVersion < 1.10:
122
        print '* WARNING: port selection may not work in OVS',
123
        print MobilitySwitch.OVSVersion
124
    print '* Testing network'
96 125
    net.pingAll()
97 126
    print '* Identifying switch interface for h1'
98
    h1, last = net.get( 'h1', 's1' )
127
    h1, old = net.get( 'h1', 's1' )
99 128
    for s in 2, 3, 1:
100
        next = net[ 's%d' % s ]
101
        print '* Moving', h1, 'from', last, 'to', next
102
        hintf, sintf = moveHost( h1, last, next )
129
        new = net[ 's%d' % s ]
130
        port = randint( 10, 20 )
131
        print '* Moving', h1, 'from', old, 'to', new, 'port', port
132
        hintf, sintf = moveHost( h1, old, new, newPort=port )
103 133
        print '*', hintf, 'is now connected to', sintf
104 134
        print '* Clearing out old flows'
105 135
        for sw in net.switches:
......
108 138
        printConnections( net.switches )
109 139
        print '* Testing connectivity:'
110 140
        net.pingAll()
111
        last = next
141
        old = new
112 142
    net.stop()
113 143

  
114 144
if __name__ == '__main__':
115 145
    mobilityTest()
116

  

Also available in: Unified diff