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