Statistics
| Branch: | Tag: | Revision:

mininet / bin / mn_run.py @ 16c57ddb

History | View | Annotate | Download (4.51 KB)

1 a6b47322 Brandon Heller
#!/usr/bin/env python
2
'''Mininet runner
3

4
@author Brandon Heller (brandonh@stanford.edu)
5
'''
6
7
from optparse import OptionParser
8
import time
9
10
from ripcord.topo import FatTreeTopo
11
12
from mininet.logging_mod import lg, set_loglevel, LEVELS
13
from mininet.net import Mininet, init
14 4804237f Brandon Heller
from mininet.node import Switch, Host, Controller, ControllerParams, NOX
15 a6b47322 Brandon Heller
from mininet.topo import TreeTopo
16
17
# built in topologies, created only when run
18
TOPO_DEF = 'minimal'
19
TOPOS = {'minimal' :  (lambda: TreeTopo(depth = 2, fanout = 2)),
20
         'tree16' :   (lambda: TreeTopo(depth = 3, fanout = 4)),
21
         'tree64' :   (lambda: TreeTopo(depth = 4, fanout = 4)),
22 83086439 Brandon Heller
         'tree1024' : (lambda: TreeTopo(depth = 3, fanout = 32)),
23 a6b47322 Brandon Heller
         'fattree4' : (lambda: FatTreeTopo(k = 4)),
24
         'fattree6' : (lambda: FatTreeTopo(k = 6))}
25
26
SWITCH_DEF = 'kernel'
27
SWITCHES = {'kernel' : Switch}
28
29
HOST_DEF = 'process'
30
HOSTS = {'process' : Host}
31
32
CONTROLLER_DEF = 'ref'
33 16c57ddb Brandon Heller
# a and b are the name and inNamespace params.
34 83086439 Brandon Heller
CONTROLLERS = {'ref' : Controller,
35 4804237f Brandon Heller
               'nox_dump' : lambda a, b: NOX(a, b, 'packetdump'),
36 16c57ddb Brandon Heller
               'nox_pysw' : lambda a, b: NOX(a, b, 'pyswitch'),
37
               'none' :     lambda a, b: None}
38 a6b47322 Brandon Heller
39
# optional tests to run
40 83086439 Brandon Heller
TESTS = ['cli', 'build', 'ping_all', 'ping_pair', 'iperf', 'all']
41 a6b47322 Brandon Heller
42
def add_dict_option(opts, choices_dict, default, name, help_str = None):
43
    '''Convenience function to add choices dicts to OptionParser.
44
    
45
    @param opts OptionParser instance
46
    @param choices_dict dictionary of valid choices, must include default
47
    @param default default choice key
48
    @param name long option name
49
    @param help string
50
    '''
51
    if default not in choices_dict:
52
        raise Exception('Invalid  default %s for choices dict: %s' %
53
                        (default, name))
54
    if not help_str:
55
        help_str = '[' + ' '.join(choices_dict.keys()) + ']'
56
    opts.add_option('--' + name,
57
                    type = 'choice',
58
                    choices = choices_dict.keys(),
59
                    default = default,
60
                    help = help_str)
61
62
63
class MininetRunner(object):
64
    '''Build, setup, and run Mininet.'''
65
66
    def __init__(self):
67
        '''Init.'''
68
        self.options = None
69
        
70
        self.parse_args()
71
        self.setup()
72
        self.begin()
73
74
    def parse_args(self):
75
        '''Parse command-line args and return options object.
76
        
77
        @return opts parse options dict
78
        '''
79
        opts = OptionParser()
80
        add_dict_option(opts, TOPOS, TOPO_DEF, 'topo')
81
        add_dict_option(opts, SWITCHES, SWITCH_DEF, 'switch')
82
        add_dict_option(opts, HOSTS, HOST_DEF, 'host')
83
        add_dict_option(opts, CONTROLLERS, CONTROLLER_DEF, 'controller')
84
85
        opts.add_option('--test', type = 'choice', choices = TESTS,
86
                        default = TESTS[0],
87
                        help = '[' + ' '.join(TESTS) + ']')
88
        opts.add_option('--xterm', '-x', action = 'store_true',
89
                        default = False, help = 'spawn xterms for each node')
90
        opts.add_option('--verbosity', '-v', type = 'choice',
91
                        choices = LEVELS.keys(), default = 'info',
92
                        help = '[' + ' '.join(LEVELS.keys()) + ']')
93
        self.options = opts.parse_args()[0]
94
95
    def setup(self):
96
        '''Setup and validate environment.'''
97
98
        # set logging verbosity
99
        set_loglevel(self.options.verbosity)
100
101
        # validate environment setup
102
        init()
103
104 83086439 Brandon Heller
        # check for invalid combinations
105
        if 'fattree' in self.options.topo and self.options.controller == 'ref':
106
            raise Exception('multipath topos require multipath-capable '
107
                            'controller.')
108
109 a6b47322 Brandon Heller
    def begin(self):
110
        '''Create and run mininet.'''
111
112
        start = time.time()
113
114
        topo = TOPOS[self.options.topo]() # build topology object
115
        switch = SWITCHES[self.options.switch]
116
        host = HOSTS[self.options.host]
117
        controller = CONTROLLERS[self.options.controller]
118
119
        controller_params = ControllerParams(0x0a000000, 8) # 10.0.0.0/8
120
        mn = Mininet(topo, switch, host, controller, controller_params)
121
122 eeb9cb3c Brandon Heller
        test = self.options.test
123
        if test != 'build':
124
            if test == 'cli':
125 a6b47322 Brandon Heller
                mn.interact()
126 83086439 Brandon Heller
            elif test == 'all':
127
                mn.start()
128
                mn.ping()
129
                mn.iperf()
130 a6b47322 Brandon Heller
                mn.stop()
131
            else:
132 eeb9cb3c Brandon Heller
                mn.run(test)
133 a6b47322 Brandon Heller
134
        elapsed = float(time.time() - start)
135
        print ('completed in %0.3f seconds' % elapsed)
136
137
138
if __name__ == "__main__":
139 83086439 Brandon Heller
    MininetRunner()