blob: 0e3986c58c4e09bce12c3e0e62f92230769af222 [file] [log] [blame]
Dan Talaycodba244e2010-02-15 14:08:53 -08001"""
Dan Talayco79f36082010-03-11 16:53:53 -08002Basic protocol and dataplane test cases
Dan Talaycodba244e2010-02-15 14:08:53 -08003
Dan Talayco48370102010-03-03 15:17:33 -08004It is recommended that these definitions be kept in their own
5namespace as different groups of tests will likely define
6similar identifiers.
7
Dan Talaycodba244e2010-02-15 14:08:53 -08008Current Assumptions:
9
Dan Talayco41eae8b2010-03-10 13:57:06 -080010 The function test_set_init is called with a complete configuration
11dictionary prior to the invocation of any tests from this file.
12
Dan Talaycodba244e2010-02-15 14:08:53 -080013 The switch is actively attempting to contact the controller at the address
14indicated oin oft_config
15
16"""
17
Dan Talaycodba244e2010-02-15 14:08:53 -080018import time
Dan Talayco710438c2010-02-18 15:16:07 -080019import signal
Dan Talaycodba244e2010-02-15 14:08:53 -080020import sys
Dan Talayco48370102010-03-03 15:17:33 -080021import logging
Dan Talaycodba244e2010-02-15 14:08:53 -080022
Dan Talayco2c0dba32010-03-06 22:47:06 -080023import unittest
24
25import oftest.controller as controller
26import oftest.cstruct as ofp
27import oftest.message as message
28import oftest.dataplane as dataplane
29import oftest.action as action
30
Dan Talayco6ce963a2010-03-07 21:58:13 -080031from testutils import *
32
33#@var basic_port_map Local copy of the configuration map from OF port
34# numbers to OS interfaces
Dan Talayco48370102010-03-03 15:17:33 -080035basic_port_map = None
Dan Talayco6ce963a2010-03-07 21:58:13 -080036#@var basic_logger Local logger object
Dan Talayco48370102010-03-03 15:17:33 -080037basic_logger = None
Dan Talayco6ce963a2010-03-07 21:58:13 -080038#@var basic_config Local copy of global configuration data
Dan Talayco48370102010-03-03 15:17:33 -080039basic_config = None
40
41def test_set_init(config):
42 """
43 Set up function for basic test classes
44
45 @param config The configuration dictionary; see oft
Dan Talayco48370102010-03-03 15:17:33 -080046 """
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 Talayco48370102010-03-03 15:17:33 -080056
Dan Talayco6ce963a2010-03-07 21:58:13 -080057class SimpleProtocol(unittest.TestCase):
Dan Talaycodba244e2010-02-15 14:08:53 -080058 """
59 Root class for setting up the controller
60 """
61
Dan Talayco710438c2010-02-18 15:16:07 -080062 def sig_handler(self):
Dan Talayco48370102010-03-03 15:17:33 -080063 basic_logger.critical("Received interrupt signal; exiting")
Dan Talayco710438c2010-02-18 15:16:07 -080064 print "Received interrupt signal; exiting"
Dan Talayco2c0dba32010-03-06 22:47:06 -080065 self.clean_shutdown = False
66 self.tearDown()
Dan Talayco710438c2010-02-18 15:16:07 -080067 sys.exit(1)
68
Dan Talaycodba244e2010-02-15 14:08:53 -080069 def setUp(self):
Dan Talayco710438c2010-02-18 15:16:07 -080070 signal.signal(signal.SIGINT, self.sig_handler)
Dan Talayco48370102010-03-03 15:17:33 -080071 basic_logger.info("Setup for " + str(self))
Dan Talayco2c0dba32010-03-06 22:47:06 -080072 self.controller = controller.Controller(
73 host=basic_config["controller_host"],
74 port=basic_config["controller_port"])
Dan Talaycof8f41402010-03-12 22:17:39 -080075 # clean_shutdown should be set to False to force quit app
Dan Talayco2c0dba32010-03-06 22:47:06 -080076 self.clean_shutdown = True
Dan Talayco710438c2010-02-18 15:16:07 -080077 self.controller.start()
78 self.controller.connect(timeout=20)
Dan Talayco48370102010-03-03 15:17:33 -080079 basic_logger.info("Connected " + str(self.controller.switch_addr))
Dan Talaycodba244e2010-02-15 14:08:53 -080080
81 def tearDown(self):
Dan Talayco48370102010-03-03 15:17:33 -080082 basic_logger.info("Teardown for simple proto test")
Dan Talaycodba244e2010-02-15 14:08:53 -080083 self.controller.shutdown()
Dan Talayco2c0dba32010-03-06 22:47:06 -080084 #@todo Review if join should be done on clean_shutdown
Dan Talaycof8f41402010-03-12 22:17:39 -080085 if self.clean_shutdown:
86 self.controller.join()
Dan Talaycodba244e2010-02-15 14:08:53 -080087
88 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -080089 # Just a simple sanity check as illustration
Dan Talayco48370102010-03-03 15:17:33 -080090 basic_logger.info("Running simple proto test")
Dan Talayco710438c2010-02-18 15:16:07 -080091 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -080092 str(self) + 'No connection to switch')
93
Dan Talayco6ce963a2010-03-07 21:58:13 -080094class SimpleDataPlane(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -080095 """
96 Root class that sets up the controller and dataplane
97 """
98 def setUp(self):
Dan Talayco6ce963a2010-03-07 21:58:13 -080099 SimpleProtocol.setUp(self)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800100 self.dataplane = dataplane.DataPlane()
Dan Talayco48370102010-03-03 15:17:33 -0800101 for of_port, ifname in basic_port_map.items():
Dan Talaycodba244e2010-02-15 14:08:53 -0800102 self.dataplane.port_add(ifname, of_port)
103
104 def tearDown(self):
Dan Talayco48370102010-03-03 15:17:33 -0800105 basic_logger.info("Teardown for simple dataplane test")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800106 SimpleProtocol.tearDown(self)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800107 self.dataplane.kill(join_threads=self.clean_shutdown)
Dan Talayco48370102010-03-03 15:17:33 -0800108 basic_logger.info("Teardown done")
Dan Talaycodba244e2010-02-15 14:08:53 -0800109
110 def runTest(self):
Dan Talayco710438c2010-02-18 15:16:07 -0800111 self.assertTrue(self.controller.switch_socket is not None,
Dan Talaycodba244e2010-02-15 14:08:53 -0800112 str(self) + 'No connection to switch')
113 # self.dataplane.show()
114 # Would like an assert that checks the data plane
115
Dan Talayco6ce963a2010-03-07 21:58:13 -0800116class Echo(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800117 """
118 Test echo response with no data
119 """
120 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800121 request = message.echo_request()
Dan Talaycoe226eb12010-02-18 23:06:30 -0800122 response, pkt = self.controller.transact(request)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800123 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800124 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800125 self.assertEqual(request.header.xid, response.header.xid,
126 'response xid != request xid')
127 self.assertEqual(len(response.data), 0, 'response data non-empty')
128
Dan Talayco6ce963a2010-03-07 21:58:13 -0800129class EchoWithData(SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -0800130 """
131 Test echo response with short string data
132 """
133 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -0800134 request = message.echo_request()
Dan Talaycodba244e2010-02-15 14:08:53 -0800135 request.data = 'OpenFlow Will Rule The World'
Dan Talaycoe226eb12010-02-18 23:06:30 -0800136 response, pkt = self.controller.transact(request)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800137 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -0800138 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -0800139 self.assertEqual(request.header.xid, response.header.xid,
140 'response xid != request xid')
141 self.assertEqual(request.data, response.data,
142 'response data does not match request')
143
Dan Talayco6ce963a2010-03-07 21:58:13 -0800144class PacketIn(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800145 """
146 Test packet in function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800147
148 Send a packet to each dataplane port and verify that a packet
149 in message is received from the controller for each
Dan Talaycodba244e2010-02-15 14:08:53 -0800150 """
151 def runTest(self):
152 # Construct packet to send to dataplane
Dan Talaycoe226eb12010-02-18 23:06:30 -0800153 # Send packet to dataplane, once to each port
Dan Talaycodba244e2010-02-15 14:08:53 -0800154 # Poll controller with expect message type packet in
Dan Talaycoe226eb12010-02-18 23:06:30 -0800155
Dan Talayco6ce963a2010-03-07 21:58:13 -0800156 rc = delete_all_flows(self.controller, basic_logger)
157 self.assertEqual(rc, 0, "Failed to delete all flows")
158
Dan Talayco48370102010-03-03 15:17:33 -0800159 for of_port in basic_port_map.keys():
160 basic_logger.info("PKT IN test, port " + str(of_port))
Dan Talayco41eae8b2010-03-10 13:57:06 -0800161 pkt = simple_tcp_packet()
Dan Talaycodba244e2010-02-15 14:08:53 -0800162 self.dataplane.send(of_port, str(pkt))
Dan Talaycoe226eb12010-02-18 23:06:30 -0800163 #@todo Check for unexpected messages?
Dan Talayco2c0dba32010-03-06 22:47:06 -0800164 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN, 2)
Dan Talaycodba244e2010-02-15 14:08:53 -0800165
Dan Talaycoe226eb12010-02-18 23:06:30 -0800166 self.assertTrue(response is not None,
Dan Talayco6ce963a2010-03-07 21:58:13 -0800167 'Packet in message not received on port ' +
168 str(of_port))
Dan Talayco48370102010-03-03 15:17:33 -0800169 if str(pkt) != response.data:
170 basic_logger.debug("pkt: "+str(pkt)+" resp: " +
171 str(response))
172
173 self.assertEqual(str(pkt), response.data,
Dan Talayco6ce963a2010-03-07 21:58:13 -0800174 'Response packet does not match send packet' +
175 ' for port ' + str(of_port))
Dan Talaycodba244e2010-02-15 14:08:53 -0800176
Dan Talayco6ce963a2010-03-07 21:58:13 -0800177class PacketOut(SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800178 """
179 Test packet out function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800180
181 Send packet out message to controller for each dataplane port and
182 verify the packet appears on the appropriate dataplane port
Dan Talaycodba244e2010-02-15 14:08:53 -0800183 """
184 def runTest(self):
185 # Construct packet to send to dataplane
186 # Send packet to dataplane
187 # Poll controller with expect message type packet in
Dan Talayco41eae8b2010-03-10 13:57:06 -0800188
189 rc = delete_all_flows(self.controller, basic_logger)
190 self.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talaycodba244e2010-02-15 14:08:53 -0800191
192 # These will get put into function
Dan Talayco41eae8b2010-03-10 13:57:06 -0800193 outpkt = simple_tcp_packet()
Dan Talayco48370102010-03-03 15:17:33 -0800194 of_ports = basic_port_map.keys()
195 of_ports.sort()
196 for dp_port in of_ports:
Dan Talayco2c0dba32010-03-06 22:47:06 -0800197 msg = message.packet_out()
Dan Talayco48370102010-03-03 15:17:33 -0800198 msg.data = str(outpkt)
Dan Talayco2c0dba32010-03-06 22:47:06 -0800199 act = action.action_output()
Dan Talayco48370102010-03-03 15:17:33 -0800200 act.port = dp_port
201 self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
Dan Talaycodba244e2010-02-15 14:08:53 -0800202
Dan Talayco48370102010-03-03 15:17:33 -0800203 basic_logger.info("PacketOut to: " + str(dp_port))
204 rv = self.controller.message_send(msg)
205 self.assertTrue(rv == 0, "Error sending out message")
Dan Talaycodba244e2010-02-15 14:08:53 -0800206
Dan Talayco48370102010-03-03 15:17:33 -0800207 (of_port, pkt, pkt_time) = self.dataplane.poll(timeout=1)
Dan Talaycodba244e2010-02-15 14:08:53 -0800208
Dan Talayco48370102010-03-03 15:17:33 -0800209 self.assertTrue(pkt is not None, 'Packet not received')
210 basic_logger.info("PacketOut: got pkt from " + str(of_port))
211 if of_port is not None:
212 self.assertEqual(of_port, dp_port, "Unexpected receive port")
213 self.assertEqual(str(outpkt), str(pkt),
214 'Response packet does not match send packet')
Dan Talaycodba244e2010-02-15 14:08:53 -0800215
Dan Talayco6ce963a2010-03-07 21:58:13 -0800216class FlowStatsGet(SimpleProtocol):
217 """
218 Get stats
Dan Talayco2c0dba32010-03-06 22:47:06 -0800219
Dan Talayco6ce963a2010-03-07 21:58:13 -0800220 Simply verify stats get transaction
221 """
222 def runTest(self):
223 basic_logger.info("Running StatsGet")
Dan Talayco41eae8b2010-03-10 13:57:06 -0800224 basic_logger.info("Inserting trial flow")
225 request = message.flow_mod()
226 request.match.wildcards = ofp.OFPFW_ALL
227 request.buffer_id = 0xffffffff
228 rv = self.controller.message_send(request)
229 self.assertTrue(rv != -1, "Failed to insert test flow")
230
231 basic_logger.info("Sending flow request")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800232 request = message.flow_stats_request()
233 request.out_port = ofp.OFPP_NONE
Dan Talayco41eae8b2010-03-10 13:57:06 -0800234 request.table_id = 0xff
235 request.match.wildcards = 0 # ofp.OFPFW_ALL
Dan Talayco6ce963a2010-03-07 21:58:13 -0800236 response, pkt = self.controller.transact(request, timeout=2)
237 self.assertTrue(response is not None, "Did not get response")
Dan Talayco41eae8b2010-03-10 13:57:06 -0800238 basic_logger.info(response.show())
Dan Talayco6ce963a2010-03-07 21:58:13 -0800239
240class FlowMod(SimpleProtocol):
241 """
242 Insert a flow
243
244 Simple verification of a flow mod transaction
245 """
246
247 def runTest(self):
248 basic_logger.info("Running " + str(self))
249 request = message.flow_mod()
Dan Talayco6ce963a2010-03-07 21:58:13 -0800250 request.match.wildcards = ofp.OFPFW_ALL
251 request.buffer_id = 0xffffffff
252 rv = self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800253 self.assertTrue(rv != -1, "Error installing flow mod")
254
Dan Talaycodba244e2010-02-15 14:08:53 -0800255if __name__ == "__main__":
Dan Talayco2c0dba32010-03-06 22:47:06 -0800256 print "Please run through oft script: ./oft --test_spec=basic"