blob: e9059cca3fd2fcd1eb2a2c7d3d13539e0aafc9b2 [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 Chiang35a74372012-10-01 15:39:25 -0700103class HandshakeAndKeepalive(BaseHandshake):
104 """
105 Complete handshake and respond to echo request, but otherwise do nothing.
106 Good for manual testing.
107 """
Rich Laned1d9c282012-10-04 22:07:10 -0700108
109 priority = -1
110
Ken Chiang35a74372012-10-01 15:39:25 -0700111 def runTest(self):
Ken Chiange875baf2012-10-09 15:24:40 -0700112 self.num_controllers = test_param_get('num_controllers', default=1)
Ken Chiang265fc492012-10-09 16:16:18 -0700113 self.controller_timeout = test_param_get('controller_timeout',
114 default=-1)
Ken Chiang77173992012-10-30 15:44:39 -0700115 self.hello_timeout = test_param_get('hello_timeout',
116 default=5)
117 self.features_req_timeout = test_param_get('features_req_timeout',
118 default=5)
Ken Chiang35a74372012-10-01 15:39:25 -0700119
Ken Chiangadc950f2012-10-05 13:50:03 -0700120 for i in range(self.num_controllers):
Ken Chiange875baf2012-10-09 15:24:40 -0700121 self.controllerSetup(config["controller_host"],
122 config["controller_port"]+i)
Ken Chiangadc950f2012-10-05 13:50:03 -0700123 for i in range(self.num_controllers):
Ken Chiang77173992012-10-30 15:44:39 -0700124 self.controllers[i].cstate = 0
125 self.controllers[i].keep_alive = True
126 tick = 0.1 # time period in seconds at which controllers are handled
Ken Chiang35a74372012-10-01 15:39:25 -0700127
Ken Chiangadc950f2012-10-05 13:50:03 -0700128 while True:
129 for con in self.controllers:
Ken Chiang77173992012-10-30 15:44:39 -0700130 condesc = con.host + ":" + str(con.port) + ": "
131 logging.debug("Checking " + condesc)
Ken Chiang35a74372012-10-01 15:39:25 -0700132
Ken Chiang77173992012-10-30 15:44:39 -0700133 if con.switch_socket:
134 if con.cstate == 0:
135 logging.info(condesc + "Sending hello to " +
136 str(con.switch_addr))
137 con.message_send(message.hello())
138 con.cstate = 1
139 con.count = 0
140 elif con.cstate == 1:
141 reply, pkt = con.poll(exp_msg=ofp.OFPT_HELLO,
142 timeout=0)
143 if reply is not None:
144 logging.info(condesc +
145 "Hello received from " +
146 str(con.switch_addr))
147 con.cstate = 2
148 else:
149 con.count = con.count + 1
150 # fall back to previous state on timeout
151 if con.count >= self.hello_timeout/tick:
152 logging.info(condesc +
153 "Timeout hello from " +
154 str(con.switch_addr))
155 con.cstate = 0
156 elif con.cstate == 2:
157 logging.info(condesc + "Sending features request to " +
158 str(con.switch_addr))
159 con.message_send(message.features_request())
160 con.cstate = 3
161 con.count = 0
162 elif con.cstate == 3:
163 reply, pkt = con.poll(exp_msg=ofp.OFPT_FEATURES_REPLY,
164 timeout=0)
165 if reply is not None:
166 logging.info(condesc +
167 "Features request received from " +
168 str(con.switch_addr))
169 con.cstate = 4
170 con.count = 0
171 else:
172 con.count = con.count + 1
173 # fall back to previous state on timeout
174 if con.count >= self.features_req_timeout/tick:
175 logging.info(condesc +
176 "Timeout features request from " +
177 str(con.switch_addr))
178 con.cstate = 2
179 elif con.cstate == 4:
180 if (self.controller_timeout < 0 or
181 con.count < self.controller_timeout/tick):
182 logging.debug(condesc +
183 "Maintaining connection to " +
184 str(con.switch_addr))
185 con.count = con.count + 1
186 else:
187 logging.info(condesc +
188 "Disconnecting from " +
189 str(con.switch_addr))
190 con.disconnect()
191 con.cstate = 0
192 else:
193 con.cstate = 0
194
195 time.sleep(tick)