Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 1 | """ |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 2 | Basic protocol and dataplane test cases |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 3 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 4 | It is recommended that these definitions be kept in their own |
| 5 | namespace as different groups of tests will likely define |
| 6 | similar identifiers. |
| 7 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 8 | Current Assumptions: |
| 9 | |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 10 | The function test_set_init is called with a complete configuration |
| 11 | dictionary prior to the invocation of any tests from this file. |
| 12 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 13 | The switch is actively attempting to contact the controller at the address |
| 14 | indicated oin oft_config |
| 15 | |
| 16 | """ |
| 17 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 18 | import time |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 19 | import signal |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 20 | import sys |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 21 | import logging |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 22 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 23 | import unittest |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 24 | import random |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 25 | |
| 26 | import oftest.controller as controller |
| 27 | import oftest.cstruct as ofp |
| 28 | import oftest.message as message |
| 29 | import oftest.dataplane as dataplane |
| 30 | import oftest.action as action |
| 31 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 32 | from testutils import * |
| 33 | |
| 34 | #@var basic_port_map Local copy of the configuration map from OF port |
| 35 | # numbers to OS interfaces |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 36 | basic_port_map = None |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 37 | #@var basic_logger Local logger object |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 38 | basic_logger = None |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 39 | #@var basic_config Local copy of global configuration data |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 40 | basic_config = None |
| 41 | |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 42 | test_prio = {} |
| 43 | |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 44 | TEST_VID_DEFAULT = 2 |
| 45 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 46 | def test_set_init(config): |
| 47 | """ |
| 48 | Set up function for basic test classes |
| 49 | |
| 50 | @param config The configuration dictionary; see oft |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 51 | """ |
| 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 Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 61 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 62 | class SimpleProtocol(unittest.TestCase): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 63 | """ |
| 64 | Root class for setting up the controller |
| 65 | """ |
| 66 | |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame] | 67 | def sig_handler(self, v1, v2): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 68 | basic_logger.critical("Received interrupt signal; exiting") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 69 | print "Received interrupt signal; exiting" |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 70 | self.clean_shutdown = False |
| 71 | self.tearDown() |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 72 | raise KeyboardInterrupt |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 73 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 74 | def setUp(self): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 75 | self.logger = basic_logger |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 76 | self.config = basic_config |
Ed Swierk | 022d02e | 2012-08-22 06:26:36 -0700 | [diff] [blame] | 77 | #@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 Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 83 | basic_logger.info("** START TEST CASE " + str(self)) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 84 | self.controller = controller.Controller( |
| 85 | host=basic_config["controller_host"], |
| 86 | port=basic_config["controller_port"]) |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 87 | # clean_shutdown should be set to False to force quit app |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 88 | self.clean_shutdown = True |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 89 | self.controller.start() |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame] | 90 | #@todo Add an option to wait for a pkt transaction to ensure version |
| 91 | # compatibilty? |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 92 | self.controller.connect(timeout=20) |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame] | 93 | if not self.controller.active: |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 94 | raise Exception("Controller startup failed") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 95 | if self.controller.switch_addr is None: |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 96 | raise Exception("Controller startup failed (no switch addr)") |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 97 | basic_logger.info("Connected " + str(self.controller.switch_addr)) |
Ed Swierk | c7193a2 | 2012-08-22 06:51:02 -0700 | [diff] [blame] | 98 | request = message.features_request() |
| 99 | reply, pkt = self.controller.transact(request, timeout=10) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame^] | 100 | self.assertTrue(reply is not None, |
| 101 | "Did not complete features_request for handshake") |
Ed Swierk | c7193a2 | 2012-08-22 06:51:02 -0700 | [diff] [blame] | 102 | self.supported_actions = reply.actions |
| 103 | basic_logger.info("Supported actions: " + hex(self.supported_actions)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 104 | |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 105 | 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 Swierk | 6192e51 | 2012-08-22 11:41:40 -0700 | [diff] [blame] | 125 | self.supported_actions = parent.supported_actions |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 126 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 127 | def tearDown(self): |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 128 | basic_logger.info("** END TEST CASE " + str(self)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 129 | self.controller.shutdown() |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 130 | #@todo Review if join should be done on clean_shutdown |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 131 | if self.clean_shutdown: |
| 132 | self.controller.join() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 133 | |
| 134 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 135 | # Just a simple sanity check as illustration |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 136 | basic_logger.info("Running simple proto test") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 137 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 138 | str(self) + 'No connection to switch') |
| 139 | |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 140 | 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 Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 145 | test_prio["SimpleProtocol"] = 1 |
| 146 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 147 | class SimpleDataPlane(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 148 | """ |
| 149 | Root class that sets up the controller and dataplane |
| 150 | """ |
| 151 | def setUp(self): |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 152 | SimpleProtocol.setUp(self) |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 153 | self.dataplane = dataplane.DataPlane(self.config) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 154 | for of_port, ifname in basic_port_map.items(): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 155 | self.dataplane.port_add(ifname, of_port) |
| 156 | |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 157 | 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 Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 166 | def tearDown(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 167 | basic_logger.info("Teardown for simple dataplane test") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 168 | SimpleProtocol.tearDown(self) |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 169 | if hasattr(self, 'dataplane'): |
| 170 | self.dataplane.kill(join_threads=self.clean_shutdown) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 171 | basic_logger.info("Teardown done") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 172 | |
| 173 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 174 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 175 | str(self) + 'No connection to switch') |
| 176 | # self.dataplane.show() |
| 177 | # Would like an assert that checks the data plane |
| 178 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 179 | class 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 Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 189 | raise KeyboardInterrupt |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 190 | |
| 191 | def setUp(self): |
Shudong Zhou | e3582a5 | 2012-08-03 20:46:50 -0700 | [diff] [blame] | 192 | self.clean_shutdown = True |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 193 | self.logger = basic_logger |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 194 | self.config = basic_config |
Ed Swierk | 022d02e | 2012-08-22 06:26:36 -0700 | [diff] [blame] | 195 | #@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 Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 201 | basic_logger.info("** START DataPlaneOnly CASE " + str(self)) |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 202 | self.dataplane = dataplane.DataPlane(self.config) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 203 | 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 Talayco | ba4fd4f | 2010-07-21 21:49:41 -0700 | [diff] [blame] | 212 | basic_logger.info("DataPlaneOnly") |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 213 | # self.dataplane.show() |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 214 | # Would like an assert that checks the data plane |
| 215 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 216 | class Echo(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 217 | """ |
| 218 | Test echo response with no data |
| 219 | """ |
| 220 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 221 | request = message.echo_request() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 222 | response, pkt = self.controller.transact(request) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame^] | 223 | self.assertTrue(response is not None, |
| 224 | "Did not get echo reply") |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 225 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 226 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 227 | 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 Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 231 | class EchoWithData(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 232 | """ |
| 233 | Test echo response with short string data |
| 234 | """ |
| 235 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 236 | request = message.echo_request() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 237 | request.data = 'OpenFlow Will Rule The World' |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 238 | response, pkt = self.controller.transact(request) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame^] | 239 | self.assertTrue(response is not None, |
| 240 | "Did not get echo reply (with data)") |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 241 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 242 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 243 | 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 Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 248 | class PacketIn(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 249 | """ |
| 250 | Test packet in function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 251 | |
| 252 | Send a packet to each dataplane port and verify that a packet |
| 253 | in message is received from the controller for each |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 254 | """ |
| 255 | def runTest(self): |
| 256 | # Construct packet to send to dataplane |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 257 | # Send packet to dataplane, once to each port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 258 | # Poll controller with expect message type packet in |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 259 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 260 | rc = delete_all_flows(self.controller, basic_logger) |
| 261 | self.assertEqual(rc, 0, "Failed to delete all flows") |
Dan Talayco | 0fc08bd | 2012-04-09 16:56:18 -0700 | [diff] [blame] | 262 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 263 | |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 264 | vid = test_param_get(self.config, 'vid', default=TEST_VID_DEFAULT) |
| 265 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 266 | for of_port in basic_port_map.keys(): |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 267 | for pkt, pt in [ |
| 268 | (simple_tcp_packet(), "simple TCP packet"), |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 269 | (simple_tcp_packet(dl_vlan_enable=True,dl_vlan=vid,pktlen=108), |
| 270 | "simple tagged TCP packet"), |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 271 | (simple_eth_packet(), "simple Ethernet packet"), |
| 272 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 273 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 274 | 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 Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 282 | if dataplane.match_exp_pkt(pkt, response.data): # Got match |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 283 | 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 Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 289 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 290 | self.assertTrue(response is not None, |
| 291 | 'Packet in message not received on port ' + |
| 292 | str(of_port)) |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 293 | if not dataplane.match_exp_pkt(pkt, response.data): |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 294 | basic_logger.debug("Sent %s" % format_packet(pkt)) |
| 295 | basic_logger.debug("Resp %s" % format_packet(response.data)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 296 | self.assertTrue(False, |
| 297 | 'Response packet does not match send packet' + |
| 298 | ' for port ' + str(of_port)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 299 | |
Ed Swierk | 3ae7f71 | 2012-08-22 06:45:25 -0700 | [diff] [blame] | 300 | class 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 | |
| 332 | test_prio["PacketInDefaultDrop"] = -1 |
| 333 | |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 334 | |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 335 | class 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 Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 358 | (of_port, pkt_in, pkt_time) = self.dataplane.poll(exp_pkt=pkt) |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 359 | self.assertTrue(pkt_in is None, |
| 360 | 'BCast packet received on port ' + str(of_port)) |
| 361 | |
| 362 | test_prio["PacketInBroadcastCheck"] = -1 |
| 363 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 364 | class PacketOut(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 365 | """ |
| 366 | Test packet out function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 367 | |
| 368 | Send packet out message to controller for each dataplane port and |
| 369 | verify the packet appears on the appropriate dataplane port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 370 | """ |
| 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 Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 375 | |
| 376 | rc = delete_all_flows(self.controller, basic_logger) |
| 377 | self.assertEqual(rc, 0, "Failed to delete all flows") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 378 | |
| 379 | # These will get put into function |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 380 | of_ports = basic_port_map.keys() |
| 381 | of_ports.sort() |
| 382 | for dp_port in of_ports: |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 383 | 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 Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 387 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 388 | 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 Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 394 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 395 | 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 Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 398 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 399 | exp_pkt_arg = None |
| 400 | exp_port = None |
| 401 | if basic_config["relax"]: |
| 402 | exp_pkt_arg = outpkt |
| 403 | exp_port = dp_port |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 404 | (of_port, pkt, pkt_time) = self.dataplane.poll(port_number=exp_port, |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 405 | 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 Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 411 | if not dataplane.match_exp_pkt(outpkt, pkt): |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 412 | basic_logger.debug("Sent %s" % format_packet(outpkt)) |
| 413 | basic_logger.debug("Resp %s" % format_packet( |
| 414 | str(pkt)[:len(str(outpkt))])) |
Dan Talayco | dc6fca3 | 2012-03-30 10:05:49 -0700 | [diff] [blame] | 415 | self.assertEqual(str(outpkt), str(pkt)[:len(str(outpkt))], |
| 416 | 'Response packet does not match send packet') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 417 | |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 418 | class 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 Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 461 | class FlowStatsGet(SimpleProtocol): |
| 462 | """ |
| 463 | Get stats |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 464 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 465 | Simply verify stats get transaction |
| 466 | """ |
| 467 | def runTest(self): |
| 468 | basic_logger.info("Running StatsGet") |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 469 | basic_logger.info("Inserting trial flow") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 470 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 471 | 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 Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 475 | request = message.flow_stats_request() |
| 476 | request.out_port = ofp.OFPP_NONE |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 477 | request.table_id = 0xff |
| 478 | request.match.wildcards = 0 # ofp.OFPFW_ALL |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 479 | response, pkt = self.controller.transact(request) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame^] | 480 | self.assertTrue(response is not None, |
| 481 | "Did not get response for flow stats") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 482 | basic_logger.debug(response.show()) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 483 | |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 484 | test_prio["FlowStatsGet"] = -1 |
| 485 | |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 486 | class 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 Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 495 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 496 | 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 Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 501 | response, pkt = self.controller.transact(request) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame^] | 502 | self.assertTrue(response is not None, |
| 503 | "Did not get reply for table stats") |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 504 | basic_logger.debug(response.show()) |
| 505 | |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 506 | class 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 Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 517 | response, pkt = self.controller.transact(request) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame^] | 518 | self.assertTrue(response is not None, |
| 519 | "Did not get reply for desc stats") |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 520 | basic_logger.debug(response.show()) |
| 521 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 522 | class 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 Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 531 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 532 | rv = self.controller.message_send(request) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 533 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 534 | |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 535 | class 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 Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 546 | for of_port, ifname in basic_port_map.items(): # Grab first port |
| 547 | break |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 548 | |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 549 | (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 Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 559 | self.assertTrue(rv != -1, "Error sending port mod") |
| 560 | |
| 561 | # Verify change took place with same feature request |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 562 | (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 Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 570 | # Set it back |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 571 | 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 Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 574 | |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 575 | class 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 Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 614 | if __name__ == "__main__": |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 615 | print "Please run through oft script: ./oft --test_spec=basic" |