blob: ac4d89f9d94b11b9d1deb713dc354bf972f16bcc [file] [log] [blame]
Dan Talaycodba244e2010-02-15 14:08:53 -08001"""
Dan Talayco79f36082010-03-11 16:53:53 -08002Basic protocol and dataplane test cases
Dan Talaycodba244e2010-02-15 14:08:53 -08003
Dan Talayco48370102010-03-03 15:17:33 -08004It is recommended that these definitions be kept in their own
5namespace as different groups of tests will likely define
6similar identifiers.
7
Dan Talaycodba244e2010-02-15 14:08:53 -08008Current Assumptions:
9
Dan Talayco41eae8b2010-03-10 13:57:06 -080010 The function test_set_init is called with a complete configuration
11dictionary prior to the invocation of any tests from this file.
12
Dan Talaycodba244e2010-02-15 14:08:53 -080013 The switch is actively attempting to contact the controller at the address
14indicated oin oft_config
15
16"""
17
Dan Talaycodba244e2010-02-15 14:08:53 -080018import time
Dan Talayco710438c2010-02-18 15:16:07 -080019import signal
Dan Talaycodba244e2010-02-15 14:08:53 -080020import sys
Dan Talayco48370102010-03-03 15:17:33 -080021import logging
Dan Talaycodba244e2010-02-15 14:08:53 -080022
Dan Talayco2c0dba32010-03-06 22:47:06 -080023import unittest
24
25import oftest.controller as controller
26import oftest.cstruct as ofp
27import oftest.message as message
28import oftest.dataplane as dataplane
29import oftest.action as action
30
Dan Talayco6ce963a2010-03-07 21:58:13 -080031from testutils import *
32
33#@var basic_port_map Local copy of the configuration map from OF port
34# numbers to OS interfaces
Dan Talayco48370102010-03-03 15:17:33 -080035basic_port_map = None
Dan Talayco6ce963a2010-03-07 21:58:13 -080036#@var basic_logger Local logger object
Dan Talayco48370102010-03-03 15:17:33 -080037basic_logger = None
Dan Talayco6ce963a2010-03-07 21:58:13 -080038#@var basic_config Local copy of global configuration data
Dan Talayco48370102010-03-03 15:17:33 -080039basic_config = None
40
Dan Talaycoc24aaae2010-07-08 14:05:24 -070041test_prio = {}
42
Dan Talayco48370102010-03-03 15:17:33 -080043def test_set_init(config):
44 """
45 Set up function for basic test classes
46
47 @param config The configuration dictionary; see oft
Dan Talayco48370102010-03-03 15:17:33 -080048 """
49
50 global basic_port_map
51 global basic_logger
52 global basic_config
53
54 basic_logger = logging.getLogger("basic")
55 basic_logger.info("Initializing test set")
56 basic_port_map = config["port_map"]
57 basic_config = config
Dan Talayco48370102010-03-03 15:17:33 -080058
Dan Talayco6ce963a2010-03-07 21:58:13 -080059class SimpleProtocol(unittest.TestCase):
Dan Talaycodba244e2010-02-15 14:08:53 -080060 """
61 Root class for setting up the controller
62 """
63
Dan Talaycoef701f42010-05-07 09:22:35 -070064 def sig_handler(self, v1, v2):
Dan Talayco48370102010-03-03 15:17:33 -080065 basic_logger.critical("Received interrupt signal; exiting")
Dan Talayco710438c2010-02-18 15:16:07 -080066 print "Received interrupt signal; exiting"
Dan Talayco2c0dba32010-03-06 22:47:06 -080067 self.clean_shutdown = False
68 self.tearDown()
Dan Talayco710438c2010-02-18 15:16:07 -080069 sys.exit(1)
70
Dan Talaycodba244e2010-02-15 14:08:53 -080071 def setUp(self):
Dan Talayco551befa2010-07-15 17:05:32 -070072 self.logger = basic_logger
Dan Talayco285a8382010-07-20 14:06:55 -070073 self.config = basic_config
Dan Talayco710438c2010-02-18 15:16:07 -080074 signal.signal(signal.SIGINT, self.sig_handler)
Dan Talayco9f47f4d2010-06-03 13:54:37 -070075 basic_logger.info("** START TEST CASE " + str(self))
Dan Talayco2c0dba32010-03-06 22:47:06 -080076 self.controller = controller.Controller(
77 host=basic_config["controller_host"],
78 port=basic_config["controller_port"])
Dan Talaycof8f41402010-03-12 22:17:39 -080079 # clean_shutdown should be set to False to force quit app
Dan Talayco2c0dba32010-03-06 22:47:06 -080080 self.clean_shutdown = True
Dan Talayco710438c2010-02-18 15:16:07 -080081 self.controller.start()
Dan Talaycoef701f42010-05-07 09:22:35 -070082 #@todo Add an option to wait for a pkt transaction to ensure version
83 # compatibilty?
Dan Talayco710438c2010-02-18 15:16:07 -080084 self.controller.connect(timeout=20)
Dan Talaycoef701f42010-05-07 09:22:35 -070085 if not self.controller.active:
86 print "Controller startup failed; exiting"
87 sys.exit(1)
Dan Talayco677c0b72011-08-23 22:53:38 -070088 if self.controller.switch_addr is None:
89 print "Controller startup failed (no switch addr); exiting"
90 sys.exit(1)
Dan Talayco48370102010-03-03 15:17:33 -080091 basic_logger.info("Connected " + str(self.controller.switch_addr))
Dan Talaycodba244e2010-02-15 14:08:53 -080092
93 def tearDown(self):
Dan Talayco9f47f4d2010-06-03 13:54:37 -070094 basic_logger.info("** END TEST CASE " + str(self))
Dan Talaycodba244e2010-02-15 14:08:53 -080095 self.controller.shutdown()
Dan Talayco2c0dba32010-03-06 22:47:06 -080096 #@todo Review if join should be done on clean_shutdown
Dan Talaycof8f41402010-03-12 22:17:39 -080097 if self.clean_shutdown:
98 self.controller.join()
Dan Talaycodba244e2010-02-15 14:08:53 -080099
100 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -0800101 # Just a simple sanity check as illustration
Dan Talayco48370102010-03-03 15:17:33 -0800102 basic_logger.info("Running simple proto test")
Dan Talayco710438c2010-02-18 15:16:07 -0800103 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -0800104 str(self) + 'No connection to switch')
105
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700106 def assertTrue(self, cond, msg):
107 if not cond:
108 basic_logger.error("** FAILED ASSERTION: " + msg)
109 unittest.TestCase.assertTrue(self, cond, msg)
110
Dan Talaycoc24aaae2010-07-08 14:05:24 -0700111test_prio["SimpleProtocol"] = 1
112
Dan Talayco6ce963a2010-03-07 21:58:13 -0800113class SimpleDataPlane(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800114 """
115 Root class that sets up the controller and dataplane
116 """
117 def setUp(self):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800118 SimpleProtocol.setUp(self)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800119 self.dataplane = dataplane.DataPlane()
Dan Talayco48370102010-03-03 15:17:33 -0800120 for of_port, ifname in basic_port_map.items():
Dan Talaycodba244e2010-02-15 14:08:53 -0800121 self.dataplane.port_add(ifname, of_port)
122
123 def tearDown(self):
Dan Talayco48370102010-03-03 15:17:33 -0800124 basic_logger.info("Teardown for simple dataplane test")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800125 SimpleProtocol.tearDown(self)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800126 self.dataplane.kill(join_threads=self.clean_shutdown)
Dan Talayco48370102010-03-03 15:17:33 -0800127 basic_logger.info("Teardown done")
Dan Talaycodba244e2010-02-15 14:08:53 -0800128
129 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -0800130 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -0800131 str(self) + 'No connection to switch')
132 # self.dataplane.show()
133 # Would like an assert that checks the data plane
134
Dan Talayco551befa2010-07-15 17:05:32 -0700135class DataPlaneOnly(unittest.TestCase):
136 """
137 Root class that sets up only the dataplane
138 """
139
140 def sig_handler(self, v1, v2):
141 basic_logger.critical("Received interrupt signal; exiting")
142 print "Received interrupt signal; exiting"
143 self.clean_shutdown = False
144 self.tearDown()
145 sys.exit(1)
146
147 def setUp(self):
148 self.clean_shutdown = False
149 self.logger = basic_logger
Dan Talayco285a8382010-07-20 14:06:55 -0700150 self.config = basic_config
Dan Talayco551befa2010-07-15 17:05:32 -0700151 signal.signal(signal.SIGINT, self.sig_handler)
152 basic_logger.info("** START DataPlaneOnly CASE " + str(self))
153 self.dataplane = dataplane.DataPlane()
154 for of_port, ifname in basic_port_map.items():
155 self.dataplane.port_add(ifname, of_port)
156
157 def tearDown(self):
158 basic_logger.info("Teardown for simple dataplane test")
159 self.dataplane.kill(join_threads=self.clean_shutdown)
160 basic_logger.info("Teardown done")
161
162 def runTest(self):
Dan Talaycoba4fd4f2010-07-21 21:49:41 -0700163 basic_logger.info("DataPlaneOnly")
Dan Talayco285a8382010-07-20 14:06:55 -0700164 # self.dataplane.show()
Dan Talayco551befa2010-07-15 17:05:32 -0700165 # Would like an assert that checks the data plane
166
Dan Talayco6ce963a2010-03-07 21:58:13 -0800167class Echo(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800168 """
169 Test echo response with no data
170 """
171 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800172 request = message.echo_request()
Dan Talaycoe226eb12010-02-18 23:06:30 -0800173 response, pkt = self.controller.transact(request)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800174 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800175 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800176 self.assertEqual(request.header.xid, response.header.xid,
177 'response xid != request xid')
178 self.assertEqual(len(response.data), 0, 'response data non-empty')
179
Dan Talayco6ce963a2010-03-07 21:58:13 -0800180class EchoWithData(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800181 """
182 Test echo response with short string data
183 """
184 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800185 request = message.echo_request()
Dan Talaycodba244e2010-02-15 14:08:53 -0800186 request.data = 'OpenFlow Will Rule The World'
Dan Talaycoe226eb12010-02-18 23:06:30 -0800187 response, pkt = self.controller.transact(request)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800188 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800189 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800190 self.assertEqual(request.header.xid, response.header.xid,
191 'response xid != request xid')
192 self.assertEqual(request.data, response.data,
193 'response data does not match request')
194
Dan Talayco6ce963a2010-03-07 21:58:13 -0800195class PacketIn(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800196 """
197 Test packet in function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800198
199 Send a packet to each dataplane port and verify that a packet
200 in message is received from the controller for each
Dan Talaycodba244e2010-02-15 14:08:53 -0800201 """
202 def runTest(self):
203 # Construct packet to send to dataplane
Dan Talaycoe226eb12010-02-18 23:06:30 -0800204 # Send packet to dataplane, once to each port
Dan Talaycodba244e2010-02-15 14:08:53 -0800205 # Poll controller with expect message type packet in
Dan Talaycoe226eb12010-02-18 23:06:30 -0800206
Dan Talayco6ce963a2010-03-07 21:58:13 -0800207 rc = delete_all_flows(self.controller, basic_logger)
208 self.assertEqual(rc, 0, "Failed to delete all flows")
Ed Swierk0caeb1e2012-03-19 15:01:56 -0700209 do_barrier(self.controller)
Dan Talayco6ce963a2010-03-07 21:58:13 -0800210
Dan Talayco48370102010-03-03 15:17:33 -0800211 for of_port in basic_port_map.keys():
212 basic_logger.info("PKT IN test, port " + str(of_port))
Dan Talayco41eae8b2010-03-10 13:57:06 -0800213 pkt = simple_tcp_packet()
Dan Talaycodba244e2010-02-15 14:08:53 -0800214 self.dataplane.send(of_port, str(pkt))
Dan Talaycoe226eb12010-02-18 23:06:30 -0800215 #@todo Check for unexpected messages?
Dan Talaycocf26b7a2011-08-05 10:15:35 -0700216 count = 0
217 while True:
218 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2)
219 if not response: # Timeout
220 break
221 if str(pkt) == response.data: # Got match
222 break
223 if not basic_config["relax"]: # Only one attempt to match
224 break
225 count += 1
226 if count > 10: # Too many tries
227 break
Dan Talaycodba244e2010-02-15 14:08:53 -0800228
Dan Talaycoe226eb12010-02-18 23:06:30 -0800229 self.assertTrue(response is not None,
Dan Talayco6ce963a2010-03-07 21:58:13 -0800230 'Packet in message not received on port ' +
231 str(of_port))
Dan Talayco48370102010-03-03 15:17:33 -0800232 if str(pkt) != response.data:
Dan Talaycoef701f42010-05-07 09:22:35 -0700233 basic_logger.debug("pkt len " + str(len(str(pkt))) +
234 ": " + str(pkt))
235 basic_logger.debug("resp len " +
236 str(len(str(response.data))) +
237 ": " + str(response.data))
Dan Talayco48370102010-03-03 15:17:33 -0800238
239 self.assertEqual(str(pkt), response.data,
Dan Talayco6ce963a2010-03-07 21:58:13 -0800240 'Response packet does not match send packet' +
241 ' for port ' + str(of_port))
Dan Talaycodba244e2010-02-15 14:08:53 -0800242
Dan Talayco6ce963a2010-03-07 21:58:13 -0800243class PacketOut(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800244 """
245 Test packet out function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800246
247 Send packet out message to controller for each dataplane port and
248 verify the packet appears on the appropriate dataplane port
Dan Talaycodba244e2010-02-15 14:08:53 -0800249 """
250 def runTest(self):
251 # Construct packet to send to dataplane
252 # Send packet to dataplane
253 # Poll controller with expect message type packet in
Dan Talayco41eae8b2010-03-10 13:57:06 -0800254
255 rc = delete_all_flows(self.controller, basic_logger)
256 self.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talaycodba244e2010-02-15 14:08:53 -0800257
258 # These will get put into function
Dan Talayco41eae8b2010-03-10 13:57:06 -0800259 outpkt = simple_tcp_packet()
Dan Talayco48370102010-03-03 15:17:33 -0800260 of_ports = basic_port_map.keys()
261 of_ports.sort()
262 for dp_port in of_ports:
Dan Talayco2c0dba32010-03-06 22:47:06 -0800263 msg = message.packet_out()
Dan Talayco48370102010-03-03 15:17:33 -0800264 msg.data = str(outpkt)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800265 act = action.action_output()
Dan Talayco48370102010-03-03 15:17:33 -0800266 act.port = dp_port
267 self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
Dan Talaycodba244e2010-02-15 14:08:53 -0800268
Dan Talayco48370102010-03-03 15:17:33 -0800269 basic_logger.info("PacketOut to: " + str(dp_port))
270 rv = self.controller.message_send(msg)
271 self.assertTrue(rv == 0, "Error sending out message")
Dan Talaycodba244e2010-02-15 14:08:53 -0800272
Dan Talaycocf26b7a2011-08-05 10:15:35 -0700273 exp_pkt_arg = None
274 exp_port = None
275 if basic_config["relax"]:
276 exp_pkt_arg = outpkt
277 exp_port = dp_port
278 (of_port, pkt, pkt_time) = self.dataplane.poll(timeout=1,
279 port_number=exp_port,
280 exp_pkt=exp_pkt_arg)
Dan Talaycodba244e2010-02-15 14:08:53 -0800281
Dan Talayco48370102010-03-03 15:17:33 -0800282 self.assertTrue(pkt is not None, 'Packet not received')
283 basic_logger.info("PacketOut: got pkt from " + str(of_port))
284 if of_port is not None:
285 self.assertEqual(of_port, dp_port, "Unexpected receive port")
286 self.assertEqual(str(outpkt), str(pkt),
287 'Response packet does not match send packet')
Dan Talaycodba244e2010-02-15 14:08:53 -0800288
Dan Talayco6ce963a2010-03-07 21:58:13 -0800289class FlowStatsGet(SimpleProtocol):
290 """
291 Get stats
Dan Talayco2c0dba32010-03-06 22:47:06 -0800292
Dan Talayco6ce963a2010-03-07 21:58:13 -0800293 Simply verify stats get transaction
294 """
295 def runTest(self):
296 basic_logger.info("Running StatsGet")
Dan Talayco41eae8b2010-03-10 13:57:06 -0800297 basic_logger.info("Inserting trial flow")
Dan Talayco677c0b72011-08-23 22:53:38 -0700298 request = flow_mod_gen(basic_port_map, True)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800299 rv = self.controller.message_send(request)
300 self.assertTrue(rv != -1, "Failed to insert test flow")
301
302 basic_logger.info("Sending flow request")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800303 request = message.flow_stats_request()
304 request.out_port = ofp.OFPP_NONE
Dan Talayco41eae8b2010-03-10 13:57:06 -0800305 request.table_id = 0xff
306 request.match.wildcards = 0 # ofp.OFPFW_ALL
Dan Talayco6ce963a2010-03-07 21:58:13 -0800307 response, pkt = self.controller.transact(request, timeout=2)
308 self.assertTrue(response is not None, "Did not get response")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700309 basic_logger.debug(response.show())
Dan Talayco6ce963a2010-03-07 21:58:13 -0800310
Dan Talayco677c0b72011-08-23 22:53:38 -0700311test_prio["FlowStatsGet"] = -1
312
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700313class TableStatsGet(SimpleProtocol):
314 """
315 Get table stats
316
317 Simply verify table stats get transaction
318 """
319 def runTest(self):
320 basic_logger.info("Running TableStatsGet")
321 basic_logger.info("Inserting trial flow")
Dan Talayco677c0b72011-08-23 22:53:38 -0700322 request = flow_mod_gen(basic_port_map, True)
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700323 rv = self.controller.message_send(request)
324 self.assertTrue(rv != -1, "Failed to insert test flow")
325
326 basic_logger.info("Sending table stats request")
327 request = message.table_stats_request()
328 response, pkt = self.controller.transact(request, timeout=2)
329 self.assertTrue(response is not None, "Did not get response")
330 basic_logger.debug(response.show())
331
Dan Talayco6ce963a2010-03-07 21:58:13 -0800332class FlowMod(SimpleProtocol):
333 """
334 Insert a flow
335
336 Simple verification of a flow mod transaction
337 """
338
339 def runTest(self):
340 basic_logger.info("Running " + str(self))
Dan Talayco677c0b72011-08-23 22:53:38 -0700341 request = flow_mod_gen(basic_port_map, True)
Dan Talayco6ce963a2010-03-07 21:58:13 -0800342 rv = self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800343 self.assertTrue(rv != -1, "Error installing flow mod")
344
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700345class PortConfigMod(SimpleProtocol):
346 """
347 Modify a bit in port config and verify changed
348
349 Get the switch configuration, modify the port configuration
350 and write it back; get the config again and verify changed.
351 Then set it back to the way it was.
352 """
353
354 def runTest(self):
355 basic_logger.info("Running " + str(self))
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700356 for of_port, ifname in basic_port_map.items(): # Grab first port
357 break
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700358
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700359 (hw_addr, config, advert) = \
360 port_config_get(self.controller, of_port, basic_logger)
361 self.assertTrue(config is not None, "Did not get port config")
362
363 basic_logger.debug("No flood bit port " + str(of_port) + " is now " +
364 str(config & ofp.OFPPC_NO_FLOOD))
365
366 rv = port_config_set(self.controller, of_port,
367 config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD,
368 basic_logger)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700369 self.assertTrue(rv != -1, "Error sending port mod")
370
371 # Verify change took place with same feature request
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700372 (hw_addr, config2, advert) = \
373 port_config_get(self.controller, of_port, basic_logger)
374 basic_logger.debug("No flood bit port " + str(of_port) + " is now " +
375 str(config2 & ofp.OFPPC_NO_FLOOD))
376 self.assertTrue(config2 is not None, "Did not get port config2")
377 self.assertTrue(config2 & ofp.OFPPC_NO_FLOOD !=
378 config & ofp.OFPPC_NO_FLOOD,
379 "Bit change did not take")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700380 # Set it back
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700381 rv = port_config_set(self.controller, of_port, config,
382 ofp.OFPPC_NO_FLOOD, basic_logger)
383 self.assertTrue(rv != -1, "Error sending port mod")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700384
Dan Talaycodba244e2010-02-15 14:08:53 -0800385if __name__ == "__main__":
Dan Talayco2c0dba32010-03-06 22:47:06 -0800386 print "Please run through oft script: ./oft --test_spec=basic"