Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 1 | """ |
| 2 | OpenFlow Test Framework |
| 3 | |
| 4 | Controller class |
| 5 | |
| 6 | Provide the interface to the control channel to the switch under test. |
| 7 | |
| 8 | Class inherits from thread so as to run in background allowing |
| 9 | asynchronous callbacks (if needed, not required). Also supports |
| 10 | polling. |
| 11 | |
| 12 | The controller thread maintains a queue. Incoming messages that |
| 13 | are not handled by a callback function are placed in this queue for |
| 14 | poll calls. |
| 15 | |
| 16 | Callbacks and polling support specifying the message type |
| 17 | |
| 18 | @todo Support transaction semantics via xid |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 19 | @todo Set up reasonable logging facility |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 20 | @todo Support select and listen on an administrative socket (or |
| 21 | use a timeout to support clean shutdown). |
| 22 | |
| 23 | Currently only one connection is accepted during the life of |
| 24 | the controller. There seems |
| 25 | to be no clean way to interrupt an accept call. Using select that also listens |
| 26 | on an administrative socket and can shut down the socket might work. |
| 27 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 28 | """ |
| 29 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 30 | from oft_config import * |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 31 | import os |
| 32 | import socket |
| 33 | import time |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 34 | from threading import Thread |
| 35 | from threading import Lock |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 36 | from threading import Condition |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 37 | from message import * |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 38 | from parse import * |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 39 | from ofutils import * |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 40 | # For some reason, it seems select to be last (or later). |
| 41 | # Otherwise get an attribute error when calling select.select |
| 42 | import select |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 43 | |
| 44 | class Controller(Thread): |
| 45 | """ |
| 46 | Class abstracting the control interface to the switch. |
| 47 | |
| 48 | For receiving messages, two mechanism will be implemented. First, |
| 49 | query the interface with poll. Second, register to have a |
| 50 | function called by message type. The callback is passed the |
| 51 | message type as well as the raw packet (or message object) |
| 52 | |
| 53 | One of the main purposes of this object is to translate between network |
| 54 | and host byte order. 'Above' this object, things should be in host |
| 55 | byte order. |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 56 | |
| 57 | @todo Consider using SocketServer for listening socket |
| 58 | @todo Test transaction code |
| 59 | |
| 60 | @var rcv_size The receive size to use for receive calls |
| 61 | @var max_pkts The max size of the receive queue |
| 62 | @var keep_alive If true, listen for echo requests and respond w/ |
| 63 | echo replies |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 64 | @var initial_hello If true, will send a hello message immediately |
| 65 | upon connecting to the switch |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 66 | @var host The host to use for connect |
| 67 | @var port The port to connect on |
| 68 | @var packets_total Total number of packets received |
| 69 | @var packets_expired Number of packets popped from queue as queue full |
| 70 | @var packets_handled Number of packets handled by something |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 71 | @var dbg_state Debug indication of state |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 72 | """ |
| 73 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 74 | def __init__(self, max_pkts=1024): |
| 75 | Thread.__init__(self) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 76 | # Socket related |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 77 | self.rcv_size = RCV_SIZE_DEFAULT |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 78 | self.listen_socket = None |
| 79 | self.switch_socket = None |
| 80 | self.switch_addr = None |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 81 | self.socs = [] |
| 82 | self.connect_cv = Condition() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 83 | self.message_cv = Condition() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 84 | |
| 85 | # Counters |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 86 | self.socket_errors = 0 |
| 87 | self.parse_errors = 0 |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 88 | self.packets_total = 0 |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 89 | self.packets_expired = 0 |
| 90 | self.packets_handled = 0 |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 91 | self.poll_discards = 0 |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 92 | |
| 93 | # State |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 94 | self.packets = [] |
| 95 | self.sync = Lock() |
| 96 | self.handlers = {} |
| 97 | self.keep_alive = False |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 98 | self.active = True |
| 99 | self.initial_hello = True |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 100 | |
| 101 | # Settings |
| 102 | self.max_pkts = max_pkts |
| 103 | self.passive = True |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 104 | self.host = controller_host |
| 105 | self.port = controller_port |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 106 | self.dbg_state = "init" |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 107 | self.debug_level = debug_level_default |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 108 | # self.debug_level = DEBUG_VERBOSE |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 109 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 110 | # Transaction and message type waiting variables |
| 111 | # xid_cv: Condition variable (semaphore) for packet waiters |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 112 | # xid: Transaction ID being waited on |
| 113 | # xid_response: Transaction response message |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 114 | # expect_msg: Is a message being waited on |
| 115 | # expect_msg_cv: Semaphore for waiters |
| 116 | # expect_msg_type: Type of message expected |
| 117 | # expect_msg_response: Result passed through here |
| 118 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 119 | self.xid_cv = Condition() |
| 120 | self.xid = None |
| 121 | self.xid_response = None |
| 122 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 123 | self.expect_msg = False |
| 124 | self.expect_msg_cv = Condition() |
| 125 | self.expect_msg_type = None |
| 126 | self.expect_msg_response = None |
| 127 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 128 | def dbg(self, level, string): |
| 129 | debug_log("CTRL", self.debug_level, level, string) |
| 130 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 131 | def _socket_ready_handle(self, s): |
| 132 | """ |
| 133 | Handle an input-ready socket |
| 134 | @param s The socket object that is ready |
| 135 | @retval True, reset the switch connection |
| 136 | """ |
| 137 | |
| 138 | if s == self.listen_socket: |
| 139 | if self.switch_socket: |
| 140 | self.dbg(DEBUG_ERROR, "Multiple switch cxns not supported") |
| 141 | sys.exit(1) |
| 142 | |
| 143 | (self.switch_socket, self.switch_addr) = \ |
| 144 | self.listen_socket.accept() |
| 145 | self.dbg(DEBUG_INFO, "Got cxn to " + str(self.switch_addr)) |
| 146 | # Notify anyone waiting |
| 147 | self.connect_cv.acquire() |
| 148 | self.connect_cv.notify() |
| 149 | self.connect_cv.release() |
| 150 | self.socs.append(self.switch_socket) |
| 151 | if self.initial_hello: |
| 152 | self.message_send(hello()) |
| 153 | elif s == self.switch_socket: |
| 154 | try: |
| 155 | pkt = self.switch_socket.recv(self.rcv_size) |
| 156 | except: |
| 157 | self.dbg(DEBUG_INFO, "error on switch read") |
| 158 | return True |
| 159 | |
| 160 | if not self.active: |
| 161 | return False |
| 162 | |
| 163 | if len(pkt) == 0: |
| 164 | self.dbg(DEBUG_INFO, "zero-len pkt in") |
| 165 | return True |
| 166 | |
| 167 | (handled, msg) = self._pkt_handler_check(pkt) |
| 168 | if handled: |
| 169 | self.packets_handled += 1 |
| 170 | return False |
| 171 | |
| 172 | # Not handled, enqueue |
| 173 | self.sync.acquire() |
| 174 | if len(self.packets) >= self.max_pkts: |
| 175 | self.packets.pop(0) |
| 176 | self.packets_expired += 1 |
| 177 | self.packets.append((msg, pkt)) |
| 178 | self.packets_total += 1 |
| 179 | self.sync.release() |
| 180 | else: |
| 181 | self.dbg(DEBUG_ERROR, "Unknown socket ready: " + str(s)) |
| 182 | return True |
| 183 | |
| 184 | return False |
| 185 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 186 | def run(self): |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 187 | """ |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 188 | Activity function for class |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 189 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 190 | Assumes connection to switch already exists. Listens on |
| 191 | switch_socket for messages until an error (or zero len pkt) |
| 192 | occurs. |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 193 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 194 | When there is a message on the socket, check for handlers; queue the |
| 195 | packet if no one handles the packet. |
| 196 | |
| 197 | See note for controller describing the limitation of a single |
| 198 | connection for now. |
| 199 | """ |
| 200 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 201 | self.dbg_state = "starting" |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 202 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 203 | # Create listen socket |
| 204 | self.dbg(DEBUG_INFO, "Create/listen at " + self.host + ":" + |
| 205 | str(self.port)) |
| 206 | self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 207 | self.listen_socket.setsockopt(socket.SOL_SOCKET, |
| 208 | socket.SO_REUSEADDR, 1) |
| 209 | self.listen_socket.bind((self.host, self.port)) |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 210 | self.dbg_state = "listening" |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 211 | self.listen_socket.listen(LISTEN_QUEUE_SIZE) |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 212 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 213 | self.dbg(DEBUG_INFO, "Waiting for switch connection") |
| 214 | self.socs = [self.listen_socket] |
| 215 | self.dbg_state = "running" |
| 216 | while self.active: |
| 217 | reset_switch_cxn = False |
| 218 | try: |
| 219 | sel_in, sel_out, sel_err = \ |
| 220 | select.select(self.socs, [], self.socs, 1) |
| 221 | except: |
| 222 | print sys.exc_info() |
| 223 | self.dbg(DEBUG_ERROR, "Select error, exiting") |
| 224 | sys.exit(1) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 225 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 226 | if not self.active: |
| 227 | break |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 228 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 229 | for s in sel_in: |
| 230 | reset_switch_cxn = self._socket_ready_handle(s) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 231 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 232 | for s in sel_err: |
| 233 | self.dbg(DEBUG_ERROR, "Got socket error on: " + str(s)) |
| 234 | if s == self.switch_socket: |
| 235 | reset_switch_cxn = True |
| 236 | else: |
| 237 | self.dbg(DEBUG_ERROR, "Socket error; exiting") |
| 238 | self.active = False |
| 239 | break |
| 240 | |
| 241 | if self.active and reset_switch_cxn: |
| 242 | self.dbg(DEBUG_INFO, "Closing switch cxn") |
| 243 | try: |
| 244 | self.switch_socket.close() |
| 245 | except: |
| 246 | pass |
| 247 | self.switch_socket = None |
| 248 | self.socs = self.socs[0:1] |
| 249 | |
| 250 | # End of main loop |
| 251 | self.dbg_state = "closing" |
| 252 | self.dbg(DEBUG_INFO, "Exiting controller thread") |
| 253 | self.shutdown() |
| 254 | |
| 255 | def connect(self, timeout=None): |
| 256 | """ |
| 257 | Connect to the switch |
| 258 | |
| 259 | @param timeout If None, block until connected. If 0, return |
| 260 | immedidately. Otherwise, block for up to timeout seconds |
| 261 | @return Boolean, True if connected |
| 262 | """ |
| 263 | |
| 264 | if timeout == 0: |
| 265 | return self.switch_socket is not None |
| 266 | if self.switch_socket is not None: |
| 267 | return True |
| 268 | self.connect_cv.acquire() |
| 269 | self.connect_cv.wait(timeout) |
| 270 | self.connect_cv.release() |
| 271 | |
| 272 | return self.switch_socket is not None |
| 273 | |
| 274 | def kill(self): |
| 275 | """ |
| 276 | Force the controller thread to quit |
| 277 | |
| 278 | Just sets the active state variable to false and expects |
| 279 | the select timeout to kick in |
| 280 | """ |
| 281 | self.active = False |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 282 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 283 | def shutdown(self): |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 284 | """ |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 285 | Shutdown the controller closing all sockets |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 286 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 287 | @todo Might want to synchronize shutdown with self.sync... |
| 288 | """ |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 289 | self.active = False |
| 290 | try: |
| 291 | self.switch_socket.shutdown(socket.SHUT_RDWR) |
| 292 | except: |
| 293 | self.dbg(DEBUG_INFO, "Ignoring switch soc shutdown error") |
| 294 | self.switch_socket = None |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 295 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 296 | try: |
| 297 | self.listen_socket.shutdown(socket.SHUT_RDWR) |
| 298 | except: |
| 299 | self.dbg(DEBUG_INFO, "Ignoring listen soc shutdown error") |
| 300 | self.listen_socket = None |
| 301 | self.dbg_state = "down" |
| 302 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 303 | def _pkt_handler_check(self, pkt): |
| 304 | """ |
| 305 | Check for packet handling before being enqueued |
| 306 | |
| 307 | This includes checking for an ongoing transaction (see transact()) |
| 308 | an echo request in case keep_alive is true, followed by |
| 309 | registered message handlers. |
| 310 | @param pkt The raw packet (string) |
| 311 | @return (handled, msg) where handled is a boolean indicating |
| 312 | the message was handled; msg if None is the parsed message |
| 313 | """ |
| 314 | # Parse the header to get type |
| 315 | hdr = of_header_parse(pkt) |
| 316 | if not hdr: |
| 317 | self.dbg(DEBUG_INFO, "Could not parse header, pkt len", len(pkt)) |
| 318 | self.parse_errors += 1 |
| 319 | return (True, None) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 320 | # if self.debug_level <= DEBUG_VERBOSE: |
| 321 | # hdr.show() |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 322 | |
| 323 | self.dbg(DEBUG_VERBOSE, "message: len %d. type %s. hdr.len %d" % |
| 324 | (len(pkt), ofp_type_map[hdr.type], hdr.length)) |
| 325 | msg = of_message_parse(pkt) |
| 326 | if not msg: |
| 327 | self.parse_errors += 1 |
| 328 | self.dbg(DEBUG_WARN, "Could not parse message") |
| 329 | return (True, None) |
| 330 | |
| 331 | # Check if transaction is waiting |
| 332 | self.xid_cv.acquire() |
| 333 | if self.xid: |
| 334 | if hdr.xid == self.xid: |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 335 | self.xid_response = (msg, pkt) |
| 336 | self.xid = None |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 337 | self.xid_cv.notify() |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 338 | self.xid_cv.release() |
| 339 | return (True, None) |
| 340 | self.xid_cv.release() |
| 341 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 342 | # Check if anyone waiting on this type of message |
| 343 | self.expect_msg_cv.acquire() |
| 344 | if self.expect_msg: |
| 345 | if not self.expect_msg_type or self.expect_msg_type == hdr.type: |
| 346 | self.expect_msg_response = (msg, pkt) |
| 347 | self.expect_msg = False |
| 348 | self.expect_msg_cv.notify() |
| 349 | self.expect_msg_cv.release() |
| 350 | return (True, None) |
| 351 | self.expect_msg_cv.release() |
| 352 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 353 | # Check if keep alive is set; if so, respond to echo requests |
| 354 | if self.keep_alive: |
| 355 | if hdr.type == OFPT_ECHO_REQUEST: |
| 356 | rep = echo_reply() |
| 357 | rep.header.xid = hdr.xid |
| 358 | # Ignoring additional data |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 359 | self.message_send(rep.pack(), zero_xid=True) |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 360 | return (True, None) |
| 361 | |
| 362 | # Now check for message handlers; preference is given to |
| 363 | # handlers for a specific packet |
| 364 | handled = False |
| 365 | if hdr.type in self.handlers.keys(): |
| 366 | handled = self.handlers[hdr.type](self, msg, pkt) |
| 367 | if not handled and ("all" in self.handlers.keys()): |
| 368 | handled = self.handlers["all"](self, msg, pkt) |
| 369 | |
| 370 | return (handled, msg) |
| 371 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 372 | def register(self, msg_type, handler): |
| 373 | """ |
| 374 | Register a callback to receive a specific message type. |
| 375 | |
| 376 | Only one handler may be registered for a given message type. |
| 377 | @param msg_type The type of message to receive. May be DEFAULT |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 378 | for all non-handled packets. The special type, the string "all" |
| 379 | will send all packets to the handler. |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 380 | @param handler The function to call when a message of the given |
| 381 | type is received. |
| 382 | """ |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 383 | # Should check type is valid |
| 384 | if not handler and msg_type in self.handlers.keys(): |
| 385 | del self.handlers[msg_type] |
| 386 | return |
| 387 | self.handlers[msg_type] = handler |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 388 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 389 | def poll(self, exp_msg=None, timeout=None): |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 390 | """ |
| 391 | Wait for the next OF message received from the switch. |
| 392 | |
| 393 | @param exp_msg If set, return only when this type of message |
| 394 | is received. |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 395 | @param timeout If None, do not block. Otherwise, sleep in |
| 396 | intervals of 1 second until |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 397 | |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 398 | @retval A pair (msg, pkt) where msg is a message object and pkt |
| 399 | the string representing the packet as received from the socket. |
| 400 | This allows additional parsing by the receiver if necessary. |
| 401 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 402 | The data members in the message are in host endian order. |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 403 | If an error occurs, None is returned |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 404 | """ |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 405 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 406 | msg = pkt = None |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 407 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 408 | # First check the current queue |
| 409 | self.sync.acquire() |
| 410 | if len(self.packets) > 0: |
| 411 | if not exp_msg: |
| 412 | (msg, pkt) = self.packets.pop(0) |
| 413 | self.sync.release() |
| 414 | return (msg, pkt) |
| 415 | else: |
| 416 | for i in range(len(self.packets)): |
| 417 | msg = self.packets[i][0] |
| 418 | if msg.header.type == exp_msg: |
| 419 | (msg, pkt) = self.packets.pop(i) |
| 420 | self.sync.release() |
| 421 | return (msg, pkt) |
| 422 | |
| 423 | # Okay, not currently in the queue |
| 424 | if timeout is None or timeout <= 0: |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 425 | self.sync.release() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 426 | return (None, None) |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 427 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 428 | # Careful of race condition releasing sync before message cv |
Dan Talayco | 90576bd | 2010-02-19 10:59:02 -0800 | [diff] [blame] | 429 | # Also, this style is ripe for a lockup. |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 430 | self.expect_msg_cv.acquire() |
| 431 | self.sync.release() |
| 432 | self.expect_msg = True |
| 433 | self.expect_msg_type = exp_msg |
| 434 | self.expect_msg_cv.wait(timeout) |
| 435 | if self.expect_msg_response is not None: |
| 436 | (msg, pkt) = self.expect_msg_response |
| 437 | self.expect_msg_response = None |
| 438 | self.expect_msg_cv.release() |
| 439 | |
| 440 | if msg is None: |
| 441 | self.dbg(DEBUG_VERBOSE, "poll time out") |
| 442 | |
| 443 | return (msg, pkt) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 444 | |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 445 | def transact(self, msg, timeout=None, zero_xid=False): |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 446 | """ |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 447 | Run a message transaction with the switch |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 448 | |
| 449 | Send the message in msg and wait for a reply with a matching |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 450 | transaction id. Transactions have the highest priority in |
| 451 | received message handling. |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 452 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 453 | @param msg The message object to send; must not be a string |
| 454 | @param timeout The timeout in seconds (?) |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 455 | @param zero_xid Normally, if the XID is 0 an XID will be generated |
| 456 | for the message. Set xero_xid to override this behavior |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 457 | @return The matching message object or None if unsuccessful |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 458 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 459 | """ |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 460 | |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 461 | if not zero_xid and msg.header.xid == 0: |
| 462 | msg.header.xid = gen_xid() |
| 463 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 464 | self.xid_cv.acquire() |
| 465 | if self.xid: |
| 466 | self.xid_cv.release() |
| 467 | self.dbg(DEBUG_ERROR, |
| 468 | "Can only run one transaction at a time") |
| 469 | return None |
| 470 | |
| 471 | self.xid = msg.header.xid |
| 472 | self.xid_response = None |
| 473 | self.message_send(msg.pack()) |
| 474 | self.xid_cv.wait(timeout) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 475 | (msg, pkt) = self.xid_response |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 476 | self.xid_response = None |
| 477 | self.xid_cv.release() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 478 | return (msg, pkt) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 479 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 480 | def message_send(self, msg, zero_xid=False): |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 481 | """ |
| 482 | Send the message to the switch |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 483 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 484 | @param msg A string or OpenFlow message object to be forwarded to |
| 485 | the switch. |
| 486 | @param zero_xid If msg is an OpenFlow object (not a string) and if |
| 487 | the XID in the header is 0, then an XID will be generated |
| 488 | for the message. Set xero_xid to override this behavior (and keep an |
| 489 | existing 0 xid) |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 490 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 491 | @return -1 if error, 0 on success |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 492 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 493 | """ |
| 494 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 495 | if not self.switch_socket: |
| 496 | # Sending a string indicates the message is ready to go |
| 497 | self.dbg(DEBUG_INFO, "message_send: no socket") |
| 498 | return -1 |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 499 | #@todo If not string, try to pack |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 500 | if type(msg) != type(""): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 501 | try: |
| 502 | if msg.header.xid == 0 and not zero_xid: |
| 503 | msg.header.xid = gen_xid() |
| 504 | outpkt = msg.pack() |
| 505 | except: |
| 506 | self.dbg(DEBUG_INFO, |
| 507 | "message_send: not an OF message or string?") |
| 508 | return -1 |
| 509 | else: |
| 510 | outpkt = msg |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 511 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 512 | self.dbg(DEBUG_INFO, "Sending pkt of len " + str(len(outpkt))) |
| 513 | if self.switch_socket.sendall(outpkt) is None: |
| 514 | return 0 |
| 515 | |
| 516 | self.dbg(DEBUG_ERROR, "Unknown error on sendall") |
| 517 | return -1 |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 518 | |
| 519 | def __str__(self): |
| 520 | string = "Controller:\n" |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 521 | string += " state " + self.dbg_state + "\n" |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 522 | string += " switch_addr " + str(self.switch_addr) + "\n" |
| 523 | string += " pending pkts " + str(len(self.packets)) + "\n" |
| 524 | string += " total pkts " + str(self.packets_total) + "\n" |
| 525 | string += " expired pkts " + str(self.packets_expired) + "\n" |
| 526 | string += " handled pkts " + str(self.packets_handled) + "\n" |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 527 | string += " poll discards " + str(self.poll_discards) + "\n" |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 528 | string += " parse errors " + str(self.parse_errors) + "\n" |
| 529 | string += " sock errrors " + str(self.socket_errors) + "\n" |
| 530 | string += " max pkts " + str(self.max_pkts) + "\n" |
| 531 | string += " host " + str(self.host) + "\n" |
| 532 | string += " port " + str(self.port) + "\n" |
| 533 | string += " keep_alive " + str(self.keep_alive) + "\n" |
| 534 | return string |
| 535 | |
| 536 | def show(self): |
| 537 | print str(self) |
| 538 | |
| 539 | def sample_handler(controller, msg, pkt): |
| 540 | """ |
| 541 | Sample message handler |
| 542 | |
| 543 | This is the prototype for functions registered with the controller |
| 544 | class for packet reception |
| 545 | |
| 546 | @param controller The controller calling the handler |
| 547 | @param msg The parsed message object |
| 548 | @param pkt The raw packet that was received on the socket. This is |
| 549 | in case the packet contains extra unparsed data. |
| 550 | @returns Boolean value indicating if the packet was handled. If |
| 551 | not handled, the packet is placed in the queue for pollers to received |
| 552 | """ |
| 553 | pass |