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 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 44 | def test_set_init(config): |
| 45 | """ |
| 46 | Set up function for basic test classes |
| 47 | |
| 48 | @param config The configuration dictionary; see oft |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 49 | """ |
| 50 | |
| 51 | global basic_port_map |
| 52 | global basic_logger |
| 53 | global basic_config |
| 54 | |
| 55 | basic_logger = logging.getLogger("basic") |
| 56 | basic_logger.info("Initializing test set") |
| 57 | basic_port_map = config["port_map"] |
| 58 | basic_config = config |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 59 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 60 | class SimpleProtocol(unittest.TestCase): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 61 | """ |
| 62 | Root class for setting up the controller |
| 63 | """ |
| 64 | |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame] | 65 | def sig_handler(self, v1, v2): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 66 | basic_logger.critical("Received interrupt signal; exiting") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 67 | print "Received interrupt signal; exiting" |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 68 | self.clean_shutdown = False |
| 69 | self.tearDown() |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 70 | raise KeyboardInterrupt |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 71 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 72 | def setUp(self): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 73 | self.logger = basic_logger |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 74 | self.config = basic_config |
Ed Swierk | 022d02e | 2012-08-22 06:26:36 -0700 | [diff] [blame] | 75 | #@todo Test cases shouldn't monkey with signals; move SIGINT handler |
| 76 | # to top-level oft |
| 77 | try: |
| 78 | signal.signal(signal.SIGINT, self.sig_handler) |
| 79 | except ValueError, e: |
| 80 | basic_logger.info("Could not set SIGINT handler: %s" % e) |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 81 | basic_logger.info("** START TEST CASE " + str(self)) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 82 | self.controller = controller.Controller( |
| 83 | host=basic_config["controller_host"], |
| 84 | port=basic_config["controller_port"]) |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 85 | # clean_shutdown should be set to False to force quit app |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 86 | self.clean_shutdown = True |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 87 | self.controller.start() |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame] | 88 | #@todo Add an option to wait for a pkt transaction to ensure version |
| 89 | # compatibilty? |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 90 | self.controller.connect(timeout=20) |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame] | 91 | if not self.controller.active: |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 92 | raise Exception("Controller startup failed") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 93 | if self.controller.switch_addr is None: |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 94 | raise Exception("Controller startup failed (no switch addr)") |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 95 | basic_logger.info("Connected " + str(self.controller.switch_addr)) |
Ed Swierk | c7193a2 | 2012-08-22 06:51:02 -0700 | [diff] [blame^] | 96 | request = message.features_request() |
| 97 | reply, pkt = self.controller.transact(request, timeout=10) |
| 98 | self.supported_actions = reply.actions |
| 99 | basic_logger.info("Supported actions: " + hex(self.supported_actions)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 100 | |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 101 | def inheritSetup(self, parent): |
| 102 | """ |
| 103 | Inherit the setup of a parent |
| 104 | |
| 105 | This allows running at test from within another test. Do the |
| 106 | following: |
| 107 | |
| 108 | sub_test = SomeTestClass() # Create an instance of the test class |
| 109 | sub_test.inheritSetup(self) # Inherit setup of parent |
| 110 | sub_test.runTest() # Run the test |
| 111 | |
| 112 | Normally, only the parent's setUp and tearDown are called and |
| 113 | the state after the sub_test is run must be taken into account |
| 114 | by subsequent operations. |
| 115 | """ |
| 116 | self.logger = parent.logger |
| 117 | self.config = parent.config |
| 118 | basic_logger.info("** Setup " + str(self) + " inheriting from " |
| 119 | + str(parent)) |
| 120 | self.controller = parent.controller |
| 121 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 122 | def tearDown(self): |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 123 | basic_logger.info("** END TEST CASE " + str(self)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 124 | self.controller.shutdown() |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 125 | #@todo Review if join should be done on clean_shutdown |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 126 | if self.clean_shutdown: |
| 127 | self.controller.join() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 128 | |
| 129 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 130 | # Just a simple sanity check as illustration |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 131 | basic_logger.info("Running simple proto test") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 132 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 133 | str(self) + 'No connection to switch') |
| 134 | |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 135 | def assertTrue(self, cond, msg): |
| 136 | if not cond: |
| 137 | basic_logger.error("** FAILED ASSERTION: " + msg) |
| 138 | unittest.TestCase.assertTrue(self, cond, msg) |
| 139 | |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 140 | test_prio["SimpleProtocol"] = 1 |
| 141 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 142 | class SimpleDataPlane(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 143 | """ |
| 144 | Root class that sets up the controller and dataplane |
| 145 | """ |
| 146 | def setUp(self): |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 147 | SimpleProtocol.setUp(self) |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 148 | self.dataplane = dataplane.DataPlane(self.config) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 149 | for of_port, ifname in basic_port_map.items(): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 150 | self.dataplane.port_add(ifname, of_port) |
| 151 | |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 152 | def inheritSetup(self, parent): |
| 153 | """ |
| 154 | Inherit the setup of a parent |
| 155 | |
| 156 | See SimpleProtocol.inheritSetup |
| 157 | """ |
| 158 | SimpleProtocol.inheritSetup(self, parent) |
| 159 | self.dataplane = parent.dataplane |
| 160 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 161 | def tearDown(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 162 | basic_logger.info("Teardown for simple dataplane test") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 163 | SimpleProtocol.tearDown(self) |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 164 | if hasattr(self, 'dataplane'): |
| 165 | self.dataplane.kill(join_threads=self.clean_shutdown) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 166 | basic_logger.info("Teardown done") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 167 | |
| 168 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 169 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 170 | str(self) + 'No connection to switch') |
| 171 | # self.dataplane.show() |
| 172 | # Would like an assert that checks the data plane |
| 173 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 174 | class DataPlaneOnly(unittest.TestCase): |
| 175 | """ |
| 176 | Root class that sets up only the dataplane |
| 177 | """ |
| 178 | |
| 179 | def sig_handler(self, v1, v2): |
| 180 | basic_logger.critical("Received interrupt signal; exiting") |
| 181 | print "Received interrupt signal; exiting" |
| 182 | self.clean_shutdown = False |
| 183 | self.tearDown() |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 184 | raise KeyboardInterrupt |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 185 | |
| 186 | def setUp(self): |
Shudong Zhou | e3582a5 | 2012-08-03 20:46:50 -0700 | [diff] [blame] | 187 | self.clean_shutdown = True |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 188 | self.logger = basic_logger |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 189 | self.config = basic_config |
Ed Swierk | 022d02e | 2012-08-22 06:26:36 -0700 | [diff] [blame] | 190 | #@todo Test cases shouldn't monkey with signals; move SIGINT handler |
| 191 | # to top-level oft |
| 192 | try: |
| 193 | signal.signal(signal.SIGINT, self.sig_handler) |
| 194 | except ValueError, e: |
| 195 | basic_logger.info("Could not set SIGINT handler: %s" % e) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 196 | basic_logger.info("** START DataPlaneOnly CASE " + str(self)) |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 197 | self.dataplane = dataplane.DataPlane(self.config) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 198 | for of_port, ifname in basic_port_map.items(): |
| 199 | self.dataplane.port_add(ifname, of_port) |
| 200 | |
| 201 | def tearDown(self): |
| 202 | basic_logger.info("Teardown for simple dataplane test") |
| 203 | self.dataplane.kill(join_threads=self.clean_shutdown) |
| 204 | basic_logger.info("Teardown done") |
| 205 | |
| 206 | def runTest(self): |
Dan Talayco | ba4fd4f | 2010-07-21 21:49:41 -0700 | [diff] [blame] | 207 | basic_logger.info("DataPlaneOnly") |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 208 | # self.dataplane.show() |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 209 | # Would like an assert that checks the data plane |
| 210 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 211 | class Echo(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 212 | """ |
| 213 | Test echo response with no data |
| 214 | """ |
| 215 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 216 | request = message.echo_request() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 217 | response, pkt = self.controller.transact(request) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 218 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 219 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 220 | self.assertEqual(request.header.xid, response.header.xid, |
| 221 | 'response xid != request xid') |
| 222 | self.assertEqual(len(response.data), 0, 'response data non-empty') |
| 223 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 224 | class EchoWithData(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 225 | """ |
| 226 | Test echo response with short string data |
| 227 | """ |
| 228 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 229 | request = message.echo_request() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 230 | request.data = 'OpenFlow Will Rule The World' |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 231 | response, pkt = self.controller.transact(request) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 232 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 233 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 234 | self.assertEqual(request.header.xid, response.header.xid, |
| 235 | 'response xid != request xid') |
| 236 | self.assertEqual(request.data, response.data, |
| 237 | 'response data does not match request') |
| 238 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 239 | class PacketIn(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 240 | """ |
| 241 | Test packet in function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 242 | |
| 243 | Send a packet to each dataplane port and verify that a packet |
| 244 | in message is received from the controller for each |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 245 | """ |
| 246 | def runTest(self): |
| 247 | # Construct packet to send to dataplane |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 248 | # Send packet to dataplane, once to each port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 249 | # Poll controller with expect message type packet in |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 250 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 251 | rc = delete_all_flows(self.controller, basic_logger) |
| 252 | self.assertEqual(rc, 0, "Failed to delete all flows") |
Dan Talayco | 0fc08bd | 2012-04-09 16:56:18 -0700 | [diff] [blame] | 253 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 254 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 255 | for of_port in basic_port_map.keys(): |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 256 | for pkt, pt in [ |
| 257 | (simple_tcp_packet(), "simple TCP packet"), |
| 258 | (simple_eth_packet(), "simple Ethernet packet"), |
| 259 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 260 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 261 | basic_logger.info("PKT IN test with %s, port %s" % (pt, of_port)) |
| 262 | self.dataplane.send(of_port, str(pkt)) |
| 263 | #@todo Check for unexpected messages? |
| 264 | count = 0 |
| 265 | while True: |
| 266 | (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2) |
| 267 | if not response: # Timeout |
| 268 | break |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 269 | if dataplane.match_exp_pkt(pkt, response.data): # Got match |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 270 | break |
| 271 | if not basic_config["relax"]: # Only one attempt to match |
| 272 | break |
| 273 | count += 1 |
| 274 | if count > 10: # Too many tries |
| 275 | break |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 276 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 277 | self.assertTrue(response is not None, |
| 278 | 'Packet in message not received on port ' + |
| 279 | str(of_port)) |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 280 | if not dataplane.match_exp_pkt(pkt, response.data): |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 281 | basic_logger.debug("Sent %s" % format_packet(pkt)) |
| 282 | basic_logger.debug("Resp %s" % format_packet(response.data)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 283 | self.assertTrue(False, |
| 284 | 'Response packet does not match send packet' + |
| 285 | ' for port ' + str(of_port)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 286 | |
Ed Swierk | 3ae7f71 | 2012-08-22 06:45:25 -0700 | [diff] [blame] | 287 | class PacketInDefaultDrop(SimpleDataPlane): |
| 288 | """ |
| 289 | Test packet in function |
| 290 | |
| 291 | Send a packet to each dataplane port and verify that a packet |
| 292 | in message is received from the controller for each |
| 293 | """ |
| 294 | def runTest(self): |
| 295 | rc = delete_all_flows(self.controller, basic_logger) |
| 296 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 297 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 298 | |
| 299 | for of_port in basic_port_map.keys(): |
| 300 | pkt = simple_tcp_packet() |
| 301 | self.dataplane.send(of_port, str(pkt)) |
| 302 | count = 0 |
| 303 | while True: |
| 304 | (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2) |
| 305 | if not response: # Timeout |
| 306 | break |
| 307 | if dataplane.match_exp_pkt(pkt, response.data): # Got match |
| 308 | break |
| 309 | if not basic_config["relax"]: # Only one attempt to match |
| 310 | break |
| 311 | count += 1 |
| 312 | if count > 10: # Too many tries |
| 313 | break |
| 314 | |
| 315 | self.assertTrue(response is None, |
| 316 | 'Packet in message received on port ' + |
| 317 | str(of_port)) |
| 318 | |
| 319 | test_prio["PacketInDefaultDrop"] = -1 |
| 320 | |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 321 | |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 322 | class PacketInBroadcastCheck(SimpleDataPlane): |
| 323 | """ |
| 324 | Check if bcast pkts leak when no flows are present |
| 325 | |
| 326 | Clear the flow table |
| 327 | Send in a broadcast pkt |
| 328 | Look for the packet on other dataplane ports. |
| 329 | """ |
| 330 | def runTest(self): |
| 331 | # Need at least two ports |
| 332 | self.assertTrue(len(basic_port_map) > 1, "Too few ports for test") |
| 333 | |
| 334 | rc = delete_all_flows(self.controller, basic_logger) |
| 335 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 336 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 337 | |
| 338 | of_ports = basic_port_map.keys() |
| 339 | d_port = of_ports[0] |
| 340 | pkt = simple_eth_packet(dl_dst='ff:ff:ff:ff:ff:ff') |
| 341 | |
| 342 | basic_logger.info("BCast Leak Test, send to port %s" % d_port) |
| 343 | self.dataplane.send(d_port, str(pkt)) |
| 344 | |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 345 | (of_port, pkt_in, pkt_time) = self.dataplane.poll(exp_pkt=pkt) |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 346 | self.assertTrue(pkt_in is None, |
| 347 | 'BCast packet received on port ' + str(of_port)) |
| 348 | |
| 349 | test_prio["PacketInBroadcastCheck"] = -1 |
| 350 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 351 | class PacketOut(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 352 | """ |
| 353 | Test packet out function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 354 | |
| 355 | Send packet out message to controller for each dataplane port and |
| 356 | verify the packet appears on the appropriate dataplane port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 357 | """ |
| 358 | def runTest(self): |
| 359 | # Construct packet to send to dataplane |
| 360 | # Send packet to dataplane |
| 361 | # Poll controller with expect message type packet in |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 362 | |
| 363 | rc = delete_all_flows(self.controller, basic_logger) |
| 364 | self.assertEqual(rc, 0, "Failed to delete all flows") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 365 | |
| 366 | # These will get put into function |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 367 | of_ports = basic_port_map.keys() |
| 368 | of_ports.sort() |
| 369 | for dp_port in of_ports: |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 370 | for outpkt, opt in [ |
| 371 | (simple_tcp_packet(), "simple TCP packet"), |
| 372 | (simple_eth_packet(), "simple Ethernet packet"), |
| 373 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 374 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 375 | basic_logger.info("PKT OUT test with %s, port %s" % (opt, dp_port)) |
| 376 | msg = message.packet_out() |
| 377 | msg.data = str(outpkt) |
| 378 | act = action.action_output() |
| 379 | act.port = dp_port |
| 380 | self.assertTrue(msg.actions.add(act), 'Could not add action to msg') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 381 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 382 | basic_logger.info("PacketOut to: " + str(dp_port)) |
| 383 | rv = self.controller.message_send(msg) |
| 384 | self.assertTrue(rv == 0, "Error sending out message") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 385 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 386 | exp_pkt_arg = None |
| 387 | exp_port = None |
| 388 | if basic_config["relax"]: |
| 389 | exp_pkt_arg = outpkt |
| 390 | exp_port = dp_port |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 391 | (of_port, pkt, pkt_time) = self.dataplane.poll(port_number=exp_port, |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 392 | exp_pkt=exp_pkt_arg) |
| 393 | |
| 394 | self.assertTrue(pkt is not None, 'Packet not received') |
| 395 | basic_logger.info("PacketOut: got pkt from " + str(of_port)) |
| 396 | if of_port is not None: |
| 397 | self.assertEqual(of_port, dp_port, "Unexpected receive port") |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 398 | if not dataplane.match_exp_pkt(outpkt, pkt): |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 399 | basic_logger.debug("Sent %s" % format_packet(outpkt)) |
| 400 | basic_logger.debug("Resp %s" % format_packet( |
| 401 | str(pkt)[:len(str(outpkt))])) |
Dan Talayco | dc6fca3 | 2012-03-30 10:05:49 -0700 | [diff] [blame] | 402 | self.assertEqual(str(outpkt), str(pkt)[:len(str(outpkt))], |
| 403 | 'Response packet does not match send packet') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 404 | |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 405 | class PacketOutMC(SimpleDataPlane): |
| 406 | """ |
| 407 | Test packet out to multiple output ports |
| 408 | |
| 409 | Send packet out message to controller for 1 to N dataplane ports and |
| 410 | verify the packet appears on the appropriate ports |
| 411 | """ |
| 412 | def runTest(self): |
| 413 | # Construct packet to send to dataplane |
| 414 | # Send packet to dataplane |
| 415 | # Poll controller with expect message type packet in |
| 416 | |
| 417 | rc = delete_all_flows(self.controller, basic_logger) |
| 418 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 419 | |
| 420 | # These will get put into function |
| 421 | of_ports = basic_port_map.keys() |
| 422 | random.shuffle(of_ports) |
| 423 | for num_ports in range(1,len(of_ports)+1): |
| 424 | for outpkt, opt in [ |
| 425 | (simple_tcp_packet(), "simple TCP packet"), |
| 426 | (simple_eth_packet(), "simple Ethernet packet"), |
| 427 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
| 428 | |
| 429 | dp_ports = of_ports[0:num_ports] |
| 430 | basic_logger.info("PKT OUT test with " + opt + |
| 431 | ", ports " + str(dp_ports)) |
| 432 | msg = message.packet_out() |
| 433 | msg.data = str(outpkt) |
| 434 | act = action.action_output() |
| 435 | for i in range(0,num_ports): |
| 436 | act.port = dp_ports[i] |
| 437 | self.assertTrue(msg.actions.add(act), |
| 438 | 'Could not add action to msg') |
| 439 | |
| 440 | basic_logger.info("PacketOut to: " + str(dp_ports)) |
| 441 | rv = self.controller.message_send(msg) |
| 442 | self.assertTrue(rv == 0, "Error sending out message") |
| 443 | |
| 444 | receive_pkt_check(self.dataplane, outpkt, dp_ports, |
| 445 | set(of_ports).difference(dp_ports), |
| 446 | self, basic_logger, basic_config) |
| 447 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 448 | class FlowStatsGet(SimpleProtocol): |
| 449 | """ |
| 450 | Get stats |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 451 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 452 | Simply verify stats get transaction |
| 453 | """ |
| 454 | def runTest(self): |
| 455 | basic_logger.info("Running StatsGet") |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 456 | basic_logger.info("Inserting trial flow") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 457 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 458 | rv = self.controller.message_send(request) |
| 459 | self.assertTrue(rv != -1, "Failed to insert test flow") |
| 460 | |
| 461 | basic_logger.info("Sending flow request") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 462 | request = message.flow_stats_request() |
| 463 | request.out_port = ofp.OFPP_NONE |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 464 | request.table_id = 0xff |
| 465 | request.match.wildcards = 0 # ofp.OFPFW_ALL |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 466 | response, pkt = self.controller.transact(request) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 467 | self.assertTrue(response is not None, "Did not get response") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 468 | basic_logger.debug(response.show()) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 469 | |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 470 | test_prio["FlowStatsGet"] = -1 |
| 471 | |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 472 | class TableStatsGet(SimpleProtocol): |
| 473 | """ |
| 474 | Get table stats |
| 475 | |
| 476 | Simply verify table stats get transaction |
| 477 | """ |
| 478 | def runTest(self): |
| 479 | basic_logger.info("Running TableStatsGet") |
| 480 | basic_logger.info("Inserting trial flow") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 481 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 482 | rv = self.controller.message_send(request) |
| 483 | self.assertTrue(rv != -1, "Failed to insert test flow") |
| 484 | |
| 485 | basic_logger.info("Sending table stats request") |
| 486 | request = message.table_stats_request() |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 487 | response, pkt = self.controller.transact(request) |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 488 | self.assertTrue(response is not None, "Did not get response") |
| 489 | basic_logger.debug(response.show()) |
| 490 | |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 491 | class DescStatsGet(SimpleProtocol): |
| 492 | """ |
| 493 | Get stats |
| 494 | |
| 495 | Simply verify stats get transaction |
| 496 | """ |
| 497 | def runTest(self): |
| 498 | basic_logger.info("Running DescStatsGet") |
| 499 | |
| 500 | basic_logger.info("Sending stats request") |
| 501 | request = message.desc_stats_request() |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 502 | response, pkt = self.controller.transact(request) |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 503 | self.assertTrue(response is not None, "Did not get response") |
| 504 | basic_logger.debug(response.show()) |
| 505 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 506 | class FlowMod(SimpleProtocol): |
| 507 | """ |
| 508 | Insert a flow |
| 509 | |
| 510 | Simple verification of a flow mod transaction |
| 511 | """ |
| 512 | |
| 513 | def runTest(self): |
| 514 | basic_logger.info("Running " + str(self)) |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 515 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 516 | rv = self.controller.message_send(request) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 517 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 518 | |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 519 | class PortConfigMod(SimpleProtocol): |
| 520 | """ |
| 521 | Modify a bit in port config and verify changed |
| 522 | |
| 523 | Get the switch configuration, modify the port configuration |
| 524 | and write it back; get the config again and verify changed. |
| 525 | Then set it back to the way it was. |
| 526 | """ |
| 527 | |
| 528 | def runTest(self): |
| 529 | basic_logger.info("Running " + str(self)) |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 530 | for of_port, ifname in basic_port_map.items(): # Grab first port |
| 531 | break |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 532 | |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 533 | (hw_addr, config, advert) = \ |
| 534 | port_config_get(self.controller, of_port, basic_logger) |
| 535 | self.assertTrue(config is not None, "Did not get port config") |
| 536 | |
| 537 | basic_logger.debug("No flood bit port " + str(of_port) + " is now " + |
| 538 | str(config & ofp.OFPPC_NO_FLOOD)) |
| 539 | |
| 540 | rv = port_config_set(self.controller, of_port, |
| 541 | config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD, |
| 542 | basic_logger) |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 543 | self.assertTrue(rv != -1, "Error sending port mod") |
| 544 | |
| 545 | # Verify change took place with same feature request |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 546 | (hw_addr, config2, advert) = \ |
| 547 | port_config_get(self.controller, of_port, basic_logger) |
| 548 | basic_logger.debug("No flood bit port " + str(of_port) + " is now " + |
| 549 | str(config2 & ofp.OFPPC_NO_FLOOD)) |
| 550 | self.assertTrue(config2 is not None, "Did not get port config2") |
| 551 | self.assertTrue(config2 & ofp.OFPPC_NO_FLOOD != |
| 552 | config & ofp.OFPPC_NO_FLOOD, |
| 553 | "Bit change did not take") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 554 | # Set it back |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 555 | rv = port_config_set(self.controller, of_port, config, |
| 556 | ofp.OFPPC_NO_FLOOD, basic_logger) |
| 557 | self.assertTrue(rv != -1, "Error sending port mod") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 558 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 559 | if __name__ == "__main__": |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 560 | print "Please run through oft script: ./oft --test_spec=basic" |