blob: 16f3b31ac861168298f01fc24816ef51f5453079 [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 Lane97e99652013-01-02 17:23:20 -080036@group('smoke')
Rich Laneb90a1c42012-10-05 09:16:05 -070037class Echo(base_tests.SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -080038 """
39 Test echo response with no data
40 """
41 def runTest(self):
Dan Talayco2c0dba32010-03-06 22:47:06 -080042 request = message.echo_request()
Dan Talaycoe226eb12010-02-18 23:06:30 -080043 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -070044 self.assertTrue(response is not None,
45 "Did not get echo reply")
Dan Talayco2c0dba32010-03-06 22:47:06 -080046 self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -080047 'response is not echo_reply')
Dan Talaycodba244e2010-02-15 14:08:53 -080048 self.assertEqual(request.header.xid, response.header.xid,
49 'response xid != request xid')
50 self.assertEqual(len(response.data), 0, 'response data non-empty')
51
Rich Laneb90a1c42012-10-05 09:16:05 -070052class EchoWithData(base_tests.SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -080053 """
54 Test echo response with short string data
55 """
56 def runTest(self):
Rich Lanec3c2ae12013-01-04 10:13:17 -080057 request = message.echo_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 Lane97e99652013-01-02 17:23:20 -080068@group('smoke')
Rich Laneb90a1c42012-10-05 09:16:05 -070069class PacketIn(base_tests.SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -080070 """
71 Test packet in function
Dan Talayco6ce963a2010-03-07 21:58:13 -080072
73 Send a packet to each dataplane port and verify that a packet
74 in message is received from the controller for each
Dan Talaycodba244e2010-02-15 14:08:53 -080075 """
76 def runTest(self):
77 # Construct packet to send to dataplane
Dan Talaycoe226eb12010-02-18 23:06:30 -080078 # Send packet to dataplane, once to each port
Dan Talaycodba244e2010-02-15 14:08:53 -080079 # Poll controller with expect message type packet in
Dan Talaycoe226eb12010-02-18 23:06:30 -080080
Rich Lane32bf9482013-01-03 17:26:30 -080081 delete_all_flows(self.controller)
Rich Lane3a261d52013-01-03 17:45:08 -080082 do_barrier(self.controller)
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 Lane0a4f6372013-01-02 14:40:22 -0800120@nonstandard
Rich Laneb90a1c42012-10-05 09:16:05 -0700121class PacketInDefaultDrop(base_tests.SimpleDataPlane):
Ed Swierk3ae7f712012-08-22 06:45:25 -0700122 """
123 Test packet in function
124
125 Send a packet to each dataplane port and verify that a packet
126 in message is received from the controller for each
127 """
Rich Laned1d9c282012-10-04 22:07:10 -0700128
Ed Swierk3ae7f712012-08-22 06:45:25 -0700129 def runTest(self):
Rich Lane32bf9482013-01-03 17:26:30 -0800130 delete_all_flows(self.controller)
Rich Lane3a261d52013-01-03 17:45:08 -0800131 do_barrier(self.controller)
Ed Swierk3ae7f712012-08-22 06:45:25 -0700132
Rich Lane477f4812012-10-04 22:49:00 -0700133 for of_port in config["port_map"].keys():
Ed Swierk3ae7f712012-08-22 06:45:25 -0700134 pkt = simple_tcp_packet()
135 self.dataplane.send(of_port, str(pkt))
136 count = 0
137 while True:
Dan Talaycoc689a792012-09-28 14:22:53 -0700138 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN)
Ed Swierk3ae7f712012-08-22 06:45:25 -0700139 if not response: # Timeout
140 break
141 if dataplane.match_exp_pkt(pkt, response.data): # Got match
142 break
Rich Lane477f4812012-10-04 22:49:00 -0700143 if not config["relax"]: # Only one attempt to match
Ed Swierk3ae7f712012-08-22 06:45:25 -0700144 break
145 count += 1
146 if count > 10: # Too many tries
147 break
148
149 self.assertTrue(response is None,
150 'Packet in message received on port ' +
151 str(of_port))
152
Rich Lane0a4f6372013-01-02 14:40:22 -0800153@nonstandard
Rich Laneb90a1c42012-10-05 09:16:05 -0700154class PacketInBroadcastCheck(base_tests.SimpleDataPlane):
Dan Talayco1f648cb2012-05-03 09:37:56 -0700155 """
156 Check if bcast pkts leak when no flows are present
157
158 Clear the flow table
159 Send in a broadcast pkt
160 Look for the packet on other dataplane ports.
161 """
Rich Laned1d9c282012-10-04 22:07:10 -0700162
Dan Talayco1f648cb2012-05-03 09:37:56 -0700163 def runTest(self):
164 # Need at least two ports
Rich Lane477f4812012-10-04 22:49:00 -0700165 self.assertTrue(len(config["port_map"]) > 1, "Too few ports for test")
Dan Talayco1f648cb2012-05-03 09:37:56 -0700166
Rich Lane32bf9482013-01-03 17:26:30 -0800167 delete_all_flows(self.controller)
Rich Lane3a261d52013-01-03 17:45:08 -0800168 do_barrier(self.controller)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700169
Rich Lane477f4812012-10-04 22:49:00 -0700170 of_ports = config["port_map"].keys()
Dan Talayco1f648cb2012-05-03 09:37:56 -0700171 d_port = of_ports[0]
172 pkt = simple_eth_packet(dl_dst='ff:ff:ff:ff:ff:ff')
173
Rich Lane9a003812012-10-04 17:17:59 -0700174 logging.info("BCast Leak Test, send to port %s" % d_port)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700175 self.dataplane.send(d_port, str(pkt))
176
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700177 (of_port, pkt_in, pkt_time) = self.dataplane.poll(exp_pkt=pkt)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700178 self.assertTrue(pkt_in is None,
179 'BCast packet received on port ' + str(of_port))
180
Rich Lane97e99652013-01-02 17:23:20 -0800181@group('smoke')
Rich Laneb90a1c42012-10-05 09:16:05 -0700182class PacketOut(base_tests.SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800183 """
184 Test packet out function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800185
186 Send packet out message to controller for each dataplane port and
187 verify the packet appears on the appropriate dataplane port
Dan Talaycodba244e2010-02-15 14:08:53 -0800188 """
189 def runTest(self):
190 # Construct packet to send to dataplane
191 # Send packet to dataplane
192 # Poll controller with expect message type packet in
Dan Talayco41eae8b2010-03-10 13:57:06 -0800193
Rich Lane32bf9482013-01-03 17:26:30 -0800194 delete_all_flows(self.controller)
Dan Talaycodba244e2010-02-15 14:08:53 -0800195
196 # These will get put into function
Rich Lane477f4812012-10-04 22:49:00 -0700197 of_ports = config["port_map"].keys()
Dan Talayco48370102010-03-03 15:17:33 -0800198 of_ports.sort()
199 for dp_port in of_ports:
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700200 for outpkt, opt in [
201 (simple_tcp_packet(), "simple TCP packet"),
202 (simple_eth_packet(), "simple Ethernet packet"),
203 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
Dan Talaycodba244e2010-02-15 14:08:53 -0800204
Rich Lane9a003812012-10-04 17:17:59 -0700205 logging.info("PKT OUT test with %s, port %s" % (opt, dp_port))
Rich Lanec3c2ae12013-01-04 10:13:17 -0800206 msg = message.packet_out(in_port=ofp.OFPP_NONE,
Rich Lane0f0adc92013-01-04 15:13:02 -0800207 data=str(outpkt),
Rich Lane95271012013-01-04 15:24:51 -0800208 actions=[action.output(port=dp_port)])
Dan Talaycodba244e2010-02-15 14:08:53 -0800209
Rich Lane9a003812012-10-04 17:17:59 -0700210 logging.info("PacketOut to: " + str(dp_port))
Rich Lane5c3151c2013-01-03 17:15:41 -0800211 self.controller.message_send(msg)
Dan Talaycodba244e2010-02-15 14:08:53 -0800212
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700213 exp_pkt_arg = None
214 exp_port = None
Rich Lane477f4812012-10-04 22:49:00 -0700215 if config["relax"]:
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700216 exp_pkt_arg = outpkt
217 exp_port = dp_port
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700218 (of_port, pkt, pkt_time) = self.dataplane.poll(port_number=exp_port,
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700219 exp_pkt=exp_pkt_arg)
220
221 self.assertTrue(pkt is not None, 'Packet not received')
Rich Lane9a003812012-10-04 17:17:59 -0700222 logging.info("PacketOut: got pkt from " + str(of_port))
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700223 if of_port is not None:
224 self.assertEqual(of_port, dp_port, "Unexpected receive port")
Ed Swierk506614a2012-03-29 08:16:59 -0700225 if not dataplane.match_exp_pkt(outpkt, pkt):
Rich Lane9a003812012-10-04 17:17:59 -0700226 logging.debug("Sent %s" % format_packet(outpkt))
227 logging.debug("Resp %s" % format_packet(
Dan Talayco2baf8b52012-03-30 09:55:42 -0700228 str(pkt)[:len(str(outpkt))]))
Dan Talaycodc6fca32012-03-30 10:05:49 -0700229 self.assertEqual(str(outpkt), str(pkt)[:len(str(outpkt))],
230 'Response packet does not match send packet')
Dan Talaycodba244e2010-02-15 14:08:53 -0800231
Rich Laneb90a1c42012-10-05 09:16:05 -0700232class PacketOutMC(base_tests.SimpleDataPlane):
Ken Chiang1bf01602012-04-04 10:48:23 -0700233 """
234 Test packet out to multiple output ports
235
236 Send packet out message to controller for 1 to N dataplane ports and
237 verify the packet appears on the appropriate ports
238 """
239 def runTest(self):
240 # Construct packet to send to dataplane
241 # Send packet to dataplane
242 # Poll controller with expect message type packet in
243
Rich Lane32bf9482013-01-03 17:26:30 -0800244 delete_all_flows(self.controller)
Ken Chiang1bf01602012-04-04 10:48:23 -0700245
246 # These will get put into function
Rich Lane477f4812012-10-04 22:49:00 -0700247 of_ports = config["port_map"].keys()
Ken Chiang1bf01602012-04-04 10:48:23 -0700248 random.shuffle(of_ports)
249 for num_ports in range(1,len(of_ports)+1):
250 for outpkt, opt in [
251 (simple_tcp_packet(), "simple TCP packet"),
252 (simple_eth_packet(), "simple Ethernet packet"),
253 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
254
255 dp_ports = of_ports[0:num_ports]
Rich Lane9a003812012-10-04 17:17:59 -0700256 logging.info("PKT OUT test with " + opt +
Ken Chiang1bf01602012-04-04 10:48:23 -0700257 ", ports " + str(dp_ports))
Rich Lane95271012013-01-04 15:24:51 -0800258 actions = [action.output(port=port) for port in dp_ports]
Rich Lanec3c2ae12013-01-04 10:13:17 -0800259 msg = message.packet_out(in_port=ofp.OFPP_NONE,
Rich Lane0f0adc92013-01-04 15:13:02 -0800260 data=str(outpkt),
261 actions=actions)
Ken Chiang1bf01602012-04-04 10:48:23 -0700262
Rich Lane9a003812012-10-04 17:17:59 -0700263 logging.info("PacketOut to: " + str(dp_ports))
Rich Lane5c3151c2013-01-03 17:15:41 -0800264 self.controller.message_send(msg)
Ken Chiang1bf01602012-04-04 10:48:23 -0700265
266 receive_pkt_check(self.dataplane, outpkt, dp_ports,
267 set(of_ports).difference(dp_ports),
Rich Lane2014f9b2012-10-05 15:29:40 -0700268 self)
Ken Chiang1bf01602012-04-04 10:48:23 -0700269
Rich Lane0a4f6372013-01-02 14:40:22 -0800270@disabled
Rich Laneb90a1c42012-10-05 09:16:05 -0700271class FlowStatsGet(base_tests.SimpleProtocol):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800272 """
273 Get stats
Dan Talayco2c0dba32010-03-06 22:47:06 -0800274
Dan Talayco6ce963a2010-03-07 21:58:13 -0800275 Simply verify stats get transaction
276 """
Rich Laned1d9c282012-10-04 22:07:10 -0700277
Dan Talayco6ce963a2010-03-07 21:58:13 -0800278 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700279 logging.info("Running StatsGet")
280 logging.info("Inserting trial flow")
Rich Lane477f4812012-10-04 22:49:00 -0700281 request = flow_mod_gen(config["port_map"], True)
Rich Lane5c3151c2013-01-03 17:15:41 -0800282 self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800283
Rich Lane9a003812012-10-04 17:17:59 -0700284 logging.info("Sending flow request")
Rich Lanec3c2ae12013-01-04 10:13:17 -0800285 request = message.flow_stats_request(out_port=ofp.OFPP_NONE,
286 table_id=0xff)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800287 request.match.wildcards = 0 # ofp.OFPFW_ALL
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700288 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700289 self.assertTrue(response is not None,
290 "Did not get response for flow stats")
Rich Lane9a003812012-10-04 17:17:59 -0700291 logging.debug(response.show())
Dan Talayco6ce963a2010-03-07 21:58:13 -0800292
Rich Laneb90a1c42012-10-05 09:16:05 -0700293class TableStatsGet(base_tests.SimpleProtocol):
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700294 """
295 Get table stats
296
297 Simply verify table stats get transaction
298 """
299 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700300 logging.info("Running TableStatsGet")
301 logging.info("Inserting trial flow")
Rich Lane477f4812012-10-04 22:49:00 -0700302 request = flow_mod_gen(config["port_map"], True)
Rich Lane5c3151c2013-01-03 17:15:41 -0800303 self.controller.message_send(request)
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700304
Rich Lane9a003812012-10-04 17:17:59 -0700305 logging.info("Sending table stats request")
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700306 request = message.table_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700307 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700308 self.assertTrue(response is not None,
309 "Did not get reply for table stats")
Rich Lane9a003812012-10-04 17:17:59 -0700310 logging.debug(response.show())
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700311
Rich Laneb90a1c42012-10-05 09:16:05 -0700312class DescStatsGet(base_tests.SimpleProtocol):
Ed Swierkae74c362012-04-02 08:21:41 -0700313 """
314 Get stats
315
316 Simply verify stats get transaction
317 """
318 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700319 logging.info("Running DescStatsGet")
Ed Swierkae74c362012-04-02 08:21:41 -0700320
Rich Lane9a003812012-10-04 17:17:59 -0700321 logging.info("Sending stats request")
Ed Swierkae74c362012-04-02 08:21:41 -0700322 request = message.desc_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 desc stats")
Rich Lane9a003812012-10-04 17:17:59 -0700326 logging.debug(response.show())
Ed Swierkae74c362012-04-02 08:21:41 -0700327
Rich Laneb90a1c42012-10-05 09:16:05 -0700328class FlowMod(base_tests.SimpleProtocol):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800329 """
330 Insert a flow
331
332 Simple verification of a flow mod transaction
333 """
334
335 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700336 logging.info("Running " + str(self))
Rich Lane477f4812012-10-04 22:49:00 -0700337 request = flow_mod_gen(config["port_map"], True)
Rich Lane5c3151c2013-01-03 17:15:41 -0800338 self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800339
Rich Lane97e99652013-01-02 17:23:20 -0800340@group('smoke')
Rich Laneb90a1c42012-10-05 09:16:05 -0700341class PortConfigMod(base_tests.SimpleProtocol):
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700342 """
343 Modify a bit in port config and verify changed
344
345 Get the switch configuration, modify the port configuration
346 and write it back; get the config again and verify changed.
347 Then set it back to the way it was.
348 """
349
350 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700351 logging.info("Running " + str(self))
Rich Lane477f4812012-10-04 22:49:00 -0700352 for of_port, ifname in config["port_map"].items(): # Grab first port
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700353 break
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700354
Rich Lane477f4812012-10-04 22:49:00 -0700355 (hw_addr, port_config, advert) = \
Rich Lane9a003812012-10-04 17:17:59 -0700356 port_config_get(self.controller, of_port)
Rich Lane477f4812012-10-04 22:49:00 -0700357 self.assertTrue(port_config is not None, "Did not get port config")
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700358
Rich Lane9a003812012-10-04 17:17:59 -0700359 logging.debug("No flood bit port " + str(of_port) + " is now " +
Rich Lane477f4812012-10-04 22:49:00 -0700360 str(port_config & ofp.OFPPC_NO_FLOOD))
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700361
362 rv = port_config_set(self.controller, of_port,
Rich Lane477f4812012-10-04 22:49:00 -0700363 port_config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700364 self.assertTrue(rv != -1, "Error sending port mod")
Rich Lane3a261d52013-01-03 17:45:08 -0800365 do_barrier(self.controller)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700366
367 # Verify change took place with same feature request
Rich Lane477f4812012-10-04 22:49:00 -0700368 (hw_addr, port_config2, advert) = \
Rich Lane9a003812012-10-04 17:17:59 -0700369 port_config_get(self.controller, of_port)
370 logging.debug("No flood bit port " + str(of_port) + " is now " +
Rich Lane477f4812012-10-04 22:49:00 -0700371 str(port_config2 & ofp.OFPPC_NO_FLOOD))
372 self.assertTrue(port_config2 is not None, "Did not get port config2")
373 self.assertTrue(port_config2 & ofp.OFPPC_NO_FLOOD !=
374 port_config & ofp.OFPPC_NO_FLOOD,
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700375 "Bit change did not take")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700376 # Set it back
Rich Lane477f4812012-10-04 22:49:00 -0700377 rv = port_config_set(self.controller, of_port, port_config,
Rich Lane9a003812012-10-04 17:17:59 -0700378 ofp.OFPPC_NO_FLOOD)
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700379 self.assertTrue(rv != -1, "Error sending port mod")
Rich Lane3a261d52013-01-03 17:45:08 -0800380 do_barrier(self.controller)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700381
Rich Laneb90a1c42012-10-05 09:16:05 -0700382class PortConfigModErr(base_tests.SimpleProtocol):
Ken Chiangaeb23d62012-08-23 21:20:07 -0700383 """
384 Modify a bit in port config on an invalid port and verify
385 error message is received.
386 """
387
388 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700389 logging.info("Running " + str(self))
Ken Chiangaeb23d62012-08-23 21:20:07 -0700390
391 # pick a random bad port number
392 bad_port = random.randint(1, ofp.OFPP_MAX)
393 count = 0
Rich Lane477f4812012-10-04 22:49:00 -0700394 while (count < 50) and (bad_port in config["port_map"].keys()):
Ken Chiangaeb23d62012-08-23 21:20:07 -0700395 bad_port = random.randint(1, ofp.OFPP_MAX)
396 count = count + 1
397 self.assertTrue(count < 50, "Error selecting bad port")
Rich Lane9a003812012-10-04 17:17:59 -0700398 logging.info("Select " + str(bad_port) + " as invalid port")
Ken Chiangaeb23d62012-08-23 21:20:07 -0700399
400 rv = port_config_set(self.controller, bad_port,
Rich Lane9a003812012-10-04 17:17:59 -0700401 ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD)
Ken Chiangaeb23d62012-08-23 21:20:07 -0700402 self.assertTrue(rv != -1, "Error sending port mod")
403
404 # poll for error message
405 while True:
Dan Talaycoc689a792012-09-28 14:22:53 -0700406 (response, raw) = self.controller.poll(ofp.OFPT_ERROR)
Ken Chiangaeb23d62012-08-23 21:20:07 -0700407 if not response: # Timeout
408 break
409 if response.code == ofp.OFPPMFC_BAD_PORT:
Rich Lane9a003812012-10-04 17:17:59 -0700410 logging.info("Received error message with OFPPMFC_BAD_PORT code")
Ken Chiangaeb23d62012-08-23 21:20:07 -0700411 break
Rich Lane477f4812012-10-04 22:49:00 -0700412 if not config["relax"]: # Only one attempt to match
Ken Chiangaeb23d62012-08-23 21:20:07 -0700413 break
414 count += 1
415 if count > 10: # Too many tries
416 break
417
418 self.assertTrue(response is not None, 'Did not receive error message')
419
Rich Lane97e99652013-01-02 17:23:20 -0800420@group('smoke')
Rich Laneb90a1c42012-10-05 09:16:05 -0700421class BadMessage(base_tests.SimpleProtocol):
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700422 """
423 Send a message with a bad type and verify an error is returned
424 """
425
426 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700427 logging.info("Running " + str(self))
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700428 request = illegal_message.illegal_message_type()
429
Dan Talaycoc689a792012-09-28 14:22:53 -0700430 reply, pkt = self.controller.transact(request)
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700431 self.assertTrue(reply is not None, "Did not get response to bad req")
432 self.assertTrue(reply.header.type == ofp.OFPT_ERROR,
433 "reply not an error message")
434 self.assertTrue(reply.type == ofp.OFPET_BAD_REQUEST,
435 "reply error type is not bad request")
436 self.assertTrue(reply.code == ofp.OFPBRC_BAD_TYPE,
437 "reply error code is not bad type")
438
Dan Talaycodba244e2010-02-15 14:08:53 -0800439if __name__ == "__main__":
Dan Talayco2c0dba32010-03-06 22:47:06 -0800440 print "Please run through oft script: ./oft --test_spec=basic"