blob: b6a3a24478773cf63e97bd240f795766cb8d5715 [file] [log] [blame]
Dan Talayco48370102010-03-03 15:17:33 -08001#!/usr/bin/env python
2"""
3@package oft
4
5OpenFlow test framework top level script
6
7This script is the entry point for running OpenFlow tests
8using the OFT framework.
9
10The global configuration is passed around in a dictionary
11ygenerally called config. The keys have the following
12significance.
13
14 platform : String identifying the target platform
15 controller_host : Host on which test controller is running (for sockets)
16 controller_port : Port on which test controller listens for switch cxn
17 port_count : (Optional) Number of ports in dataplane
18 base_of_port : (Optional) Base OpenFlow port number in dataplane
19 base_if_index : (Optional) Base OS network interface for dataplane
20 test_spec : (TBD) Specification of test(s) to run
21 log_file : Filename for test logging
22 debug : String giving debug level (info, warning, error...)
23 dbg_level : logging module value of debug level
24 port_map : Map of dataplane OpenFlow port to OS interface names
25
26See config_defaults below for the default values.
27
28To add configuration to the system, first add an entry to config_default
29below. If you want this to be a command line parameter, edit config_setup
30to add the option and default value to the parser. Then edit config_get
31to make sure the option value gets copied into the configuration
32structure (which then gets passed to everyone else).
33
34By convention, oft attempts to import the contents of a file by the
35name of $platform.py into the local namespace.
36
37IMPORTANT: That file should define a function platform_config_update which
38takes a configuration dictionary as an argument and updates it for the
39current run. In particular, it should set up config["port_map"] with
40the proper map from OF port numbers to OF interface names.
41
42You can add your own platform, say gp104, by adding a file gp104.py
43that defines the function platform_config_update and then use the
44parameter --platform=gp104 on the command line.
45
46If platform_config_update does not set config["port_map"], an attempt
47is made to generate a default map via the function default_port_map_setup.
48This will use "local" and "remote" for platform names if available
49and generate a sequential map based on the values of base_of_port and
50base_if_index in the configuration structure.
51
52@todo Determine and implement conventions for test_spec.
53
54The current model for test sets is basic.py. The current convention is
55that the test set should implement a function test_set_init which takes
56an oft configuration dictionary and returns a unittest.TestSuite object.
57Future test sets should do the same thing.
58
Dan Talayco52f64442010-03-03 15:32:41 -080059Default setup:
60
61The default setup runs locally using veth pairs. To exercise this,
62checkout and build an openflow userspace datapath. Then start it on
63the local host:
64 sudo ~/openflow/regress/bin/veth_setup.pl
65 sudo ofdatapath -i veth0,veth2,veth4,veth6 punix:/tmp/ofd &
66 sudo ofprotocol unix:/tmp/ofd tcp:127.0.0.1 --fail=closed --max-backoff=1 &
67
68Next, run oft:
69 sudo ./oft --debug=info
70
71Examine oft.log if things don't work.
Dan Talayco48370102010-03-03 15:17:33 -080072"""
73
74import sys
75from optparse import OptionParser
76import logging
77import unittest
78
79# Import test files
80import basic
81
82##@var DEBUG_LEVELS
83# Map from strings to debugging levels
84DEBUG_LEVELS = {
85 'debug' : logging.DEBUG,
86 'verbose' : logging.DEBUG,
87 'info' : logging.INFO,
88 'warning' : logging.WARNING,
89 'warn' : logging.WARNING,
90 'error' : logging.ERROR,
91 'critical' : logging.CRITICAL
92}
93
94_debug_default = "warning"
95_debug_level_default = DEBUG_LEVELS[_debug_default]
96
97##@var config_default
98# The default configuration dictionary for OFT
99config_default = {
100 "platform" : "local",
101 "controller_host" : "127.0.0.1",
102 "controller_port" : 6633,
103 "port_count" : 4,
104 "base_of_port" : 1,
105 "base_if_index" : 1,
106 "test_spec" : "basic",
107 "log_file" : "oft.log",
108 "debug" : _debug_default,
109 "dbg_level" : _debug_level_default,
110 "port_map" : {}
111}
112
113# Map options to config structure
114def config_get(opts):
115 "Convert options class to OFT configuration dictionary"
116 cfg = config_default.copy()
117 cfg["platform"] = opts.platform
118 cfg["controller_host"] = opts.controller_host
119 cfg["controller_port"] = opts.controller_port
120 cfg["test_spec"] = opts.test_spec
121 cfg["log_file"] = opts.log_file
122 if opts.debug not in DEBUG_LEVELS.keys():
123 print "Warning: Bad value specified for debug level; using default"
124 opts.debug = _debug_default
125 cfg["debug"] = opts.debug
126 cfg["dbg_level"] = DEBUG_LEVELS[cfg["debug"]]
127 cfg["base_of_port"] = opts.base_of_port
128 cfg["base_if_index"] = opts.base_if_index
129 cfg["port_count"] = opts.port_count
130 return cfg
131
132def config_setup(cfg_dflt):
133 """
134 Set up the configuration including parsing the arguments
135
136 @param cfg_dflt The default configuration dictionary
137 @return A pair (config, args) where config is an config
138 object and args is any additional arguments from the command line
139 """
140
141 parser = OptionParser(version="%prog 0.1")
142
143 # Set up default values
144 parser.set_defaults(platform=cfg_dflt["platform"])
145 parser.set_defaults(controller_host=cfg_dflt["controller_host"])
146 parser.set_defaults(controller_port=cfg_dflt["controller_port"])
147 parser.set_defaults(test_spec=cfg_dflt["test_spec"])
148 parser.set_defaults(log_file=cfg_dflt["log_file"])
149 parser.set_defaults(debug=cfg_dflt["debug"])
150 parser.set_defaults(base_of_port=cfg_dflt["base_of_port"])
151 parser.set_defaults(base_if_index=cfg_dflt["base_if_index"])
152 parser.set_defaults(port_count=cfg_dflt["port_count"])
153
154 plat_help = """Set the platform type. Valid values include:
155 local: User space virtual ethernet pair setup
156 remote: Remote embedded Broadcom based switch
157 """
158 parser.add_option("-P", "--platform", help=plat_help)
159 parser.add_option("-H", "--host", dest="controller_host",
160 help="The IP/name of the test controller host")
161 parser.add_option("-p", "--port", dest="controller_port",
162 type="int", help="Port number of the test controller")
163 parser.add_option("--test-spec", "--test-list",
164 help="Indicate tests to run (TBD)")
165 parser.add_option("--log-file",
166 help="Name of log file, empty string to log to console")
167 parser.add_option("--debug",
168 help="Debug lvl: debug, info, warning, error, critical")
169 parser.add_option("--port_count",
170 help="Number of ports to use (optional)")
171 parser.add_option("--base_of_port",
172 help="Base OpenFlow port number (optional)")
173 parser.add_option("--base_if_index",
174 help="Base interface index number (optional)")
175 # Might need this if other parsers want command line
176 # parser.allow_interspersed_args = False
177 (options, args) = parser.parse_args()
178
179 config = config_get(options)
180
181 return (config, args)
182
183def logging_setup(config):
184 """
185 Set up logging based on config
186 """
187 _format = "%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s"
188 _datefmt = "%H:%M:%S"
189 if config["log_file"]:
190 logging.basicConfig(filename=config["log_file"],
191 level=config["dbg_level"],
192 format=_format, datefmt=_datefmt)
193 #@todo Handle "no log file"
194
195def default_port_map_setup(config):
196 """
197 Setup the OF port mapping based on config
198 @param config The OFT configuration structure
199 @return Port map dictionary
200 """
201 if (config["base_of_port"] is None) or not config["port_count"]:
202 return None
203 port_map = {}
204 if config["platform"] == "local":
205 # For local, use every other veth port
206 for idx in range(config["port_count"]):
207 port_map[config["base_of_port"] + idx] = "veth" + \
208 str(config["base_if_index"] + (2 * idx))
209 elif config["platform"] == "remote":
210 # For remote, use eth ports
211 for idx in range(config["port_count"]):
212 port_map[config["base_of_port"] + idx] = "eth" + \
213 str(config["base_if_index"] + idx)
214 else:
215 return None
216
217 logging.info("Built default port map")
218 return port_map
219
220#
221# Main script
222#
223
224# Get configuration, set up logging, import platform from file
225(config, args) = config_setup(config_default)
226logging_setup(config)
227logging.info("*** STARTING TEST RUN ***")
228
229of_os_port_map = None
230if config["platform"]:
231 _imp_string = "from " + config["platform"] + " import *"
232 logging.info("Importing: " + _imp_string)
233 try:
234 exec(_imp_string)
235 except:
236 logging.warn("Failed to import " + config["platform"] + " file")
237
238try:
239 platform_config_update(config)
240except:
241 logging.warn("Could not run platform host configuration")
242
243if not config["port_map"]:
244 # Try to set up default port mapping if not done by platform
245 config["port_map"] = default_port_map_setup(config)
246
247if not config["port_map"]:
248 logging.critical("Interface port map is not defined. Exiting")
249 print("Interface port map is not defined. Exiting")
250 sys.exit(1)
251
252logging.debug("Configuration: " + str(config))
253logging.info("OF port map: " + str(config["port_map"]))
254
255# Init the test sets
256#@todo Use test-spec from config to determine which tests to run
257basic_suite = basic.test_set_init(config)
258if config["dbg_level"] >= logging.WARNING: _verb = 1
259else: _verb = 2
260
261unittest.TextTestRunner(verbosity=_verb).run(basic_suite)
262
263logging.info("*** END OF TESTS ***")
264