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