blob: e4b933ee6774ceb2de92f8d1856afabb83fec215 [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 Talaycodba244e2010-02-15 14:08:53 -080010 The switch is actively attempting to contact the controller at the address
Rich Lane477f4812012-10-04 22:49:00 -070011indicated in oftest.config.
Dan Talaycodba244e2010-02-15 14:08:53 -080012
13"""
14
Dan Talaycodba244e2010-02-15 14:08:53 -080015import time
16import sys
Dan Talayco48370102010-03-03 15:17:33 -080017import logging
Dan Talaycodba244e2010-02-15 14:08:53 -080018
Dan Talayco2c0dba32010-03-06 22:47:06 -080019import unittest
Ken Chiang1bf01602012-04-04 10:48:23 -070020import random
Dan Talayco2c0dba32010-03-06 22:47:06 -080021
Rich Lane477f4812012-10-04 22:49:00 -070022from oftest import config
Dan Talayco2c0dba32010-03-06 22:47:06 -080023import oftest.controller as controller
24import oftest.cstruct as ofp
25import oftest.message as message
26import oftest.dataplane as dataplane
27import oftest.action as action
Rich Laneb90a1c42012-10-05 09:16:05 -070028import oftest.base_tests as base_tests
Dan Talayco2c0dba32010-03-06 22:47:06 -080029
Dan Talaycoe605b1b2012-09-18 06:56:20 -070030import oftest.illegal_message as illegal_message
31
Rich Laneda3b5ad2012-10-03 09:05:32 -070032from oftest.testutils import *
Dan Talayco6ce963a2010-03-07 21:58:13 -080033
Ken Chiangaeb23d62012-08-23 21:20:07 -070034TEST_VID_DEFAULT = 2
35
Rich Laneb90a1c42012-10-05 09:16:05 -070036class Echo(base_tests.SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -080037 """
38 Test echo response with no data
39 """
40 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -080041 request = message.echo_request()
Dan Talaycoe226eb12010-02-18 23:06:30 -080042 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -070043 self.assertTrue(response is not None,
44 "Did not get echo reply")
Dan Talayco2c0dba32010-03-06 22:47:06 -080045 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -080046 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -080047 self.assertEqual(request.header.xid, response.header.xid,
48 'response xid != request xid')
49 self.assertEqual(len(response.data), 0, 'response data non-empty')
50
Rich Laneb90a1c42012-10-05 09:16:05 -070051class EchoWithData(base_tests.SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -080052 """
53 Test echo response with short string data
54 """
55 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -080056 request = message.echo_request()
Dan Talaycodba244e2010-02-15 14:08:53 -080057 request.data = 'OpenFlow Will Rule The World'
Dan Talaycoe226eb12010-02-18 23:06:30 -080058 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -070059 self.assertTrue(response is not None,
60 "Did not get echo reply (with data)")
Dan Talayco2c0dba32010-03-06 22:47:06 -080061 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -080062 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -080063 self.assertEqual(request.header.xid, response.header.xid,
64 'response xid != request xid')
65 self.assertEqual(request.data, response.data,
66 'response data does not match request')
67
Rich Laneb90a1c42012-10-05 09:16:05 -070068class PacketIn(base_tests.SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -080069 """
70 Test packet in function
Dan Talayco6ce963a2010-03-07 21:58:13 -080071
72 Send a packet to each dataplane port and verify that a packet
73 in message is received from the controller for each
Dan Talaycodba244e2010-02-15 14:08:53 -080074 """
75 def runTest(self):
76 # Construct packet to send to dataplane
Dan Talaycoe226eb12010-02-18 23:06:30 -080077 # Send packet to dataplane, once to each port
Dan Talaycodba244e2010-02-15 14:08:53 -080078 # Poll controller with expect message type packet in
Dan Talaycoe226eb12010-02-18 23:06:30 -080079
Rich Lane9a003812012-10-04 17:17:59 -070080 rc = delete_all_flows(self.controller)
Dan Talayco6ce963a2010-03-07 21:58:13 -080081 self.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talayco0fc08bd2012-04-09 16:56:18 -070082 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
Dan Talayco6ce963a2010-03-07 21:58:13 -080083
Rich Lane2014f9b2012-10-05 15:29:40 -070084 vid = test_param_get('vid', default=TEST_VID_DEFAULT)
Ken Chiangaeb23d62012-08-23 21:20:07 -070085
Rich Lane477f4812012-10-04 22:49:00 -070086 for of_port in config["port_map"].keys():
Ed Swierk0aeff8c2012-03-23 20:27:18 -070087 for pkt, pt in [
88 (simple_tcp_packet(), "simple TCP packet"),
Ken Chiangaeb23d62012-08-23 21:20:07 -070089 (simple_tcp_packet(dl_vlan_enable=True,dl_vlan=vid,pktlen=108),
90 "simple tagged TCP packet"),
Ed Swierk0aeff8c2012-03-23 20:27:18 -070091 (simple_eth_packet(), "simple Ethernet packet"),
92 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
Dan Talaycodba244e2010-02-15 14:08:53 -080093
Rich Lane9a003812012-10-04 17:17:59 -070094 logging.info("PKT IN test with %s, port %s" % (pt, of_port))
Ed Swierk0aeff8c2012-03-23 20:27:18 -070095 self.dataplane.send(of_port, str(pkt))
96 #@todo Check for unexpected messages?
97 count = 0
98 while True:
Dan Talaycoc689a792012-09-28 14:22:53 -070099 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN)
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700100 if not response: # Timeout
101 break
Ed Swierk506614a2012-03-29 08:16:59 -0700102 if dataplane.match_exp_pkt(pkt, response.data): # Got match
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700103 break
Rich Lane477f4812012-10-04 22:49:00 -0700104 if not config["relax"]: # Only one attempt to match
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700105 break
106 count += 1
107 if count > 10: # Too many tries
108 break
Dan Talayco48370102010-03-03 15:17:33 -0800109
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700110 self.assertTrue(response is not None,
111 'Packet in message not received on port ' +
112 str(of_port))
Ed Swierk506614a2012-03-29 08:16:59 -0700113 if not dataplane.match_exp_pkt(pkt, response.data):
Rich Lane9a003812012-10-04 17:17:59 -0700114 logging.debug("Sent %s" % format_packet(pkt))
115 logging.debug("Resp %s" % format_packet(response.data))
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700116 self.assertTrue(False,
117 'Response packet does not match send packet' +
118 ' for port ' + str(of_port))
Dan Talaycodba244e2010-02-15 14:08:53 -0800119
Rich Laneb90a1c42012-10-05 09:16:05 -0700120class PacketInDefaultDrop(base_tests.SimpleDataPlane):
Ed Swierk3ae7f712012-08-22 06:45:25 -0700121 """
122 Test packet in function
123
124 Send a packet to each dataplane port and verify that a packet
125 in message is received from the controller for each
126 """
Rich Laned1d9c282012-10-04 22:07:10 -0700127
128 priority = -1
129
Ed Swierk3ae7f712012-08-22 06:45:25 -0700130 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700131 rc = delete_all_flows(self.controller)
Ed Swierk3ae7f712012-08-22 06:45:25 -0700132 self.assertEqual(rc, 0, "Failed to delete all flows")
133 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
134
Rich Lane477f4812012-10-04 22:49:00 -0700135 for of_port in config["port_map"].keys():
Ed Swierk3ae7f712012-08-22 06:45:25 -0700136 pkt = simple_tcp_packet()
137 self.dataplane.send(of_port, str(pkt))
138 count = 0
139 while True:
Dan Talaycoc689a792012-09-28 14:22:53 -0700140 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN)
Ed Swierk3ae7f712012-08-22 06:45:25 -0700141 if not response: # Timeout
142 break
143 if dataplane.match_exp_pkt(pkt, response.data): # Got match
144 break
Rich Lane477f4812012-10-04 22:49:00 -0700145 if not config["relax"]: # Only one attempt to match
Ed Swierk3ae7f712012-08-22 06:45:25 -0700146 break
147 count += 1
148 if count > 10: # Too many tries
149 break
150
151 self.assertTrue(response is None,
152 'Packet in message received on port ' +
153 str(of_port))
154
Rich Laneb90a1c42012-10-05 09:16:05 -0700155class PacketInBroadcastCheck(base_tests.SimpleDataPlane):
Dan Talayco1f648cb2012-05-03 09:37:56 -0700156 """
157 Check if bcast pkts leak when no flows are present
158
159 Clear the flow table
160 Send in a broadcast pkt
161 Look for the packet on other dataplane ports.
162 """
Rich Laned1d9c282012-10-04 22:07:10 -0700163
164 priority = -1
165
Dan Talayco1f648cb2012-05-03 09:37:56 -0700166 def runTest(self):
167 # Need at least two ports
Rich Lane477f4812012-10-04 22:49:00 -0700168 self.assertTrue(len(config["port_map"]) > 1, "Too few ports for test")
Dan Talayco1f648cb2012-05-03 09:37:56 -0700169
Rich Lane9a003812012-10-04 17:17:59 -0700170 rc = delete_all_flows(self.controller)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700171 self.assertEqual(rc, 0, "Failed to delete all flows")
172 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
173
Rich Lane477f4812012-10-04 22:49:00 -0700174 of_ports = config["port_map"].keys()
Dan Talayco1f648cb2012-05-03 09:37:56 -0700175 d_port = of_ports[0]
176 pkt = simple_eth_packet(dl_dst='ff:ff:ff:ff:ff:ff')
177
Rich Lane9a003812012-10-04 17:17:59 -0700178 logging.info("BCast Leak Test, send to port %s" % d_port)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700179 self.dataplane.send(d_port, str(pkt))
180
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700181 (of_port, pkt_in, pkt_time) = self.dataplane.poll(exp_pkt=pkt)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700182 self.assertTrue(pkt_in is None,
183 'BCast packet received on port ' + str(of_port))
184
Rich Laneb90a1c42012-10-05 09:16:05 -0700185class PacketOut(base_tests.SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800186 """
187 Test packet out function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800188
189 Send packet out message to controller for each dataplane port and
190 verify the packet appears on the appropriate dataplane port
Dan Talaycodba244e2010-02-15 14:08:53 -0800191 """
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 Talayco41eae8b2010-03-10 13:57:06 -0800196
Rich Lane9a003812012-10-04 17:17:59 -0700197 rc = delete_all_flows(self.controller)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800198 self.assertEqual(rc, 0, "Failed to delete all flows")
Dan Talaycodba244e2010-02-15 14:08:53 -0800199
200 # These will get put into function
Rich Lane477f4812012-10-04 22:49:00 -0700201 of_ports = config["port_map"].keys()
Dan Talayco48370102010-03-03 15:17:33 -0800202 of_ports.sort()
203 for dp_port in of_ports:
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700204 for outpkt, opt in [
205 (simple_tcp_packet(), "simple TCP packet"),
206 (simple_eth_packet(), "simple Ethernet packet"),
207 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
Dan Talaycodba244e2010-02-15 14:08:53 -0800208
Rich Lane9a003812012-10-04 17:17:59 -0700209 logging.info("PKT OUT test with %s, port %s" % (opt, dp_port))
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700210 msg = message.packet_out()
211 msg.data = str(outpkt)
212 act = action.action_output()
213 act.port = dp_port
214 self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
Dan Talaycodba244e2010-02-15 14:08:53 -0800215
Rich Lane9a003812012-10-04 17:17:59 -0700216 logging.info("PacketOut to: " + str(dp_port))
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700217 rv = self.controller.message_send(msg)
218 self.assertTrue(rv == 0, "Error sending out message")
Dan Talaycodba244e2010-02-15 14:08:53 -0800219
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700220 exp_pkt_arg = None
221 exp_port = None
Rich Lane477f4812012-10-04 22:49:00 -0700222 if config["relax"]:
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700223 exp_pkt_arg = outpkt
224 exp_port = dp_port
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700225 (of_port, pkt, pkt_time) = self.dataplane.poll(port_number=exp_port,
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700226 exp_pkt=exp_pkt_arg)
227
228 self.assertTrue(pkt is not None, 'Packet not received')
Rich Lane9a003812012-10-04 17:17:59 -0700229 logging.info("PacketOut: got pkt from " + str(of_port))
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700230 if of_port is not None:
231 self.assertEqual(of_port, dp_port, "Unexpected receive port")
Ed Swierk506614a2012-03-29 08:16:59 -0700232 if not dataplane.match_exp_pkt(outpkt, pkt):
Rich Lane9a003812012-10-04 17:17:59 -0700233 logging.debug("Sent %s" % format_packet(outpkt))
234 logging.debug("Resp %s" % format_packet(
Dan Talayco2baf8b52012-03-30 09:55:42 -0700235 str(pkt)[:len(str(outpkt))]))
Dan Talaycodc6fca32012-03-30 10:05:49 -0700236 self.assertEqual(str(outpkt), str(pkt)[:len(str(outpkt))],
237 'Response packet does not match send packet')
Dan Talaycodba244e2010-02-15 14:08:53 -0800238
Rich Laneb90a1c42012-10-05 09:16:05 -0700239class PacketOutMC(base_tests.SimpleDataPlane):
Ken Chiang1bf01602012-04-04 10:48:23 -0700240 """
241 Test packet out to multiple output ports
242
243 Send packet out message to controller for 1 to N dataplane ports and
244 verify the packet appears on the appropriate ports
245 """
246 def runTest(self):
247 # Construct packet to send to dataplane
248 # Send packet to dataplane
249 # Poll controller with expect message type packet in
250
Rich Lane9a003812012-10-04 17:17:59 -0700251 rc = delete_all_flows(self.controller)
Ken Chiang1bf01602012-04-04 10:48:23 -0700252 self.assertEqual(rc, 0, "Failed to delete all flows")
253
254 # These will get put into function
Rich Lane477f4812012-10-04 22:49:00 -0700255 of_ports = config["port_map"].keys()
Ken Chiang1bf01602012-04-04 10:48:23 -0700256 random.shuffle(of_ports)
257 for num_ports in range(1,len(of_ports)+1):
258 for outpkt, opt in [
259 (simple_tcp_packet(), "simple TCP packet"),
260 (simple_eth_packet(), "simple Ethernet packet"),
261 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
262
263 dp_ports = of_ports[0:num_ports]
Rich Lane9a003812012-10-04 17:17:59 -0700264 logging.info("PKT OUT test with " + opt +
Ken Chiang1bf01602012-04-04 10:48:23 -0700265 ", ports " + str(dp_ports))
266 msg = message.packet_out()
267 msg.data = str(outpkt)
268 act = action.action_output()
269 for i in range(0,num_ports):
270 act.port = dp_ports[i]
271 self.assertTrue(msg.actions.add(act),
272 'Could not add action to msg')
273
Rich Lane9a003812012-10-04 17:17:59 -0700274 logging.info("PacketOut to: " + str(dp_ports))
Ken Chiang1bf01602012-04-04 10:48:23 -0700275 rv = self.controller.message_send(msg)
276 self.assertTrue(rv == 0, "Error sending out message")
277
278 receive_pkt_check(self.dataplane, outpkt, dp_ports,
279 set(of_ports).difference(dp_ports),
Rich Lane2014f9b2012-10-05 15:29:40 -0700280 self)
Ken Chiang1bf01602012-04-04 10:48:23 -0700281
Rich Laneb90a1c42012-10-05 09:16:05 -0700282class FlowStatsGet(base_tests.SimpleProtocol):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800283 """
284 Get stats
Dan Talayco2c0dba32010-03-06 22:47:06 -0800285
Dan Talayco6ce963a2010-03-07 21:58:13 -0800286 Simply verify stats get transaction
287 """
Rich Laned1d9c282012-10-04 22:07:10 -0700288
289 priority = -1
290
Dan Talayco6ce963a2010-03-07 21:58:13 -0800291 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700292 logging.info("Running StatsGet")
293 logging.info("Inserting trial flow")
Rich Lane477f4812012-10-04 22:49:00 -0700294 request = flow_mod_gen(config["port_map"], True)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800295 rv = self.controller.message_send(request)
296 self.assertTrue(rv != -1, "Failed to insert test flow")
297
Rich Lane9a003812012-10-04 17:17:59 -0700298 logging.info("Sending flow request")
Dan Talayco6ce963a2010-03-07 21:58:13 -0800299 request = message.flow_stats_request()
300 request.out_port = ofp.OFPP_NONE
Dan Talayco41eae8b2010-03-10 13:57:06 -0800301 request.table_id = 0xff
302 request.match.wildcards = 0 # ofp.OFPFW_ALL
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700303 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700304 self.assertTrue(response is not None,
305 "Did not get response for flow stats")
Rich Lane9a003812012-10-04 17:17:59 -0700306 logging.debug(response.show())
Dan Talayco6ce963a2010-03-07 21:58:13 -0800307
Rich Laneb90a1c42012-10-05 09:16:05 -0700308class TableStatsGet(base_tests.SimpleProtocol):
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700309 """
310 Get table stats
311
312 Simply verify table stats get transaction
313 """
314 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700315 logging.info("Running TableStatsGet")
316 logging.info("Inserting trial flow")
Rich Lane477f4812012-10-04 22:49:00 -0700317 request = flow_mod_gen(config["port_map"], True)
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700318 rv = self.controller.message_send(request)
319 self.assertTrue(rv != -1, "Failed to insert test flow")
320
Rich Lane9a003812012-10-04 17:17:59 -0700321 logging.info("Sending table stats request")
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700322 request = message.table_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700323 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700324 self.assertTrue(response is not None,
325 "Did not get reply for table stats")
Rich Lane9a003812012-10-04 17:17:59 -0700326 logging.debug(response.show())
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700327
Rich Laneb90a1c42012-10-05 09:16:05 -0700328class DescStatsGet(base_tests.SimpleProtocol):
Ed Swierkae74c362012-04-02 08:21:41 -0700329 """
330 Get stats
331
332 Simply verify stats get transaction
333 """
334 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700335 logging.info("Running DescStatsGet")
Ed Swierkae74c362012-04-02 08:21:41 -0700336
Rich Lane9a003812012-10-04 17:17:59 -0700337 logging.info("Sending stats request")
Ed Swierkae74c362012-04-02 08:21:41 -0700338 request = message.desc_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700339 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700340 self.assertTrue(response is not None,
341 "Did not get reply for desc stats")
Rich Lane9a003812012-10-04 17:17:59 -0700342 logging.debug(response.show())
Ed Swierkae74c362012-04-02 08:21:41 -0700343
Rich Laneb90a1c42012-10-05 09:16:05 -0700344class FlowMod(base_tests.SimpleProtocol):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800345 """
346 Insert a flow
347
348 Simple verification of a flow mod transaction
349 """
350
351 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700352 logging.info("Running " + str(self))
Rich Lane477f4812012-10-04 22:49:00 -0700353 request = flow_mod_gen(config["port_map"], True)
Dan Talayco6ce963a2010-03-07 21:58:13 -0800354 rv = self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800355 self.assertTrue(rv != -1, "Error installing flow mod")
356
Rich Laneb90a1c42012-10-05 09:16:05 -0700357class PortConfigMod(base_tests.SimpleProtocol):
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700358 """
359 Modify a bit in port config and verify changed
360
361 Get the switch configuration, modify the port configuration
362 and write it back; get the config again and verify changed.
363 Then set it back to the way it was.
364 """
365
366 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700367 logging.info("Running " + str(self))
Rich Lane477f4812012-10-04 22:49:00 -0700368 for of_port, ifname in config["port_map"].items(): # Grab first port
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700369 break
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700370
Rich Lane477f4812012-10-04 22:49:00 -0700371 (hw_addr, port_config, advert) = \
Rich Lane9a003812012-10-04 17:17:59 -0700372 port_config_get(self.controller, of_port)
Rich Lane477f4812012-10-04 22:49:00 -0700373 self.assertTrue(port_config is not None, "Did not get port config")
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700374
Rich Lane9a003812012-10-04 17:17:59 -0700375 logging.debug("No flood bit port " + str(of_port) + " is now " +
Rich Lane477f4812012-10-04 22:49:00 -0700376 str(port_config & ofp.OFPPC_NO_FLOOD))
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700377
378 rv = port_config_set(self.controller, of_port,
Rich Lane477f4812012-10-04 22:49:00 -0700379 port_config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700380 self.assertTrue(rv != -1, "Error sending port mod")
Rich Lanebbd4cf92012-10-11 23:44:42 -0700381 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700382
383 # Verify change took place with same feature request
Rich Lane477f4812012-10-04 22:49:00 -0700384 (hw_addr, port_config2, advert) = \
Rich Lane9a003812012-10-04 17:17:59 -0700385 port_config_get(self.controller, of_port)
386 logging.debug("No flood bit port " + str(of_port) + " is now " +
Rich Lane477f4812012-10-04 22:49:00 -0700387 str(port_config2 & ofp.OFPPC_NO_FLOOD))
388 self.assertTrue(port_config2 is not None, "Did not get port config2")
389 self.assertTrue(port_config2 & ofp.OFPPC_NO_FLOOD !=
390 port_config & ofp.OFPPC_NO_FLOOD,
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700391 "Bit change did not take")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700392 # Set it back
Rich Lane477f4812012-10-04 22:49:00 -0700393 rv = port_config_set(self.controller, of_port, port_config,
Rich Lane9a003812012-10-04 17:17:59 -0700394 ofp.OFPPC_NO_FLOOD)
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700395 self.assertTrue(rv != -1, "Error sending port mod")
Rich Lanebbd4cf92012-10-11 23:44:42 -0700396 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700397
Rich Laneb90a1c42012-10-05 09:16:05 -0700398class PortConfigModErr(base_tests.SimpleProtocol):
Ken Chiangaeb23d62012-08-23 21:20:07 -0700399 """
400 Modify a bit in port config on an invalid port and verify
401 error message is received.
402 """
403
404 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700405 logging.info("Running " + str(self))
Ken Chiangaeb23d62012-08-23 21:20:07 -0700406
407 # pick a random bad port number
408 bad_port = random.randint(1, ofp.OFPP_MAX)
409 count = 0
Rich Lane477f4812012-10-04 22:49:00 -0700410 while (count < 50) and (bad_port in config["port_map"].keys()):
Ken Chiangaeb23d62012-08-23 21:20:07 -0700411 bad_port = random.randint(1, ofp.OFPP_MAX)
412 count = count + 1
413 self.assertTrue(count < 50, "Error selecting bad port")
Rich Lane9a003812012-10-04 17:17:59 -0700414 logging.info("Select " + str(bad_port) + " as invalid port")
Ken Chiangaeb23d62012-08-23 21:20:07 -0700415
416 rv = port_config_set(self.controller, bad_port,
Rich Lane9a003812012-10-04 17:17:59 -0700417 ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD)
Ken Chiangaeb23d62012-08-23 21:20:07 -0700418 self.assertTrue(rv != -1, "Error sending port mod")
419
420 # poll for error message
421 while True:
Dan Talaycoc689a792012-09-28 14:22:53 -0700422 (response, raw) = self.controller.poll(ofp.OFPT_ERROR)
Ken Chiangaeb23d62012-08-23 21:20:07 -0700423 if not response: # Timeout
424 break
425 if response.code == ofp.OFPPMFC_BAD_PORT:
Rich Lane9a003812012-10-04 17:17:59 -0700426 logging.info("Received error message with OFPPMFC_BAD_PORT code")
Ken Chiangaeb23d62012-08-23 21:20:07 -0700427 break
Rich Lane477f4812012-10-04 22:49:00 -0700428 if not config["relax"]: # Only one attempt to match
Ken Chiangaeb23d62012-08-23 21:20:07 -0700429 break
430 count += 1
431 if count > 10: # Too many tries
432 break
433
434 self.assertTrue(response is not None, 'Did not receive error message')
435
Rich Laneb90a1c42012-10-05 09:16:05 -0700436class BadMessage(base_tests.SimpleProtocol):
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700437 """
438 Send a message with a bad type and verify an error is returned
439 """
440
441 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700442 logging.info("Running " + str(self))
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700443 request = illegal_message.illegal_message_type()
444
Dan Talaycoc689a792012-09-28 14:22:53 -0700445 reply, pkt = self.controller.transact(request)
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700446 self.assertTrue(reply is not None, "Did not get response to bad req")
447 self.assertTrue(reply.header.type == ofp.OFPT_ERROR,
448 "reply not an error message")
449 self.assertTrue(reply.type == ofp.OFPET_BAD_REQUEST,
450 "reply error type is not bad request")
451 self.assertTrue(reply.code == ofp.OFPBRC_BAD_TYPE,
452 "reply error code is not bad type")
453
Dan Talaycodba244e2010-02-15 14:08:53 -0800454if __name__ == "__main__":
Dan Talayco2c0dba32010-03-06 22:47:06 -0800455 print "Please run through oft script: ./oft --test_spec=basic"