blob: 465a3c9094f79a42b93f3ee9e6b70e69f77b7a19 [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 Talayco710438c2010-02-18 15:16:07 -080072 signal.signal(signal.SIGINT, self.sig_handler)
Dan Talayco9f47f4d2010-06-03 13:54:37 -070073 basic_logger.info("** START TEST CASE " + str(self))
Dan Talayco2c0dba32010-03-06 22:47:06 -080074 self.controller = controller.Controller(
75 host=basic_config["controller_host"],
76 port=basic_config["controller_port"])
Dan Talaycof8f41402010-03-12 22:17:39 -080077 # clean_shutdown should be set to False to force quit app
Dan Talayco2c0dba32010-03-06 22:47:06 -080078 self.clean_shutdown = True
Dan Talayco710438c2010-02-18 15:16:07 -080079 self.controller.start()
Dan Talaycoef701f42010-05-07 09:22:35 -070080 #@todo Add an option to wait for a pkt transaction to ensure version
81 # compatibilty?
Dan Talayco710438c2010-02-18 15:16:07 -080082 self.controller.connect(timeout=20)
Dan Talaycoef701f42010-05-07 09:22:35 -070083 if not self.controller.active:
84 print "Controller startup failed; exiting"
85 sys.exit(1)
Dan Talayco48370102010-03-03 15:17:33 -080086 basic_logger.info("Connected " + str(self.controller.switch_addr))
Dan Talaycodba244e2010-02-15 14:08:53 -080087
88 def tearDown(self):
Dan Talayco9f47f4d2010-06-03 13:54:37 -070089 basic_logger.info("** END TEST CASE " + str(self))
Dan Talaycodba244e2010-02-15 14:08:53 -080090 self.controller.shutdown()
Dan Talayco2c0dba32010-03-06 22:47:06 -080091 #@todo Review if join should be done on clean_shutdown
Dan Talaycof8f41402010-03-12 22:17:39 -080092 if self.clean_shutdown:
93 self.controller.join()
Dan Talaycodba244e2010-02-15 14:08:53 -080094
95 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -080096 # Just a simple sanity check as illustration
Dan Talayco48370102010-03-03 15:17:33 -080097 basic_logger.info("Running simple proto test")
Dan Talayco710438c2010-02-18 15:16:07 -080098 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -080099 str(self) + 'No connection to switch')
100
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700101 def assertTrue(self, cond, msg):
102 if not cond:
103 basic_logger.error("** FAILED ASSERTION: " + msg)
104 unittest.TestCase.assertTrue(self, cond, msg)
105
Dan Talaycoc24aaae2010-07-08 14:05:24 -0700106test_prio["SimpleProtocol"] = 1
107
Dan Talayco6ce963a2010-03-07 21:58:13 -0800108class SimpleDataPlane(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800109 """
110 Root class that sets up the controller and dataplane
111 """
112 def setUp(self):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800113 SimpleProtocol.setUp(self)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800114 self.dataplane = dataplane.DataPlane()
Dan Talayco48370102010-03-03 15:17:33 -0800115 for of_port, ifname in basic_port_map.items():
Dan Talaycodba244e2010-02-15 14:08:53 -0800116 self.dataplane.port_add(ifname, of_port)
117
118 def tearDown(self):
Dan Talayco48370102010-03-03 15:17:33 -0800119 basic_logger.info("Teardown for simple dataplane test")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800120 SimpleProtocol.tearDown(self)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800121 self.dataplane.kill(join_threads=self.clean_shutdown)
Dan Talayco48370102010-03-03 15:17:33 -0800122 basic_logger.info("Teardown done")
Dan Talaycodba244e2010-02-15 14:08:53 -0800123
124 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -0800125 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -0800126 str(self) + 'No connection to switch')
127 # self.dataplane.show()
128 # Would like an assert that checks the data plane
129
Dan Talayco6ce963a2010-03-07 21:58:13 -0800130class Echo(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800131 """
132 Test echo response with no data
133 """
134 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800135 request = message.echo_request()
Dan Talaycoe226eb12010-02-18 23:06:30 -0800136 response, pkt = self.controller.transact(request)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800137 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800138 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800139 self.assertEqual(request.header.xid, response.header.xid,
140 'response xid != request xid')
141 self.assertEqual(len(response.data), 0, 'response data non-empty')
142
Dan Talayco6ce963a2010-03-07 21:58:13 -0800143class EchoWithData(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800144 """
145 Test echo response with short string data
146 """
147 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800148 request = message.echo_request()
Dan Talaycodba244e2010-02-15 14:08:53 -0800149 request.data = 'OpenFlow Will Rule The World'
Dan Talaycoe226eb12010-02-18 23:06:30 -0800150 response, pkt = self.controller.transact(request)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800151 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800152 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800153 self.assertEqual(request.header.xid, response.header.xid,
154 'response xid != request xid')
155 self.assertEqual(request.data, response.data,
156 'response data does not match request')
157
Dan Talayco6ce963a2010-03-07 21:58:13 -0800158class PacketIn(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800159 """
160 Test packet in function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800161
162 Send a packet to each dataplane port and verify that a packet
163 in message is received from the controller for each
Dan Talaycodba244e2010-02-15 14:08:53 -0800164 """
165 def runTest(self):
166 # Construct packet to send to dataplane
Dan Talaycoe226eb12010-02-18 23:06:30 -0800167 # Send packet to dataplane, once to each port
Dan Talaycodba244e2010-02-15 14:08:53 -0800168 # Poll controller with expect message type packet in
Dan Talaycoe226eb12010-02-18 23:06:30 -0800169
Dan Talayco6ce963a2010-03-07 21:58:13 -0800170 rc = delete_all_flows(self.controller, basic_logger)
171 self.assertEqual(rc, 0, "Failed to delete all flows")
172
Dan Talayco48370102010-03-03 15:17:33 -0800173 for of_port in basic_port_map.keys():
174 basic_logger.info("PKT IN test, port " + str(of_port))
Dan Talayco41eae8b2010-03-10 13:57:06 -0800175 pkt = simple_tcp_packet()
Dan Talaycodba244e2010-02-15 14:08:53 -0800176 self.dataplane.send(of_port, str(pkt))
Dan Talaycoe226eb12010-02-18 23:06:30 -0800177 #@todo Check for unexpected messages?
Dan Talayco2c0dba32010-03-06 22:47:06 -0800178 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2)
Dan Talaycodba244e2010-02-15 14:08:53 -0800179
Dan Talaycoe226eb12010-02-18 23:06:30 -0800180 self.assertTrue(response is not None,
Dan Talayco6ce963a2010-03-07 21:58:13 -0800181 'Packet in message not received on port ' +
182 str(of_port))
Dan Talayco48370102010-03-03 15:17:33 -0800183 if str(pkt) != response.data:
Dan Talaycoef701f42010-05-07 09:22:35 -0700184 basic_logger.debug("pkt len " + str(len(str(pkt))) +
185 ": " + str(pkt))
186 basic_logger.debug("resp len " +
187 str(len(str(response.data))) +
188 ": " + str(response.data))
Dan Talayco48370102010-03-03 15:17:33 -0800189
190 self.assertEqual(str(pkt), response.data,
Dan Talayco6ce963a2010-03-07 21:58:13 -0800191 'Response packet does not match send packet' +
192 ' for port ' + str(of_port))
Dan Talaycodba244e2010-02-15 14:08:53 -0800193
Dan Talayco6ce963a2010-03-07 21:58:13 -0800194class PacketOut(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800195 """
196 Test packet out function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800197
198 Send packet out message to controller for each dataplane port and
199 verify the packet appears on the appropriate dataplane port
Dan Talaycodba244e2010-02-15 14:08:53 -0800200 """
201 def runTest(self):
202 # Construct packet to send to dataplane
203 # Send packet to dataplane
204 # Poll controller with expect message type packet in
Dan Talayco41eae8b2010-03-10 13:57:06 -0800205
206 rc = delete_all_flows(self.controller, basic_logger)
207 self.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talaycodba244e2010-02-15 14:08:53 -0800208
209 # These will get put into function
Dan Talayco41eae8b2010-03-10 13:57:06 -0800210 outpkt = simple_tcp_packet()
Dan Talayco48370102010-03-03 15:17:33 -0800211 of_ports = basic_port_map.keys()
212 of_ports.sort()
213 for dp_port in of_ports:
Dan Talayco2c0dba32010-03-06 22:47:06 -0800214 msg = message.packet_out()
Dan Talayco48370102010-03-03 15:17:33 -0800215 msg.data = str(outpkt)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800216 act = action.action_output()
Dan Talayco48370102010-03-03 15:17:33 -0800217 act.port = dp_port
218 self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
Dan Talaycodba244e2010-02-15 14:08:53 -0800219
Dan Talayco48370102010-03-03 15:17:33 -0800220 basic_logger.info("PacketOut to: " + str(dp_port))
221 rv = self.controller.message_send(msg)
222 self.assertTrue(rv == 0, "Error sending out message")
Dan Talaycodba244e2010-02-15 14:08:53 -0800223
Dan Talayco48370102010-03-03 15:17:33 -0800224 (of_port, pkt, pkt_time) = self.dataplane.poll(timeout=1)
Dan Talaycodba244e2010-02-15 14:08:53 -0800225
Dan Talayco48370102010-03-03 15:17:33 -0800226 self.assertTrue(pkt is not None, 'Packet not received')
227 basic_logger.info("PacketOut: got pkt from " + str(of_port))
228 if of_port is not None:
229 self.assertEqual(of_port, dp_port, "Unexpected receive port")
230 self.assertEqual(str(outpkt), str(pkt),
231 'Response packet does not match send packet')
Dan Talaycodba244e2010-02-15 14:08:53 -0800232
Dan Talayco6ce963a2010-03-07 21:58:13 -0800233class FlowStatsGet(SimpleProtocol):
234 """
235 Get stats
Dan Talayco2c0dba32010-03-06 22:47:06 -0800236
Dan Talayco6ce963a2010-03-07 21:58:13 -0800237 Simply verify stats get transaction
238 """
239 def runTest(self):
240 basic_logger.info("Running StatsGet")
Dan Talayco41eae8b2010-03-10 13:57:06 -0800241 basic_logger.info("Inserting trial flow")
242 request = message.flow_mod()
243 request.match.wildcards = ofp.OFPFW_ALL
244 request.buffer_id = 0xffffffff
245 rv = self.controller.message_send(request)
246 self.assertTrue(rv != -1, "Failed to insert test flow")
247
248 basic_logger.info("Sending flow request")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800249 request = message.flow_stats_request()
250 request.out_port = ofp.OFPP_NONE
Dan Talayco41eae8b2010-03-10 13:57:06 -0800251 request.table_id = 0xff
252 request.match.wildcards = 0 # ofp.OFPFW_ALL
Dan Talayco6ce963a2010-03-07 21:58:13 -0800253 response, pkt = self.controller.transact(request, timeout=2)
254 self.assertTrue(response is not None, "Did not get response")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700255 basic_logger.debug(response.show())
Dan Talayco6ce963a2010-03-07 21:58:13 -0800256
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700257class TableStatsGet(SimpleProtocol):
258 """
259 Get table stats
260
261 Simply verify table stats get transaction
262 """
263 def runTest(self):
264 basic_logger.info("Running TableStatsGet")
265 basic_logger.info("Inserting trial flow")
266 request = message.flow_mod()
267 request.match.wildcards = ofp.OFPFW_ALL
268 request.buffer_id = 0xffffffff
269 rv = self.controller.message_send(request)
270 self.assertTrue(rv != -1, "Failed to insert test flow")
271
272 basic_logger.info("Sending table stats request")
273 request = message.table_stats_request()
274 response, pkt = self.controller.transact(request, timeout=2)
275 self.assertTrue(response is not None, "Did not get response")
276 basic_logger.debug(response.show())
277
Dan Talayco6ce963a2010-03-07 21:58:13 -0800278class FlowMod(SimpleProtocol):
279 """
280 Insert a flow
281
282 Simple verification of a flow mod transaction
283 """
284
285 def runTest(self):
286 basic_logger.info("Running " + str(self))
287 request = message.flow_mod()
Dan Talayco6ce963a2010-03-07 21:58:13 -0800288 request.match.wildcards = ofp.OFPFW_ALL
289 request.buffer_id = 0xffffffff
290 rv = self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800291 self.assertTrue(rv != -1, "Error installing flow mod")
292
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700293class PortConfigMod(SimpleProtocol):
294 """
295 Modify a bit in port config and verify changed
296
297 Get the switch configuration, modify the port configuration
298 and write it back; get the config again and verify changed.
299 Then set it back to the way it was.
300 """
301
302 def runTest(self):
303 basic_logger.info("Running " + str(self))
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700304 for of_port, ifname in basic_port_map.items(): # Grab first port
305 break
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700306
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700307 (hw_addr, config, advert) = \
308 port_config_get(self.controller, of_port, basic_logger)
309 self.assertTrue(config is not None, "Did not get port config")
310
311 basic_logger.debug("No flood bit port " + str(of_port) + " is now " +
312 str(config & ofp.OFPPC_NO_FLOOD))
313
314 rv = port_config_set(self.controller, of_port,
315 config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD,
316 basic_logger)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700317 self.assertTrue(rv != -1, "Error sending port mod")
318
319 # Verify change took place with same feature request
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700320 (hw_addr, config2, advert) = \
321 port_config_get(self.controller, of_port, basic_logger)
322 basic_logger.debug("No flood bit port " + str(of_port) + " is now " +
323 str(config2 & ofp.OFPPC_NO_FLOOD))
324 self.assertTrue(config2 is not None, "Did not get port config2")
325 self.assertTrue(config2 & ofp.OFPPC_NO_FLOOD !=
326 config & ofp.OFPPC_NO_FLOOD,
327 "Bit change did not take")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700328 # Set it back
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700329 rv = port_config_set(self.controller, of_port, config,
330 ofp.OFPPC_NO_FLOOD, basic_logger)
331 self.assertTrue(rv != -1, "Error sending port mod")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700332
Dan Talaycodba244e2010-02-15 14:08:53 -0800333if __name__ == "__main__":
Dan Talayco2c0dba32010-03-06 22:47:06 -0800334 print "Please run through oft script: ./oft --test_spec=basic"