blob: 0e91ad5559cb139fa826da5dbba99bae05e047fe [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
41def test_set_init(config):
42 """
43 Set up function for basic test classes
44
45 @param config The configuration dictionary; see oft
Dan Talayco48370102010-03-03 15:17:33 -080046 """
47
48 global basic_port_map
49 global basic_logger
50 global basic_config
51
52 basic_logger = logging.getLogger("basic")
53 basic_logger.info("Initializing test set")
54 basic_port_map = config["port_map"]
55 basic_config = config
Dan Talayco48370102010-03-03 15:17:33 -080056
Dan Talayco6ce963a2010-03-07 21:58:13 -080057class SimpleProtocol(unittest.TestCase):
Dan Talaycodba244e2010-02-15 14:08:53 -080058 """
59 Root class for setting up the controller
60 """
61
Dan Talaycoef701f42010-05-07 09:22:35 -070062 def sig_handler(self, v1, v2):
Dan Talayco48370102010-03-03 15:17:33 -080063 basic_logger.critical("Received interrupt signal; exiting")
Dan Talayco710438c2010-02-18 15:16:07 -080064 print "Received interrupt signal; exiting"
Dan Talayco2c0dba32010-03-06 22:47:06 -080065 self.clean_shutdown = False
66 self.tearDown()
Dan Talayco710438c2010-02-18 15:16:07 -080067 sys.exit(1)
68
Dan Talaycodba244e2010-02-15 14:08:53 -080069 def setUp(self):
Dan Talayco710438c2010-02-18 15:16:07 -080070 signal.signal(signal.SIGINT, self.sig_handler)
Dan Talayco9f47f4d2010-06-03 13:54:37 -070071 basic_logger.info("** START TEST CASE " + str(self))
Dan Talayco2c0dba32010-03-06 22:47:06 -080072 self.controller = controller.Controller(
73 host=basic_config["controller_host"],
74 port=basic_config["controller_port"])
Dan Talaycof8f41402010-03-12 22:17:39 -080075 # clean_shutdown should be set to False to force quit app
Dan Talayco2c0dba32010-03-06 22:47:06 -080076 self.clean_shutdown = True
Dan Talayco710438c2010-02-18 15:16:07 -080077 self.controller.start()
Dan Talaycoef701f42010-05-07 09:22:35 -070078 #@todo Add an option to wait for a pkt transaction to ensure version
79 # compatibilty?
Dan Talayco710438c2010-02-18 15:16:07 -080080 self.controller.connect(timeout=20)
Dan Talaycoef701f42010-05-07 09:22:35 -070081 if not self.controller.active:
82 print "Controller startup failed; exiting"
83 sys.exit(1)
Dan Talayco48370102010-03-03 15:17:33 -080084 basic_logger.info("Connected " + str(self.controller.switch_addr))
Dan Talaycodba244e2010-02-15 14:08:53 -080085
86 def tearDown(self):
Dan Talayco9f47f4d2010-06-03 13:54:37 -070087 basic_logger.info("** END TEST CASE " + str(self))
Dan Talaycodba244e2010-02-15 14:08:53 -080088 self.controller.shutdown()
Dan Talayco2c0dba32010-03-06 22:47:06 -080089 #@todo Review if join should be done on clean_shutdown
Dan Talaycof8f41402010-03-12 22:17:39 -080090 if self.clean_shutdown:
91 self.controller.join()
Dan Talaycodba244e2010-02-15 14:08:53 -080092
93 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -080094 # Just a simple sanity check as illustration
Dan Talayco48370102010-03-03 15:17:33 -080095 basic_logger.info("Running simple proto test")
Dan Talayco710438c2010-02-18 15:16:07 -080096 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -080097 str(self) + 'No connection to switch')
98
Dan Talayco9f47f4d2010-06-03 13:54:37 -070099 def assertTrue(self, cond, msg):
100 if not cond:
101 basic_logger.error("** FAILED ASSERTION: " + msg)
102 unittest.TestCase.assertTrue(self, cond, msg)
103
Dan Talayco6ce963a2010-03-07 21:58:13 -0800104class SimpleDataPlane(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800105 """
106 Root class that sets up the controller and dataplane
107 """
108 def setUp(self):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800109 SimpleProtocol.setUp(self)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800110 self.dataplane = dataplane.DataPlane()
Dan Talayco48370102010-03-03 15:17:33 -0800111 for of_port, ifname in basic_port_map.items():
Dan Talaycodba244e2010-02-15 14:08:53 -0800112 self.dataplane.port_add(ifname, of_port)
113
114 def tearDown(self):
Dan Talayco48370102010-03-03 15:17:33 -0800115 basic_logger.info("Teardown for simple dataplane test")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800116 SimpleProtocol.tearDown(self)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800117 self.dataplane.kill(join_threads=self.clean_shutdown)
Dan Talayco48370102010-03-03 15:17:33 -0800118 basic_logger.info("Teardown done")
Dan Talaycodba244e2010-02-15 14:08:53 -0800119
120 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -0800121 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -0800122 str(self) + 'No connection to switch')
123 # self.dataplane.show()
124 # Would like an assert that checks the data plane
125
Dan Talayco6ce963a2010-03-07 21:58:13 -0800126class Echo(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800127 """
128 Test echo response with no data
129 """
130 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800131 request = message.echo_request()
Dan Talaycoe226eb12010-02-18 23:06:30 -0800132 response, pkt = self.controller.transact(request)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800133 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800134 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800135 self.assertEqual(request.header.xid, response.header.xid,
136 'response xid != request xid')
137 self.assertEqual(len(response.data), 0, 'response data non-empty')
138
Dan Talayco6ce963a2010-03-07 21:58:13 -0800139class EchoWithData(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800140 """
141 Test echo response with short string data
142 """
143 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800144 request = message.echo_request()
Dan Talaycodba244e2010-02-15 14:08:53 -0800145 request.data = 'OpenFlow Will Rule The World'
Dan Talaycoe226eb12010-02-18 23:06:30 -0800146 response, pkt = self.controller.transact(request)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800147 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800148 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800149 self.assertEqual(request.header.xid, response.header.xid,
150 'response xid != request xid')
151 self.assertEqual(request.data, response.data,
152 'response data does not match request')
153
Dan Talayco6ce963a2010-03-07 21:58:13 -0800154class PacketIn(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800155 """
156 Test packet in function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800157
158 Send a packet to each dataplane port and verify that a packet
159 in message is received from the controller for each
Dan Talaycodba244e2010-02-15 14:08:53 -0800160 """
161 def runTest(self):
162 # Construct packet to send to dataplane
Dan Talaycoe226eb12010-02-18 23:06:30 -0800163 # Send packet to dataplane, once to each port
Dan Talaycodba244e2010-02-15 14:08:53 -0800164 # Poll controller with expect message type packet in
Dan Talaycoe226eb12010-02-18 23:06:30 -0800165
Dan Talayco6ce963a2010-03-07 21:58:13 -0800166 rc = delete_all_flows(self.controller, basic_logger)
167 self.assertEqual(rc, 0, "Failed to delete all flows")
168
Dan Talayco48370102010-03-03 15:17:33 -0800169 for of_port in basic_port_map.keys():
170 basic_logger.info("PKT IN test, port " + str(of_port))
Dan Talayco41eae8b2010-03-10 13:57:06 -0800171 pkt = simple_tcp_packet()
Dan Talaycodba244e2010-02-15 14:08:53 -0800172 self.dataplane.send(of_port, str(pkt))
Dan Talaycoe226eb12010-02-18 23:06:30 -0800173 #@todo Check for unexpected messages?
Dan Talayco2c0dba32010-03-06 22:47:06 -0800174 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2)
Dan Talaycodba244e2010-02-15 14:08:53 -0800175
Dan Talaycoe226eb12010-02-18 23:06:30 -0800176 self.assertTrue(response is not None,
Dan Talayco6ce963a2010-03-07 21:58:13 -0800177 'Packet in message not received on port ' +
178 str(of_port))
Dan Talayco48370102010-03-03 15:17:33 -0800179 if str(pkt) != response.data:
Dan Talaycoef701f42010-05-07 09:22:35 -0700180 basic_logger.debug("pkt len " + str(len(str(pkt))) +
181 ": " + str(pkt))
182 basic_logger.debug("resp len " +
183 str(len(str(response.data))) +
184 ": " + str(response.data))
Dan Talayco48370102010-03-03 15:17:33 -0800185
186 self.assertEqual(str(pkt), response.data,
Dan Talayco6ce963a2010-03-07 21:58:13 -0800187 'Response packet does not match send packet' +
188 ' for port ' + str(of_port))
Dan Talaycodba244e2010-02-15 14:08:53 -0800189
Dan Talayco6ce963a2010-03-07 21:58:13 -0800190class PacketOut(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800191 """
192 Test packet out function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800193
194 Send packet out message to controller for each dataplane port and
195 verify the packet appears on the appropriate dataplane port
Dan Talaycodba244e2010-02-15 14:08:53 -0800196 """
197 def runTest(self):
198 # Construct packet to send to dataplane
199 # Send packet to dataplane
200 # Poll controller with expect message type packet in
Dan Talayco41eae8b2010-03-10 13:57:06 -0800201
202 rc = delete_all_flows(self.controller, basic_logger)
203 self.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talaycodba244e2010-02-15 14:08:53 -0800204
205 # These will get put into function
Dan Talayco41eae8b2010-03-10 13:57:06 -0800206 outpkt = simple_tcp_packet()
Dan Talayco48370102010-03-03 15:17:33 -0800207 of_ports = basic_port_map.keys()
208 of_ports.sort()
209 for dp_port in of_ports:
Dan Talayco2c0dba32010-03-06 22:47:06 -0800210 msg = message.packet_out()
Dan Talayco48370102010-03-03 15:17:33 -0800211 msg.data = str(outpkt)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800212 act = action.action_output()
Dan Talayco48370102010-03-03 15:17:33 -0800213 act.port = dp_port
214 self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
Dan Talaycodba244e2010-02-15 14:08:53 -0800215
Dan Talayco48370102010-03-03 15:17:33 -0800216 basic_logger.info("PacketOut to: " + str(dp_port))
217 rv = self.controller.message_send(msg)
218 self.assertTrue(rv == 0, "Error sending out message")
Dan Talaycodba244e2010-02-15 14:08:53 -0800219
Dan Talayco48370102010-03-03 15:17:33 -0800220 (of_port, pkt, pkt_time) = self.dataplane.poll(timeout=1)
Dan Talaycodba244e2010-02-15 14:08:53 -0800221
Dan Talayco48370102010-03-03 15:17:33 -0800222 self.assertTrue(pkt is not None, 'Packet not received')
223 basic_logger.info("PacketOut: got pkt from " + str(of_port))
224 if of_port is not None:
225 self.assertEqual(of_port, dp_port, "Unexpected receive port")
226 self.assertEqual(str(outpkt), str(pkt),
227 'Response packet does not match send packet')
Dan Talaycodba244e2010-02-15 14:08:53 -0800228
Dan Talayco6ce963a2010-03-07 21:58:13 -0800229class FlowStatsGet(SimpleProtocol):
230 """
231 Get stats
Dan Talayco2c0dba32010-03-06 22:47:06 -0800232
Dan Talayco6ce963a2010-03-07 21:58:13 -0800233 Simply verify stats get transaction
234 """
235 def runTest(self):
236 basic_logger.info("Running StatsGet")
Dan Talayco41eae8b2010-03-10 13:57:06 -0800237 basic_logger.info("Inserting trial flow")
238 request = message.flow_mod()
239 request.match.wildcards = ofp.OFPFW_ALL
240 request.buffer_id = 0xffffffff
241 rv = self.controller.message_send(request)
242 self.assertTrue(rv != -1, "Failed to insert test flow")
243
244 basic_logger.info("Sending flow request")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800245 request = message.flow_stats_request()
246 request.out_port = ofp.OFPP_NONE
Dan Talayco41eae8b2010-03-10 13:57:06 -0800247 request.table_id = 0xff
248 request.match.wildcards = 0 # ofp.OFPFW_ALL
Dan Talayco6ce963a2010-03-07 21:58:13 -0800249 response, pkt = self.controller.transact(request, timeout=2)
250 self.assertTrue(response is not None, "Did not get response")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700251 basic_logger.debug(response.show())
Dan Talayco6ce963a2010-03-07 21:58:13 -0800252
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700253class TableStatsGet(SimpleProtocol):
254 """
255 Get table stats
256
257 Simply verify table stats get transaction
258 """
259 def runTest(self):
260 basic_logger.info("Running TableStatsGet")
261 basic_logger.info("Inserting trial flow")
262 request = message.flow_mod()
263 request.match.wildcards = ofp.OFPFW_ALL
264 request.buffer_id = 0xffffffff
265 rv = self.controller.message_send(request)
266 self.assertTrue(rv != -1, "Failed to insert test flow")
267
268 basic_logger.info("Sending table stats request")
269 request = message.table_stats_request()
270 response, pkt = self.controller.transact(request, timeout=2)
271 self.assertTrue(response is not None, "Did not get response")
272 basic_logger.debug(response.show())
273
Dan Talayco6ce963a2010-03-07 21:58:13 -0800274class FlowMod(SimpleProtocol):
275 """
276 Insert a flow
277
278 Simple verification of a flow mod transaction
279 """
280
281 def runTest(self):
282 basic_logger.info("Running " + str(self))
283 request = message.flow_mod()
Dan Talayco6ce963a2010-03-07 21:58:13 -0800284 request.match.wildcards = ofp.OFPFW_ALL
285 request.buffer_id = 0xffffffff
286 rv = self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800287 self.assertTrue(rv != -1, "Error installing flow mod")
288
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700289class PortConfigMod(SimpleProtocol):
290 """
291 Modify a bit in port config and verify changed
292
293 Get the switch configuration, modify the port configuration
294 and write it back; get the config again and verify changed.
295 Then set it back to the way it was.
296 """
297
298 def runTest(self):
299 basic_logger.info("Running " + str(self))
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700300 for of_port, ifname in basic_port_map.items(): # Grab first port
301 break
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700302
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700303 (hw_addr, config, advert) = \
304 port_config_get(self.controller, of_port, basic_logger)
305 self.assertTrue(config is not None, "Did not get port config")
306
307 basic_logger.debug("No flood bit port " + str(of_port) + " is now " +
308 str(config & ofp.OFPPC_NO_FLOOD))
309
310 rv = port_config_set(self.controller, of_port,
311 config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD,
312 basic_logger)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700313 self.assertTrue(rv != -1, "Error sending port mod")
314
315 # Verify change took place with same feature request
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700316 (hw_addr, config2, advert) = \
317 port_config_get(self.controller, of_port, basic_logger)
318 basic_logger.debug("No flood bit port " + str(of_port) + " is now " +
319 str(config2 & ofp.OFPPC_NO_FLOOD))
320 self.assertTrue(config2 is not None, "Did not get port config2")
321 self.assertTrue(config2 & ofp.OFPPC_NO_FLOOD !=
322 config & ofp.OFPPC_NO_FLOOD,
323 "Bit change did not take")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700324 # Set it back
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700325 rv = port_config_set(self.controller, of_port, config,
326 ofp.OFPPC_NO_FLOOD, basic_logger)
327 self.assertTrue(rv != -1, "Error sending port mod")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700328
Dan Talaycodba244e2010-02-15 14:08:53 -0800329if __name__ == "__main__":
Dan Talayco2c0dba32010-03-06 22:47:06 -0800330 print "Please run through oft script: ./oft --test_spec=basic"