Revision a6b47322

View differences:

bin/mn_run.py
1
#!/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
from mininet.node import Switch, Host, Controller, ControllerParams
15
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
         'fattree4' : (lambda: FatTreeTopo(k = 4)),
23
         'fattree6' : (lambda: FatTreeTopo(k = 6))}
24

  
25
SWITCH_DEF = 'kernel'
26
SWITCHES = {'kernel' : Switch}
27

  
28
HOST_DEF = 'process'
29
HOSTS = {'process' : Host}
30

  
31
CONTROLLER_DEF = 'ref'
32
CONTROLLERS = {'ref' : Controller}
33

  
34
# optional tests to run
35
TESTS = ['cli', 'build', 'ping_all', 'ping_pair']
36

  
37

  
38
def add_dict_option(opts, choices_dict, default, name, help_str = None):
39
    '''Convenience function to add choices dicts to OptionParser.
40
    
41
    @param opts OptionParser instance
42
    @param choices_dict dictionary of valid choices, must include default
43
    @param default default choice key
44
    @param name long option name
45
    @param help string
46
    '''
47
    if default not in choices_dict:
48
        raise Exception('Invalid  default %s for choices dict: %s' %
49
                        (default, name))
50
    if not help_str:
51
        help_str = '[' + ' '.join(choices_dict.keys()) + ']'
52
    opts.add_option('--' + name,
53
                    type = 'choice',
54
                    choices = choices_dict.keys(),
55
                    default = default,
56
                    help = help_str)
57

  
58

  
59
class MininetRunner(object):
60
    '''Build, setup, and run Mininet.'''
61

  
62
    def __init__(self):
63
        '''Init.'''
64
        self.options = None
65
        
66
        self.parse_args()
67
        self.setup()
68
        self.begin()
69

  
70
    def parse_args(self):
71
        '''Parse command-line args and return options object.
72
        
73
        @return opts parse options dict
74
        '''
75
        opts = OptionParser()
76
        add_dict_option(opts, TOPOS, TOPO_DEF, 'topo')
77
        add_dict_option(opts, SWITCHES, SWITCH_DEF, 'switch')
78
        add_dict_option(opts, HOSTS, HOST_DEF, 'host')
79
        add_dict_option(opts, CONTROLLERS, CONTROLLER_DEF, 'controller')
80

  
81
        opts.add_option('--test', type = 'choice', choices = TESTS,
82
                        default = TESTS[0],
83
                        help = '[' + ' '.join(TESTS) + ']')
84
        opts.add_option('--xterm', '-x', action = 'store_true',
85
                        default = False, help = 'spawn xterms for each node')
86
        opts.add_option('--verbosity', '-v', type = 'choice',
87
                        choices = LEVELS.keys(), default = 'info',
88
                        help = '[' + ' '.join(LEVELS.keys()) + ']')
89
        self.options = opts.parse_args()[0]
90

  
91
    def setup(self):
92
        '''Setup and validate environment.'''
93

  
94
        # set logging verbosity
95
        set_loglevel(self.options.verbosity)
96

  
97
        # validate environment setup
98
        init()
99

  
100
    def begin(self):
101
        '''Create and run mininet.'''
102

  
103
        start = time.time()
104

  
105
        topo = TOPOS[self.options.topo]() # build topology object
106
        switch = SWITCHES[self.options.switch]
107
        host = HOSTS[self.options.host]
108
        controller = CONTROLLERS[self.options.controller]
109

  
110
        controller_params = ControllerParams(0x0a000000, 8) # 10.0.0.0/8
111
        mn = Mininet(topo, switch, host, controller, controller_params)
112

  
113
        if self.options.test != 'build':
114
    
115
            if self.options.test == 'cli':
116
                mn.interact()
117
            elif self.options.test == 'ping_all':
118
                mn.start()
119
                dropped = mn.ping_test(verbose = True)
120
                lg.info("*** Test results: %i%% dropped\n", dropped)
121
                mn.stop()
122
            elif self.options.test == 'ping_pair':
123
                mn.start()
124
                hosts_unsorted = sorted(mn.topo.hosts())
125
                hosts = [hosts_unsorted[0], hosts_unsorted[1]]
126
                dropped = mn.ping_test(hosts = hosts, verbose = True)
127
                lg.info("*** Test results: %i%% dropped\n", dropped)
128
                mn.stop()
129
            else:
130
                raise NotImplementedError('unknown test: %s' %
131
                                          self.options.test)
132

  
133
        elapsed = float(time.time() - start)
134
        print ('completed in %0.3f seconds' % elapsed)
135

  
136

  
137
if __name__ == "__main__":
138
    MininetRunner()

Also available in: Unified diff