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 | e605b1b | 2012-09-18 06:56:20 -0700 | [diff] [blame] | 32 | import oftest.illegal_message as illegal_message |
| 33 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 34 | from oftest.testutils import * |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 35 | |
| 36 | #@var basic_port_map Local copy of the configuration map from OF port |
| 37 | # numbers to OS interfaces |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 38 | basic_port_map = 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 |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 54 | global basic_config |
| 55 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 56 | basic_port_map = config["port_map"] |
| 57 | basic_config = config |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 58 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 59 | class SimpleProtocol(unittest.TestCase): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 60 | """ |
| 61 | Root class for setting up the controller |
| 62 | """ |
| 63 | |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame] | 64 | def sig_handler(self, v1, v2): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 65 | logging.critical("Received interrupt signal; exiting") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 66 | print "Received interrupt signal; exiting" |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 67 | self.clean_shutdown = False |
| 68 | self.tearDown() |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 69 | raise KeyboardInterrupt |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 70 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 71 | def setUp(self): |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 72 | self.config = basic_config |
Ed Swierk | 022d02e | 2012-08-22 06:26:36 -0700 | [diff] [blame] | 73 | #@todo Test cases shouldn't monkey with signals; move SIGINT handler |
| 74 | # to top-level oft |
| 75 | try: |
| 76 | signal.signal(signal.SIGINT, self.sig_handler) |
| 77 | except ValueError, e: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 78 | logging.info("Could not set SIGINT handler: %s" % e) |
| 79 | logging.info("** START TEST CASE " + str(self)) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 80 | self.controller = controller.Controller( |
| 81 | host=basic_config["controller_host"], |
| 82 | port=basic_config["controller_port"]) |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 83 | # clean_shutdown should be set to False to force quit app |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 84 | self.clean_shutdown = True |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 85 | self.controller.start() |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame] | 86 | #@todo Add an option to wait for a pkt transaction to ensure version |
| 87 | # compatibilty? |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 88 | self.controller.connect(timeout=20) |
Dan Talayco | 5d0f7cc | 2012-09-19 11:28:58 -0700 | [diff] [blame] | 89 | |
| 90 | # By default, respond to echo requests |
| 91 | self.controller.keep_alive = True |
| 92 | |
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)") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 97 | logging.info("Connected " + str(self.controller.switch_addr)) |
Ed Swierk | c7193a2 | 2012-08-22 06:51:02 -0700 | [diff] [blame] | 98 | request = message.features_request() |
Dan Talayco | c689a79 | 2012-09-28 14:22:53 -0700 | [diff] [blame] | 99 | reply, pkt = self.controller.transact(request) |
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 |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 103 | logging.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 | """ |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 120 | self.config = parent.config |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 121 | logging.info("** Setup " + str(self) + " inheriting from " |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 122 | + str(parent)) |
| 123 | self.controller = parent.controller |
Ed Swierk | 6192e51 | 2012-08-22 11:41:40 -0700 | [diff] [blame] | 124 | self.supported_actions = parent.supported_actions |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 125 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 126 | def tearDown(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 127 | logging.info("** END TEST CASE " + str(self)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 128 | self.controller.shutdown() |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 129 | #@todo Review if join should be done on clean_shutdown |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 130 | if self.clean_shutdown: |
| 131 | self.controller.join() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 132 | |
| 133 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 134 | # Just a simple sanity check as illustration |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 135 | logging.info("Running simple proto test") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 136 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 137 | str(self) + 'No connection to switch') |
| 138 | |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 139 | def assertTrue(self, cond, msg): |
| 140 | if not cond: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 141 | logging.error("** FAILED ASSERTION: " + msg) |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 142 | unittest.TestCase.assertTrue(self, cond, msg) |
| 143 | |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 144 | test_prio["SimpleProtocol"] = 1 |
| 145 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 146 | class SimpleDataPlane(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 147 | """ |
| 148 | Root class that sets up the controller and dataplane |
| 149 | """ |
| 150 | def setUp(self): |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 151 | SimpleProtocol.setUp(self) |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 152 | self.dataplane = dataplane.DataPlane(self.config) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 153 | for of_port, ifname in basic_port_map.items(): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 154 | self.dataplane.port_add(ifname, of_port) |
| 155 | |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 156 | def inheritSetup(self, parent): |
| 157 | """ |
| 158 | Inherit the setup of a parent |
| 159 | |
| 160 | See SimpleProtocol.inheritSetup |
| 161 | """ |
| 162 | SimpleProtocol.inheritSetup(self, parent) |
| 163 | self.dataplane = parent.dataplane |
| 164 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 165 | def tearDown(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 166 | logging.info("Teardown for simple dataplane test") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 167 | SimpleProtocol.tearDown(self) |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 168 | if hasattr(self, 'dataplane'): |
| 169 | self.dataplane.kill(join_threads=self.clean_shutdown) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 170 | logging.info("Teardown done") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 171 | |
| 172 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 173 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 174 | str(self) + 'No connection to switch') |
| 175 | # self.dataplane.show() |
| 176 | # Would like an assert that checks the data plane |
| 177 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 178 | class DataPlaneOnly(unittest.TestCase): |
| 179 | """ |
| 180 | Root class that sets up only the dataplane |
| 181 | """ |
| 182 | |
| 183 | def sig_handler(self, v1, v2): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 184 | logging.critical("Received interrupt signal; exiting") |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 185 | print "Received interrupt signal; exiting" |
| 186 | self.clean_shutdown = False |
| 187 | self.tearDown() |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 188 | raise KeyboardInterrupt |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 189 | |
| 190 | def setUp(self): |
Shudong Zhou | e3582a5 | 2012-08-03 20:46:50 -0700 | [diff] [blame] | 191 | self.clean_shutdown = True |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 192 | self.config = basic_config |
Ed Swierk | 022d02e | 2012-08-22 06:26:36 -0700 | [diff] [blame] | 193 | #@todo Test cases shouldn't monkey with signals; move SIGINT handler |
| 194 | # to top-level oft |
| 195 | try: |
| 196 | signal.signal(signal.SIGINT, self.sig_handler) |
| 197 | except ValueError, e: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 198 | logging.info("Could not set SIGINT handler: %s" % e) |
| 199 | logging.info("** START DataPlaneOnly CASE " + str(self)) |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 200 | self.dataplane = dataplane.DataPlane(self.config) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 201 | for of_port, ifname in basic_port_map.items(): |
| 202 | self.dataplane.port_add(ifname, of_port) |
| 203 | |
| 204 | def tearDown(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 205 | logging.info("Teardown for simple dataplane test") |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 206 | self.dataplane.kill(join_threads=self.clean_shutdown) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 207 | logging.info("Teardown done") |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 208 | |
| 209 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 210 | logging.info("DataPlaneOnly") |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 211 | # self.dataplane.show() |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 212 | # Would like an assert that checks the data plane |
| 213 | |
Dan Talayco | a6cea8b | 2012-09-18 06:58:54 -0700 | [diff] [blame] | 214 | test_prio["DataPlaneOnly"] = -1 |
| 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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 260 | rc = delete_all_flows(self.controller) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 274 | logging.info("PKT IN test with %s, port %s" % (pt, of_port)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 275 | self.dataplane.send(of_port, str(pkt)) |
| 276 | #@todo Check for unexpected messages? |
| 277 | count = 0 |
| 278 | while True: |
Dan Talayco | c689a79 | 2012-09-28 14:22:53 -0700 | [diff] [blame] | 279 | (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 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): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 294 | logging.debug("Sent %s" % format_packet(pkt)) |
| 295 | logging.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): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 308 | rc = delete_all_flows(self.controller) |
Ed Swierk | 3ae7f71 | 2012-08-22 06:45:25 -0700 | [diff] [blame] | 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: |
Dan Talayco | c689a79 | 2012-09-28 14:22:53 -0700 | [diff] [blame] | 317 | (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN) |
Ed Swierk | 3ae7f71 | 2012-08-22 06:45:25 -0700 | [diff] [blame] | 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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 347 | rc = delete_all_flows(self.controller) |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 355 | logging.info("BCast Leak Test, send to port %s" % d_port) |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 376 | rc = delete_all_flows(self.controller) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 388 | logging.info("PKT OUT test with %s, port %s" % (opt, dp_port)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 395 | logging.info("PacketOut to: " + str(dp_port)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 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') |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 408 | logging.info("PacketOut: got pkt from " + str(of_port)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 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): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 412 | logging.debug("Sent %s" % format_packet(outpkt)) |
| 413 | logging.debug("Resp %s" % format_packet( |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 430 | rc = delete_all_flows(self.controller) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 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] |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 443 | logging.info("PKT OUT test with " + opt + |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 453 | logging.info("PacketOut to: " + str(dp_ports)) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 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), |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 459 | self, basic_config) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 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): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 468 | logging.info("Running StatsGet") |
| 469 | logging.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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 474 | logging.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") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 482 | logging.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): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 493 | logging.info("Running TableStatsGet") |
| 494 | logging.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 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 499 | logging.info("Sending table stats request") |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 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") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 504 | logging.debug(response.show()) |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 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): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 513 | logging.info("Running DescStatsGet") |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 514 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 515 | logging.info("Sending stats request") |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 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") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 520 | logging.debug(response.show()) |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 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): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 530 | logging.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): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 545 | logging.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) = \ |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 550 | port_config_get(self.controller, of_port) |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 551 | self.assertTrue(config is not None, "Did not get port config") |
| 552 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 553 | logging.debug("No flood bit port " + str(of_port) + " is now " + |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 554 | str(config & ofp.OFPPC_NO_FLOOD)) |
| 555 | |
| 556 | rv = port_config_set(self.controller, of_port, |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 557 | config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD) |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 558 | self.assertTrue(rv != -1, "Error sending port mod") |
| 559 | |
| 560 | # Verify change took place with same feature request |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 561 | (hw_addr, config2, advert) = \ |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 562 | port_config_get(self.controller, of_port) |
| 563 | logging.debug("No flood bit port " + str(of_port) + " is now " + |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 564 | str(config2 & ofp.OFPPC_NO_FLOOD)) |
| 565 | self.assertTrue(config2 is not None, "Did not get port config2") |
| 566 | self.assertTrue(config2 & ofp.OFPPC_NO_FLOOD != |
| 567 | config & ofp.OFPPC_NO_FLOOD, |
| 568 | "Bit change did not take") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 569 | # Set it back |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 570 | rv = port_config_set(self.controller, of_port, config, |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 571 | ofp.OFPPC_NO_FLOOD) |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 572 | self.assertTrue(rv != -1, "Error sending port mod") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 573 | |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 574 | class PortConfigModErr(SimpleProtocol): |
| 575 | """ |
| 576 | Modify a bit in port config on an invalid port and verify |
| 577 | error message is received. |
| 578 | """ |
| 579 | |
| 580 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 581 | logging.info("Running " + str(self)) |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 582 | |
| 583 | # pick a random bad port number |
| 584 | bad_port = random.randint(1, ofp.OFPP_MAX) |
| 585 | count = 0 |
| 586 | while (count < 50) and (bad_port in basic_port_map.keys()): |
| 587 | bad_port = random.randint(1, ofp.OFPP_MAX) |
| 588 | count = count + 1 |
| 589 | self.assertTrue(count < 50, "Error selecting bad port") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 590 | logging.info("Select " + str(bad_port) + " as invalid port") |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 591 | |
| 592 | rv = port_config_set(self.controller, bad_port, |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 593 | ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD) |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 594 | self.assertTrue(rv != -1, "Error sending port mod") |
| 595 | |
| 596 | # poll for error message |
| 597 | while True: |
Dan Talayco | c689a79 | 2012-09-28 14:22:53 -0700 | [diff] [blame] | 598 | (response, raw) = self.controller.poll(ofp.OFPT_ERROR) |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 599 | if not response: # Timeout |
| 600 | break |
| 601 | if response.code == ofp.OFPPMFC_BAD_PORT: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 602 | logging.info("Received error message with OFPPMFC_BAD_PORT code") |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 603 | break |
| 604 | if not basic_config["relax"]: # Only one attempt to match |
| 605 | break |
| 606 | count += 1 |
| 607 | if count > 10: # Too many tries |
| 608 | break |
| 609 | |
| 610 | self.assertTrue(response is not None, 'Did not receive error message') |
| 611 | |
Dan Talayco | e605b1b | 2012-09-18 06:56:20 -0700 | [diff] [blame] | 612 | class BadMessage(SimpleProtocol): |
| 613 | """ |
| 614 | Send a message with a bad type and verify an error is returned |
| 615 | """ |
| 616 | |
| 617 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame^] | 618 | logging.info("Running " + str(self)) |
Dan Talayco | e605b1b | 2012-09-18 06:56:20 -0700 | [diff] [blame] | 619 | request = illegal_message.illegal_message_type() |
| 620 | |
Dan Talayco | c689a79 | 2012-09-28 14:22:53 -0700 | [diff] [blame] | 621 | reply, pkt = self.controller.transact(request) |
Dan Talayco | e605b1b | 2012-09-18 06:56:20 -0700 | [diff] [blame] | 622 | self.assertTrue(reply is not None, "Did not get response to bad req") |
| 623 | self.assertTrue(reply.header.type == ofp.OFPT_ERROR, |
| 624 | "reply not an error message") |
| 625 | self.assertTrue(reply.type == ofp.OFPET_BAD_REQUEST, |
| 626 | "reply error type is not bad request") |
| 627 | self.assertTrue(reply.code == ofp.OFPBRC_BAD_TYPE, |
| 628 | "reply error code is not bad type") |
| 629 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 630 | if __name__ == "__main__": |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 631 | print "Please run through oft script: ./oft --test_spec=basic" |