blob: 5ea03c6ac4e6523a14b5b8c9d8e2593abd1983af [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
Ken Chiang1bf01602012-04-04 10:48:23 -070024import random
Dan Talayco2c0dba32010-03-06 22:47:06 -080025
26import oftest.controller as controller
27import oftest.cstruct as ofp
28import oftest.message as message
29import oftest.dataplane as dataplane
30import oftest.action as action
31
Dan Talaycoe605b1b2012-09-18 06:56:20 -070032import oftest.illegal_message as illegal_message
33
Dan Talayco6ce963a2010-03-07 21:58:13 -080034from testutils import *
35
36#@var basic_port_map Local copy of the configuration map from OF port
37# numbers to OS interfaces
Dan Talayco48370102010-03-03 15:17:33 -080038basic_port_map = None
Dan Talayco6ce963a2010-03-07 21:58:13 -080039#@var basic_logger Local logger object
Dan Talayco48370102010-03-03 15:17:33 -080040basic_logger = None
Dan Talayco6ce963a2010-03-07 21:58:13 -080041#@var basic_config Local copy of global configuration data
Dan Talayco48370102010-03-03 15:17:33 -080042basic_config = None
43
Dan Talaycoc24aaae2010-07-08 14:05:24 -070044test_prio = {}
45
Ken Chiangaeb23d62012-08-23 21:20:07 -070046TEST_VID_DEFAULT = 2
47
Dan Talayco48370102010-03-03 15:17:33 -080048def test_set_init(config):
49 """
50 Set up function for basic test classes
51
52 @param config The configuration dictionary; see oft
Dan Talayco48370102010-03-03 15:17:33 -080053 """
54
55 global basic_port_map
56 global basic_logger
57 global basic_config
58
59 basic_logger = logging.getLogger("basic")
60 basic_logger.info("Initializing test set")
61 basic_port_map = config["port_map"]
62 basic_config = config
Dan Talayco48370102010-03-03 15:17:33 -080063
Dan Talayco6ce963a2010-03-07 21:58:13 -080064class SimpleProtocol(unittest.TestCase):
Dan Talaycodba244e2010-02-15 14:08:53 -080065 """
66 Root class for setting up the controller
67 """
68
Dan Talaycoef701f42010-05-07 09:22:35 -070069 def sig_handler(self, v1, v2):
Dan Talayco48370102010-03-03 15:17:33 -080070 basic_logger.critical("Received interrupt signal; exiting")
Dan Talayco710438c2010-02-18 15:16:07 -080071 print "Received interrupt signal; exiting"
Dan Talayco2c0dba32010-03-06 22:47:06 -080072 self.clean_shutdown = False
73 self.tearDown()
Rich Lane58cf05f2012-07-11 16:41:47 -070074 raise KeyboardInterrupt
Dan Talayco710438c2010-02-18 15:16:07 -080075
Dan Talaycodba244e2010-02-15 14:08:53 -080076 def setUp(self):
Dan Talayco551befa2010-07-15 17:05:32 -070077 self.logger = basic_logger
Dan Talayco285a8382010-07-20 14:06:55 -070078 self.config = basic_config
Ed Swierk022d02e2012-08-22 06:26:36 -070079 #@todo Test cases shouldn't monkey with signals; move SIGINT handler
80 # to top-level oft
81 try:
82 signal.signal(signal.SIGINT, self.sig_handler)
83 except ValueError, e:
84 basic_logger.info("Could not set SIGINT handler: %s" % e)
Dan Talayco9f47f4d2010-06-03 13:54:37 -070085 basic_logger.info("** START TEST CASE " + str(self))
Dan Talayco2c0dba32010-03-06 22:47:06 -080086 self.controller = controller.Controller(
87 host=basic_config["controller_host"],
88 port=basic_config["controller_port"])
Dan Talaycof8f41402010-03-12 22:17:39 -080089 # clean_shutdown should be set to False to force quit app
Dan Talayco2c0dba32010-03-06 22:47:06 -080090 self.clean_shutdown = True
Dan Talayco710438c2010-02-18 15:16:07 -080091 self.controller.start()
Dan Talaycoef701f42010-05-07 09:22:35 -070092 #@todo Add an option to wait for a pkt transaction to ensure version
93 # compatibilty?
Dan Talayco710438c2010-02-18 15:16:07 -080094 self.controller.connect(timeout=20)
Dan Talaycoef701f42010-05-07 09:22:35 -070095 if not self.controller.active:
Rich Lane58cf05f2012-07-11 16:41:47 -070096 raise Exception("Controller startup failed")
Dan Talayco677c0b72011-08-23 22:53:38 -070097 if self.controller.switch_addr is None:
Rich Lane58cf05f2012-07-11 16:41:47 -070098 raise Exception("Controller startup failed (no switch addr)")
Dan Talayco48370102010-03-03 15:17:33 -080099 basic_logger.info("Connected " + str(self.controller.switch_addr))
Ed Swierkc7193a22012-08-22 06:51:02 -0700100 request = message.features_request()
101 reply, pkt = self.controller.transact(request, timeout=10)
Dan Talayco97d4f362012-09-18 03:22:09 -0700102 self.assertTrue(reply is not None,
103 "Did not complete features_request for handshake")
Ed Swierkc7193a22012-08-22 06:51:02 -0700104 self.supported_actions = reply.actions
105 basic_logger.info("Supported actions: " + hex(self.supported_actions))
Dan Talaycodba244e2010-02-15 14:08:53 -0800106
Dan Talayco677cc112012-03-27 10:28:58 -0700107 def inheritSetup(self, parent):
108 """
109 Inherit the setup of a parent
110
111 This allows running at test from within another test. Do the
112 following:
113
114 sub_test = SomeTestClass() # Create an instance of the test class
115 sub_test.inheritSetup(self) # Inherit setup of parent
116 sub_test.runTest() # Run the test
117
118 Normally, only the parent's setUp and tearDown are called and
119 the state after the sub_test is run must be taken into account
120 by subsequent operations.
121 """
122 self.logger = parent.logger
123 self.config = parent.config
124 basic_logger.info("** Setup " + str(self) + " inheriting from "
125 + str(parent))
126 self.controller = parent.controller
Ed Swierk6192e512012-08-22 11:41:40 -0700127 self.supported_actions = parent.supported_actions
Dan Talayco677cc112012-03-27 10:28:58 -0700128
Dan Talaycodba244e2010-02-15 14:08:53 -0800129 def tearDown(self):
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700130 basic_logger.info("** END TEST CASE " + str(self))
Dan Talaycodba244e2010-02-15 14:08:53 -0800131 self.controller.shutdown()
Dan Talayco2c0dba32010-03-06 22:47:06 -0800132 #@todo Review if join should be done on clean_shutdown
Dan Talaycof8f41402010-03-12 22:17:39 -0800133 if self.clean_shutdown:
134 self.controller.join()
Dan Talaycodba244e2010-02-15 14:08:53 -0800135
136 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -0800137 # Just a simple sanity check as illustration
Dan Talayco48370102010-03-03 15:17:33 -0800138 basic_logger.info("Running simple proto test")
Dan Talayco710438c2010-02-18 15:16:07 -0800139 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -0800140 str(self) + 'No connection to switch')
141
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700142 def assertTrue(self, cond, msg):
143 if not cond:
144 basic_logger.error("** FAILED ASSERTION: " + msg)
145 unittest.TestCase.assertTrue(self, cond, msg)
146
Dan Talaycoc24aaae2010-07-08 14:05:24 -0700147test_prio["SimpleProtocol"] = 1
148
Dan Talayco6ce963a2010-03-07 21:58:13 -0800149class SimpleDataPlane(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800150 """
151 Root class that sets up the controller and dataplane
152 """
153 def setUp(self):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800154 SimpleProtocol.setUp(self)
Jeffrey Townsend4d5ca922012-07-11 11:37:35 -0700155 self.dataplane = dataplane.DataPlane(self.config)
Dan Talayco48370102010-03-03 15:17:33 -0800156 for of_port, ifname in basic_port_map.items():
Dan Talaycodba244e2010-02-15 14:08:53 -0800157 self.dataplane.port_add(ifname, of_port)
158
Dan Talayco677cc112012-03-27 10:28:58 -0700159 def inheritSetup(self, parent):
160 """
161 Inherit the setup of a parent
162
163 See SimpleProtocol.inheritSetup
164 """
165 SimpleProtocol.inheritSetup(self, parent)
166 self.dataplane = parent.dataplane
167
Dan Talaycodba244e2010-02-15 14:08:53 -0800168 def tearDown(self):
Dan Talayco48370102010-03-03 15:17:33 -0800169 basic_logger.info("Teardown for simple dataplane test")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800170 SimpleProtocol.tearDown(self)
Rich Lane58cf05f2012-07-11 16:41:47 -0700171 if hasattr(self, 'dataplane'):
172 self.dataplane.kill(join_threads=self.clean_shutdown)
Dan Talayco48370102010-03-03 15:17:33 -0800173 basic_logger.info("Teardown done")
Dan Talaycodba244e2010-02-15 14:08:53 -0800174
175 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -0800176 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -0800177 str(self) + 'No connection to switch')
178 # self.dataplane.show()
179 # Would like an assert that checks the data plane
180
Dan Talayco551befa2010-07-15 17:05:32 -0700181class DataPlaneOnly(unittest.TestCase):
182 """
183 Root class that sets up only the dataplane
184 """
185
186 def sig_handler(self, v1, v2):
187 basic_logger.critical("Received interrupt signal; exiting")
188 print "Received interrupt signal; exiting"
189 self.clean_shutdown = False
190 self.tearDown()
Rich Lane58cf05f2012-07-11 16:41:47 -0700191 raise KeyboardInterrupt
Dan Talayco551befa2010-07-15 17:05:32 -0700192
193 def setUp(self):
Shudong Zhoue3582a52012-08-03 20:46:50 -0700194 self.clean_shutdown = True
Dan Talayco551befa2010-07-15 17:05:32 -0700195 self.logger = basic_logger
Dan Talayco285a8382010-07-20 14:06:55 -0700196 self.config = basic_config
Ed Swierk022d02e2012-08-22 06:26:36 -0700197 #@todo Test cases shouldn't monkey with signals; move SIGINT handler
198 # to top-level oft
199 try:
200 signal.signal(signal.SIGINT, self.sig_handler)
201 except ValueError, e:
202 basic_logger.info("Could not set SIGINT handler: %s" % e)
Dan Talayco551befa2010-07-15 17:05:32 -0700203 basic_logger.info("** START DataPlaneOnly CASE " + str(self))
Jeffrey Townsend4d5ca922012-07-11 11:37:35 -0700204 self.dataplane = dataplane.DataPlane(self.config)
Dan Talayco551befa2010-07-15 17:05:32 -0700205 for of_port, ifname in basic_port_map.items():
206 self.dataplane.port_add(ifname, of_port)
207
208 def tearDown(self):
209 basic_logger.info("Teardown for simple dataplane test")
210 self.dataplane.kill(join_threads=self.clean_shutdown)
211 basic_logger.info("Teardown done")
212
213 def runTest(self):
Dan Talaycoba4fd4f2010-07-21 21:49:41 -0700214 basic_logger.info("DataPlaneOnly")
Dan Talayco285a8382010-07-20 14:06:55 -0700215 # self.dataplane.show()
Dan Talayco551befa2010-07-15 17:05:32 -0700216 # Would like an assert that checks the data plane
217
Dan Talayco6ce963a2010-03-07 21:58:13 -0800218class Echo(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800219 """
220 Test echo response with no data
221 """
222 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800223 request = message.echo_request()
Dan Talaycoe226eb12010-02-18 23:06:30 -0800224 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700225 self.assertTrue(response is not None,
226 "Did not get echo reply")
Dan Talayco2c0dba32010-03-06 22:47:06 -0800227 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800228 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800229 self.assertEqual(request.header.xid, response.header.xid,
230 'response xid != request xid')
231 self.assertEqual(len(response.data), 0, 'response data non-empty')
232
Dan Talayco6ce963a2010-03-07 21:58:13 -0800233class EchoWithData(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800234 """
235 Test echo response with short string data
236 """
237 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800238 request = message.echo_request()
Dan Talaycodba244e2010-02-15 14:08:53 -0800239 request.data = 'OpenFlow Will Rule The World'
Dan Talaycoe226eb12010-02-18 23:06:30 -0800240 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700241 self.assertTrue(response is not None,
242 "Did not get echo reply (with data)")
Dan Talayco2c0dba32010-03-06 22:47:06 -0800243 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800244 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800245 self.assertEqual(request.header.xid, response.header.xid,
246 'response xid != request xid')
247 self.assertEqual(request.data, response.data,
248 'response data does not match request')
249
Dan Talayco6ce963a2010-03-07 21:58:13 -0800250class PacketIn(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800251 """
252 Test packet in function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800253
254 Send a packet to each dataplane port and verify that a packet
255 in message is received from the controller for each
Dan Talaycodba244e2010-02-15 14:08:53 -0800256 """
257 def runTest(self):
258 # Construct packet to send to dataplane
Dan Talaycoe226eb12010-02-18 23:06:30 -0800259 # Send packet to dataplane, once to each port
Dan Talaycodba244e2010-02-15 14:08:53 -0800260 # Poll controller with expect message type packet in
Dan Talaycoe226eb12010-02-18 23:06:30 -0800261
Dan Talayco6ce963a2010-03-07 21:58:13 -0800262 rc = delete_all_flows(self.controller, basic_logger)
263 self.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talayco0fc08bd2012-04-09 16:56:18 -0700264 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800265
Ken Chiangaeb23d62012-08-23 21:20:07 -0700266 vid = test_param_get(self.config, 'vid', default=TEST_VID_DEFAULT)
267
Dan Talayco48370102010-03-03 15:17:33 -0800268 for of_port in basic_port_map.keys():
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700269 for pkt, pt in [
270 (simple_tcp_packet(), "simple TCP packet"),
Ken Chiangaeb23d62012-08-23 21:20:07 -0700271 (simple_tcp_packet(dl_vlan_enable=True,dl_vlan=vid,pktlen=108),
272 "simple tagged TCP packet"),
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700273 (simple_eth_packet(), "simple Ethernet packet"),
274 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
Dan Talaycodba244e2010-02-15 14:08:53 -0800275
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700276 basic_logger.info("PKT IN test with %s, port %s" % (pt, of_port))
277 self.dataplane.send(of_port, str(pkt))
278 #@todo Check for unexpected messages?
279 count = 0
280 while True:
281 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2)
282 if not response: # Timeout
283 break
Ed Swierk506614a2012-03-29 08:16:59 -0700284 if dataplane.match_exp_pkt(pkt, response.data): # Got match
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700285 break
286 if not basic_config["relax"]: # Only one attempt to match
287 break
288 count += 1
289 if count > 10: # Too many tries
290 break
Dan Talayco48370102010-03-03 15:17:33 -0800291
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700292 self.assertTrue(response is not None,
293 'Packet in message not received on port ' +
294 str(of_port))
Ed Swierk506614a2012-03-29 08:16:59 -0700295 if not dataplane.match_exp_pkt(pkt, response.data):
Dan Talayco2baf8b52012-03-30 09:55:42 -0700296 basic_logger.debug("Sent %s" % format_packet(pkt))
297 basic_logger.debug("Resp %s" % format_packet(response.data))
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700298 self.assertTrue(False,
299 'Response packet does not match send packet' +
300 ' for port ' + str(of_port))
Dan Talaycodba244e2010-02-15 14:08:53 -0800301
Ed Swierk3ae7f712012-08-22 06:45:25 -0700302class PacketInDefaultDrop(SimpleDataPlane):
303 """
304 Test packet in function
305
306 Send a packet to each dataplane port and verify that a packet
307 in message is received from the controller for each
308 """
309 def runTest(self):
310 rc = delete_all_flows(self.controller, basic_logger)
311 self.assertEqual(rc, 0, "Failed to delete all flows")
312 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
313
314 for of_port in basic_port_map.keys():
315 pkt = simple_tcp_packet()
316 self.dataplane.send(of_port, str(pkt))
317 count = 0
318 while True:
319 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2)
320 if not response: # Timeout
321 break
322 if dataplane.match_exp_pkt(pkt, response.data): # Got match
323 break
324 if not basic_config["relax"]: # Only one attempt to match
325 break
326 count += 1
327 if count > 10: # Too many tries
328 break
329
330 self.assertTrue(response is None,
331 'Packet in message received on port ' +
332 str(of_port))
333
334test_prio["PacketInDefaultDrop"] = -1
335
Jeffrey Townsend4d5ca922012-07-11 11:37:35 -0700336
Dan Talayco1f648cb2012-05-03 09:37:56 -0700337class PacketInBroadcastCheck(SimpleDataPlane):
338 """
339 Check if bcast pkts leak when no flows are present
340
341 Clear the flow table
342 Send in a broadcast pkt
343 Look for the packet on other dataplane ports.
344 """
345 def runTest(self):
346 # Need at least two ports
347 self.assertTrue(len(basic_port_map) > 1, "Too few ports for test")
348
349 rc = delete_all_flows(self.controller, basic_logger)
350 self.assertEqual(rc, 0, "Failed to delete all flows")
351 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
352
353 of_ports = basic_port_map.keys()
354 d_port = of_ports[0]
355 pkt = simple_eth_packet(dl_dst='ff:ff:ff:ff:ff:ff')
356
357 basic_logger.info("BCast Leak Test, send to port %s" % d_port)
358 self.dataplane.send(d_port, str(pkt))
359
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700360 (of_port, pkt_in, pkt_time) = self.dataplane.poll(exp_pkt=pkt)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700361 self.assertTrue(pkt_in is None,
362 'BCast packet received on port ' + str(of_port))
363
364test_prio["PacketInBroadcastCheck"] = -1
365
Dan Talayco6ce963a2010-03-07 21:58:13 -0800366class PacketOut(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800367 """
368 Test packet out function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800369
370 Send packet out message to controller for each dataplane port and
371 verify the packet appears on the appropriate dataplane port
Dan Talaycodba244e2010-02-15 14:08:53 -0800372 """
373 def runTest(self):
374 # Construct packet to send to dataplane
375 # Send packet to dataplane
376 # Poll controller with expect message type packet in
Dan Talayco41eae8b2010-03-10 13:57:06 -0800377
378 rc = delete_all_flows(self.controller, basic_logger)
379 self.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talaycodba244e2010-02-15 14:08:53 -0800380
381 # These will get put into function
Dan Talayco48370102010-03-03 15:17:33 -0800382 of_ports = basic_port_map.keys()
383 of_ports.sort()
384 for dp_port in of_ports:
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700385 for outpkt, opt in [
386 (simple_tcp_packet(), "simple TCP packet"),
387 (simple_eth_packet(), "simple Ethernet packet"),
388 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
Dan Talaycodba244e2010-02-15 14:08:53 -0800389
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700390 basic_logger.info("PKT OUT test with %s, port %s" % (opt, dp_port))
391 msg = message.packet_out()
392 msg.data = str(outpkt)
393 act = action.action_output()
394 act.port = dp_port
395 self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
Dan Talaycodba244e2010-02-15 14:08:53 -0800396
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700397 basic_logger.info("PacketOut to: " + str(dp_port))
398 rv = self.controller.message_send(msg)
399 self.assertTrue(rv == 0, "Error sending out message")
Dan Talaycodba244e2010-02-15 14:08:53 -0800400
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700401 exp_pkt_arg = None
402 exp_port = None
403 if basic_config["relax"]:
404 exp_pkt_arg = outpkt
405 exp_port = dp_port
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700406 (of_port, pkt, pkt_time) = self.dataplane.poll(port_number=exp_port,
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700407 exp_pkt=exp_pkt_arg)
408
409 self.assertTrue(pkt is not None, 'Packet not received')
410 basic_logger.info("PacketOut: got pkt from " + str(of_port))
411 if of_port is not None:
412 self.assertEqual(of_port, dp_port, "Unexpected receive port")
Ed Swierk506614a2012-03-29 08:16:59 -0700413 if not dataplane.match_exp_pkt(outpkt, pkt):
Dan Talayco2baf8b52012-03-30 09:55:42 -0700414 basic_logger.debug("Sent %s" % format_packet(outpkt))
415 basic_logger.debug("Resp %s" % format_packet(
416 str(pkt)[:len(str(outpkt))]))
Dan Talaycodc6fca32012-03-30 10:05:49 -0700417 self.assertEqual(str(outpkt), str(pkt)[:len(str(outpkt))],
418 'Response packet does not match send packet')
Dan Talaycodba244e2010-02-15 14:08:53 -0800419
Ken Chiang1bf01602012-04-04 10:48:23 -0700420class PacketOutMC(SimpleDataPlane):
421 """
422 Test packet out to multiple output ports
423
424 Send packet out message to controller for 1 to N dataplane ports and
425 verify the packet appears on the appropriate ports
426 """
427 def runTest(self):
428 # Construct packet to send to dataplane
429 # Send packet to dataplane
430 # Poll controller with expect message type packet in
431
432 rc = delete_all_flows(self.controller, basic_logger)
433 self.assertEqual(rc, 0, "Failed to delete all flows")
434
435 # These will get put into function
436 of_ports = basic_port_map.keys()
437 random.shuffle(of_ports)
438 for num_ports in range(1,len(of_ports)+1):
439 for outpkt, opt in [
440 (simple_tcp_packet(), "simple TCP packet"),
441 (simple_eth_packet(), "simple Ethernet packet"),
442 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
443
444 dp_ports = of_ports[0:num_ports]
445 basic_logger.info("PKT OUT test with " + opt +
446 ", ports " + str(dp_ports))
447 msg = message.packet_out()
448 msg.data = str(outpkt)
449 act = action.action_output()
450 for i in range(0,num_ports):
451 act.port = dp_ports[i]
452 self.assertTrue(msg.actions.add(act),
453 'Could not add action to msg')
454
455 basic_logger.info("PacketOut to: " + str(dp_ports))
456 rv = self.controller.message_send(msg)
457 self.assertTrue(rv == 0, "Error sending out message")
458
459 receive_pkt_check(self.dataplane, outpkt, dp_ports,
460 set(of_ports).difference(dp_ports),
461 self, basic_logger, basic_config)
462
Dan Talayco6ce963a2010-03-07 21:58:13 -0800463class FlowStatsGet(SimpleProtocol):
464 """
465 Get stats
Dan Talayco2c0dba32010-03-06 22:47:06 -0800466
Dan Talayco6ce963a2010-03-07 21:58:13 -0800467 Simply verify stats get transaction
468 """
469 def runTest(self):
470 basic_logger.info("Running StatsGet")
Dan Talayco41eae8b2010-03-10 13:57:06 -0800471 basic_logger.info("Inserting trial flow")
Dan Talayco677c0b72011-08-23 22:53:38 -0700472 request = flow_mod_gen(basic_port_map, True)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800473 rv = self.controller.message_send(request)
474 self.assertTrue(rv != -1, "Failed to insert test flow")
475
476 basic_logger.info("Sending flow request")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800477 request = message.flow_stats_request()
478 request.out_port = ofp.OFPP_NONE
Dan Talayco41eae8b2010-03-10 13:57:06 -0800479 request.table_id = 0xff
480 request.match.wildcards = 0 # ofp.OFPFW_ALL
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700481 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700482 self.assertTrue(response is not None,
483 "Did not get response for flow stats")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700484 basic_logger.debug(response.show())
Dan Talayco6ce963a2010-03-07 21:58:13 -0800485
Dan Talayco677c0b72011-08-23 22:53:38 -0700486test_prio["FlowStatsGet"] = -1
487
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700488class TableStatsGet(SimpleProtocol):
489 """
490 Get table stats
491
492 Simply verify table stats get transaction
493 """
494 def runTest(self):
495 basic_logger.info("Running TableStatsGet")
496 basic_logger.info("Inserting trial flow")
Dan Talayco677c0b72011-08-23 22:53:38 -0700497 request = flow_mod_gen(basic_port_map, True)
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700498 rv = self.controller.message_send(request)
499 self.assertTrue(rv != -1, "Failed to insert test flow")
500
501 basic_logger.info("Sending table stats request")
502 request = message.table_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700503 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700504 self.assertTrue(response is not None,
505 "Did not get reply for table stats")
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700506 basic_logger.debug(response.show())
507
Ed Swierkae74c362012-04-02 08:21:41 -0700508class DescStatsGet(SimpleProtocol):
509 """
510 Get stats
511
512 Simply verify stats get transaction
513 """
514 def runTest(self):
515 basic_logger.info("Running DescStatsGet")
516
517 basic_logger.info("Sending stats request")
518 request = message.desc_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700519 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700520 self.assertTrue(response is not None,
521 "Did not get reply for desc stats")
Ed Swierkae74c362012-04-02 08:21:41 -0700522 basic_logger.debug(response.show())
523
Dan Talayco6ce963a2010-03-07 21:58:13 -0800524class FlowMod(SimpleProtocol):
525 """
526 Insert a flow
527
528 Simple verification of a flow mod transaction
529 """
530
531 def runTest(self):
532 basic_logger.info("Running " + str(self))
Dan Talayco677c0b72011-08-23 22:53:38 -0700533 request = flow_mod_gen(basic_port_map, True)
Dan Talayco6ce963a2010-03-07 21:58:13 -0800534 rv = self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800535 self.assertTrue(rv != -1, "Error installing flow mod")
536
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700537class PortConfigMod(SimpleProtocol):
538 """
539 Modify a bit in port config and verify changed
540
541 Get the switch configuration, modify the port configuration
542 and write it back; get the config again and verify changed.
543 Then set it back to the way it was.
544 """
545
546 def runTest(self):
547 basic_logger.info("Running " + str(self))
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700548 for of_port, ifname in basic_port_map.items(): # Grab first port
549 break
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700550
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700551 (hw_addr, config, advert) = \
552 port_config_get(self.controller, of_port, basic_logger)
553 self.assertTrue(config is not None, "Did not get port config")
554
555 basic_logger.debug("No flood bit port " + str(of_port) + " is now " +
556 str(config & ofp.OFPPC_NO_FLOOD))
557
558 rv = port_config_set(self.controller, of_port,
559 config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD,
560 basic_logger)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700561 self.assertTrue(rv != -1, "Error sending port mod")
562
563 # Verify change took place with same feature request
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700564 (hw_addr, config2, advert) = \
565 port_config_get(self.controller, of_port, basic_logger)
566 basic_logger.debug("No flood bit port " + str(of_port) + " is now " +
567 str(config2 & ofp.OFPPC_NO_FLOOD))
568 self.assertTrue(config2 is not None, "Did not get port config2")
569 self.assertTrue(config2 & ofp.OFPPC_NO_FLOOD !=
570 config & ofp.OFPPC_NO_FLOOD,
571 "Bit change did not take")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700572 # Set it back
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700573 rv = port_config_set(self.controller, of_port, config,
574 ofp.OFPPC_NO_FLOOD, basic_logger)
575 self.assertTrue(rv != -1, "Error sending port mod")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700576
Ken Chiangaeb23d62012-08-23 21:20:07 -0700577class PortConfigModErr(SimpleProtocol):
578 """
579 Modify a bit in port config on an invalid port and verify
580 error message is received.
581 """
582
583 def runTest(self):
584 basic_logger.info("Running " + str(self))
585
586 # pick a random bad port number
587 bad_port = random.randint(1, ofp.OFPP_MAX)
588 count = 0
589 while (count < 50) and (bad_port in basic_port_map.keys()):
590 bad_port = random.randint(1, ofp.OFPP_MAX)
591 count = count + 1
592 self.assertTrue(count < 50, "Error selecting bad port")
593 basic_logger.info("Select " + str(bad_port) + " as invalid port")
594
595 rv = port_config_set(self.controller, bad_port,
596 ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD,
597 basic_logger)
598 self.assertTrue(rv != -1, "Error sending port mod")
599
600 # poll for error message
601 while True:
602 (response, raw) = self.controller.poll(ofp.OFPT_ERROR, 2)
603 if not response: # Timeout
604 break
605 if response.code == ofp.OFPPMFC_BAD_PORT:
606 basic_logger.info("Received error message with OFPPMFC_BAD_PORT code")
607 break
608 if not basic_config["relax"]: # Only one attempt to match
609 break
610 count += 1
611 if count > 10: # Too many tries
612 break
613
614 self.assertTrue(response is not None, 'Did not receive error message')
615
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700616class BadMessage(SimpleProtocol):
617 """
618 Send a message with a bad type and verify an error is returned
619 """
620
621 def runTest(self):
622 basic_logger.info("Running " + str(self))
623 request = illegal_message.illegal_message_type()
624
625 reply, pkt = self.controller.transact(request, timeout=10)
626 self.assertTrue(reply is not None, "Did not get response to bad req")
627 self.assertTrue(reply.header.type == ofp.OFPT_ERROR,
628 "reply not an error message")
629 self.assertTrue(reply.type == ofp.OFPET_BAD_REQUEST,
630 "reply error type is not bad request")
631 self.assertTrue(reply.code == ofp.OFPBRC_BAD_TYPE,
632 "reply error code is not bad type")
633
Dan Talaycodba244e2010-02-15 14:08:53 -0800634if __name__ == "__main__":
Dan Talayco2c0dba32010-03-06 22:47:06 -0800635 print "Please run through oft script: ./oft --test_spec=basic"