Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 1 | """ |
| 2 | Basic test cases for the oftest OpenFlow test framework |
| 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 | |
| 10 | The oftest framework source is in ../src/python/oftest |
| 11 | Configuration of the platform and system is stored in oft_config in that dir |
| 12 | The switch is actively attempting to contact the controller at the address |
| 13 | indicated oin oft_config |
| 14 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 15 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 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 scapy.all as scapy |
| 24 | import unittest |
| 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 | |
| 42 | def test_set_init(config): |
| 43 | """ |
| 44 | Set up function for basic test classes |
| 45 | |
| 46 | @param config The configuration dictionary; see oft |
| 47 | @return TestSuite object for the class; may depend on config |
| 48 | """ |
| 49 | |
| 50 | global basic_port_map |
| 51 | global basic_logger |
| 52 | global basic_config |
| 53 | |
| 54 | basic_logger = logging.getLogger("basic") |
| 55 | basic_logger.info("Initializing test set") |
| 56 | basic_port_map = config["port_map"] |
| 57 | basic_config = config |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 58 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 59 | class SimpleProtocol(unittest.TestCase): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 60 | """ |
| 61 | Root class for setting up the controller |
| 62 | """ |
| 63 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 64 | def sig_handler(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 65 | basic_logger.critical("Received interrupt signal; exiting") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 66 | print "Received interrupt signal; exiting" |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 67 | self.clean_shutdown = False |
| 68 | self.tearDown() |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 69 | sys.exit(1) |
| 70 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 71 | def setUp(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 72 | signal.signal(signal.SIGINT, self.sig_handler) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 73 | basic_logger.info("Setup for " + str(self)) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 74 | self.controller = controller.Controller( |
| 75 | host=basic_config["controller_host"], |
| 76 | port=basic_config["controller_port"]) |
| 77 | # clean_shutdown is set to False to force quit app |
| 78 | self.clean_shutdown = True |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 79 | self.controller.start() |
| 80 | self.controller.connect(timeout=20) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 81 | basic_logger.info("Connected " + str(self.controller.switch_addr)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 82 | |
| 83 | def tearDown(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 84 | basic_logger.info("Teardown for simple proto test") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 85 | self.controller.shutdown() |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 86 | #@todo Review if join should be done on clean_shutdown |
| 87 | # if self.clean_shutdown: |
| 88 | # self.controller.join() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 89 | |
| 90 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 91 | # Just a simple sanity check as illustration |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 92 | basic_logger.info("Running simple proto test") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 93 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 94 | str(self) + 'No connection to switch') |
| 95 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 96 | class SimpleDataPlane(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 97 | """ |
| 98 | Root class that sets up the controller and dataplane |
| 99 | """ |
| 100 | def setUp(self): |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 101 | SimpleProtocol.setUp(self) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 102 | self.dataplane = dataplane.DataPlane() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 103 | for of_port, ifname in basic_port_map.items(): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 104 | self.dataplane.port_add(ifname, of_port) |
| 105 | |
| 106 | def tearDown(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 107 | basic_logger.info("Teardown for simple dataplane test") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 108 | SimpleProtocol.tearDown(self) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 109 | self.dataplane.kill(join_threads=self.clean_shutdown) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 110 | basic_logger.info("Teardown done") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 111 | |
| 112 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 113 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 114 | str(self) + 'No connection to switch') |
| 115 | # self.dataplane.show() |
| 116 | # Would like an assert that checks the data plane |
| 117 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 118 | class Echo(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 119 | """ |
| 120 | Test echo response with no data |
| 121 | """ |
| 122 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 123 | request = message.echo_request() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 124 | response, pkt = self.controller.transact(request) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 125 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 126 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 127 | self.assertEqual(request.header.xid, response.header.xid, |
| 128 | 'response xid != request xid') |
| 129 | self.assertEqual(len(response.data), 0, 'response data non-empty') |
| 130 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 131 | class EchoWithData(SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 132 | """ |
| 133 | Test echo response with short string data |
| 134 | """ |
| 135 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 136 | request = message.echo_request() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 137 | request.data = 'OpenFlow Will Rule The World' |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 138 | response, pkt = self.controller.transact(request) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 139 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 140 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 141 | self.assertEqual(request.header.xid, response.header.xid, |
| 142 | 'response xid != request xid') |
| 143 | self.assertEqual(request.data, response.data, |
| 144 | 'response data does not match request') |
| 145 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 146 | class PacketIn(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 147 | """ |
| 148 | Test packet in function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 149 | |
| 150 | Send a packet to each dataplane port and verify that a packet |
| 151 | in message is received from the controller for each |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 152 | """ |
| 153 | def runTest(self): |
| 154 | # Construct packet to send to dataplane |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 155 | # Send packet to dataplane, once to each port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 156 | # Poll controller with expect message type packet in |
| 157 | # For now, a random packet from scapy tutorial |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 158 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 159 | rc = delete_all_flows(self.controller, basic_logger) |
| 160 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 161 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 162 | for of_port in basic_port_map.keys(): |
| 163 | basic_logger.info("PKT IN test, port " + str(of_port)) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 164 | pkt = scapy.Ether()/scapy.IP(dst="www.slashdot.org")/scapy.TCP()/\ |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 165 | ("GET /index.html HTTP/1.0. port" + str(of_port)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 166 | self.dataplane.send(of_port, str(pkt)) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 167 | #@todo Check for unexpected messages? |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 168 | (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 169 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 170 | self.assertTrue(response is not None, |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 171 | 'Packet in message not received on port ' + |
| 172 | str(of_port)) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 173 | if str(pkt) != response.data: |
| 174 | basic_logger.debug("pkt: "+str(pkt)+" resp: " + |
| 175 | str(response)) |
| 176 | |
| 177 | self.assertEqual(str(pkt), response.data, |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 178 | 'Response packet does not match send packet' + |
| 179 | ' for port ' + str(of_port)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 180 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 181 | class PacketOut(SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 182 | """ |
| 183 | Test packet out function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 184 | |
| 185 | Send packet out message to controller for each dataplane port and |
| 186 | verify the packet appears on the appropriate dataplane port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 187 | """ |
| 188 | def runTest(self): |
| 189 | # Construct packet to send to dataplane |
| 190 | # Send packet to dataplane |
| 191 | # Poll controller with expect message type packet in |
| 192 | # For now, a random packet from scapy tutorial |
| 193 | |
| 194 | # These will get put into function |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 195 | outpkt = scapy.Ether()/scapy.IP(dst="www.slashdot.org")/scapy.TCP()/\ |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 196 | "GET /index.html HTTP/1.0 \n\n" |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 197 | of_ports = basic_port_map.keys() |
| 198 | of_ports.sort() |
| 199 | for dp_port in of_ports: |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 200 | msg = message.packet_out() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 201 | msg.data = str(outpkt) |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 202 | act = action.action_output() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 203 | act.port = dp_port |
| 204 | self.assertTrue(msg.actions.add(act), 'Could not add action to msg') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 205 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 206 | basic_logger.info("PacketOut to: " + str(dp_port)) |
| 207 | rv = self.controller.message_send(msg) |
| 208 | self.assertTrue(rv == 0, "Error sending out message") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 209 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 210 | (of_port, pkt, pkt_time) = self.dataplane.poll(timeout=1) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 211 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 212 | self.assertTrue(pkt is not None, 'Packet not received') |
| 213 | basic_logger.info("PacketOut: got pkt from " + str(of_port)) |
| 214 | if of_port is not None: |
| 215 | self.assertEqual(of_port, dp_port, "Unexpected receive port") |
| 216 | self.assertEqual(str(outpkt), str(pkt), |
| 217 | 'Response packet does not match send packet') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 218 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 219 | class FlowStatsGet(SimpleProtocol): |
| 220 | """ |
| 221 | Get stats |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 222 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 223 | Simply verify stats get transaction |
| 224 | """ |
| 225 | def runTest(self): |
| 226 | basic_logger.info("Running StatsGet") |
| 227 | request = message.flow_stats_request() |
| 228 | request.out_port = ofp.OFPP_NONE |
| 229 | request.match.wildcards = ofp.OFPFW_ALL |
| 230 | response, pkt = self.controller.transact(request, timeout=2) |
| 231 | self.assertTrue(response is not None, "Did not get response") |
| 232 | |
| 233 | class FlowMod(SimpleProtocol): |
| 234 | """ |
| 235 | Insert a flow |
| 236 | |
| 237 | Simple verification of a flow mod transaction |
| 238 | """ |
| 239 | |
| 240 | def runTest(self): |
| 241 | basic_logger.info("Running " + str(self)) |
| 242 | request = message.flow_mod() |
| 243 | of_ports = basic_port_map.keys() |
| 244 | of_ports.sort() |
| 245 | request.out_port = of_ports[0] |
| 246 | request.match.wildcards = ofp.OFPFW_ALL |
| 247 | request.buffer_id = 0xffffffff |
| 248 | rv = self.controller.message_send(request) |
| 249 | self.assertTrue(rv != -1, "Did not get response") |
| 250 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 251 | if __name__ == "__main__": |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 252 | print "Please run through oft script: ./oft --test_spec=basic" |