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 |
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: |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 87 | raise Exception("Controller startup failed") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 88 | if self.controller.switch_addr is None: |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 89 | raise Exception("Controller startup failed (no switch addr)") |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 90 | basic_logger.info("Connected " + str(self.controller.switch_addr)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 91 | |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 92 | def inheritSetup(self, parent): |
| 93 | """ |
| 94 | Inherit the setup of a parent |
| 95 | |
| 96 | This allows running at test from within another test. Do the |
| 97 | following: |
| 98 | |
| 99 | sub_test = SomeTestClass() # Create an instance of the test class |
| 100 | sub_test.inheritSetup(self) # Inherit setup of parent |
| 101 | sub_test.runTest() # Run the test |
| 102 | |
| 103 | Normally, only the parent's setUp and tearDown are called and |
| 104 | the state after the sub_test is run must be taken into account |
| 105 | by subsequent operations. |
| 106 | """ |
| 107 | self.logger = parent.logger |
| 108 | self.config = parent.config |
| 109 | basic_logger.info("** Setup " + str(self) + " inheriting from " |
| 110 | + str(parent)) |
| 111 | self.controller = parent.controller |
| 112 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 113 | def tearDown(self): |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 114 | basic_logger.info("** END TEST CASE " + str(self)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 115 | self.controller.shutdown() |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 116 | #@todo Review if join should be done on clean_shutdown |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 117 | if self.clean_shutdown: |
| 118 | self.controller.join() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 119 | |
| 120 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 121 | # Just a simple sanity check as illustration |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 122 | basic_logger.info("Running simple proto test") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 123 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 124 | str(self) + 'No connection to switch') |
| 125 | |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 126 | def assertTrue(self, cond, msg): |
| 127 | if not cond: |
| 128 | basic_logger.error("** FAILED ASSERTION: " + msg) |
| 129 | unittest.TestCase.assertTrue(self, cond, msg) |
| 130 | |
Dan Talayco | c24aaae | 2010-07-08 14:05:24 -0700 | [diff] [blame] | 131 | test_prio["SimpleProtocol"] = 1 |
| 132 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 133 | class SimpleDataPlane(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 134 | """ |
| 135 | Root class that sets up the controller and dataplane |
| 136 | """ |
| 137 | def setUp(self): |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 138 | SimpleProtocol.setUp(self) |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 139 | self.dataplane = dataplane.DataPlane(self.config) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 140 | for of_port, ifname in basic_port_map.items(): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 141 | self.dataplane.port_add(ifname, of_port) |
| 142 | |
Dan Talayco | 677cc11 | 2012-03-27 10:28:58 -0700 | [diff] [blame] | 143 | def inheritSetup(self, parent): |
| 144 | """ |
| 145 | Inherit the setup of a parent |
| 146 | |
| 147 | See SimpleProtocol.inheritSetup |
| 148 | """ |
| 149 | SimpleProtocol.inheritSetup(self, parent) |
| 150 | self.dataplane = parent.dataplane |
| 151 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 152 | def tearDown(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 153 | basic_logger.info("Teardown for simple dataplane test") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 154 | SimpleProtocol.tearDown(self) |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 155 | if hasattr(self, 'dataplane'): |
| 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() |
Rich Lane | 58cf05f | 2012-07-11 16:41:47 -0700 | [diff] [blame] | 175 | raise KeyboardInterrupt |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 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)) |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 183 | self.dataplane = dataplane.DataPlane(self.config) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 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") |
Dan Talayco | 0fc08bd | 2012-04-09 16:56:18 -0700 | [diff] [blame] | 239 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
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 | |
Jeffrey Townsend | 4d5ca92 | 2012-07-11 11:37:35 -0700 | [diff] [blame] | 273 | |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 274 | class PacketInBroadcastCheck(SimpleDataPlane): |
| 275 | """ |
| 276 | Check if bcast pkts leak when no flows are present |
| 277 | |
| 278 | Clear the flow table |
| 279 | Send in a broadcast pkt |
| 280 | Look for the packet on other dataplane ports. |
| 281 | """ |
| 282 | def runTest(self): |
| 283 | # Need at least two ports |
| 284 | self.assertTrue(len(basic_port_map) > 1, "Too few ports for test") |
| 285 | |
| 286 | rc = delete_all_flows(self.controller, basic_logger) |
| 287 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 288 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 289 | |
| 290 | of_ports = basic_port_map.keys() |
| 291 | d_port = of_ports[0] |
| 292 | pkt = simple_eth_packet(dl_dst='ff:ff:ff:ff:ff:ff') |
| 293 | |
| 294 | basic_logger.info("BCast Leak Test, send to port %s" % d_port) |
| 295 | self.dataplane.send(d_port, str(pkt)) |
| 296 | |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame^] | 297 | (of_port, pkt_in, pkt_time) = self.dataplane.poll(exp_pkt=pkt) |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 298 | self.assertTrue(pkt_in is None, |
| 299 | 'BCast packet received on port ' + str(of_port)) |
| 300 | |
| 301 | test_prio["PacketInBroadcastCheck"] = -1 |
| 302 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 303 | class PacketOut(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 304 | """ |
| 305 | Test packet out function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 306 | |
| 307 | Send packet out message to controller for each dataplane port and |
| 308 | verify the packet appears on the appropriate dataplane port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 309 | """ |
| 310 | def runTest(self): |
| 311 | # Construct packet to send to dataplane |
| 312 | # Send packet to dataplane |
| 313 | # Poll controller with expect message type packet in |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 314 | |
| 315 | rc = delete_all_flows(self.controller, basic_logger) |
| 316 | self.assertEqual(rc, 0, "Failed to delete all flows") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 317 | |
| 318 | # These will get put into function |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 319 | of_ports = basic_port_map.keys() |
| 320 | of_ports.sort() |
| 321 | for dp_port in of_ports: |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 322 | for outpkt, opt in [ |
| 323 | (simple_tcp_packet(), "simple TCP packet"), |
| 324 | (simple_eth_packet(), "simple Ethernet packet"), |
| 325 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 326 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 327 | basic_logger.info("PKT OUT test with %s, port %s" % (opt, dp_port)) |
| 328 | msg = message.packet_out() |
| 329 | msg.data = str(outpkt) |
| 330 | act = action.action_output() |
| 331 | act.port = dp_port |
| 332 | self.assertTrue(msg.actions.add(act), 'Could not add action to msg') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 333 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 334 | basic_logger.info("PacketOut to: " + str(dp_port)) |
| 335 | rv = self.controller.message_send(msg) |
| 336 | self.assertTrue(rv == 0, "Error sending out message") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 337 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 338 | exp_pkt_arg = None |
| 339 | exp_port = None |
| 340 | if basic_config["relax"]: |
| 341 | exp_pkt_arg = outpkt |
| 342 | exp_port = dp_port |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame^] | 343 | (of_port, pkt, pkt_time) = self.dataplane.poll(port_number=exp_port, |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 344 | exp_pkt=exp_pkt_arg) |
| 345 | |
| 346 | self.assertTrue(pkt is not None, 'Packet not received') |
| 347 | basic_logger.info("PacketOut: got pkt from " + str(of_port)) |
| 348 | if of_port is not None: |
| 349 | self.assertEqual(of_port, dp_port, "Unexpected receive port") |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 350 | if not dataplane.match_exp_pkt(outpkt, pkt): |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 351 | basic_logger.debug("Sent %s" % format_packet(outpkt)) |
| 352 | basic_logger.debug("Resp %s" % format_packet( |
| 353 | str(pkt)[:len(str(outpkt))])) |
Dan Talayco | dc6fca3 | 2012-03-30 10:05:49 -0700 | [diff] [blame] | 354 | self.assertEqual(str(outpkt), str(pkt)[:len(str(outpkt))], |
| 355 | 'Response packet does not match send packet') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 356 | |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 357 | class PacketOutMC(SimpleDataPlane): |
| 358 | """ |
| 359 | Test packet out to multiple output ports |
| 360 | |
| 361 | Send packet out message to controller for 1 to N dataplane ports and |
| 362 | verify the packet appears on the appropriate ports |
| 363 | """ |
| 364 | def runTest(self): |
| 365 | # Construct packet to send to dataplane |
| 366 | # Send packet to dataplane |
| 367 | # Poll controller with expect message type packet in |
| 368 | |
| 369 | rc = delete_all_flows(self.controller, basic_logger) |
| 370 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 371 | |
| 372 | # These will get put into function |
| 373 | of_ports = basic_port_map.keys() |
| 374 | random.shuffle(of_ports) |
| 375 | for num_ports in range(1,len(of_ports)+1): |
| 376 | for outpkt, opt in [ |
| 377 | (simple_tcp_packet(), "simple TCP packet"), |
| 378 | (simple_eth_packet(), "simple Ethernet packet"), |
| 379 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
| 380 | |
| 381 | dp_ports = of_ports[0:num_ports] |
| 382 | basic_logger.info("PKT OUT test with " + opt + |
| 383 | ", ports " + str(dp_ports)) |
| 384 | msg = message.packet_out() |
| 385 | msg.data = str(outpkt) |
| 386 | act = action.action_output() |
| 387 | for i in range(0,num_ports): |
| 388 | act.port = dp_ports[i] |
| 389 | self.assertTrue(msg.actions.add(act), |
| 390 | 'Could not add action to msg') |
| 391 | |
| 392 | basic_logger.info("PacketOut to: " + str(dp_ports)) |
| 393 | rv = self.controller.message_send(msg) |
| 394 | self.assertTrue(rv == 0, "Error sending out message") |
| 395 | |
| 396 | receive_pkt_check(self.dataplane, outpkt, dp_ports, |
| 397 | set(of_ports).difference(dp_ports), |
| 398 | self, basic_logger, basic_config) |
| 399 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 400 | class FlowStatsGet(SimpleProtocol): |
| 401 | """ |
| 402 | Get stats |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 403 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 404 | Simply verify stats get transaction |
| 405 | """ |
| 406 | def runTest(self): |
| 407 | basic_logger.info("Running StatsGet") |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 408 | basic_logger.info("Inserting trial flow") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 409 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 410 | rv = self.controller.message_send(request) |
| 411 | self.assertTrue(rv != -1, "Failed to insert test flow") |
| 412 | |
| 413 | basic_logger.info("Sending flow request") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 414 | request = message.flow_stats_request() |
| 415 | request.out_port = ofp.OFPP_NONE |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 416 | request.table_id = 0xff |
| 417 | request.match.wildcards = 0 # ofp.OFPFW_ALL |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame^] | 418 | response, pkt = self.controller.transact(request) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 419 | self.assertTrue(response is not None, "Did not get response") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 420 | basic_logger.debug(response.show()) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 421 | |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 422 | test_prio["FlowStatsGet"] = -1 |
| 423 | |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 424 | class TableStatsGet(SimpleProtocol): |
| 425 | """ |
| 426 | Get table stats |
| 427 | |
| 428 | Simply verify table stats get transaction |
| 429 | """ |
| 430 | def runTest(self): |
| 431 | basic_logger.info("Running TableStatsGet") |
| 432 | basic_logger.info("Inserting trial flow") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 433 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 434 | rv = self.controller.message_send(request) |
| 435 | self.assertTrue(rv != -1, "Failed to insert test flow") |
| 436 | |
| 437 | basic_logger.info("Sending table stats request") |
| 438 | request = message.table_stats_request() |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame^] | 439 | response, pkt = self.controller.transact(request) |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 440 | self.assertTrue(response is not None, "Did not get response") |
| 441 | basic_logger.debug(response.show()) |
| 442 | |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 443 | class DescStatsGet(SimpleProtocol): |
| 444 | """ |
| 445 | Get stats |
| 446 | |
| 447 | Simply verify stats get transaction |
| 448 | """ |
| 449 | def runTest(self): |
| 450 | basic_logger.info("Running DescStatsGet") |
| 451 | |
| 452 | basic_logger.info("Sending stats request") |
| 453 | request = message.desc_stats_request() |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame^] | 454 | response, pkt = self.controller.transact(request) |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 455 | self.assertTrue(response is not None, "Did not get response") |
| 456 | basic_logger.debug(response.show()) |
| 457 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 458 | class FlowMod(SimpleProtocol): |
| 459 | """ |
| 460 | Insert a flow |
| 461 | |
| 462 | Simple verification of a flow mod transaction |
| 463 | """ |
| 464 | |
| 465 | def runTest(self): |
| 466 | basic_logger.info("Running " + str(self)) |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 467 | request = flow_mod_gen(basic_port_map, True) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 468 | rv = self.controller.message_send(request) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 469 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 470 | |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 471 | class PortConfigMod(SimpleProtocol): |
| 472 | """ |
| 473 | Modify a bit in port config and verify changed |
| 474 | |
| 475 | Get the switch configuration, modify the port configuration |
| 476 | and write it back; get the config again and verify changed. |
| 477 | Then set it back to the way it was. |
| 478 | """ |
| 479 | |
| 480 | def runTest(self): |
| 481 | basic_logger.info("Running " + str(self)) |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 482 | for of_port, ifname in basic_port_map.items(): # Grab first port |
| 483 | break |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 484 | |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 485 | (hw_addr, config, advert) = \ |
| 486 | port_config_get(self.controller, of_port, basic_logger) |
| 487 | self.assertTrue(config is not None, "Did not get port config") |
| 488 | |
| 489 | basic_logger.debug("No flood bit port " + str(of_port) + " is now " + |
| 490 | str(config & ofp.OFPPC_NO_FLOOD)) |
| 491 | |
| 492 | rv = port_config_set(self.controller, of_port, |
| 493 | config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD, |
| 494 | basic_logger) |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 495 | self.assertTrue(rv != -1, "Error sending port mod") |
| 496 | |
| 497 | # Verify change took place with same feature request |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 498 | (hw_addr, config2, advert) = \ |
| 499 | port_config_get(self.controller, of_port, basic_logger) |
| 500 | basic_logger.debug("No flood bit port " + str(of_port) + " is now " + |
| 501 | str(config2 & ofp.OFPPC_NO_FLOOD)) |
| 502 | self.assertTrue(config2 is not None, "Did not get port config2") |
| 503 | self.assertTrue(config2 & ofp.OFPPC_NO_FLOOD != |
| 504 | config & ofp.OFPPC_NO_FLOOD, |
| 505 | "Bit change did not take") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 506 | # Set it back |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 507 | rv = port_config_set(self.controller, of_port, config, |
| 508 | ofp.OFPPC_NO_FLOOD, basic_logger) |
| 509 | self.assertTrue(rv != -1, "Error sending port mod") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 510 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 511 | if __name__ == "__main__": |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 512 | print "Please run through oft script: ./oft --test_spec=basic" |