blob: 66e035547f856d067590ae036d8d81372507f304 [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
Dan Talayco2c0dba32010-03-06 22:47:06 -080024import oftest.dataplane as dataplane
Rich Laneb90a1c42012-10-05 09:16:05 -070025import oftest.base_tests as base_tests
Rich Laneaecd7162013-01-11 11:33:00 -080026import ofp
Dan Talayco2c0dba32010-03-06 22:47:06 -080027
Dan Talaycoe605b1b2012-09-18 06:56:20 -070028import oftest.illegal_message as illegal_message
29
Rich Laneda3b5ad2012-10-03 09:05:32 -070030from oftest.testutils import *
Dan Talayco6ce963a2010-03-07 21:58:13 -080031
Ken Chiangaeb23d62012-08-23 21:20:07 -070032TEST_VID_DEFAULT = 2
33
Rich Lane97e99652013-01-02 17:23:20 -080034@group('smoke')
Rich Laneb90a1c42012-10-05 09:16:05 -070035class Echo(base_tests.SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -080036 """
37 Test echo response with no data
38 """
39 def runTest(self):
Rich Laneaecd7162013-01-11 11:33:00 -080040 request = ofp.message.echo_request()
Dan Talaycoe226eb12010-02-18 23:06:30 -080041 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -070042 self.assertTrue(response is not None,
43 "Did not get echo reply")
Rich Laneb73808c2013-03-11 15:22:23 -070044 self.assertEqual(response.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -080045 'response is not echo_reply')
Rich Laneb73808c2013-03-11 15:22:23 -070046 self.assertEqual(request.xid, response.xid,
Dan Talaycodba244e2010-02-15 14:08:53 -080047 'response xid != request xid')
48 self.assertEqual(len(response.data), 0, 'response data non-empty')
49
Rich Laneb90a1c42012-10-05 09:16:05 -070050class EchoWithData(base_tests.SimpleProtocol):
Dan Talaycodba244e2010-02-15 14:08:53 -080051 """
52 Test echo response with short string data
53 """
54 def runTest(self):
Rich Laneaecd7162013-01-11 11:33:00 -080055 request = ofp.message.echo_request(data='OpenFlow Will Rule The World')
Dan Talaycoe226eb12010-02-18 23:06:30 -080056 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -070057 self.assertTrue(response is not None,
58 "Did not get echo reply (with data)")
Rich Laneb73808c2013-03-11 15:22:23 -070059 self.assertEqual(response.type, ofp.OFPT_ECHO_REPLY,
Dan Talaycoa92e75b2010-02-16 20:53:56 -080060 'response is not echo_reply')
Rich Laneb73808c2013-03-11 15:22:23 -070061 self.assertEqual(request.xid, response.xid,
Dan Talaycodba244e2010-02-15 14:08:53 -080062 'response xid != request xid')
63 self.assertEqual(request.data, response.data,
64 'response data does not match request')
65
Rich Lane97e99652013-01-02 17:23:20 -080066@group('smoke')
Rich Laneb90a1c42012-10-05 09:16:05 -070067class PacketIn(base_tests.SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -080068 """
69 Test packet in function
Dan Talayco6ce963a2010-03-07 21:58:13 -080070
71 Send a packet to each dataplane port and verify that a packet
72 in message is received from the controller for each
Dan Talaycodba244e2010-02-15 14:08:53 -080073 """
74 def runTest(self):
75 # Construct packet to send to dataplane
Dan Talaycoe226eb12010-02-18 23:06:30 -080076 # Send packet to dataplane, once to each port
Dan Talaycodba244e2010-02-15 14:08:53 -080077 # Poll controller with expect message type packet in
Dan Talaycoe226eb12010-02-18 23:06:30 -080078
Rich Lane32bf9482013-01-03 17:26:30 -080079 delete_all_flows(self.controller)
Rich Lane3a261d52013-01-03 17:45:08 -080080 do_barrier(self.controller)
Dan Talayco6ce963a2010-03-07 21:58:13 -080081
Rich Lane2014f9b2012-10-05 15:29:40 -070082 vid = test_param_get('vid', default=TEST_VID_DEFAULT)
Ken Chiangaeb23d62012-08-23 21:20:07 -070083
Rich Lane477f4812012-10-04 22:49:00 -070084 for of_port in config["port_map"].keys():
Ed Swierk0aeff8c2012-03-23 20:27:18 -070085 for pkt, pt in [
86 (simple_tcp_packet(), "simple TCP packet"),
Rich Laned0478ff2013-03-11 12:46:58 -070087 (simple_tcp_packet(dl_vlan_enable=True,vlan_vid=vid,pktlen=108),
Ken Chiangaeb23d62012-08-23 21:20:07 -070088 "simple tagged TCP packet"),
Ed Swierk0aeff8c2012-03-23 20:27:18 -070089 (simple_eth_packet(), "simple Ethernet packet"),
90 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
Dan Talaycodba244e2010-02-15 14:08:53 -080091
Rich Lane9a003812012-10-04 17:17:59 -070092 logging.info("PKT IN test with %s, port %s" % (pt, of_port))
Ed Swierk0aeff8c2012-03-23 20:27:18 -070093 self.dataplane.send(of_port, str(pkt))
94 #@todo Check for unexpected messages?
95 count = 0
96 while True:
Dan Talaycoc689a792012-09-28 14:22:53 -070097 (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN)
Ed Swierk0aeff8c2012-03-23 20:27:18 -070098 if not response: # Timeout
99 break
Ed Swierk506614a2012-03-29 08:16:59 -0700100 if dataplane.match_exp_pkt(pkt, response.data): # Got match
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700101 break
Rich Lane477f4812012-10-04 22:49:00 -0700102 if not config["relax"]: # Only one attempt to match
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700103 break
104 count += 1
105 if count > 10: # Too many tries
106 break
Dan Talayco48370102010-03-03 15:17:33 -0800107
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700108 self.assertTrue(response is not None,
109 'Packet in message not received on port ' +
110 str(of_port))
Ed Swierk506614a2012-03-29 08:16:59 -0700111 if not dataplane.match_exp_pkt(pkt, response.data):
Rich Lane9a003812012-10-04 17:17:59 -0700112 logging.debug("Sent %s" % format_packet(pkt))
113 logging.debug("Resp %s" % format_packet(response.data))
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700114 self.assertTrue(False,
115 'Response packet does not match send packet' +
116 ' for port ' + str(of_port))
Dan Talaycodba244e2010-02-15 14:08:53 -0800117
Rich Laneb90a1c42012-10-05 09:16:05 -0700118class PacketInBroadcastCheck(base_tests.SimpleDataPlane):
Dan Talayco1f648cb2012-05-03 09:37:56 -0700119 """
120 Check if bcast pkts leak when no flows are present
121
122 Clear the flow table
123 Send in a broadcast pkt
124 Look for the packet on other dataplane ports.
125 """
Rich Laned1d9c282012-10-04 22:07:10 -0700126
Dan Talayco1f648cb2012-05-03 09:37:56 -0700127 def runTest(self):
128 # Need at least two ports
Rich Lane477f4812012-10-04 22:49:00 -0700129 self.assertTrue(len(config["port_map"]) > 1, "Too few ports for test")
Dan Talayco1f648cb2012-05-03 09:37:56 -0700130
Rich Lane32bf9482013-01-03 17:26:30 -0800131 delete_all_flows(self.controller)
Rich Lane3a261d52013-01-03 17:45:08 -0800132 do_barrier(self.controller)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700133
Rich Lane477f4812012-10-04 22:49:00 -0700134 of_ports = config["port_map"].keys()
Dan Talayco1f648cb2012-05-03 09:37:56 -0700135 d_port = of_ports[0]
Rich Laned0478ff2013-03-11 12:46:58 -0700136 pkt = simple_eth_packet(eth_dst='ff:ff:ff:ff:ff:ff')
Dan Talayco1f648cb2012-05-03 09:37:56 -0700137
Rich Lane9a003812012-10-04 17:17:59 -0700138 logging.info("BCast Leak Test, send to port %s" % d_port)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700139 self.dataplane.send(d_port, str(pkt))
140
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700141 (of_port, pkt_in, pkt_time) = self.dataplane.poll(exp_pkt=pkt)
Dan Talayco1f648cb2012-05-03 09:37:56 -0700142 self.assertTrue(pkt_in is None,
143 'BCast packet received on port ' + str(of_port))
144
Rich Lane97e99652013-01-02 17:23:20 -0800145@group('smoke')
Rich Laneb90a1c42012-10-05 09:16:05 -0700146class PacketOut(base_tests.SimpleDataPlane):
Dan Talaycodba244e2010-02-15 14:08:53 -0800147 """
148 Test packet out function
Dan Talayco6ce963a2010-03-07 21:58:13 -0800149
150 Send packet out message to controller for each dataplane port and
151 verify the packet appears on the appropriate dataplane port
Dan Talaycodba244e2010-02-15 14:08:53 -0800152 """
153 def runTest(self):
154 # Construct packet to send to dataplane
155 # Send packet to dataplane
156 # Poll controller with expect message type packet in
Dan Talayco41eae8b2010-03-10 13:57:06 -0800157
Rich Lane32bf9482013-01-03 17:26:30 -0800158 delete_all_flows(self.controller)
Dan Talaycodba244e2010-02-15 14:08:53 -0800159
160 # These will get put into function
Rich Lane477f4812012-10-04 22:49:00 -0700161 of_ports = config["port_map"].keys()
Dan Talayco48370102010-03-03 15:17:33 -0800162 of_ports.sort()
163 for dp_port in of_ports:
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700164 for outpkt, opt in [
165 (simple_tcp_packet(), "simple TCP packet"),
166 (simple_eth_packet(), "simple Ethernet packet"),
167 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
Dan Talaycodba244e2010-02-15 14:08:53 -0800168
Rich Lane9a003812012-10-04 17:17:59 -0700169 logging.info("PKT OUT test with %s, port %s" % (opt, dp_port))
Rich Laneaecd7162013-01-11 11:33:00 -0800170 msg = ofp.message.packet_out(in_port=ofp.OFPP_NONE,
Rich Lane0f0adc92013-01-04 15:13:02 -0800171 data=str(outpkt),
Rich Laneaecd7162013-01-11 11:33:00 -0800172 actions=[ofp.action.output(port=dp_port)])
Dan Talaycodba244e2010-02-15 14:08:53 -0800173
Rich Lane9a003812012-10-04 17:17:59 -0700174 logging.info("PacketOut to: " + str(dp_port))
Rich Lane5c3151c2013-01-03 17:15:41 -0800175 self.controller.message_send(msg)
Dan Talaycodba244e2010-02-15 14:08:53 -0800176
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700177 exp_pkt_arg = None
178 exp_port = None
Rich Lane477f4812012-10-04 22:49:00 -0700179 if config["relax"]:
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700180 exp_pkt_arg = outpkt
181 exp_port = dp_port
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700182 (of_port, pkt, pkt_time) = self.dataplane.poll(port_number=exp_port,
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700183 exp_pkt=exp_pkt_arg)
184
185 self.assertTrue(pkt is not None, 'Packet not received')
Rich Lane9a003812012-10-04 17:17:59 -0700186 logging.info("PacketOut: got pkt from " + str(of_port))
Ed Swierk0aeff8c2012-03-23 20:27:18 -0700187 if of_port is not None:
188 self.assertEqual(of_port, dp_port, "Unexpected receive port")
Ed Swierk506614a2012-03-29 08:16:59 -0700189 if not dataplane.match_exp_pkt(outpkt, pkt):
Rich Lane9a003812012-10-04 17:17:59 -0700190 logging.debug("Sent %s" % format_packet(outpkt))
191 logging.debug("Resp %s" % format_packet(
Dan Talayco2baf8b52012-03-30 09:55:42 -0700192 str(pkt)[:len(str(outpkt))]))
Dan Talaycodc6fca32012-03-30 10:05:49 -0700193 self.assertEqual(str(outpkt), str(pkt)[:len(str(outpkt))],
194 'Response packet does not match send packet')
Dan Talaycodba244e2010-02-15 14:08:53 -0800195
Rich Laneb90a1c42012-10-05 09:16:05 -0700196class PacketOutMC(base_tests.SimpleDataPlane):
Ken Chiang1bf01602012-04-04 10:48:23 -0700197 """
198 Test packet out to multiple output ports
199
200 Send packet out message to controller for 1 to N dataplane ports and
201 verify the packet appears on the appropriate ports
202 """
203 def runTest(self):
204 # Construct packet to send to dataplane
205 # Send packet to dataplane
206 # Poll controller with expect message type packet in
207
Rich Lane32bf9482013-01-03 17:26:30 -0800208 delete_all_flows(self.controller)
Ken Chiang1bf01602012-04-04 10:48:23 -0700209
210 # These will get put into function
Rich Lane477f4812012-10-04 22:49:00 -0700211 of_ports = config["port_map"].keys()
Ken Chiang1bf01602012-04-04 10:48:23 -0700212 random.shuffle(of_ports)
213 for num_ports in range(1,len(of_ports)+1):
214 for outpkt, opt in [
215 (simple_tcp_packet(), "simple TCP packet"),
216 (simple_eth_packet(), "simple Ethernet packet"),
217 (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]:
218
219 dp_ports = of_ports[0:num_ports]
Rich Lane9a003812012-10-04 17:17:59 -0700220 logging.info("PKT OUT test with " + opt +
Ken Chiang1bf01602012-04-04 10:48:23 -0700221 ", ports " + str(dp_ports))
Rich Laneaecd7162013-01-11 11:33:00 -0800222 actions = [ofp.action.output(port=port) for port in dp_ports]
223 msg = ofp.message.packet_out(in_port=ofp.OFPP_NONE,
Rich Lane0f0adc92013-01-04 15:13:02 -0800224 data=str(outpkt),
225 actions=actions)
Ken Chiang1bf01602012-04-04 10:48:23 -0700226
Rich Lane9a003812012-10-04 17:17:59 -0700227 logging.info("PacketOut to: " + str(dp_ports))
Rich Lane5c3151c2013-01-03 17:15:41 -0800228 self.controller.message_send(msg)
Ken Chiang1bf01602012-04-04 10:48:23 -0700229
230 receive_pkt_check(self.dataplane, outpkt, dp_ports,
231 set(of_ports).difference(dp_ports),
Rich Lane2014f9b2012-10-05 15:29:40 -0700232 self)
Ken Chiang1bf01602012-04-04 10:48:23 -0700233
Rich Laneb90a1c42012-10-05 09:16:05 -0700234class FlowStatsGet(base_tests.SimpleProtocol):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800235 """
236 Get stats
Dan Talayco2c0dba32010-03-06 22:47:06 -0800237
Dan Talayco6ce963a2010-03-07 21:58:13 -0800238 Simply verify stats get transaction
239 """
Rich Laned1d9c282012-10-04 22:07:10 -0700240
Dan Talayco6ce963a2010-03-07 21:58:13 -0800241 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700242 logging.info("Running StatsGet")
243 logging.info("Inserting trial flow")
Rich Lane477f4812012-10-04 22:49:00 -0700244 request = flow_mod_gen(config["port_map"], True)
Rich Lane5c3151c2013-01-03 17:15:41 -0800245 self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800246
Rich Lane9a003812012-10-04 17:17:59 -0700247 logging.info("Sending flow request")
Rich Laneaecd7162013-01-11 11:33:00 -0800248 request = ofp.message.flow_stats_request(out_port=ofp.OFPP_NONE,
Rich Lanec3c2ae12013-01-04 10:13:17 -0800249 table_id=0xff)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800250 request.match.wildcards = 0 # ofp.OFPFW_ALL
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700251 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700252 self.assertTrue(response is not None,
253 "Did not get response for flow stats")
Rich Lane9a003812012-10-04 17:17:59 -0700254 logging.debug(response.show())
Dan Talayco6ce963a2010-03-07 21:58:13 -0800255
Rich Laneb90a1c42012-10-05 09:16:05 -0700256class TableStatsGet(base_tests.SimpleProtocol):
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700257 """
258 Get table stats
259
260 Simply verify table stats get transaction
261 """
262 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700263 logging.info("Running TableStatsGet")
264 logging.info("Inserting trial flow")
Rich Lane477f4812012-10-04 22:49:00 -0700265 request = flow_mod_gen(config["port_map"], True)
Rich Lane5c3151c2013-01-03 17:15:41 -0800266 self.controller.message_send(request)
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700267
Rich Lane9a003812012-10-04 17:17:59 -0700268 logging.info("Sending table stats request")
Rich Laneaecd7162013-01-11 11:33:00 -0800269 request = ofp.message.table_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700270 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700271 self.assertTrue(response is not None,
272 "Did not get reply for table stats")
Rich Lane9a003812012-10-04 17:17:59 -0700273 logging.debug(response.show())
Dan Talayco79c6c4d2010-06-08 14:01:53 -0700274
Rich Laneb90a1c42012-10-05 09:16:05 -0700275class DescStatsGet(base_tests.SimpleProtocol):
Ed Swierkae74c362012-04-02 08:21:41 -0700276 """
277 Get stats
278
279 Simply verify stats get transaction
280 """
281 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700282 logging.info("Running DescStatsGet")
Ed Swierkae74c362012-04-02 08:21:41 -0700283
Rich Lane9a003812012-10-04 17:17:59 -0700284 logging.info("Sending stats request")
Rich Laneaecd7162013-01-11 11:33:00 -0800285 request = ofp.message.desc_stats_request()
Rich Lanec8aaa3e2012-07-26 19:28:02 -0700286 response, pkt = self.controller.transact(request)
Dan Talayco97d4f362012-09-18 03:22:09 -0700287 self.assertTrue(response is not None,
288 "Did not get reply for desc stats")
Rich Lane9a003812012-10-04 17:17:59 -0700289 logging.debug(response.show())
Ed Swierkae74c362012-04-02 08:21:41 -0700290
Rich Laneb90a1c42012-10-05 09:16:05 -0700291class FlowMod(base_tests.SimpleProtocol):
Dan Talayco6ce963a2010-03-07 21:58:13 -0800292 """
293 Insert a flow
294
295 Simple verification of a flow mod transaction
296 """
297
298 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700299 logging.info("Running " + str(self))
Rich Lane477f4812012-10-04 22:49:00 -0700300 request = flow_mod_gen(config["port_map"], True)
Rich Lane5c3151c2013-01-03 17:15:41 -0800301 self.controller.message_send(request)
Dan Talayco41eae8b2010-03-10 13:57:06 -0800302
Rich Lane97e99652013-01-02 17:23:20 -0800303@group('smoke')
Rich Laneb90a1c42012-10-05 09:16:05 -0700304class PortConfigMod(base_tests.SimpleProtocol):
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700305 """
306 Modify a bit in port config and verify changed
307
308 Get the switch configuration, modify the port configuration
309 and write it back; get the config again and verify changed.
310 Then set it back to the way it was.
311 """
312
313 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700314 logging.info("Running " + str(self))
Rich Lane477f4812012-10-04 22:49:00 -0700315 for of_port, ifname in config["port_map"].items(): # Grab first port
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700316 break
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700317
Rich Lane477f4812012-10-04 22:49:00 -0700318 (hw_addr, port_config, advert) = \
Rich Lane9a003812012-10-04 17:17:59 -0700319 port_config_get(self.controller, of_port)
Rich Lane477f4812012-10-04 22:49:00 -0700320 self.assertTrue(port_config is not None, "Did not get port config")
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700321
Rich Lane9a003812012-10-04 17:17:59 -0700322 logging.debug("No flood bit port " + str(of_port) + " is now " +
Rich Lane477f4812012-10-04 22:49:00 -0700323 str(port_config & ofp.OFPPC_NO_FLOOD))
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700324
325 rv = port_config_set(self.controller, of_port,
Rich Lane477f4812012-10-04 22:49:00 -0700326 port_config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700327 self.assertTrue(rv != -1, "Error sending port mod")
Rich Lane3a261d52013-01-03 17:45:08 -0800328 do_barrier(self.controller)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700329
330 # Verify change took place with same feature request
Rich Lane477f4812012-10-04 22:49:00 -0700331 (hw_addr, port_config2, advert) = \
Rich Lane9a003812012-10-04 17:17:59 -0700332 port_config_get(self.controller, of_port)
333 logging.debug("No flood bit port " + str(of_port) + " is now " +
Rich Lane477f4812012-10-04 22:49:00 -0700334 str(port_config2 & ofp.OFPPC_NO_FLOOD))
335 self.assertTrue(port_config2 is not None, "Did not get port config2")
336 self.assertTrue(port_config2 & ofp.OFPPC_NO_FLOOD !=
337 port_config & ofp.OFPPC_NO_FLOOD,
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700338 "Bit change did not take")
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700339 # Set it back
Rich Lane477f4812012-10-04 22:49:00 -0700340 rv = port_config_set(self.controller, of_port, port_config,
Rich Lane9a003812012-10-04 17:17:59 -0700341 ofp.OFPPC_NO_FLOOD)
Dan Talayco9f47f4d2010-06-03 13:54:37 -0700342 self.assertTrue(rv != -1, "Error sending port mod")
Rich Lane3a261d52013-01-03 17:45:08 -0800343 do_barrier(self.controller)
Dan Talaycob3f43fe2010-05-13 14:24:20 -0700344
Rich Laneb90a1c42012-10-05 09:16:05 -0700345class PortConfigModErr(base_tests.SimpleProtocol):
Ken Chiangaeb23d62012-08-23 21:20:07 -0700346 """
347 Modify a bit in port config on an invalid port and verify
348 error message is received.
349 """
350
351 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700352 logging.info("Running " + str(self))
Ken Chiangaeb23d62012-08-23 21:20:07 -0700353
354 # pick a random bad port number
355 bad_port = random.randint(1, ofp.OFPP_MAX)
356 count = 0
Rich Lane477f4812012-10-04 22:49:00 -0700357 while (count < 50) and (bad_port in config["port_map"].keys()):
Ken Chiangaeb23d62012-08-23 21:20:07 -0700358 bad_port = random.randint(1, ofp.OFPP_MAX)
359 count = count + 1
360 self.assertTrue(count < 50, "Error selecting bad port")
Rich Lane9a003812012-10-04 17:17:59 -0700361 logging.info("Select " + str(bad_port) + " as invalid port")
Ken Chiangaeb23d62012-08-23 21:20:07 -0700362
363 rv = port_config_set(self.controller, bad_port,
Rich Lane9a003812012-10-04 17:17:59 -0700364 ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD)
Ken Chiangaeb23d62012-08-23 21:20:07 -0700365 self.assertTrue(rv != -1, "Error sending port mod")
366
367 # poll for error message
368 while True:
Dan Talaycoc689a792012-09-28 14:22:53 -0700369 (response, raw) = self.controller.poll(ofp.OFPT_ERROR)
Ken Chiangaeb23d62012-08-23 21:20:07 -0700370 if not response: # Timeout
371 break
372 if response.code == ofp.OFPPMFC_BAD_PORT:
Rich Lane9a003812012-10-04 17:17:59 -0700373 logging.info("Received error message with OFPPMFC_BAD_PORT code")
Ken Chiangaeb23d62012-08-23 21:20:07 -0700374 break
Rich Lane477f4812012-10-04 22:49:00 -0700375 if not config["relax"]: # Only one attempt to match
Ken Chiangaeb23d62012-08-23 21:20:07 -0700376 break
377 count += 1
378 if count > 10: # Too many tries
379 break
380
381 self.assertTrue(response is not None, 'Did not receive error message')
382
Rich Lane97e99652013-01-02 17:23:20 -0800383@group('smoke')
Rich Laneb90a1c42012-10-05 09:16:05 -0700384class BadMessage(base_tests.SimpleProtocol):
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700385 """
386 Send a message with a bad type and verify an error is returned
387 """
388
389 def runTest(self):
Rich Lane9a003812012-10-04 17:17:59 -0700390 logging.info("Running " + str(self))
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700391 request = illegal_message.illegal_message_type()
392
Dan Talaycoc689a792012-09-28 14:22:53 -0700393 reply, pkt = self.controller.transact(request)
Rich Laneb73808c2013-03-11 15:22:23 -0700394 logging.info(repr(pkt))
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700395 self.assertTrue(reply is not None, "Did not get response to bad req")
Rich Laneb73808c2013-03-11 15:22:23 -0700396 self.assertTrue(reply.type == ofp.OFPT_ERROR,
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700397 "reply not an error message")
Rich Laneb73808c2013-03-11 15:22:23 -0700398 logging.info(reply.err_type)
Rich Lane4e361bb2013-03-11 13:57:31 -0700399 self.assertTrue(reply.err_type == ofp.OFPET_BAD_REQUEST,
Dan Talaycoe605b1b2012-09-18 06:56:20 -0700400 "reply error type is not bad request")
401 self.assertTrue(reply.code == ofp.OFPBRC_BAD_TYPE,
402 "reply error code is not bad type")
403
Rich Lane5a9a1922013-01-11 14:29:30 -0800404@group('smoke')
405@version('1.1+')
406class TableModConfig(base_tests.SimpleProtocol):
407 """
408 Simple table modification
409
410 Mostly to make sure the switch correctly responds to these messages.
411 More complicated tests in the multi-tables.py tests
412 """
413 def runTest(self):
414 # First table should always exist
415 table_id = 0
416
417 def get_table_config():
418 request = ofp.message.table_stats_request()
419 response, _ = self.controller.transact(request)
420 try:
Rich Lane5fd6faf2013-03-11 13:30:20 -0700421 table_stats = [x for x in response.entries if x.table_id == table_id][0]
Rich Lane5a9a1922013-01-11 14:29:30 -0800422 except IndexError:
423 raise AssertionError("table id %d not found" % table_id)
424 return table_stats.config
425
426 # Get current configuration
427 orig_table_config = get_table_config()
428
429 # Change the configuration
430 if orig_table_config == ofp.OFPTC_TABLE_MISS_CONTROLLER:
431 new_table_config = ofp.OFPTC_TABLE_MISS_DROP
432 else:
433 new_table_config = ofp.OFPTC_TABLE_MISS_CONTROLLER
434 request = ofp.message.table_mod(table_id=table_id, config=new_table_config)
435 self.controller.message_send(request)
436 self.controller.transact(ofp.message.barrier_request())
437
438 # Check the configuration took
439 self.assertEqual(get_table_config(), new_table_config)
440
441 # Change the configuration back
442 request = ofp.message.table_mod(table_id=table_id, config=orig_table_config)
443 self.controller.message_send(request)
444 self.controller.transact(ofp.message.barrier_request())
445
Dan Talaycodba244e2010-02-15 14:08:53 -0800446if __name__ == "__main__":
Dan Talayco2c0dba32010-03-06 22:47:06 -0800447 print "Please run through oft script: ./oft --test_spec=basic"