blob: 1a00f1f46a7a8b890cdd7b3bf74766cb78a57b97 [file] [log] [blame]
Ken Chiangd6f5f862012-09-28 15:40:33 -07001"""
2Connection test cases
3
4"""
5
6import time
Ken Chiangd6f5f862012-09-28 15:40:33 -07007import sys
8import logging
9
10import unittest
11import random
12
Rich Lane477f4812012-10-04 22:49:00 -070013from oftest import config
Ken Chiangd6f5f862012-09-28 15:40:33 -070014import oftest.controller as controller
Rich Laned7b0ffa2013-03-08 15:53:42 -080015import ofp
Ken Chiangd6f5f862012-09-28 15:40:33 -070016import oftest.dataplane as dataplane
Ken Chiangd6f5f862012-09-28 15:40:33 -070017
Rich Laneda3b5ad2012-10-03 09:05:32 -070018from oftest.testutils import *
Ken Chiangd6f5f862012-09-28 15:40:33 -070019
Rich Lane0a4f6372013-01-02 14:40:22 -080020@disabled
Ken Chiangd6f5f862012-09-28 15:40:33 -070021class BaseHandshake(unittest.TestCase):
22 """
23 Base handshake case to set up controller, but do not send hello.
24 """
25
Ken Chiangd6f5f862012-09-28 15:40:33 -070026 def controllerSetup(self, host, port):
Ken Chiangadc950f2012-10-05 13:50:03 -070027 con = controller.Controller(host=host,port=port)
Ken Chiangd6f5f862012-09-28 15:40:33 -070028
29 # clean_shutdown should be set to False to force quit app
30 self.clean_shutdown = True
31 # disable initial hello so hello is under control of test
Ken Chiangadc950f2012-10-05 13:50:03 -070032 con.initial_hello = False
Ken Chiangd6f5f862012-09-28 15:40:33 -070033
Ken Chiangadc950f2012-10-05 13:50:03 -070034 con.start()
35 self.controllers.append(con)
Ken Chiangd6f5f862012-09-28 15:40:33 -070036
37 def setUp(self):
Rich Lane9a003812012-10-04 17:17:59 -070038 logging.info("** START TEST CASE " + str(self))
Ken Chiangd6f5f862012-09-28 15:40:33 -070039
Ed Swierkcfdcbd32013-01-02 18:44:52 -080040 self.controllers = []
Ken Chiange875baf2012-10-09 15:24:40 -070041 self.default_timeout = test_param_get('default_timeout',
42 default=2)
Ken Chiangd6f5f862012-09-28 15:40:33 -070043
Ken Chiangd6f5f862012-09-28 15:40:33 -070044 def tearDown(self):
Rich Lane9a003812012-10-04 17:17:59 -070045 logging.info("** END TEST CASE " + str(self))
Ken Chiangadc950f2012-10-05 13:50:03 -070046 for con in self.controllers:
47 con.shutdown()
48 if self.clean_shutdown:
49 con.join()
Ken Chiangd6f5f862012-09-28 15:40:33 -070050
51 def runTest(self):
52 # do nothing in the base case
53 pass
54
55 def assertTrue(self, cond, msg):
56 if not cond:
Rich Lane9a003812012-10-04 17:17:59 -070057 logging.error("** FAILED ASSERTION: " + msg)
Ken Chiangd6f5f862012-09-28 15:40:33 -070058 unittest.TestCase.assertTrue(self, cond, msg)
59
Ken Chiangd6f5f862012-09-28 15:40:33 -070060class HandshakeNoHello(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -070061 """
62 TCP connect to switch, but do not sent hello,
63 and wait for disconnect.
64 """
Ken Chiangd6f5f862012-09-28 15:40:33 -070065 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -070066 self.controllerSetup(config["controller_host"],
67 config["controller_port"])
Ken Chiangadc950f2012-10-05 13:50:03 -070068 self.controllers[0].connect(self.default_timeout)
Ken Chiangd6f5f862012-09-28 15:40:33 -070069
Rich Lane9a003812012-10-04 17:17:59 -070070 logging.info("TCP Connected " +
Ken Chiange875baf2012-10-09 15:24:40 -070071 str(self.controllers[0].switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -070072 logging.info("Hello not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -070073
74 # wait for controller to die
Ken Chiangadc950f2012-10-05 13:50:03 -070075 self.assertTrue(self.controllers[0].wait_disconnected(timeout=10),
76 "Not notified of controller disconnect")
Ken Chiangd6f5f862012-09-28 15:40:33 -070077
78class HandshakeNoFeaturesRequest(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -070079 """
80 TCP connect to switch, send hello, but do not send features request,
81 and wait for disconnect.
82 """
Ken Chiangd6f5f862012-09-28 15:40:33 -070083 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -070084 self.controllerSetup(config["controller_host"],
85 config["controller_port"])
Ken Chiangadc950f2012-10-05 13:50:03 -070086 self.controllers[0].connect(self.default_timeout)
Ken Chiangd6f5f862012-09-28 15:40:33 -070087
Rich Lane9a003812012-10-04 17:17:59 -070088 logging.info("TCP Connected " +
Ken Chiange875baf2012-10-09 15:24:40 -070089 str(self.controllers[0].switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -070090 logging.info("Sending hello")
Rich Lane28fa9272013-03-08 16:00:25 -080091 self.controllers[0].message_send(ofp.message.hello())
Ken Chiangd6f5f862012-09-28 15:40:33 -070092
Rich Lane9a003812012-10-04 17:17:59 -070093 logging.info("Features request not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -070094
95 # wait for controller to die
Ken Chiangadc950f2012-10-05 13:50:03 -070096 self.assertTrue(self.controllers[0].wait_disconnected(timeout=10),
97 "Not notified of controller disconnect")
Ken Chiangd6f5f862012-09-28 15:40:33 -070098
Rich Lane0a4f6372013-01-02 14:40:22 -080099@disabled
Ken Chiang9c93e672012-12-18 12:00:14 -0800100class CompleteHandshake(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -0700101 """
Ken Chiang9c93e672012-12-18 12:00:14 -0800102 Set up multiple controllers and complete handshake, but otherwise do nothing.
Ken Chiang35a74372012-10-01 15:39:25 -0700103 """
Rich Laned1d9c282012-10-04 22:07:10 -0700104
Ken Chiang9c93e672012-12-18 12:00:14 -0800105 def buildControllerList(self):
106 # controller_list is a list of IP:port tuples
107 con_list = test_param_get('controller_list')
108 if con_list is not None:
109 self.controller_list = []
110 for controller in con_list:
111 ip,portstr = controller.split(':')
112 try:
113 port = int(portstr)
114 except:
115 self.assertTrue(0, "failure converting port " +
116 portstr + " to integer")
117 self.controller_list.append( (ip, int(port)) )
118 else:
119 self.controller_list = [(config["controller_host"],
120 config["controller_port"])]
Ken Chiang35a74372012-10-01 15:39:25 -0700121
Ken Chiang9c93e672012-12-18 12:00:14 -0800122 def __init__(self, keep_alive=True, cxn_cycles=5,
123 controller_timeout=-1, hello_timeout=5,
124 features_req_timeout=5, disconnected_timeout=3):
125 BaseHandshake.__init__(self)
126 self.buildControllerList()
127 self.keep_alive = keep_alive
128 self.cxn_cycles = test_param_get('cxn_cycles') \
129 or cxn_cycles
130 self.controller_timeout = test_param_get('controller_timeout') \
131 or controller_timeout
132 self.hello_timeout = test_param_get('hello_timeout') \
133 or hello_timeout
134 self.features_req_timeout = test_param_get('features_req_timeout') \
135 or features_req_timeout
136 self.disconnected_timeout = test_param_get('disconnected_timeout') \
137 or disconnected_timeout
138
139 def runTest(self):
140 for conspec in self.controller_list:
141 self.controllerSetup(conspec[0], conspec[1])
142 for i in range(len(self.controller_list)):
Ken Chiang77173992012-10-30 15:44:39 -0700143 self.controllers[i].cstate = 0
Ken Chiang9c93e672012-12-18 12:00:14 -0800144 self.controllers[i].keep_alive = self.keep_alive
Ken Chiang74be4722012-12-21 13:07:03 -0800145 self.controllers[i].saved_switch_addr = None
Ken Chiang77173992012-10-30 15:44:39 -0700146 tick = 0.1 # time period in seconds at which controllers are handled
Ken Chiang35a74372012-10-01 15:39:25 -0700147
Ken Chiang9c93e672012-12-18 12:00:14 -0800148 disconnected_count = 0
149 cycle = 0
Ken Chiangadc950f2012-10-05 13:50:03 -0700150 while True:
Ken Chiang9c93e672012-12-18 12:00:14 -0800151 states = []
Ken Chiangadc950f2012-10-05 13:50:03 -0700152 for con in self.controllers:
Ken Chiang77173992012-10-30 15:44:39 -0700153 condesc = con.host + ":" + str(con.port) + ": "
154 logging.debug("Checking " + condesc)
Ken Chiang35a74372012-10-01 15:39:25 -0700155
Ken Chiang74be4722012-12-21 13:07:03 -0800156 if con.switch_socket:
157 if con.switch_addr != con.saved_switch_addr:
158 con.saved_switch_addr = con.switch_addr
159 con.cstate = 0
160
Ken Chiang77173992012-10-30 15:44:39 -0700161 if con.cstate == 0:
162 logging.info(condesc + "Sending hello to " +
163 str(con.switch_addr))
Rich Lane28fa9272013-03-08 16:00:25 -0800164 con.message_send(ofp.message.hello())
Ken Chiang77173992012-10-30 15:44:39 -0700165 con.cstate = 1
166 con.count = 0
167 elif con.cstate == 1:
168 reply, pkt = con.poll(exp_msg=ofp.OFPT_HELLO,
169 timeout=0)
170 if reply is not None:
171 logging.info(condesc +
172 "Hello received from " +
173 str(con.switch_addr))
174 con.cstate = 2
175 else:
176 con.count = con.count + 1
177 # fall back to previous state on timeout
178 if con.count >= self.hello_timeout/tick:
179 logging.info(condesc +
180 "Timeout hello from " +
181 str(con.switch_addr))
182 con.cstate = 0
183 elif con.cstate == 2:
184 logging.info(condesc + "Sending features request to " +
185 str(con.switch_addr))
Rich Lane28fa9272013-03-08 16:00:25 -0800186 con.message_send(ofp.message.features_request())
Ken Chiang77173992012-10-30 15:44:39 -0700187 con.cstate = 3
188 con.count = 0
189 elif con.cstate == 3:
190 reply, pkt = con.poll(exp_msg=ofp.OFPT_FEATURES_REPLY,
191 timeout=0)
192 if reply is not None:
193 logging.info(condesc +
194 "Features request received from " +
195 str(con.switch_addr))
196 con.cstate = 4
197 con.count = 0
Ken Chiang9c93e672012-12-18 12:00:14 -0800198 cycle = cycle + 1
Ken Chiang74be4722012-12-21 13:07:03 -0800199 logging.info("Cycle " + str(cycle))
Ken Chiang77173992012-10-30 15:44:39 -0700200 else:
201 con.count = con.count + 1
202 # fall back to previous state on timeout
203 if con.count >= self.features_req_timeout/tick:
204 logging.info(condesc +
205 "Timeout features request from " +
206 str(con.switch_addr))
207 con.cstate = 2
208 elif con.cstate == 4:
209 if (self.controller_timeout < 0 or
210 con.count < self.controller_timeout/tick):
211 logging.debug(condesc +
212 "Maintaining connection to " +
213 str(con.switch_addr))
214 con.count = con.count + 1
215 else:
216 logging.info(condesc +
217 "Disconnecting from " +
218 str(con.switch_addr))
219 con.disconnect()
220 con.cstate = 0
221 else:
222 con.cstate = 0
Ken Chiang9c93e672012-12-18 12:00:14 -0800223
224 states.append(con.cstate)
Ken Chiang77173992012-10-30 15:44:39 -0700225
Ken Chiang9c93e672012-12-18 12:00:14 -0800226 logging.debug("Cycle " + str(cycle) +
227 ", states " + str(states) +
228 ", disconnected_count " + str(disconnected_count))
229 if 4 in states:
230 disconnected_count = 0
231 else:
232 disconnected_count = disconnected_count + 1
233 if cycle != 0:
234 self.assertTrue(disconnected_count < self.disconnected_timeout/tick,
235 "Timeout expired connecting to controller")
236 else:
237 # on first cycle, allow more time for initial connect
238 self.assertTrue(disconnected_count < 2*self.disconnected_timeout/tick,
239 "Timeout expired connecting to controller on init")
240
241 if cycle > self.cxn_cycles:
242 break
Ken Chiang77173992012-10-30 15:44:39 -0700243 time.sleep(tick)
Ken Chiang9c93e672012-12-18 12:00:14 -0800244
Rich Lane0a4f6372013-01-02 14:40:22 -0800245@disabled
Ken Chiang9c93e672012-12-18 12:00:14 -0800246class HandshakeAndKeepalive(CompleteHandshake):
247 """
248 Complete handshake and respond to echo request, but otherwise do nothing.
249 Good for manual testing.
250 """
251
Ken Chiang9c93e672012-12-18 12:00:14 -0800252 def __init__(self):
253 CompleteHandshake.__init__(self, keep_alive=True)
254
Rich Lane0a4f6372013-01-02 14:40:22 -0800255@disabled
Ken Chiang9c93e672012-12-18 12:00:14 -0800256class HandshakeNoEcho(CompleteHandshake):
257 """
258 Complete handshake, but otherwise do nothing, and do not respond to echo.
259 """
260
Ken Chiang9c93e672012-12-18 12:00:14 -0800261 def __init__(self):
262 CompleteHandshake.__init__(self, keep_alive=False)
263
Rich Lane0a4f6372013-01-02 14:40:22 -0800264@disabled
Ken Chiang9c93e672012-12-18 12:00:14 -0800265class HandshakeAndDrop(CompleteHandshake):
266 """
267 Complete handshake, but otherwise do nothing, and drop connection after a while.
268 """
269
Ken Chiang9c93e672012-12-18 12:00:14 -0800270 def __init__(self):
271 CompleteHandshake.__init__(self, keep_alive=True, controller_timeout=10)
272