Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 1 | """ |
| 2 | Connection test cases |
| 3 | |
| 4 | """ |
| 5 | |
| 6 | import time |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 7 | import sys |
| 8 | import logging |
| 9 | |
| 10 | import unittest |
| 11 | import random |
| 12 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 13 | from oftest import config |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 14 | import oftest.controller as controller |
Rich Lane | d7b0ffa | 2013-03-08 15:53:42 -0800 | [diff] [blame] | 15 | import ofp |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 16 | import oftest.dataplane as dataplane |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 17 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 18 | from oftest.testutils import * |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 19 | |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 20 | @disabled |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 21 | class BaseHandshake(unittest.TestCase): |
| 22 | """ |
| 23 | Base handshake case to set up controller, but do not send hello. |
| 24 | """ |
| 25 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 26 | def controllerSetup(self, host, port): |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 27 | con = controller.Controller(host=host,port=port) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 28 | |
| 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 Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 32 | con.initial_hello = False |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 33 | |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 34 | con.start() |
| 35 | self.controllers.append(con) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 36 | |
| 37 | def setUp(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 38 | logging.info("** START TEST CASE " + str(self)) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 39 | |
Ed Swierk | cfdcbd3 | 2013-01-02 18:44:52 -0800 | [diff] [blame] | 40 | self.controllers = [] |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 41 | self.default_timeout = test_param_get('default_timeout', |
| 42 | default=2) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 43 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 44 | def tearDown(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 45 | logging.info("** END TEST CASE " + str(self)) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 46 | for con in self.controllers: |
| 47 | con.shutdown() |
| 48 | if self.clean_shutdown: |
| 49 | con.join() |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 50 | |
| 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 Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 57 | logging.error("** FAILED ASSERTION: " + msg) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 58 | unittest.TestCase.assertTrue(self, cond, msg) |
| 59 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 60 | class HandshakeNoHello(BaseHandshake): |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 61 | """ |
| 62 | TCP connect to switch, but do not sent hello, |
| 63 | and wait for disconnect. |
| 64 | """ |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 65 | def runTest(self): |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 66 | self.controllerSetup(config["controller_host"], |
| 67 | config["controller_port"]) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 68 | self.controllers[0].connect(self.default_timeout) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 69 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 70 | logging.info("TCP Connected " + |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 71 | str(self.controllers[0].switch_addr)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 72 | logging.info("Hello not sent, waiting for timeout") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 73 | |
| 74 | # wait for controller to die |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 75 | self.assertTrue(self.controllers[0].wait_disconnected(timeout=10), |
| 76 | "Not notified of controller disconnect") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 77 | |
| 78 | class HandshakeNoFeaturesRequest(BaseHandshake): |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 79 | """ |
| 80 | TCP connect to switch, send hello, but do not send features request, |
| 81 | and wait for disconnect. |
| 82 | """ |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 83 | def runTest(self): |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 84 | self.controllerSetup(config["controller_host"], |
| 85 | config["controller_port"]) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 86 | self.controllers[0].connect(self.default_timeout) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 87 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 88 | logging.info("TCP Connected " + |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 89 | str(self.controllers[0].switch_addr)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 90 | logging.info("Sending hello") |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame^] | 91 | self.controllers[0].message_send(ofp.message.hello()) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 92 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 93 | logging.info("Features request not sent, waiting for timeout") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 94 | |
| 95 | # wait for controller to die |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 96 | self.assertTrue(self.controllers[0].wait_disconnected(timeout=10), |
| 97 | "Not notified of controller disconnect") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 98 | |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 99 | @disabled |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 100 | class CompleteHandshake(BaseHandshake): |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 101 | """ |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 102 | Set up multiple controllers and complete handshake, but otherwise do nothing. |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 103 | """ |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 104 | |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 105 | 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 Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 121 | |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 122 | 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 Chiang | 7717399 | 2012-10-30 15:44:39 -0700 | [diff] [blame] | 143 | self.controllers[i].cstate = 0 |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 144 | self.controllers[i].keep_alive = self.keep_alive |
Ken Chiang | 74be472 | 2012-12-21 13:07:03 -0800 | [diff] [blame] | 145 | self.controllers[i].saved_switch_addr = None |
Ken Chiang | 7717399 | 2012-10-30 15:44:39 -0700 | [diff] [blame] | 146 | tick = 0.1 # time period in seconds at which controllers are handled |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 147 | |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 148 | disconnected_count = 0 |
| 149 | cycle = 0 |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 150 | while True: |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 151 | states = [] |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 152 | for con in self.controllers: |
Ken Chiang | 7717399 | 2012-10-30 15:44:39 -0700 | [diff] [blame] | 153 | condesc = con.host + ":" + str(con.port) + ": " |
| 154 | logging.debug("Checking " + condesc) |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 155 | |
Ken Chiang | 74be472 | 2012-12-21 13:07:03 -0800 | [diff] [blame] | 156 | 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 Chiang | 7717399 | 2012-10-30 15:44:39 -0700 | [diff] [blame] | 161 | if con.cstate == 0: |
| 162 | logging.info(condesc + "Sending hello to " + |
| 163 | str(con.switch_addr)) |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame^] | 164 | con.message_send(ofp.message.hello()) |
Ken Chiang | 7717399 | 2012-10-30 15:44:39 -0700 | [diff] [blame] | 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)) |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame^] | 186 | con.message_send(ofp.message.features_request()) |
Ken Chiang | 7717399 | 2012-10-30 15:44:39 -0700 | [diff] [blame] | 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 Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 198 | cycle = cycle + 1 |
Ken Chiang | 74be472 | 2012-12-21 13:07:03 -0800 | [diff] [blame] | 199 | logging.info("Cycle " + str(cycle)) |
Ken Chiang | 7717399 | 2012-10-30 15:44:39 -0700 | [diff] [blame] | 200 | 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 Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 223 | |
| 224 | states.append(con.cstate) |
Ken Chiang | 7717399 | 2012-10-30 15:44:39 -0700 | [diff] [blame] | 225 | |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 226 | 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 Chiang | 7717399 | 2012-10-30 15:44:39 -0700 | [diff] [blame] | 243 | time.sleep(tick) |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 244 | |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 245 | @disabled |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 246 | class HandshakeAndKeepalive(CompleteHandshake): |
| 247 | """ |
| 248 | Complete handshake and respond to echo request, but otherwise do nothing. |
| 249 | Good for manual testing. |
| 250 | """ |
| 251 | |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 252 | def __init__(self): |
| 253 | CompleteHandshake.__init__(self, keep_alive=True) |
| 254 | |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 255 | @disabled |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 256 | class HandshakeNoEcho(CompleteHandshake): |
| 257 | """ |
| 258 | Complete handshake, but otherwise do nothing, and do not respond to echo. |
| 259 | """ |
| 260 | |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 261 | def __init__(self): |
| 262 | CompleteHandshake.__init__(self, keep_alive=False) |
| 263 | |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 264 | @disabled |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 265 | class HandshakeAndDrop(CompleteHandshake): |
| 266 | """ |
| 267 | Complete handshake, but otherwise do nothing, and drop connection after a while. |
| 268 | """ |
| 269 | |
Ken Chiang | 9c93e67 | 2012-12-18 12:00:14 -0800 | [diff] [blame] | 270 | def __init__(self): |
| 271 | CompleteHandshake.__init__(self, keep_alive=True, controller_timeout=10) |
| 272 | |