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 | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 40 | |
| 41 | class Controller(Thread): |
| 42 | """ |
| 43 | Class abstracting the control interface to the switch. |
| 44 | |
| 45 | For receiving messages, two mechanism will be implemented. First, |
| 46 | query the interface with poll. Second, register to have a |
| 47 | function called by message type. The callback is passed the |
| 48 | message type as well as the raw packet (or message object) |
| 49 | |
| 50 | One of the main purposes of this object is to translate between network |
| 51 | and host byte order. 'Above' this object, things should be in host |
| 52 | byte order. |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 53 | |
| 54 | @todo Consider using SocketServer for listening socket |
| 55 | @todo Test transaction code |
| 56 | |
| 57 | @var rcv_size The receive size to use for receive calls |
| 58 | @var max_pkts The max size of the receive queue |
| 59 | @var keep_alive If true, listen for echo requests and respond w/ |
| 60 | echo replies |
| 61 | @var host The host to use for connect |
| 62 | @var port The port to connect on |
| 63 | @var packets_total Total number of packets received |
| 64 | @var packets_expired Number of packets popped from queue as queue full |
| 65 | @var packets_handled Number of packets handled by something |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 66 | @var dbg_state Debug indication of state |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 67 | """ |
| 68 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 69 | def __init__(self, max_pkts=1024): |
| 70 | Thread.__init__(self) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 71 | # Socket related |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 72 | self.rcv_size = RCV_SIZE_DEFAULT |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 73 | self.listen_socket = None |
| 74 | self.switch_socket = None |
| 75 | self.switch_addr = None |
| 76 | |
| 77 | # Counters |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 78 | self.socket_errors = 0 |
| 79 | self.parse_errors = 0 |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 80 | self.packets_total = 0 |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 81 | self.packets_expired = 0 |
| 82 | self.packets_handled = 0 |
| 83 | |
| 84 | # State |
| 85 | self.connected = False |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 86 | self.packets = [] |
| 87 | self.sync = Lock() |
| 88 | self.handlers = {} |
| 89 | self.keep_alive = False |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 90 | |
| 91 | # Settings |
| 92 | self.max_pkts = max_pkts |
| 93 | self.passive = True |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 94 | self.host = controller_host |
| 95 | self.port = controller_port |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 96 | self.dbg_state = "init" |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 97 | # self.debug_level = DEBUG_VERBOSE |
| 98 | self.debug_level = debug_level_default |
| 99 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 100 | # Transaction variables |
| 101 | # xid_cv: Condition variable (semaphore) for transaction |
| 102 | # xid: Transaction ID being waited on |
| 103 | # xid_response: Transaction response message |
| 104 | self.xid_cv = Condition() |
| 105 | self.xid = None |
| 106 | self.xid_response = None |
| 107 | |
| 108 | def dbg(self, level, string): |
| 109 | debug_log("CTRL", self.debug_level, level, string) |
| 110 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 111 | def run(self): |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 112 | """ |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 113 | Activity function for class |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 114 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 115 | Assumes connection to switch already exists. Listens on |
| 116 | switch_socket for messages until an error (or zero len pkt) |
| 117 | occurs. |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 118 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 119 | When there is a message on the socket, check for handlers; queue the |
| 120 | packet if no one handles the packet. |
| 121 | |
| 122 | See note for controller describing the limitation of a single |
| 123 | connection for now. |
| 124 | """ |
| 125 | |
| 126 | if not self.switch_socket: |
| 127 | self.dbg(DEBUG_ERROR, |
| 128 | "Error in controller thread, no switch socket") |
| 129 | self.shutdown() |
| 130 | return |
| 131 | |
| 132 | # Read and process packets from socket connected to switch |
| 133 | while 1: |
| 134 | try: |
| 135 | pkt = self.switch_socket.recv(self.rcv_size) |
| 136 | except socket.error: |
| 137 | self.dbg(DEBUG_ERROR, "Controller socket read error") |
| 138 | self.socket_errors += 1 |
| 139 | break |
| 140 | |
| 141 | if len(pkt) == 0: |
| 142 | # Considered an error; usually means switch has disconnected |
| 143 | self.dbg(DEBUG_INFO, "length 0 pkt in") |
| 144 | self.socket_errors += 1 |
| 145 | break |
| 146 | |
| 147 | if not self.connected: |
| 148 | break |
| 149 | |
| 150 | (handled, msg) = self._pkt_handler_check(pkt) |
| 151 | if handled: |
| 152 | self.packets_handled += 1 |
| 153 | continue |
| 154 | |
| 155 | # Not handled, enqueue |
| 156 | self.sync.acquire() |
| 157 | if len(self.packets) >= self.max_pkts: |
| 158 | self.packets.pop(0) |
| 159 | self.packets_expired += 1 |
| 160 | self.packets.append((msg, pkt)) |
| 161 | self.packets_total += 1 |
| 162 | self.sync.release() |
| 163 | |
| 164 | self.shutdown() |
| 165 | |
| 166 | def connect(self, send_hello=True): |
| 167 | """ |
| 168 | Connect to a switch and start this thread |
| 169 | |
| 170 | Create the listening socket, accept and block until a |
| 171 | connection is made to the switch. Then start the local |
| 172 | thread and return. Parameters to the call are take from |
| 173 | member variables. |
| 174 | |
| 175 | @param send_hello If True, send the initial hello packet on connection |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 176 | @return Boolean where True indicates success |
| 177 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 178 | If already connected, returns True |
| 179 | If the listen socket does not exist, will create it |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 180 | """ |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 181 | |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 182 | self.dbg_state = "connecting" |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 183 | self.dbg(DEBUG_INFO, "open ctl host: >" + str(self.host) + "< port " + |
| 184 | str(self.port)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 185 | # Create the listening socket |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 186 | if not self.listen_socket: |
| 187 | self.listen_socket = socket.socket(socket.AF_INET, |
| 188 | socket.SOCK_STREAM) |
| 189 | self.listen_socket.setsockopt(socket.SOL_SOCKET, |
| 190 | socket.SO_REUSEADDR, 1) |
| 191 | self.listen_socket.bind((self.host, self.port)) |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 192 | self.dbg_state = "listening" |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 193 | self.listen_socket.listen(LISTEN_QUEUE_SIZE) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 194 | self.dbg_state = "accepting" |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 195 | (self.switch_socket, self.switch_addr) = self.listen_socket.accept() |
| 196 | if not self.switch_socket: |
| 197 | self.socket_errors += 1 |
| 198 | self.dbg(DEBUG_WARN, "Failed on accept") |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 199 | self.dbg_state = "error" |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 200 | self.listen_socket.close() |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 201 | return False |
| 202 | |
| 203 | self.connected = True |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 204 | self.dbg_state = "connected" |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 205 | self.dbg(DEBUG_INFO, "Got connection to " + str(self.switch_addr)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 206 | |
| 207 | if send_hello: |
| 208 | h = hello() |
| 209 | self.message_send(h.pack()) |
| 210 | |
| 211 | # Start the background thread |
| 212 | self.start() |
| 213 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 214 | return True |
| 215 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 216 | def shutdown(self): |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 217 | """ |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 218 | Shutdown the controller closing all sockets |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 219 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 220 | @todo Might want to synchronize shutdown with self.sync... |
| 221 | """ |
| 222 | self.connected = False |
| 223 | self.dbg_state = "closed" |
| 224 | if self.switch_socket: |
| 225 | try: |
| 226 | self.switch_socket.shutdown(socket.SHUT_RDWR) |
| 227 | except: |
| 228 | self.dbg(DEBUG_INFO, "Ignoring switch soc shutdown error") |
| 229 | self.switch_socket = None |
| 230 | |
| 231 | if self.listen_socket: |
| 232 | try: |
| 233 | self.listen_socket.shutdown(socket.SHUT_RDWR) |
| 234 | except: |
| 235 | self.dbg(DEBUG_INFO, "Ignoring listen soc shutdown error") |
| 236 | self.listen_socket = None |
| 237 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 238 | def _pkt_handler_check(self, pkt): |
| 239 | """ |
| 240 | Check for packet handling before being enqueued |
| 241 | |
| 242 | This includes checking for an ongoing transaction (see transact()) |
| 243 | an echo request in case keep_alive is true, followed by |
| 244 | registered message handlers. |
| 245 | @param pkt The raw packet (string) |
| 246 | @return (handled, msg) where handled is a boolean indicating |
| 247 | the message was handled; msg if None is the parsed message |
| 248 | """ |
| 249 | # Parse the header to get type |
| 250 | hdr = of_header_parse(pkt) |
| 251 | if not hdr: |
| 252 | self.dbg(DEBUG_INFO, "Could not parse header, pkt len", len(pkt)) |
| 253 | self.parse_errors += 1 |
| 254 | return (True, None) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 255 | # if self.debug_level <= DEBUG_VERBOSE: |
| 256 | # hdr.show() |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 257 | |
| 258 | self.dbg(DEBUG_VERBOSE, "message: len %d. type %s. hdr.len %d" % |
| 259 | (len(pkt), ofp_type_map[hdr.type], hdr.length)) |
| 260 | msg = of_message_parse(pkt) |
| 261 | if not msg: |
| 262 | self.parse_errors += 1 |
| 263 | self.dbg(DEBUG_WARN, "Could not parse message") |
| 264 | return (True, None) |
| 265 | |
| 266 | # Check if transaction is waiting |
| 267 | self.xid_cv.acquire() |
| 268 | if self.xid: |
| 269 | if hdr.xid == self.xid: |
| 270 | self.xid_response = msg |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 271 | self.xid_cv.notify() |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 272 | self.xid_cv.release() |
| 273 | return (True, None) |
| 274 | self.xid_cv.release() |
| 275 | |
| 276 | # Check if keep alive is set; if so, respond to echo requests |
| 277 | if self.keep_alive: |
| 278 | if hdr.type == OFPT_ECHO_REQUEST: |
| 279 | rep = echo_reply() |
| 280 | rep.header.xid = hdr.xid |
| 281 | # Ignoring additional data |
| 282 | self.message_send(rep.pack()) |
| 283 | return (True, None) |
| 284 | |
| 285 | # Now check for message handlers; preference is given to |
| 286 | # handlers for a specific packet |
| 287 | handled = False |
| 288 | if hdr.type in self.handlers.keys(): |
| 289 | handled = self.handlers[hdr.type](self, msg, pkt) |
| 290 | if not handled and ("all" in self.handlers.keys()): |
| 291 | handled = self.handlers["all"](self, msg, pkt) |
| 292 | |
| 293 | return (handled, msg) |
| 294 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 295 | def register(self, msg_type, handler): |
| 296 | """ |
| 297 | Register a callback to receive a specific message type. |
| 298 | |
| 299 | Only one handler may be registered for a given message type. |
| 300 | @param msg_type The type of message to receive. May be DEFAULT |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 301 | for all non-handled packets. The special type, the string "all" |
| 302 | will send all packets to the handler. |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 303 | @param handler The function to call when a message of the given |
| 304 | type is received. |
| 305 | """ |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 306 | # Should check type is valid |
| 307 | if not handler and msg_type in self.handlers.keys(): |
| 308 | del self.handlers[msg_type] |
| 309 | return |
| 310 | self.handlers[msg_type] = handler |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 311 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 312 | def poll(self, exp_msg=None, timeout=None): |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 313 | """ |
| 314 | Wait for the next OF message received from the switch. |
| 315 | |
| 316 | @param exp_msg If set, return only when this type of message |
| 317 | is received. |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 318 | @param timeout Not yet supported |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 319 | |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 320 | @retval A pair (msg, pkt) where msg is a message object and pkt |
| 321 | the string representing the packet as received from the socket. |
| 322 | This allows additional parsing by the receiver if necessary. |
| 323 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 324 | The data members in the message are in host endian order. |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 325 | If an error occurs, None is returned |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 326 | """ |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 327 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 328 | # For now do not support time out; |
| 329 | if timeout: |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 330 | self.dbg(DEBUG_WARN, "Poll time out not supported") |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 331 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 332 | while len(self.packets) > 0: |
| 333 | self.sync.acquire() |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 334 | (msg, pkt) = self.packets.pop(0) |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 335 | self.sync.release() |
| 336 | if not exp_msg or (exp_msg and (msg.header.type == exp_msg)): |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 337 | return msg, pkt |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 338 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 339 | return None, None |
| 340 | |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 341 | def transact(self, msg, timeout=None, zero_xid=False): |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 342 | """ |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 343 | Run a message transaction with the switch |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 344 | |
| 345 | 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] | 346 | transaction id. Transactions have the highest priority in |
| 347 | received message handling. |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 348 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 349 | @param msg The message object to send; must not be a string |
| 350 | @param timeout The timeout in seconds (?) |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 351 | @param zero_xid Normally, if the XID is 0 an XID will be generated |
| 352 | for the message. Set xero_xid to override this behavior |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 353 | @return The matching message object or None if unsuccessful |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 354 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 355 | """ |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 356 | |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 357 | if not zero_xid and msg.header.xid == 0: |
| 358 | msg.header.xid = gen_xid() |
| 359 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 360 | self.xid_cv.acquire() |
| 361 | if self.xid: |
| 362 | self.xid_cv.release() |
| 363 | self.dbg(DEBUG_ERROR, |
| 364 | "Can only run one transaction at a time") |
| 365 | return None |
| 366 | |
| 367 | self.xid = msg.header.xid |
| 368 | self.xid_response = None |
| 369 | self.message_send(msg.pack()) |
| 370 | self.xid_cv.wait(timeout) |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 371 | msg = self.xid_response |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 372 | self.xid_response = None |
| 373 | self.xid_cv.release() |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 374 | return msg |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 375 | |
| 376 | def message_send(self, msg): |
| 377 | """ |
| 378 | Send the message to the switch |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 379 | |
| 380 | @param msg An OpenFlow message object to be forwarded to the switch. |
| 381 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 382 | @return None on success |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 383 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 384 | """ |
| 385 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 386 | if not self.switch_socket: |
| 387 | # Sending a string indicates the message is ready to go |
| 388 | self.dbg(DEBUG_INFO, "message_send: no socket") |
| 389 | return -1 |
| 390 | |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 391 | if type(msg) != type(""): |
| 392 | # Sending a string indicates the message is ready to go |
| 393 | self.dbg(DEBUG_INFO, "message_send requires packet as string") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 394 | return -1 |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 395 | |
| 396 | self.dbg(DEBUG_INFO, "Sending pkt of len " + str(len(msg))) |
| 397 | return self.switch_socket.sendall(msg) |
| 398 | |
| 399 | def __str__(self): |
| 400 | string = "Controller:\n" |
| 401 | string += " connected " + str(self.connected) + "\n" |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 402 | string += " state " + self.dbg_state + "\n" |
Dan Talayco | 21c75c7 | 2010-02-12 22:59:24 -0800 | [diff] [blame] | 403 | string += " switch_addr " + str(self.switch_addr) + "\n" |
| 404 | string += " pending pkts " + str(len(self.packets)) + "\n" |
| 405 | string += " total pkts " + str(self.packets_total) + "\n" |
| 406 | string += " expired pkts " + str(self.packets_expired) + "\n" |
| 407 | string += " handled pkts " + str(self.packets_handled) + "\n" |
| 408 | string += " parse errors " + str(self.parse_errors) + "\n" |
| 409 | string += " sock errrors " + str(self.socket_errors) + "\n" |
| 410 | string += " max pkts " + str(self.max_pkts) + "\n" |
| 411 | string += " host " + str(self.host) + "\n" |
| 412 | string += " port " + str(self.port) + "\n" |
| 413 | string += " keep_alive " + str(self.keep_alive) + "\n" |
| 414 | return string |
| 415 | |
| 416 | def show(self): |
| 417 | print str(self) |
| 418 | |
| 419 | def sample_handler(controller, msg, pkt): |
| 420 | """ |
| 421 | Sample message handler |
| 422 | |
| 423 | This is the prototype for functions registered with the controller |
| 424 | class for packet reception |
| 425 | |
| 426 | @param controller The controller calling the handler |
| 427 | @param msg The parsed message object |
| 428 | @param pkt The raw packet that was received on the socket. This is |
| 429 | in case the packet contains extra unparsed data. |
| 430 | @returns Boolean value indicating if the packet was handled. If |
| 431 | not handled, the packet is placed in the queue for pollers to received |
| 432 | """ |
| 433 | pass |