blob: 97ab2f630ab2d056d3f7051be4b4dd53eef34673 [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 Talayco6ce963a2010-03-07 21:58:13 -080032from testutils import *
33
34#@var basic_port_map Local copy of the configuration map from OF port
35# numbers to OS interfaces
Dan Talayco48370102010-03-03 15:17:33 -080036basic_port_map = None
Dan Talayco6ce963a2010-03-07 21:58:13 -080037#@var basic_logger Local logger object
Dan Talayco48370102010-03-03 15:17:33 -080038basic_logger = None
Dan Talayco6ce963a2010-03-07 21:58:13 -080039#@var basic_config Local copy of global configuration data
Dan Talayco48370102010-03-03 15:17:33 -080040basic_config = None
41
Dan Talaycoc24aaae2010-07-08 14:05:24 -070042test_prio = {}
43
Ken Chiangaeb23d62012-08-23 21:20:07 -070044TEST_VID_DEFAULT = 2
45
Dan Talayco48370102010-03-03 15:17:33 -080046def test_set_init(config):
47 """
48 Set up function for basic test classes
49
50 @param config The configuration dictionary; see oft
Dan Talayco48370102010-03-03 15:17:33 -080051 """
52
53 global basic_port_map
54 global basic_logger
55 global basic_config
56
57 basic_logger = logging.getLogger("basic")
58 basic_logger.info("Initializing test set")
59 basic_port_map = config["port_map"]
60 basic_config = config
Dan Talayco48370102010-03-03 15:17:33 -080061
Dan Talayco6ce963a2010-03-07 21:58:13 -080062class SimpleProtocol(unittest.TestCase):
Dan Talaycodba244e2010-02-15 14:08:53 -080063 """
64 Root class for setting up the controller
65 """
66
Dan Talaycoef701f42010-05-07 09:22:35 -070067 def sig_handler(self, v1, v2):
Dan Talayco48370102010-03-03 15:17:33 -080068 basic_logger.critical("Received interrupt signal; exiting")
Dan Talayco710438c2010-02-18 15:16:07 -080069 print "Received interrupt signal; exiting"
Dan Talayco2c0dba32010-03-06 22:47:06 -080070 self.clean_shutdown = False
71 self.tearDown()
Rich Lane58cf05f2012-07-11 16:41:47 -070072 raise KeyboardInterrupt
Dan Talayco710438c2010-02-18 15:16:07 -080073
Dan Talaycodba244e2010-02-15 14:08:53 -080074 def setUp(self):
Dan Talayco551befa2010-07-15 17:05:32 -070075 self.logger = basic_logger
Dan Talayco285a8382010-07-20 14:06:55 -070076 self.config = basic_config
Ed Swierk022d02e2012-08-22 06:26:36 -070077 #@todo Test cases shouldn't monkey with signals; move SIGINT handler
78 # to top-level oft
79 try:
80 signal.signal(signal.SIGINT, self.sig_handler)
81 except ValueError, e:
82 basic_logger.info("Could not set SIGINT handler: %s" % e)
Dan Talayco9f47f4d2010-06-03 13:54:37 -070083 basic_logger.info("** START TEST CASE " + str(self))
Dan Talayco2c0dba32010-03-06 22:47:06 -080084 self.controller = controller.Controller(
85 host=basic_config["controller_host"],
86 port=basic_config["controller_port"])
Dan Talaycof8f41402010-03-12 22:17:39 -080087 # clean_shutdown should be set to False to force quit app
Dan Talayco2c0dba32010-03-06 22:47:06 -080088 self.clean_shutdown = True
Dan Talayco710438c2010-02-18 15:16:07 -080089 self.controller.start()
Dan Talaycoef701f42010-05-07 09:22:35 -070090 #@todo Add an option to wait for a pkt transaction to ensure version
91 # compatibilty?
Dan Talayco710438c2010-02-18 15:16:07 -080092 self.controller.connect(timeout=20)
Dan Talaycoef701f42010-05-07 09:22:35 -070093 if not self.controller.active:
Rich Lane58cf05f2012-07-11 16:41:47 -070094 raise Exception("Controller startup failed")
Dan Talayco677c0b72011-08-23 22:53:38 -070095 if self.controller.switch_addr is None:
Rich Lane58cf05f2012-07-11 16:41:47 -070096 raise Exception("Controller startup failed (no switch addr)")
Dan Talayco48370102010-03-03 15:17:33 -080097 basic_logger.info("Connected " + str(self.controller.switch_addr))
Ed Swierkc7193a22012-08-22 06:51:02 -070098 request = message.features_request()
99 reply, pkt = self.controller.transact(request, timeout=10)
Dan Talayco97d4f362012-09-18 03:22:09 -0700100 self.assertTrue(reply is not None,
101 "Did not complete features_request for handshake")
Ed Swierkc7193a22012-08-22 06:51:02 -0700102 self.supported_actions = reply.actions
103 basic_logger.info("Supported actions: " + hex(self.supported_actions))
Dan Talaycodba244e2010-02-15 14:08:53 -0800104
Dan Talayco677cc112012-03-27 10:28:58 -0700105 def inheritSetup(self, parent):
106 """
107 Inherit the setup of a parent
108
109 This allows running at test from within another test. Do the
110 following:
111
112 sub_test = SomeTestClass() # Create an instance of the test class
113 sub_test.inheritSetup(self) # Inherit setup of parent
114 sub_test.runTest() # Run the test
115
116 Normally, only the parent's setUp and tearDown are called and
117 the state after the sub_test is run must be taken into account
118 by subsequent operations.
119 """
120 self.logger = parent.logger
121 self.config = parent.config
122 basic_logger.info("** Setup " + str(self) + " inheriting from "
123 + str(parent))
124 self.controller = parent.controller
Ed Swierk6192e512012-08-22 11:41:40 -0700125 self.supported_actions = parent.supported_actions
Dan Talayco677cc112012-03-27 10:28:58 -0700126
Dan Talaycodba244e2010-02-15 14:08:53 -0800127 def tearDown(self):
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700128 basic_logger.info("** END TEST CASE " + str(self))
Dan Talaycodba244e2010-02-15 14:08:53 -0800129 self.controller.shutdown()
Dan Talayco2c0dba32010-03-06 22:47:06 -0800130 #@todo Review if join should be done on clean_shutdown
Dan Talaycof8f41402010-03-12 22:17:39 -0800131 if self.clean_shutdown:
132 self.controller.join()
Dan Talaycodba244e2010-02-15 14:08:53 -0800133
134 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -0800135 # Just a simple sanity check as illustration
Dan Talayco48370102010-03-03 15:17:33 -0800136 basic_logger.info("Running simple proto test")
Dan Talayco710438c2010-02-18 15:16:07 -0800137 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -0800138 str(self) + 'No connection to switch')
139
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700140 def assertTrue(self, cond, msg):
141 if not cond:
142 basic_logger.error("** FAILED ASSERTION: " + msg)
143 unittest.TestCase.assertTrue(self, cond, msg)
144
Dan Talaycoc24aaae2010-07-08 14:05:24 -0700145test_prio["SimpleProtocol"] = 1
146
Dan Talayco6ce963a2010-03-07 21:58:13 -0800147class SimpleDataPlane(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800148 """
149 Root class that sets up the controller and dataplane
150 """
151 def setUp(self):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800152 SimpleProtocol.setUp(self)
Jeffrey Townsend4d5ca922012-07-11 11:37:35 -0700153 self.dataplane = dataplane.DataPlane(self.config)
Dan Talayco48370102010-03-03 15:17:33 -0800154 for of_port, ifname in basic_port_map.items():
Dan Talaycodba244e2010-02-15 14:08:53 -0800155 self.dataplane.port_add(ifname, of_port)
156
Dan Talayco677cc112012-03-27 10:28:58 -0700157 def inheritSetup(self, parent):
158 """
159 Inherit the setup of a parent
160
161 See SimpleProtocol.inheritSetup
162 """
163 SimpleProtocol.inheritSetup(self, parent)
164 self.dataplane = parent.dataplane
165
Dan Talaycodba244e2010-02-15 14:08:53 -0800166 def tearDown(self):
Dan Talayco48370102010-03-03 15:17:33 -0800167 basic_logger.info("Teardown for simple dataplane test")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800168 SimpleProtocol.tearDown(self)
Rich Lane58cf05f2012-07-11 16:41:47 -0700169 if hasattr(self, 'dataplane'):
170 self.dataplane.kill(join_threads=self.clean_shutdown)
Dan Talayco48370102010-03-03 15:17:33 -0800171 basic_logger.info("Teardown done")
Dan Talaycodba244e2010-02-15 14:08:53 -0800172
173 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -0800174 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -0800175 str(self) + 'No connection to switch')
176 # self.dataplane.show()
177 # Would like an assert that checks the data plane
178
Dan Talayco551befa2010-07-15 17:05:32 -0700179class DataPlaneOnly(unittest.TestCase):
180 """
181 Root class that sets up only the dataplane
182 """
183
184 def sig_handler(self, v1, v2):
185 basic_logger.critical("Received interrupt signal; exiting")
186 print "Received interrupt signal; exiting"
187 self.clean_shutdown = False
188 self.tearDown()
Rich Lane58cf05f2012-07-11 16:41:47 -0700189 raise KeyboardInterrupt
Dan Talayco551befa2010-07-15 17:05:32 -0700190
191 def setUp(self):
Shudong Zhoue3582a52012-08-03 20:46:50 -0700192 self.clean_shutdown = True
Dan Talayco551befa2010-07-15 17:05:32 -0700193 self.logger = basic_logger
Dan Talayco285a8382010-07-20 14:06:55 -0700194 self.config = basic_config
Ed Swierk022d02e2012-08-22 06:26:36 -0700195 #@todo Test cases shouldn't monkey with signals; move SIGINT handler
196 # to top-level oft
197 try:
198 signal.signal(signal.SIGINT, self.sig_handler)
199 except ValueError, e:
200 basic_logger.info("Could not set SIGINT handler: %s" % e)
Dan Talayco551befa2010-07-15 17:05:32 -0700201 basic_logger.info("** START DataPlaneOnly CASE " + str(self))
Jeffrey Townsend4d5ca922012-07-11 11:37:35 -0700202 self.dataplane = dataplane.DataPlane(self.config)
Dan Talayco551befa2010-07-15 17:05:32 -0700203 for of_port, ifname in basic_port_map.items():
204 self.dataplane.port_add(ifname, of_port)
205
206 def tearDown(self):
207 basic_logger.info("Teardown for simple dataplane test")
208 self.dataplane.kill(join_threads=self.clean_shutdown)
209 basic_logger.info("Teardown done")
210
211 def runTest(self):
Dan Talaycoba4fd4f2010-07-21 21:49:41 -0700212 basic_logger.info("DataPlaneOnly")
Dan Talayco285a8382010-07-20 14:06:55 -0700213 # self.dataplane.show()
Dan Talayco551befa2010-07-15 17:05:32 -0700214 # Would like an assert that checks the data plane
215
Dan Talayco6ce963a2010-03-07 21:58:13 -0800216class Echo(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800217 """
218 Test echo response with no data
219 """
220 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800221 request = message.echo_request()
Dan Talaycoe226eb12010-02-18 23:06:30 -0800222 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700223 self.assertTrue(response is not None,
224 "Did not get echo reply")
Dan Talayco2c0dba32010-03-06 22:47:06 -0800225 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800226 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800227 self.assertEqual(request.header.xid, response.header.xid,
228 'response xid != request xid')
229 self.assertEqual(len(response.data), 0, 'response data non-empty')
230
Dan Talayco6ce963a2010-03-07 21:58:13 -0800231class EchoWithData(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800232 """
233 Test echo response with short string data
234 """
235 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800236 request = message.echo_request()
Dan Talaycodba244e2010-02-15 14:08:53 -0800237 request.data = 'OpenFlow Will Rule The World'
Dan Talaycoe226eb12010-02-18 23:06:30 -0800238 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700239 self.assertTrue(response is not None,
240 "Did not get echo reply (with data)")
Dan Talayco2c0dba32010-03-06 22:47:06 -0800241 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800242 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800243 self.assertEqual(request.header.xid, response.header.xid,
244 'response xid != request xid')
245 self.assertEqual(request.data, response.data,
246 'response data does not match request')
247
Dan Talayco6ce963a2010-03-07 21:58:13 -0800248class PacketIn(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800249 """
250 Test packet in function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800251
252 Send a packet to each dataplane port and verify that a packet
253 in message is received from the controller for each
Dan Talaycodba244e2010-02-15 14:08:53 -0800254 """
255 def runTest(self):
256 # Construct packet to send to dataplane
Dan Talaycoe226eb12010-02-18 23:06:30 -0800257 # Send packet to dataplane, once to each port
Dan Talaycodba244e2010-02-15 14:08:53 -0800258 # Poll controller with expect message type packet in
Dan Talaycoe226eb12010-02-18 23:06:30 -0800259
Dan Talayco6ce963a2010-03-07 21:58:13 -0800260 rc = delete_all_flows(self.controller, basic_logger)
261 self.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talayco0fc08bd2012-04-09 16:56:18 -0700262 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800263
Ken Chiangaeb23d62012-08-23 21:20:07 -0700264 vid = test_param_get(self.config, 'vid', default=TEST_VID_DEFAULT)
265
Dan Talayco48370102010-03-03 15:17:33 -0800266 for of_port in basic_port_map.keys():
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700267 for pkt, pt in [
268 (simple_tcp_packet(), "simple TCP packet"),
Ken Chiangaeb23d62012-08-23 21:20:07 -0700269 (simple_tcp_packet(dl_vlan_enable=True,dl_vlan=vid,pktlen=108),
270 "simple tagged TCP packet"),
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700271 (simple_eth_packet(), "simple Ethernet packet"),
272 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
Dan Talaycodba244e2010-02-15 14:08:53 -0800273
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700274 basic_logger.info("PKT IN test with %s, port %s" % (pt, of_port))
275 self.dataplane.send(of_port, str(pkt))
276 #@todo Check for unexpected messages?
277 count = 0
278 while True:
279 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2)
280 if not response: # Timeout
281 break
Ed Swierk506614a2012-03-29 08:16:59 -0700282 if dataplane.match_exp_pkt(pkt, response.data): # Got match
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700283 break
284 if not basic_config["relax"]: # Only one attempt to match
285 break
286 count += 1
287 if count > 10: # Too many tries
288 break
Dan Talayco48370102010-03-03 15:17:33 -0800289
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700290 self.assertTrue(response is not None,
291 'Packet in message not received on port ' +
292 str(of_port))
Ed Swierk506614a2012-03-29 08:16:59 -0700293 if not dataplane.match_exp_pkt(pkt, response.data):
Dan Talayco2baf8b52012-03-30 09:55:42 -0700294 basic_logger.debug("Sent %s" % format_packet(pkt))
295 basic_logger.debug("Resp %s" % format_packet(response.data))
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700296 self.assertTrue(False,
297 'Response packet does not match send packet' +
298 ' for port ' + str(of_port))
Dan Talaycodba244e2010-02-15 14:08:53 -0800299
Ed Swierk3ae7f712012-08-22 06:45:25 -0700300class PacketInDefaultDrop(SimpleDataPlane):
301 """
302 Test packet in function
303
304 Send a packet to each dataplane port and verify that a packet
305 in message is received from the controller for each
306 """
307 def runTest(self):
308 rc = delete_all_flows(self.controller, basic_logger)
309 self.assertEqual(rc, 0, "Failed to delete all flows")
310 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
311
312 for of_port in basic_port_map.keys():
313 pkt = simple_tcp_packet()
314 self.dataplane.send(of_port, str(pkt))
315 count = 0
316 while True:
317 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2)
318 if not response: # Timeout
319 break
320 if dataplane.match_exp_pkt(pkt, response.data): # Got match
321 break
322 if not basic_config["relax"]: # Only one attempt to match
323 break
324 count += 1
325 if count > 10: # Too many tries
326 break
327
328 self.assertTrue(response is None,
329 'Packet in message received on port ' +
330 str(of_port))
331
332test_prio["PacketInDefaultDrop"] = -1
333
Jeffrey Townsend4d5ca922012-07-11 11:37:35 -0700334
Dan Talayco1f648cb2012-05-03 09:37:56 -0700335class PacketInBroadcastCheck(SimpleDataPlane):
336 """
337 Check if bcast pkts leak when no flows are present
338
339 Clear the flow table
340 Send in a broadcast pkt
341 Look for the packet on other dataplane ports.
342 """
343 def runTest(self):
344 # Need at least two ports
345 self.assertTrue(len(basic_port_map) > 1, "Too few ports for test")
346
347 rc = delete_all_flows(self.controller, basic_logger)
348 self.assertEqual(rc, 0, "Failed to delete all flows")
349 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
350
351 of_ports = basic_port_map.keys()
352 d_port = of_ports[0]
353 pkt = simple_eth_packet(dl_dst='ff:ff:ff:ff:ff:ff')
354
355 basic_logger.info("BCast Leak Test, send to port %s" % d_port)
356 self.dataplane.send(d_port, str(pkt))
357
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700358 (of_port, pkt_in, pkt_time) = self.dataplane.poll(exp_pkt=pkt)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700359 self.assertTrue(pkt_in is None,
360 'BCast packet received on port ' + str(of_port))
361
362test_prio["PacketInBroadcastCheck"] = -1
363
Dan Talayco6ce963a2010-03-07 21:58:13 -0800364class PacketOut(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800365 """
366 Test packet out function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800367
368 Send packet out message to controller for each dataplane port and
369 verify the packet appears on the appropriate dataplane port
Dan Talaycodba244e2010-02-15 14:08:53 -0800370 """
371 def runTest(self):
372 # Construct packet to send to dataplane
373 # Send packet to dataplane
374 # Poll controller with expect message type packet in
Dan Talayco41eae8b2010-03-10 13:57:06 -0800375
376 rc = delete_all_flows(self.controller, basic_logger)
377 self.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talaycodba244e2010-02-15 14:08:53 -0800378
379 # These will get put into function
Dan Talayco48370102010-03-03 15:17:33 -0800380 of_ports = basic_port_map.keys()
381 of_ports.sort()
382 for dp_port in of_ports:
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700383 for outpkt, opt in [
384 (simple_tcp_packet(), "simple TCP packet"),
385 (simple_eth_packet(), "simple Ethernet packet"),
386 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
Dan Talaycodba244e2010-02-15 14:08:53 -0800387
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700388 basic_logger.info("PKT OUT test with %s, port %s" % (opt, dp_port))
389 msg = message.packet_out()
390 msg.data = str(outpkt)
391 act = action.action_output()
392 act.port = dp_port
393 self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
Dan Talaycodba244e2010-02-15 14:08:53 -0800394
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700395 basic_logger.info("PacketOut to: " + str(dp_port))
396 rv = self.controller.message_send(msg)
397 self.assertTrue(rv == 0, "Error sending out message")
Dan Talaycodba244e2010-02-15 14:08:53 -0800398
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700399 exp_pkt_arg = None
400 exp_port = None
401 if basic_config["relax"]:
402 exp_pkt_arg = outpkt
403 exp_port = dp_port
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700404 (of_port, pkt, pkt_time) = self.dataplane.poll(port_number=exp_port,
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700405 exp_pkt=exp_pkt_arg)
406
407 self.assertTrue(pkt is not None, 'Packet not received')
408 basic_logger.info("PacketOut: got pkt from " + str(of_port))
409 if of_port is not None:
410 self.assertEqual(of_port, dp_port, "Unexpected receive port")
Ed Swierk506614a2012-03-29 08:16:59 -0700411 if not dataplane.match_exp_pkt(outpkt, pkt):
Dan Talayco2baf8b52012-03-30 09:55:42 -0700412 basic_logger.debug("Sent %s" % format_packet(outpkt))
413 basic_logger.debug("Resp %s" % format_packet(
414 str(pkt)[:len(str(outpkt))]))
Dan Talaycodc6fca32012-03-30 10:05:49 -0700415 self.assertEqual(str(outpkt), str(pkt)[:len(str(outpkt))],
416 'Response packet does not match send packet')
Dan Talaycodba244e2010-02-15 14:08:53 -0800417
Ken Chiang1bf01602012-04-04 10:48:23 -0700418class PacketOutMC(SimpleDataPlane):
419 """
420 Test packet out to multiple output ports
421
422 Send packet out message to controller for 1 to N dataplane ports and
423 verify the packet appears on the appropriate ports
424 """
425 def runTest(self):
426 # Construct packet to send to dataplane
427 # Send packet to dataplane
428 # Poll controller with expect message type packet in
429
430 rc = delete_all_flows(self.controller, basic_logger)
431 self.assertEqual(rc, 0, "Failed to delete all flows")
432
433 # These will get put into function
434 of_ports = basic_port_map.keys()
435 random.shuffle(of_ports)
436 for num_ports in range(1,len(of_ports)+1):
437 for outpkt, opt in [
438 (simple_tcp_packet(), "simple TCP packet"),
439 (simple_eth_packet(), "simple Ethernet packet"),
440 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
441
442 dp_ports = of_ports[0:num_ports]
443 basic_logger.info("PKT OUT test with " + opt +
444 ", ports " + str(dp_ports))
445 msg = message.packet_out()
446 msg.data = str(outpkt)
447 act = action.action_output()
448 for i in range(0,num_ports):
449 act.port = dp_ports[i]
450 self.assertTrue(msg.actions.add(act),
451 'Could not add action to msg')
452
453 basic_logger.info("PacketOut to: " + str(dp_ports))
454 rv = self.controller.message_send(msg)
455 self.assertTrue(rv == 0, "Error sending out message")
456
457 receive_pkt_check(self.dataplane, outpkt, dp_ports,
458 set(of_ports).difference(dp_ports),
459 self, basic_logger, basic_config)
460
Dan Talayco6ce963a2010-03-07 21:58:13 -0800461class FlowStatsGet(SimpleProtocol):
462 """
463 Get stats
Dan Talayco2c0dba32010-03-06 22:47:06 -0800464
Dan Talayco6ce963a2010-03-07 21:58:13 -0800465 Simply verify stats get transaction
466 """
467 def runTest(self):
468 basic_logger.info("Running StatsGet")
Dan Talayco41eae8b2010-03-10 13:57:06 -0800469 basic_logger.info("Inserting trial flow")
Dan Talayco677c0b72011-08-23 22:53:38 -0700470 request = flow_mod_gen(basic_port_map, True)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800471 rv = self.controller.message_send(request)
472 self.assertTrue(rv != -1, "Failed to insert test flow")
473
474 basic_logger.info("Sending flow request")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800475 request = message.flow_stats_request()
476 request.out_port = ofp.OFPP_NONE
Dan Talayco41eae8b2010-03-10 13:57:06 -0800477 request.table_id = 0xff
478 request.match.wildcards = 0 # ofp.OFPFW_ALL
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700479 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700480 self.assertTrue(response is not None,
481 "Did not get response for flow stats")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700482 basic_logger.debug(response.show())
Dan Talayco6ce963a2010-03-07 21:58:13 -0800483
Dan Talayco677c0b72011-08-23 22:53:38 -0700484test_prio["FlowStatsGet"] = -1
485
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700486class TableStatsGet(SimpleProtocol):
487 """
488 Get table stats
489
490 Simply verify table stats get transaction
491 """
492 def runTest(self):
493 basic_logger.info("Running TableStatsGet")
494 basic_logger.info("Inserting trial flow")
Dan Talayco677c0b72011-08-23 22:53:38 -0700495 request = flow_mod_gen(basic_port_map, True)
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700496 rv = self.controller.message_send(request)
497 self.assertTrue(rv != -1, "Failed to insert test flow")
498
499 basic_logger.info("Sending table stats request")
500 request = message.table_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700501 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700502 self.assertTrue(response is not None,
503 "Did not get reply for table stats")
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700504 basic_logger.debug(response.show())
505
Ed Swierkae74c362012-04-02 08:21:41 -0700506class DescStatsGet(SimpleProtocol):
507 """
508 Get stats
509
510 Simply verify stats get transaction
511 """
512 def runTest(self):
513 basic_logger.info("Running DescStatsGet")
514
515 basic_logger.info("Sending stats request")
516 request = message.desc_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700517 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700518 self.assertTrue(response is not None,
519 "Did not get reply for desc stats")
Ed Swierkae74c362012-04-02 08:21:41 -0700520 basic_logger.debug(response.show())
521
Dan Talayco6ce963a2010-03-07 21:58:13 -0800522class FlowMod(SimpleProtocol):
523 """
524 Insert a flow
525
526 Simple verification of a flow mod transaction
527 """
528
529 def runTest(self):
530 basic_logger.info("Running " + str(self))
Dan Talayco677c0b72011-08-23 22:53:38 -0700531 request = flow_mod_gen(basic_port_map, True)
Dan Talayco6ce963a2010-03-07 21:58:13 -0800532 rv = self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800533 self.assertTrue(rv != -1, "Error installing flow mod")
534
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700535class PortConfigMod(SimpleProtocol):
536 """
537 Modify a bit in port config and verify changed
538
539 Get the switch configuration, modify the port configuration
540 and write it back; get the config again and verify changed.
541 Then set it back to the way it was.
542 """
543
544 def runTest(self):
545 basic_logger.info("Running " + str(self))
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700546 for of_port, ifname in basic_port_map.items(): # Grab first port
547 break
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700548
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700549 (hw_addr, config, advert) = \
550 port_config_get(self.controller, of_port, basic_logger)
551 self.assertTrue(config is not None, "Did not get port config")
552
553 basic_logger.debug("No flood bit port " + str(of_port) + " is now " +
554 str(config & ofp.OFPPC_NO_FLOOD))
555
556 rv = port_config_set(self.controller, of_port,
557 config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD,
558 basic_logger)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700559 self.assertTrue(rv != -1, "Error sending port mod")
560
561 # Verify change took place with same feature request
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700562 (hw_addr, config2, advert) = \
563 port_config_get(self.controller, of_port, basic_logger)
564 basic_logger.debug("No flood bit port " + str(of_port) + " is now " +
565 str(config2 & ofp.OFPPC_NO_FLOOD))
566 self.assertTrue(config2 is not None, "Did not get port config2")
567 self.assertTrue(config2 & ofp.OFPPC_NO_FLOOD !=
568 config & ofp.OFPPC_NO_FLOOD,
569 "Bit change did not take")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700570 # Set it back
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700571 rv = port_config_set(self.controller, of_port, config,
572 ofp.OFPPC_NO_FLOOD, basic_logger)
573 self.assertTrue(rv != -1, "Error sending port mod")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700574
Ken Chiangaeb23d62012-08-23 21:20:07 -0700575class PortConfigModErr(SimpleProtocol):
576 """
577 Modify a bit in port config on an invalid port and verify
578 error message is received.
579 """
580
581 def runTest(self):
582 basic_logger.info("Running " + str(self))
583
584 # pick a random bad port number
585 bad_port = random.randint(1, ofp.OFPP_MAX)
586 count = 0
587 while (count < 50) and (bad_port in basic_port_map.keys()):
588 bad_port = random.randint(1, ofp.OFPP_MAX)
589 count = count + 1
590 self.assertTrue(count < 50, "Error selecting bad port")
591 basic_logger.info("Select " + str(bad_port) + " as invalid port")
592
593 rv = port_config_set(self.controller, bad_port,
594 ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD,
595 basic_logger)
596 self.assertTrue(rv != -1, "Error sending port mod")
597
598 # poll for error message
599 while True:
600 (response, raw) = self.controller.poll(ofp.OFPT_ERROR, 2)
601 if not response: # Timeout
602 break
603 if response.code == ofp.OFPPMFC_BAD_PORT:
604 basic_logger.info("Received error message with OFPPMFC_BAD_PORT code")
605 break
606 if not basic_config["relax"]: # Only one attempt to match
607 break
608 count += 1
609 if count > 10: # Too many tries
610 break
611
612 self.assertTrue(response is not None, 'Did not receive error message')
613
Dan Talaycodba244e2010-02-15 14:08:53 -0800614if __name__ == "__main__":
Dan Talayco2c0dba32010-03-06 22:47:06 -0800615 print "Please run through oft script: ./oft --test_spec=basic"