Revision a6bcad8f mininet/net.py

View differences:

mininet/net.py
1 1
"""
2 2

  
3
    Mininet: A simple networking testbed for OpenFlow!
3
    Mininet: A simple networking testbed for OpenFlow/SDN!
4 4

  
5 5
author: Bob Lantz (rlantz@cs.stanford.edu)
6 6
author: Brandon Heller (brandonh@stanford.edu)
......
96 96
from mininet.log import info, error, debug, output
97 97
from mininet.node import Host, UserSwitch, OVSKernelSwitch, Controller
98 98
from mininet.node import ControllerParams
99
from mininet.link import Link
99 100
from mininet.util import quietRun, fixLimits
100 101
from mininet.util import createLink, macColonHex, ipStr, ipParse
101 102
from mininet.term import cleanUpScreens, makeTerms
......
104 105
    "Network emulation with hosts spawned in network namespaces."
105 106

  
106 107
    def __init__( self, topo=None, switch=OVSKernelSwitch, host=Host,
107
                 controller=Controller,
108
                 controller=Controller, link=Link,
108 109
                 cparams=ControllerParams( '10.0.0.0', 8 ),
109 110
                 build=True, xterms=False, cleanup=False,
110 111
                 inNamespace=False,
111 112
                 autoSetMacs=False, autoStaticArp=False, listenPort=None ):
112 113
        """Create Mininet object.
113 114
           topo: Topo (topology) object or None
114
           switch: Switch class
115
           host: Host class
116
           controller: Controller class
115
           switch: default Switch class
116
           host: default Host class/constructor
117
           controller: default Controller class/constructor
118
           link: default Link class/constructor
117 119
           cparams: ControllerParams object
118 120
           build: build now from topo?
119 121
           xterms: if build now, spawn xterms?
......
126 128
        self.switch = switch
127 129
        self.host = host
128 130
        self.controller = controller
131
        self.link = link
129 132
        self.cparams = cparams
130 133
        self.topo = topo
131 134
        self.inNamespace = inNamespace
......
150 153
        if topo and build:
151 154
            self.build()
152 155

  
153
    def addHost( self, name, mac=None, ip=None ):
156
    # BL Note:
157
    # The specific items for host/switch/etc. should probably be
158
    # handled in the node classes rather than here!!
159

  
160
    def addHost( self, name, mac=None, ip=None, host=None, **params ):
154 161
        """Add host.
155 162
           name: name of host to add
156 163
           mac: default MAC address for intf 0
157 164
           ip: default IP address for intf 0
158 165
           returns: added host"""
159
        host = self.host( name, defaultMAC=mac, defaultIP=ip )
160
        self.hosts.append( host )
161
        self.nameToNode[ name ] = host
162
        return host
163

  
164
    def addSwitch( self, name, mac=None, ip=None ):
166
        if not host:
167
            host = self.host
168
        defaults = { 'defaultMAC': mac, 'defaultIP': ip }
169
        defaults.update( params )
170
        h = host( name, **defaults)
171
        self.hosts.append( h )
172
        self.nameToNode[ name ] = h
173
        return h
174

  
175
    def addSwitch( self, name, switch=None, **params ):
165 176
        """Add switch.
166 177
           name: name of switch to add
167
           mac: default MAC address for kernel/OVS switch intf 0
168 178
           returns: added switch
169
           side effect: increments the listenPort member variable."""
170
        if self.switch == UserSwitch:
171
            sw = self.switch( name, listenPort=self.listenPort,
172
                defaultMAC=mac, defaultIP=ip, inNamespace=self.inNamespace )
173
        else:
174
            sw = self.switch( name, listenPort=self.listenPort,
175
                defaultMAC=mac, defaultIP=ip, dp=self.dps,
176
                inNamespace=self.inNamespace )
179
           side effect: increments listenPort and dps ivars."""
180
        defaults = { 'listenPort': self.listenPort, 
181
                     'inNamespace': self.inNamespace }
182
        if not switch:
183
            switch = self.switch
184
        if switch != UserSwitch:
185
            defaults[ 'dps' ] = self.dps
186
        defaults.update( params )
187
        sw = self.switch( name, **defaults )
177 188
        if not self.inNamespace and self.listenPort:
178 189
            self.listenPort += 1
179 190
        self.dps += 1
......
181 192
        self.nameToNode[ name ] = sw
182 193
        return sw
183 194

  
184
    def addController( self, name='c0', controller=None, **kwargs ):
195
    def addController( self, name='c0', controller=None, **params ):
185 196
        """Add controller.
