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