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 |
| 24 | |
| 25 | import oftest.controller as controller |
| 26 | import oftest.cstruct as ofp |
| 27 | import oftest.message as message |
| 28 | import oftest.dataplane as dataplane |
| 29 | import oftest.action as action |
| 30 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 31 | from testutils import * |
| 32 | |
| 33 | #@var basic_port_map Local copy of the configuration map from OF port |
| 34 | # numbers to OS interfaces |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 35 | basic_port_map = None |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 36 | #@var basic_logger Local logger object |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 37 | basic_logger = None |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 38 | #@var basic_config Local copy of global configuration data |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 39 | basic_config = None |
| 40 | |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 41 | test_prio = {} |
| 42 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 43 | def test_set_init(config): |
| 44 | """ |
| 45 | Set up function for basic test classes |
| 46 | |
| 47 | @param config The configuration dictionary; see oft |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 48 | """ |
| 49 | |
| 50 | global basic_port_map |
| 51 | global basic_logger |
| 52 | global basic_config |
| 53 | |
| 54 | basic_logger = logging.getLogger("basic") |
| 55 | basic_logger.info("Initializing test set") |
| 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): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 65 | basic_logger.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() |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 69 | sys.exit(1) |
| 70 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 71 | def setUp(self): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 72 | self.logger = basic_logger |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 73 | self.config = basic_config |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 74 | signal.signal(signal.SIGINT, self.sig_handler) |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 75 | basic_logger.info("** START TEST CASE " + str(self)) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 76 | self.controller = controller.Controller( |
| 77 | host=basic_config["controller_host"], |
| 78 | port=basic_config["controller_port"]) |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 79 | # clean_shutdown should be set to False to force quit app |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 80 | self.clean_shutdown = True |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 81 | self.controller.start() |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame] | 82 | #@todo Add an option to wait for a pkt transaction to ensure version |
| 83 | # compatibilty? |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 84 | self.controller.connect(timeout=20) |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame] | 85 | if not self.controller.active: |
| 86 | print "Controller startup failed; exiting" |
| 87 | sys.exit(1) |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 88 | if self.controller.switch_addr is None: |
| 89 | print "Controller startup failed (no switch addr); exiting" |
| 90 | sys.exit(1) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 91 | basic_logger.info("Connected " + str(self.controller.switch_addr)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 92 | |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 93 | def inheritSetup(self, parent): |
| 94 | """ |
| 95 | Inherit the setup of a parent |
| 96 | |
| 97 | This allows running at test from within another test. Do the |
| 98 | following: |
| 99 | |
| 100 | sub_test = SomeTestClass() # Create an instance of the test class |
| 101 | sub_test.inheritSetup(self) # Inherit setup of parent |
| 102 | sub_test.runTest() # Run the test |
| 103 | |
| 104 | Normally, only the parent's setUp and tearDown are called and |
| 105 | the state after the sub_test is run must be taken into account |
| 106 | by subsequent operations. |
| 107 | """ |
| 108 | self.logger = parent.logger |
| 109 | self.config = parent.config |
| 110 | basic_logger.info("** Setup " + str(self) + " inheriting from " |
| 111 | + str(parent)) |
| 112 | self.controller = parent.controller |
| 113 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 114 | def tearDown(self): |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 115 | basic_logger.info("** END TEST CASE " + str(self)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 116 | self.controller.shutdown() |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 117 | #@todo Review if join should be done on clean_shutdown |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 118 | if self.clean_shutdown: |
| 119 | self.controller.join() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 120 | |
| 121 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 122 | # Just a simple sanity check as illustration |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 123 | basic_logger.info("Running simple proto test") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 124 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 125 | str(self) + 'No connection to switch') |
| 126 | |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 127 | def assertTrue(self, cond, msg): |
| 128 | if not cond: |
| 129 | basic_logger.error("** FAILED ASSERTION: " + msg) |
| 130 | unittest.TestCase.assertTrue(self, cond, msg) |
| 131 | |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 132 | test_prio["SimpleProtocol"] = 1 |
| 133 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 134 | class SimpleDataPlane(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 135 | """ |
| 136 | Root class that sets up the controller and dataplane |
| 137 | """ |
| 138 | def setUp(self): |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 139 | SimpleProtocol.setUp(self) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 140 | self.dataplane = dataplane.DataPlane() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 141 | for of_port, ifname in basic_port_map.items(): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 142 | self.dataplane.port_add(ifname, of_port) |
| 143 | |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 144 | def inheritSetup(self, parent): |
| 145 | """ |
| 146 | Inherit the setup of a parent |
| 147 | |
| 148 | See SimpleProtocol.inheritSetup |
| 149 | """ |
| 150 | SimpleProtocol.inheritSetup(self, parent) |
| 151 | self.dataplane = parent.dataplane |
| 152 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 153 | def tearDown(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 154 | basic_logger.info("Teardown for simple dataplane test") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 155 | SimpleProtocol.tearDown(self) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 156 | self.dataplane.kill(join_threads=self.clean_shutdown) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 157 | basic_logger.info("Teardown done") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 158 | |
| 159 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 160 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 161 | str(self) + 'No connection to switch') |
| 162 | # self.dataplane.show() |
| 163 | # Would like an assert that checks the data plane |
| 164 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 165 | class DataPlaneOnly(unittest.TestCase): |
| 166 | """ |
| 167 | Root class that sets up only the dataplane |
| 168 | """ |
| 169 | |
| 170 | def sig_handler(self, v1, v2): |
| 171 | basic_logger.critical("Received interrupt signal; exiting") |
| 172 | print "Received interrupt signal; exiting" |
| 173 | self.clean_shutdown = False |
| 174 | self.tearDown() |
| 175 | sys.exit(1) |
| 176 | |
| 177 | def setUp(self): |
| 178 | self.clean_shutdown = False |
| 179 | self.logger = basic_logger |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 180 | self.config = basic_config |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 181 | signal.signal(signal.SIGINT, self.sig_handler) |
| 182 | basic_logger.info("** START DataPlaneOnly CASE " + str(self)) |
| 183 | self.dataplane = dataplane.DataPlane() |
| 184 | for of_port, ifname in basic_port_map.items(): |
| 185 | self.dataplane.port_add(ifname, of_port) |
| 186 | |
| 187 | def tearDown(self): |
| 188 | basic_logger.info("Teardown for simple dataplane test") |
| 189 | self.dataplane.kill(join_threads=self.clean_shutdown) |
| 190 | basic_logger.info("Teardown done") |
| 191 | |
| 192 | def runTest(self): |
Dan Talayco | ba4fd4f | 2010-07-21 21:49:41 -0700 | [diff] [blame] | 193 | basic_logger.info("DataPlaneOnly") |
Dan Talayco | 285a838 | 2010-07-20 14:06:55 -0700 | [diff] [blame] | 194 | # self.dataplane.show() |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 195 | # Would like an assert that checks the data plane |
| 196 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 197 | class Echo(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 198 | """ |
| 199 | Test echo response with no data |
| 200 | """ |
| 201 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 202 | request = message.echo_request() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 203 | response, pkt = self.controller.transact(request) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 204 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 205 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 206 | self.assertEqual(request.header.xid, response.header.xid, |
| 207 | 'response xid != request xid') |
| 208 | self.assertEqual(len(response.data), 0, 'response data non-empty') |
| 209 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 210 | class EchoWithData(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 211 | """ |
| 212 | Test echo response with short string data |
| 213 | """ |
| 214 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 215 | request = message.echo_request() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 216 | request.data = 'OpenFlow Will Rule The World' |
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(request.data, response.data, |
| 223 | 'response data does not match request') |
| 224 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 225 | class PacketIn(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 226 | """ |
| 227 | Test packet in function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 228 | |
| 229 | Send a packet to each dataplane port and verify that a packet |
| 230 | in message is received from the controller for each |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 231 | """ |
| 232 | def runTest(self): |
| 233 | # Construct packet to send to dataplane |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 234 | # Send packet to dataplane, once to each port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 235 | # Poll controller with expect message type packet in |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 236 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 237 | rc = delete_all_flows(self.controller, basic_logger) |
| 238 | self.assertEqual(rc, 0, "Failed to delete all flows") |
Ed Swierk | 0caeb1e | 2012-03-19 15:01:56 -0700 | [diff] [blame] | 239 | do_barrier(self.controller) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 240 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 241 | for of_port in basic_port_map.keys(): |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 242 | for pkt, pt in [ |
| 243 | (simple_tcp_packet(), "simple TCP packet"), |
| 244 | (simple_eth_packet(), "simple Ethernet packet"), |
| 245 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 246 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 247 | basic_logger.info("PKT IN test with %s, port %s" % (pt, of_port)) |
| 248 | self.dataplane.send(of_port, str(pkt)) |
| 249 | #@todo Check for unexpected messages? |
| 250 | count = 0 |
| 251 | while True: |
| 252 | (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2) |
| 253 | if not response: # Timeout |
| 254 | break |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 255 | if dataplane.match_exp_pkt(pkt, response.data): # Got match |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 256 | break |
| 257 | if not basic_config["relax"]: # Only one attempt to match |
| 258 | break |
| 259 | count += 1 |
| 260 | if count > 10: # Too many tries |
| 261 | break |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 262 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 263 | self.assertTrue(response is not None, |
| 264 | 'Packet in message not received on port ' + |
| 265 | str(of_port)) |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 266 | if not dataplane.match_exp_pkt(pkt, response.data): |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 267 | basic_logger.debug("Sent %s" % format_packet(pkt)) |
| 268 | basic_logger.debug("Resp %s" % format_packet(response.data)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 269 | self.assertTrue(False, |
| 270 | 'Response packet does not match send packet' + |
| 271 | ' for port ' + str(of_port)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 272 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 273 | class PacketOut(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 274 | """ |
| 275 | Test packet out function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 276 | |
| 277 | Send packet out message to controller for each dataplane port and |
| 278 | verify the packet appears on the appropriate dataplane port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 279 | """ |
| 280 | def runTest(self): |
| 281 | # Construct packet to send to dataplane |
| 282 | # Send packet to dataplane |
| 283 | # Poll controller with expect message type packet in |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 284 | |
| 285 | rc = delete_all_flows(self.controller, basic_logger) |
| 286 | self.assertEqual(rc, 0, "Failed to delete all flows") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 287 | |
| 288 | # These will get put into function |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 289 | of_ports = basic_port_map.keys() |
| 290 | of_ports.sort() |
| 291 | for dp_port in of_ports: |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 292 | for outpkt, opt in [ |
| 293 | (simple_tcp_packet(), "simple TCP packet"), |
| 294 | (simple_eth_packet(), "simple Ethernet packet"), |
| 295 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 296 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 297 | basic_logger.info("PKT OUT test with %s, port %s" % (opt, dp_port)) |
| 298 | msg = message.packet_out() |
| 299 | msg.data = str(outpkt) |
| 300 | act = action.action_output() |
| 301 | act.port = dp_port |
| 302 | self.assertTrue(msg.actions.add(act), 'Could not add action to msg') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 303 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 304 | basic_logger.info("PacketOut to: " + str(dp_port)) |
| 305 | rv = self.controller.message_send(msg) |
| 306 | self.assertTrue(rv == 0, "Error sending out message") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 307 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 308 | exp_pkt_arg = None |
| 309 | exp_port = None |
| 310 | if basic_config["relax"]: |
| 311 | exp_pkt_arg = outpkt |
| 312 | exp_port = dp_port |
| 313 | (of_port, pkt, pkt_time) = self.dataplane.poll(timeout=1, |
| 314 | port_number=exp_port, |
| 315 | exp_pkt=exp_pkt_arg) |
| 316 | |
| 317 | self.assertTrue(pkt is not None, 'Packet not received') |
| 318 | basic_logger.info("PacketOut: got pkt from " + str(of_port)) |
| 319 | if of_port is not None: |
| 320 | self.assertEqual(of_port, dp_port, "Unexpected receive port") |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 321 | if not dataplane.match_exp_pkt(outpkt, pkt): |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 322 | basic_logger.debug("Sent %s" % format_packet(outpkt)) |
| 323 | basic_logger.debug("Resp %s" % format_packet( |
| 324 | str(pkt)[:len(str(outpkt))])) |
Dan Talayco | dc6fca3 | 2012-03-30 10:05:49 -0700 | [diff] [blame] | 325 | self.assertEqual(str(outpkt), str(pkt)[:len(str(outpkt))], |
| 326 | 'Response packet does not match send packet') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 327 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 328 | class FlowStatsGet(SimpleProtocol): |
| 329 | """ |
| 330 | Get stats |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 331 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 332 | Simply verify stats get transaction |
| 333 | """ |
| 334 | def runTest(self): |
| 335 | basic_logger.info("Running StatsGet") |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 336 | basic_logger.info("Inserting trial flow") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 337 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 338 | rv = self.controller.message_send(request) |
| 339 | self.assertTrue(rv != -1, "Failed to insert test flow") |
| 340 | |
| 341 | basic_logger.info("Sending flow request") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 342 | request = message.flow_stats_request() |
| 343 | request.out_port = ofp.OFPP_NONE |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 344 | request.table_id = 0xff |
| 345 | request.match.wildcards = 0 # ofp.OFPFW_ALL |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 346 | response, pkt = self.controller.transact(request, timeout=2) |
| 347 | self.assertTrue(response is not None, "Did not get response") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 348 | basic_logger.debug(response.show()) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 349 | |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 350 | test_prio["FlowStatsGet"] = -1 |
| 351 | |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 352 | class TableStatsGet(SimpleProtocol): |
| 353 | """ |
| 354 | Get table stats |
| 355 | |
| 356 | Simply verify table stats get transaction |
| 357 | """ |
| 358 | def runTest(self): |
| 359 | basic_logger.info("Running TableStatsGet") |
| 360 | basic_logger.info("Inserting trial flow") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 361 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 362 | rv = self.controller.message_send(request) |
| 363 | self.assertTrue(rv != -1, "Failed to insert test flow") |
| 364 | |
| 365 | basic_logger.info("Sending table stats request") |
| 366 | request = message.table_stats_request() |
| 367 | response, pkt = self.controller.transact(request, timeout=2) |
| 368 | self.assertTrue(response is not None, "Did not get response") |
| 369 | basic_logger.debug(response.show()) |
| 370 | |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame^] | 371 | class DescStatsGet(SimpleProtocol): |
| 372 | """ |
| 373 | Get stats |
| 374 | |
| 375 | Simply verify stats get transaction |
| 376 | """ |
| 377 | def runTest(self): |
| 378 | basic_logger.info("Running DescStatsGet") |
| 379 | |
| 380 | basic_logger.info("Sending stats request") |
| 381 | request = message.desc_stats_request() |
| 382 | response, pkt = self.controller.transact(request, timeout=2) |
| 383 | self.assertTrue(response is not None, "Did not get response") |
| 384 | basic_logger.debug(response.show()) |
| 385 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 386 | class FlowMod(SimpleProtocol): |
| 387 | """ |
| 388 | Insert a flow |
| 389 | |
| 390 | Simple verification of a flow mod transaction |
| 391 | """ |
| 392 | |
| 393 | def runTest(self): |
| 394 | basic_logger.info("Running " + str(self)) |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 395 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 396 | rv = self.controller.message_send(request) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 397 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 398 | |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 399 | class PortConfigMod(SimpleProtocol): |
| 400 | """ |
| 401 | Modify a bit in port config and verify changed |
| 402 | |
| 403 | Get the switch configuration, modify the port configuration |
| 404 | and write it back; get the config again and verify changed. |
| 405 | Then set it back to the way it was. |
| 406 | """ |
| 407 | |
| 408 | def runTest(self): |
| 409 | basic_logger.info("Running " + str(self)) |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 410 | for of_port, ifname in basic_port_map.items(): # Grab first port |
| 411 | break |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 412 | |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 413 | (hw_addr, config, advert) = \ |
| 414 | port_config_get(self.controller, of_port, basic_logger) |
| 415 | self.assertTrue(config is not None, "Did not get port config") |
| 416 | |
| 417 | basic_logger.debug("No flood bit port " + str(of_port) + " is now " + |
| 418 | str(config & ofp.OFPPC_NO_FLOOD)) |
| 419 | |
| 420 | rv = port_config_set(self.controller, of_port, |
| 421 | config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD, |
| 422 | basic_logger) |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 423 | self.assertTrue(rv != -1, "Error sending port mod") |
| 424 | |
| 425 | # Verify change took place with same feature request |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 426 | (hw_addr, config2, advert) = \ |
| 427 | port_config_get(self.controller, of_port, basic_logger) |
| 428 | basic_logger.debug("No flood bit port " + str(of_port) + " is now " + |
| 429 | str(config2 & ofp.OFPPC_NO_FLOOD)) |
| 430 | self.assertTrue(config2 is not None, "Did not get port config2") |
| 431 | self.assertTrue(config2 & ofp.OFPPC_NO_FLOOD != |
| 432 | config & ofp.OFPPC_NO_FLOOD, |
| 433 | "Bit change did not take") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 434 | # Set it back |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 435 | rv = port_config_set(self.controller, of_port, config, |
| 436 | ofp.OFPPC_NO_FLOOD, basic_logger) |
| 437 | self.assertTrue(rv != -1, "Error sending port mod") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 438 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 439 | if __name__ == "__main__": |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 440 | print "Please run through oft script: ./oft --test_spec=basic" |