Revision 376bcba4

View differences:

bin/mn_run.py
99 99
                        help = '[' + ' '.join(TESTS) + ']')
100 100
        opts.add_option('--xterms', '-x', action = 'store_true',
101 101
                        default = False, help = 'spawn xterms for each node')
102
        opts.add_option('--mac', action = 'store_true',
103
                        default = False, help = 'set host MACs equal to DPIDs')
104
        opts.add_option('--arp', action = 'store_true',
105
                        default = False, help = 'set all-pairs ARP entries')
102 106
        opts.add_option('--verbosity', '-v', type = 'choice',
103 107
                        choices = LEVELS.keys(), default = 'info',
104 108
                        help = '[' + ' '.join(LEVELS.keys()) + ']')
......
131 135

  
132 136
        controller_params = ControllerParams(0x0a000000, 8) # 10.0.0.0/8
133 137
        xterms = self.options.xterms
138
        mac = self.options.mac
139
        arp = self.options.arp
134 140
        mn = Mininet(topo, switch, host, controller, controller_params,
135
                     xterms = xterms)
141
                     xterms = xterms, auto_set_macs = mac,
142
                     auto_static_arp = arp)
136 143

  
137 144
        test = self.options.test
138 145
        if test != 'build':
mininet/net.py
79 79

  
80 80
    def __init__(self, topo, switch, host, controller, cparams,
81 81
                 build = True, xterms = False, cleanup = False,
82
                 in_namespace = False, switch_is_kernel = True):
82
                 in_namespace = False, switch_is_kernel = True,
83
                 auto_set_macs = False, auto_static_arp = False):
83 84
        '''Create Mininet object.
84 85

  
85 86
        @param topo Topo object
......
91 92
        @param xterms if build now, spawn xterms?
92 93
        @param cleanup if build now, cleanup before creating?
93 94
        @param in_namespace spawn switches and hosts in their own namespace?
95
        @param switch_is_kernel is the switch kernel-based?
96
        @param auto_set_macs set MAC addrs to DPIDs?
97
        @param auto_static_arp set all-pairs static MAC addrs?
94 98
        '''
95 99
        self.topo = topo
96 100
        self.switch = switch
......
102 106
        self.dps = 0 # number of created kernel datapaths
103 107
        self.in_namespace = in_namespace
104 108
        self.switch_is_kernel = switch_is_kernel
109
        self.xterms = xterms
110
        self.cleanup = cleanup
111
        self.auto_set_macs = auto_set_macs
112
        self.auto_static_arp = auto_static_arp
105 113

  
106 114
        self.terms = [] # list of spawned xterm processes
107 115

  
108 116
        self.kernel = True #temporary!
109 117

  
110 118
        if build:
111
            self.build(xterms, cleanup)
119
            self.build()
112 120

  
113 121
    def _add_host(self, dpid):
114 122
        '''Add host.
......
249 257
            lg.info('%s ', host.name)
250 258
        lg.info('\n')
251 259

  
252
    def build(self, xterms, cleanup):
260
    def build(self):
253 261
        '''Build mininet.
254 262

  
255 263
        At the end of this function, everything should be connected and up.
256

  
257
        @param xterms spawn xterms on build?
258
        @param cleanup cleanup before creating?
259 264
        '''
260
        if cleanup:
265
        if self.cleanup:
261 266
            pass # cleanup
262 267
        # validate topo?
263 268
        kernel = self.kernel
......
289 294
        lg.info('*** Configuring hosts\n')
290 295
        self._config_hosts()
291 296

  
292
        if xterms:
297
        if self.xterms:
293 298
            self.start_xterms()
299
        if self.auto_set_macs:
300
            self.set_macs()
301
        if self.auto_static_arp:
302
            self.static_arp()
294 303

  
295 304
    def switch_nodes(self):
296 305
        '''Return switch nodes.'''
......
315 324
            os.kill(term.pid, signal.SIGKILL)
316 325
        cleanUpScreens()
317 326

  
327
    def set_macs(self):
328
        '''Set MAC addrs to correspond to datapath IDs on hosts.
329

  
330
        Assume that the host only has one interface.
331
        '''
332
        for dpid in self.topo.hosts():
333
            host_node = self.nodes[dpid]
334
            host_node.setMAC(host_node.intfs[0], dpid)
335

  
336
    def static_arp(self):
337
        '''Add all-pairs ARP entries to remove the need to handle broadcast.'''
338
        for src in self.topo.hosts():
339
            src_node = self.nodes[src]
340
            for dst in self.topo.hosts():
341
                if src != dst:
342
                    src_node.setARP(dst, dst)
343

  
318 344
    def start(self):
319 345
        '''Start controller and switches\n'''
320 346
        lg.info('*** Starting controller\n')
mininet/node.py
6 6
flush = sys.stdout.flush
7 7

  
8 8
from mininet.logging_mod import lg
9
from mininet.util import quietRun
9
from mininet.util import quietRun, macColonHex, ipStr
10 10

  
11 11
class Node(object):
12 12
    '''A virtual network node is simply a shell in a network namespace.
......
160 160
        self.intfs += [intfName]
161 161
        return intfName
162 162

  
163
    def setMAC(self, intf, mac):
164
        '''Set the MAC address for an interface.
165

  
166
        @param mac MAC address as unsigned int
167
        '''
168
        mac_str = macColonHex(mac)
169
        result = self.cmd(['ifconfig', intf, 'down'])
170
        result += self.cmd(['ifconfig', intf, 'hw', 'ether', mac_str])
171
        result += self.cmd(['ifconfig', intf, 'up'])
172
        return result
173

  
174
    def setARP(self, ip, mac):
175
        '''Add an ARP entry.
176

  
177
        @param ip IP address as unsigned int
178
        @param mac MAC address as unsigned int
179
        '''
180
        ip_str = ipStr(ip)
181
        mac_str = macColonHex(mac)
182
        result = self.cmd(['arp', '-s', ip_str, mac_str])
183
        return result
184

  
163 185
    def setIP(self, intf, ip, bits):
164 186
        '''Set the IP address for an interface.
165 187

  
mininet/util.py
175 175
def fixLimits():
176 176
    '''Fix ridiculously small resource limits.'''
177 177
    setrlimit( RLIMIT_NPROC, (4096, 8192))
178
    setrlimit( RLIMIT_NOFILE, (16384, 32768))
178
    setrlimit( RLIMIT_NOFILE, (16384, 32768))
179

  
180
def macColonHex(mac):
181
    '''Generate MAC colon-hex string from unsigned int.
182

  
183
    @param mac MAC address as unsigned int
184
    @return mac_str MAC colon-hex string
185
    '''
186
    mac_pieces = []
187
    for i in range (5, -1, -1):
188
        mac_pieces.append('%02x' % (((0xff << (i * 8)) & mac) >> (i * 8)))
189
    mac_str = ':'.join(mac_pieces)
190
    return mac_str
191

  
192
def ipStr(ip):
193
    '''Generate IP address string
194

  
195
    @return ip addr string
196
    '''
197
    hi = (ip & 0xff0000) >> 16
198
    mid = (ip & 0xff00) >> 8
199
    lo = ip & 0xff
200
    return "10.%i.%i.%i" % (hi, mid, lo)

Also available in: Unified diff