186 197
           controller: Controller class"""
187 198
        if not controller:
188 199
            controller = self.controller
189
        controller_new = controller( name, **kwargs )
200
        controller_new = controller( name, **params )
190 201
        if controller_new:  # allow controller-less setups
191 202
            self.controllers.append( controller_new )
192 203
            self.nameToNode[ name ] = controller_new
......
210 221
    # 4. Even if we dispense with this in general, it could still be
211 222
    #    useful for people who wish to simulate a separate control
212 223
    #    network (since real networks may need one!)
224
    #
225
    # 5. Basically nobody ever uses this method, so perhaps it should be moved
226
    #    out of this core class.
213 227

  
214 228
    def configureControlNetwork( self ):
215 229
        "Configure control network."
......
221 235
    def configureRoutedControlNetwork( self, ip='192.168.123.1',
222 236
        prefixLen=16 ):
223 237
        """Configure a routed control network on controller and switches.
224
           For use with the user datapath only right now.
225
           """
238
           For use with the user datapath only right now."""
226 239
        controller = self.controllers[ 0 ]
227 240
        info( controller.name + ' <->' )
228 241
        cip = ip
......
256 269
        "Configure a set of hosts."
257 270
        # params were: hosts, ips
258 271
        for host in self.hosts:
259
            hintf = host.intfs[ 0 ]
260
            host.setIP( hintf, host.defaultIP, self.cparams.prefixLen )
272
            hintf = host.defaultIntf()
273
            host.setIP( host.defaultIP, self.cparams.prefixLen, hintf )
261 274
            host.setDefaultRoute( hintf )
262 275
            # You're low priority, dude!
263 276
            quietRun( 'renice +18 -p ' + repr( host.pid ) )
......
272 285
        def addNode( prefix, addMethod, nodeId ):
273 286
            "Add a host or a switch."
274 287
            name = prefix + topo.name( nodeId )
288
            # MAC and IP should probably be from nodeInfo...
275 289
            mac = macColonHex( nodeId ) if self.setMacs else None
276 290
            ip = topo.ip( nodeId )
277
            node = addMethod( name, mac=mac, ip=ip )
291
            ni = topo.nodeInfo( nodeId )
292
            node = addMethod( name, cls=ni.cls, mac=mac, ip=ip, **ni.params )
278 293
            self.idToNode[ nodeId ] = node
279 294
            info( name + ' ' )
280 295

  
......
291 306
            addNode( 'h', self.addHost, hostId )
292 307
        info( '\n*** Adding switches:\n' )
293 308
        for switchId in sorted( topo.switches() ):
294
            addNode( 's', self.addSwitch, switchId )
309
            addNode( 's', self.addSwitch, switchId)
295 310
        info( '\n*** Adding links:\n' )
296 311
        for srcId, dstId in sorted( topo.edges() ):
297 312
            src, dst = self.idToNode[ srcId ], self.idToNode[ dstId ]
298 313
            srcPort, dstPort = topo.port( srcId, dstId )
299
            createLink( src, dst, srcPort, dstPort )
314
            ei = topo.edgeInfo( srcId, dstId )
315
            link, params = ei.cls, ei.params
316
            if not link:
317
                link = self.link
318
            link( src, dst, srcPort, dstPort, **params )
300 319
            info( '(%s, %s) ' % ( src.name, dst.name ) )
301 320
        info( '\n' )
302 321

  
......
510 529
            servout += server.monitor()
511 530
        while 'Connected' not in client.cmd(
512 531
            'sh -c "echo A | telnet -e A %s 5001"' % server.IP()):
513
            output('waiting for iperf to start up')
532
            output('waiting for iperf to start up...')
514 533
            sleep(.5)
515 534
        cliout = client.cmd( iperfArgs + '-t 5 -c ' + server.IP() + ' ' +
516 535
                           bwArgs )

Also available in: Unified diff