Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 1 | """ |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 2 | Basic protocol and dataplane test cases |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 3 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 4 | It is recommended that these definitions be kept in their own |
| 5 | namespace as different groups of tests will likely define |
| 6 | similar identifiers. |
| 7 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 8 | Current Assumptions: |
| 9 | |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 10 | The function test_set_init is called with a complete configuration |
| 11 | dictionary prior to the invocation of any tests from this file. |
| 12 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 13 | The switch is actively attempting to contact the controller at the address |
| 14 | indicated oin oft_config |
| 15 | |
| 16 | """ |
| 17 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 18 | import time |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 19 | import signal |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 20 | import sys |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 21 | import logging |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 22 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 23 | import unittest |
| 24 | |
| 25 | import oftest.controller as controller |
| 26 | import oftest.cstruct as ofp |
| 27 | import oftest.message as message |
| 28 | import oftest.dataplane as dataplane |
| 29 | import oftest.action as action |
| 30 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 31 | from testutils import * |
| 32 | |
| 33 | #@var basic_port_map Local copy of the configuration map from OF port |
| 34 | # numbers to OS interfaces |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 35 | basic_port_map = None |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 36 | #@var basic_logger Local logger object |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 37 | basic_logger = None |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 38 | #@var basic_config Local copy of global configuration data |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 39 | basic_config = None |
| 40 | |
| 41 | def test_set_init(config): |
| 42 | """ |
| 43 | Set up function for basic test classes |
| 44 | |
| 45 | @param config The configuration dictionary; see oft |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 46 | """ |
| 47 | |
| 48 | global basic_port_map |
| 49 | global basic_logger |
| 50 | global basic_config |
| 51 | |
| 52 | basic_logger = logging.getLogger("basic") |
| 53 | basic_logger.info("Initializing test set") |
| 54 | basic_port_map = config["port_map"] |
| 55 | basic_config = config |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 56 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 57 | class SimpleProtocol(unittest.TestCase): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 58 | """ |
| 59 | Root class for setting up the controller |
| 60 | """ |
| 61 | |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame^] | 62 | def sig_handler(self, v1, v2): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 63 | basic_logger.critical("Received interrupt signal; exiting") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 64 | print "Received interrupt signal; exiting" |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 65 | self.clean_shutdown = False |
| 66 | self.tearDown() |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 67 | sys.exit(1) |
| 68 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 69 | def setUp(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 70 | signal.signal(signal.SIGINT, self.sig_handler) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 71 | basic_logger.info("Setup for " + str(self)) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 72 | self.controller = controller.Controller( |
| 73 | host=basic_config["controller_host"], |
| 74 | port=basic_config["controller_port"]) |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 75 | # clean_shutdown should be set to False to force quit app |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 76 | self.clean_shutdown = True |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 77 | self.controller.start() |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame^] | 78 | #@todo Add an option to wait for a pkt transaction to ensure version |
| 79 | # compatibilty? |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 80 | self.controller.connect(timeout=20) |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame^] | 81 | if not self.controller.active: |
| 82 | print "Controller startup failed; exiting" |
| 83 | sys.exit(1) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 84 | basic_logger.info("Connected " + str(self.controller.switch_addr)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 85 | |
| 86 | def tearDown(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 87 | basic_logger.info("Teardown for simple proto test") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 88 | self.controller.shutdown() |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 89 | #@todo Review if join should be done on clean_shutdown |
Dan Talayco | f8f4140 | 2010-03-12 22:17:39 -0800 | [diff] [blame] | 90 | if self.clean_shutdown: |
| 91 | self.controller.join() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 92 | |
| 93 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 94 | # Just a simple sanity check as illustration |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 95 | basic_logger.info("Running simple proto test") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 96 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 97 | str(self) + 'No connection to switch') |
| 98 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 99 | class SimpleDataPlane(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 100 | """ |
| 101 | Root class that sets up the controller and dataplane |
| 102 | """ |
| 103 | def setUp(self): |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 104 | SimpleProtocol.setUp(self) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 105 | self.dataplane = dataplane.DataPlane() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 106 | for of_port, ifname in basic_port_map.items(): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 107 | self.dataplane.port_add(ifname, of_port) |
| 108 | |
| 109 | def tearDown(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 110 | basic_logger.info("Teardown for simple dataplane test") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 111 | SimpleProtocol.tearDown(self) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 112 | self.dataplane.kill(join_threads=self.clean_shutdown) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 113 | basic_logger.info("Teardown done") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 114 | |
| 115 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 116 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 117 | str(self) + 'No connection to switch') |
| 118 | # self.dataplane.show() |
| 119 | # Would like an assert that checks the data plane |
| 120 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 121 | class Echo(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 122 | """ |
| 123 | Test echo response with no data |
| 124 | """ |
| 125 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 126 | request = message.echo_request() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 127 | response, pkt = self.controller.transact(request) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 128 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 129 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 130 | self.assertEqual(request.header.xid, response.header.xid, |
| 131 | 'response xid != request xid') |
| 132 | self.assertEqual(len(response.data), 0, 'response data non-empty') |
| 133 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 134 | class EchoWithData(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 135 | """ |
| 136 | Test echo response with short string data |
| 137 | """ |
| 138 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 139 | request = message.echo_request() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 140 | request.data = 'OpenFlow Will Rule The World' |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 141 | response, pkt = self.controller.transact(request) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 142 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 143 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 144 | self.assertEqual(request.header.xid, response.header.xid, |
| 145 | 'response xid != request xid') |
| 146 | self.assertEqual(request.data, response.data, |
| 147 | 'response data does not match request') |
| 148 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 149 | class PacketIn(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 150 | """ |
| 151 | Test packet in function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 152 | |
| 153 | Send a packet to each dataplane port and verify that a packet |
| 154 | in message is received from the controller for each |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 155 | """ |
| 156 | def runTest(self): |
| 157 | # Construct packet to send to dataplane |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 158 | # Send packet to dataplane, once to each port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 159 | # Poll controller with expect message type packet in |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 160 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 161 | rc = delete_all_flows(self.controller, basic_logger) |
| 162 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 163 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 164 | for of_port in basic_port_map.keys(): |
| 165 | basic_logger.info("PKT IN test, port " + str(of_port)) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 166 | pkt = simple_tcp_packet() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 167 | self.dataplane.send(of_port, str(pkt)) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 168 | #@todo Check for unexpected messages? |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 169 | (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 170 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 171 | self.assertTrue(response is not None, |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 172 | 'Packet in message not received on port ' + |
| 173 | str(of_port)) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 174 | if str(pkt) != response.data: |
Dan Talayco | ef701f4 | 2010-05-07 09:22:35 -0700 | [diff] [blame^] | 175 | basic_logger.debug("pkt len " + str(len(str(pkt))) + |
| 176 | ": " + str(pkt)) |
| 177 | basic_logger.debug("resp len " + |
| 178 | str(len(str(response.data))) + |
| 179 | ": " + str(response.data)) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 180 | |
| 181 | self.assertEqual(str(pkt), response.data, |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 182 | 'Response packet does not match send packet' + |
| 183 | ' for port ' + str(of_port)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 184 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 185 | class PacketOut(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 186 | """ |
| 187 | Test packet out function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 188 | |
| 189 | Send packet out message to controller for each dataplane port and |
| 190 | verify the packet appears on the appropriate dataplane port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 191 | """ |
| 192 | def runTest(self): |
| 193 | # Construct packet to send to dataplane |
| 194 | # Send packet to dataplane |
| 195 | # Poll controller with expect message type packet in |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 196 | |
| 197 | rc = delete_all_flows(self.controller, basic_logger) |
| 198 | self.assertEqual(rc, 0, "Failed to delete all flows") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 199 | |
| 200 | # These will get put into function |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 201 | outpkt = simple_tcp_packet() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 202 | of_ports = basic_port_map.keys() |
| 203 | of_ports.sort() |
| 204 | for dp_port in of_ports: |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 205 | msg = message.packet_out() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 206 | msg.data = str(outpkt) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 207 | act = action.action_output() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 208 | act.port = dp_port |
| 209 | self.assertTrue(msg.actions.add(act), 'Could not add action to msg') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 210 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 211 | basic_logger.info("PacketOut to: " + str(dp_port)) |
| 212 | rv = self.controller.message_send(msg) |
| 213 | self.assertTrue(rv == 0, "Error sending out message") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 214 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 215 | (of_port, pkt, pkt_time) = self.dataplane.poll(timeout=1) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 216 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 217 | self.assertTrue(pkt is not None, 'Packet not received') |
| 218 | basic_logger.info("PacketOut: got pkt from " + str(of_port)) |
| 219 | if of_port is not None: |
| 220 | self.assertEqual(of_port, dp_port, "Unexpected receive port") |
| 221 | self.assertEqual(str(outpkt), str(pkt), |
| 222 | 'Response packet does not match send packet') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 223 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 224 | class FlowStatsGet(SimpleProtocol): |
| 225 | """ |
| 226 | Get stats |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 227 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 228 | Simply verify stats get transaction |
| 229 | """ |
| 230 | def runTest(self): |
| 231 | basic_logger.info("Running StatsGet") |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 232 | basic_logger.info("Inserting trial flow") |
| 233 | request = message.flow_mod() |
| 234 | request.match.wildcards = ofp.OFPFW_ALL |
| 235 | request.buffer_id = 0xffffffff |
| 236 | rv = self.controller.message_send(request) |
| 237 | self.assertTrue(rv != -1, "Failed to insert test flow") |
| 238 | |
| 239 | basic_logger.info("Sending flow request") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 240 | request = message.flow_stats_request() |
| 241 | request.out_port = ofp.OFPP_NONE |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 242 | request.table_id = 0xff |
| 243 | request.match.wildcards = 0 # ofp.OFPFW_ALL |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 244 | response, pkt = self.controller.transact(request, timeout=2) |
| 245 | self.assertTrue(response is not None, "Did not get response") |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 246 | basic_logger.info(response.show()) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 247 | |
| 248 | class FlowMod(SimpleProtocol): |
| 249 | """ |
| 250 | Insert a flow |
| 251 | |
| 252 | Simple verification of a flow mod transaction |
| 253 | """ |
| 254 | |
| 255 | def runTest(self): |
| 256 | basic_logger.info("Running " + str(self)) |
| 257 | request = message.flow_mod() |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 258 | request.match.wildcards = ofp.OFPFW_ALL |
| 259 | request.buffer_id = 0xffffffff |
| 260 | rv = self.controller.message_send(request) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 261 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 262 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 263 | if __name__ == "__main__": |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 264 | print "Please run through oft script: ./oft --test_spec=basic" |