blob: db377be319be81fcd86186485738bdfb993e91d9 [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
15import oftest.cstruct as ofp
16import oftest.message as message
17import oftest.dataplane as dataplane
18import oftest.action as action
19
Rich Laneda3b5ad2012-10-03 09:05:32 -070020from oftest.testutils import *
Ken Chiangd6f5f862012-09-28 15:40:33 -070021
Ken Chiangd6f5f862012-09-28 15:40:33 -070022class BaseHandshake(unittest.TestCase):
23 """
24 Base handshake case to set up controller, but do not send hello.
25 """
26
Rich Laned1d9c282012-10-04 22:07:10 -070027 priority = -1
Ken Chiangadc950f2012-10-05 13:50:03 -070028 controllers = []
29 default_timeout = 2
30
Ken Chiangd6f5f862012-09-28 15:40:33 -070031 def controllerSetup(self, host, port):
Ken Chiangadc950f2012-10-05 13:50:03 -070032 con = controller.Controller(host=host,port=port)
Ken Chiangd6f5f862012-09-28 15:40:33 -070033
34 # clean_shutdown should be set to False to force quit app
35 self.clean_shutdown = True
36 # disable initial hello so hello is under control of test
Ken Chiangadc950f2012-10-05 13:50:03 -070037 con.initial_hello = False
Ken Chiangd6f5f862012-09-28 15:40:33 -070038
Ken Chiangadc950f2012-10-05 13:50:03 -070039 con.start()
40 self.controllers.append(con)
Ken Chiangd6f5f862012-09-28 15:40:33 -070041
42 def setUp(self):
Rich Lane9a003812012-10-04 17:17:59 -070043 logging.info("** START TEST CASE " + str(self))
Ken Chiangd6f5f862012-09-28 15:40:33 -070044
Ken Chiange875baf2012-10-09 15:24:40 -070045 self.default_timeout = test_param_get('default_timeout',
46 default=2)
Ken Chiangd6f5f862012-09-28 15:40:33 -070047
Ken Chiangd6f5f862012-09-28 15:40:33 -070048 def tearDown(self):
Rich Lane9a003812012-10-04 17:17:59 -070049 logging.info("** END TEST CASE " + str(self))
Ken Chiangadc950f2012-10-05 13:50:03 -070050 for con in self.controllers:
51 con.shutdown()
52 if self.clean_shutdown:
53 con.join()
Ken Chiangd6f5f862012-09-28 15:40:33 -070054
55 def runTest(self):
56 # do nothing in the base case
57 pass
58
59 def assertTrue(self, cond, msg):
60 if not cond:
Rich Lane9a003812012-10-04 17:17:59 -070061 logging.error("** FAILED ASSERTION: " + msg)
Ken Chiangd6f5f862012-09-28 15:40:33 -070062 unittest.TestCase.assertTrue(self, cond, msg)
63
Ken Chiangd6f5f862012-09-28 15:40:33 -070064class HandshakeNoHello(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -070065 """
66 TCP connect to switch, but do not sent hello,
67 and wait for disconnect.
68 """
Ken Chiangd6f5f862012-09-28 15:40:33 -070069 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -070070 self.controllerSetup(config["controller_host"],
71 config["controller_port"])
Ken Chiangadc950f2012-10-05 13:50:03 -070072 self.controllers[0].connect(self.default_timeout)
Ken Chiangd6f5f862012-09-28 15:40:33 -070073
Rich Lane9a003812012-10-04 17:17:59 -070074 logging.info("TCP Connected " +
Ken Chiange875baf2012-10-09 15:24:40 -070075 str(self.controllers[0].switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -070076 logging.info("Hello not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -070077
78 # wait for controller to die
Ken Chiangadc950f2012-10-05 13:50:03 -070079 self.assertTrue(self.controllers[0].wait_disconnected(timeout=10),
80 "Not notified of controller disconnect")
Ken Chiangd6f5f862012-09-28 15:40:33 -070081
82class HandshakeNoFeaturesRequest(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -070083 """
84 TCP connect to switch, send hello, but do not send features request,
85 and wait for disconnect.
86 """
Ken Chiangd6f5f862012-09-28 15:40:33 -070087 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -070088 self.controllerSetup(config["controller_host"],
89 config["controller_port"])
Ken Chiangadc950f2012-10-05 13:50:03 -070090 self.controllers[0].connect(self.default_timeout)
Ken Chiangd6f5f862012-09-28 15:40:33 -070091
Rich Lane9a003812012-10-04 17:17:59 -070092 logging.info("TCP Connected " +
Ken Chiange875baf2012-10-09 15:24:40 -070093 str(self.controllers[0].switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -070094 logging.info("Sending hello")
Ken Chiangadc950f2012-10-05 13:50:03 -070095 self.controllers[0].message_send(message.hello())
Ken Chiangd6f5f862012-09-28 15:40:33 -070096
Rich Lane9a003812012-10-04 17:17:59 -070097 logging.info("Features request not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -070098
99 # wait for controller to die
Ken Chiangadc950f2012-10-05 13:50:03 -0700100 self.assertTrue(self.controllers[0].wait_disconnected(timeout=10),
101 "Not notified of controller disconnect")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700102
Ken Chiang9c93e672012-12-18 12:00:14 -0800103class CompleteHandshake(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -0700104 """
Ken Chiang9c93e672012-12-18 12:00:14 -0800105 Set up multiple controllers and complete handshake, but otherwise do nothing.
Ken Chiang35a74372012-10-01 15:39:25 -0700106 """
Rich Laned1d9c282012-10-04 22:07:10 -0700107
108 priority = -1
109
Ken Chiang9c93e672012-12-18 12:00:14 -0800110 def buildControllerList(self):
111 # controller_list is a list of IP:port tuples
112 con_list = test_param_get('controller_list')
113 if con_list is not None:
114 self.controller_list = []
115 for controller in con_list:
116 ip,portstr = controller.split(':')
117 try:
118 port = int(portstr)
119 except:
120 self.assertTrue(0, "failure converting port " +
121 portstr + " to integer")
122 self.controller_list.append( (ip, int(port)) )
123 else:
124 self.controller_list = [(config["controller_host"],
125 config["controller_port"])]
Ken Chiang35a74372012-10-01 15:39:25 -0700126
Ken Chiang9c93e672012-12-18 12:00:14 -0800127 def __init__(self, keep_alive=True, cxn_cycles=5,
128 controller_timeout=-1, hello_timeout=5,
129 features_req_timeout=5, disconnected_timeout=3):
130 BaseHandshake.__init__(self)
131 self.buildControllerList()
132 self.keep_alive = keep_alive
133 self.cxn_cycles = test_param_get('cxn_cycles') \
134 or cxn_cycles
135 self.controller_timeout = test_param_get('controller_timeout') \
136 or controller_timeout
137 self.hello_timeout = test_param_get('hello_timeout') \
138 or hello_timeout
139 self.features_req_timeout = test_param_get('features_req_timeout') \
140 or features_req_timeout
141 self.disconnected_timeout = test_param_get('disconnected_timeout') \
142 or disconnected_timeout
143
144 def runTest(self):
145 for conspec in self.controller_list:
146 self.controllerSetup(conspec[0], conspec[1])
147 for i in range(len(self.controller_list)):
Ken Chiang77173992012-10-30 15:44:39 -0700148 self.controllers[i].cstate = 0
Ken Chiang9c93e672012-12-18 12:00:14 -0800149 self.controllers[i].keep_alive = self.keep_alive
Ken Chiang77173992012-10-30 15:44:39 -0700150 tick = 0.1 # time period in seconds at which controllers are handled
Ken Chiang35a74372012-10-01 15:39:25 -0700151
Ken Chiang9c93e672012-12-18 12:00:14 -0800152 disconnected_count = 0
153 cycle = 0
Ken Chiangadc950f2012-10-05 13:50:03 -0700154 while True:
Ken Chiang9c93e672012-12-18 12:00:14 -0800155 states = []
Ken Chiangadc950f2012-10-05 13:50:03 -0700156 for con in self.controllers:
Ken Chiang77173992012-10-30 15:44:39 -0700157 condesc = con.host + ":" + str(con.port) + ": "
158 logging.debug("Checking " + condesc)
Ken Chiang35a74372012-10-01 15:39:25 -0700159
Ken Chiang77173992012-10-30 15:44:39 -0700160 if con.switch_socket:
161 if con.cstate == 0:
162 logging.info(condesc + "Sending hello to " +
163 str(con.switch_addr))
164 con.message_send(message.hello())
165 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))
186 con.message_send(message.features_request())
187 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 Chiang77173992012-10-30 15:44:39 -0700199 else:
200 con.count = con.count + 1
201 # fall back to previous state on timeout
202 if con.count >= self.features_req_timeout/tick:
203 logging.info(condesc +
204 "Timeout features request from " +
205 str(con.switch_addr))
206 con.cstate = 2
207 elif con.cstate == 4:
208 if (self.controller_timeout < 0 or
209 con.count < self.controller_timeout/tick):
210 logging.debug(condesc +
211 "Maintaining connection to " +
212 str(con.switch_addr))
213 con.count = con.count + 1
214 else:
215 logging.info(condesc +
216 "Disconnecting from " +
217 str(con.switch_addr))
218 con.disconnect()
219 con.cstate = 0
220 else:
221 con.cstate = 0
Ken Chiang9c93e672012-12-18 12:00:14 -0800222
223 states.append(con.cstate)
Ken Chiang77173992012-10-30 15:44:39 -0700224
Ken Chiang9c93e672012-12-18 12:00:14 -0800225 logging.debug("Cycle " + str(cycle) +
226 ", states " + str(states) +
227 ", disconnected_count " + str(disconnected_count))
228 if 4 in states:
229 disconnected_count = 0
230 else:
231 disconnected_count = disconnected_count + 1
232 if cycle != 0:
233 self.assertTrue(disconnected_count < self.disconnected_timeout/tick,
234 "Timeout expired connecting to controller")
235 else:
236 # on first cycle, allow more time for initial connect
237 self.assertTrue(disconnected_count < 2*self.disconnected_timeout/tick,
238 "Timeout expired connecting to controller on init")
239
240 if cycle > self.cxn_cycles:
241 break
Ken Chiang77173992012-10-30 15:44:39 -0700242 time.sleep(tick)
Ken Chiang9c93e672012-12-18 12:00:14 -0800243
244class HandshakeAndKeepalive(CompleteHandshake):
245 """
246 Complete handshake and respond to echo request, but otherwise do nothing.
247 Good for manual testing.
248 """
249
250 priority = -1
251
252 def __init__(self):
253 CompleteHandshake.__init__(self, keep_alive=True)
254
255class HandshakeNoEcho(CompleteHandshake):
256 """
257 Complete handshake, but otherwise do nothing, and do not respond to echo.
258 """
259
260 priority = -1
261
262 def __init__(self):
263 CompleteHandshake.__init__(self, keep_alive=False)
264
265class HandshakeAndDrop(CompleteHandshake):
266 """
267 Complete handshake, but otherwise do nothing, and drop connection after a while.
268 """
269
270 priority = -1
271
272 def __init__(self):
273 CompleteHandshake.__init__(self, keep_alive=True, controller_timeout=10)
274