1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
40 #include "poll-loop.h"
41 #include "ofp-print.h"
45 #define THIS_MODULE VLM_rconn
48 /* A reliable connection to an OpenFlow switch or controller.
50 * See the large comment in rconn.h for more information. */
58 time_t backoff_deadline;
62 static struct rconn *create_rconn(const char *name, int txq_limit,
64 static int try_send(struct rconn *);
65 static void disconnect(struct rconn *, int error);
67 /* Creates and returns a new rconn that connects (and re-connects as necessary)
68 * to the vconn named 'name'.
70 * 'txq_limit' is the maximum length of the send queue, in packets. */
72 rconn_new(const char *name, int txq_limit)
74 return create_rconn(name, txq_limit, NULL);
77 /* Creates and returns a new rconn that is initially connected to 'vconn' and
78 * has the given 'name'. The rconn will not re-connect after the connection
81 * 'txq_limit' is the maximum length of the send queue, in packets. */
83 rconn_new_from_vconn(const char *name, int txq_limit, struct vconn *vconn)
85 assert(vconn != NULL);
86 return create_rconn(name, txq_limit, vconn);
89 /* Disconnects 'rc' and frees the underlying storage. */
91 rconn_destroy(struct rconn *rc)
95 vconn_close(rc->vconn);
96 queue_destroy(&rc->txq);
101 /* Performs whatever activities are necessary to maintain 'rc': if 'rc' is
102 * disconnected, attempts to (re)connect, backing off as necessary; if 'rc' is
103 * connected, attempts to send packets in the send queue, if any. */
105 rconn_run(struct rconn *rc)
108 if (rc->reliable && time(0) >= rc->backoff_deadline) {
111 retval = vconn_open(rc->name, &rc->vconn);
113 rc->backoff_deadline = time(0) + rc->backoff;
114 rc->connected = false;
116 VLOG_WARN("%s: connection failed (%s)",
117 rc->name, strerror(retval));
121 } else if (!rc->connected) {
122 int error = vconn_connect(rc->vconn);
124 VLOG_WARN("%s: connected", rc->name);
125 if (vconn_is_passive(rc->vconn)) {
126 fatal(0, "%s: passive vconn not supported in switch",
129 rc->connected = true;
130 } else if (error != EAGAIN) {
131 VLOG_WARN("%s: connection failed (%s)", rc->name, strerror(error));
135 while (rc->txq.n > 0) {
136 int error = try_send(rc);
137 if (error == EAGAIN) {
140 disconnect(rc, error);
147 /* Causes the next call to poll_block() to wake up when rconn_run() should be
150 rconn_run_wait(struct rconn *rc)
154 vconn_wait(rc->vconn, WAIT_SEND);
157 poll_timer_wait((rc->backoff_deadline - time(0)) * 1000);
161 /* Attempts to receive a packet from 'rc'. If successful, returns the packet;
162 * otherwise, returns a null pointer. The caller is responsible for freeing
163 * the packet (with buffer_delete()). */
165 rconn_recv(struct rconn *rc)
167 if (rc->vconn && rc->connected) {
168 struct buffer *buffer;
169 int error = vconn_recv(rc->vconn, &buffer);
172 } else if (error != EAGAIN) {
173 disconnect(rc, error);
179 /* Causes the next call to poll_block() to wake up when a packet may be ready
180 * to be received by vconn_recv() on 'rc'. */
182 rconn_recv_wait(struct rconn *rc)
185 vconn_wait(rc->vconn, WAIT_RECV);
189 /* Sends 'b' on 'rc'. Returns 0 if successful, EAGAIN if at least 'txq_limit'
190 * packets are already queued, otherwise a positive errno value. */
192 do_send(struct rconn *rc, struct buffer *b, int txq_limit)
195 if (rc->txq.n < txq_limit) {
196 queue_push_tail(&rc->txq, b);
197 if (rc->txq.n == 1) {
209 /* Sends 'b' on 'rc'. Returns 0 if successful, EAGAIN if the send queue is
210 * full, otherwise a positive errno value.
212 * There is no rconn_send_wait() function: an rconn has a send queue that it
213 * takes care of sending if you call rconn_wait(), which will have the side
214 * effect of waking up poll_block(). */
216 rconn_send(struct rconn *rc, struct buffer *b)
218 return do_send(rc, b, rc->txq_limit);
221 /* Sends 'b' on 'rc'. Returns 0 if successful, EAGAIN if the send queue is
222 * full, otherwise a positive errno value.
224 * Compared to rconn_send(), this function relaxes the queue limit, allowing
225 * more packets than usual to be queued. */
227 rconn_force_send(struct rconn *rc, struct buffer *b)
229 return do_send(rc, b, 2 * rc->txq_limit);
232 /* Returns true if 'rc''s send buffer is full,
233 * false if it has room for at least one more packet. */
235 rconn_is_full(const struct rconn *rc)
237 return rc->txq.n >= rc->txq_limit;
240 /* Returns 'rc''s name (the 'name' argument passed to rconn_new()). */
242 rconn_get_name(const struct rconn *rc)
247 /* Returns true if 'rconn' is connected or in the process of reconnecting,
248 * false if 'rconn' is disconnected and will not be reconnected. */
250 rconn_is_alive(const struct rconn *rconn)
252 return rconn->reliable || rconn->vconn;
255 /* Returns true if 'rconn' is connected, false otherwise. */
257 rconn_is_connected(const struct rconn *rconn)
259 return rconn->vconn && !vconn_connect(rconn->vconn);
262 static struct rconn *
263 create_rconn(const char *name, int txq_limit, struct vconn *vconn)
265 struct rconn *rc = xmalloc(sizeof *rc);
266 assert(txq_limit > 0);
267 rc->reliable = vconn == NULL;
268 rc->connected = vconn != NULL;
269 rc->name = xstrdup(name);
271 queue_init(&rc->txq);
272 rc->txq_limit = txq_limit;
273 rc->backoff_deadline = 0;
278 /* Tries to send a packet from 'rc''s send buffer. Returns 0 if successful,
279 * otherwise a positive errno value. */
281 try_send(struct rconn *rc)
284 struct buffer *next = rc->txq.head->next;
285 retval = vconn_send(rc->vconn, rc->txq.head);
289 queue_advance_head(&rc->txq, next);
293 /* Disconnects 'rc'. 'error' is used only for logging purposes. If it is
294 * nonzero, then it should be EOF to indicate the connection was closed by the
295 * peer in a normal fashion or a positive errno value. */
297 disconnect(struct rconn *rc, int error)
299 time_t now = time(0);
303 VLOG_WARN("%s: connection dropped (%s)",
304 rc->name, strerror(error));
305 } else if (error == EOF) {
307 VLOG_WARN("%s: connection closed", rc->name);
310 VLOG_WARN("%s: connection dropped", rc->name);
312 vconn_close(rc->vconn);
314 queue_clear(&rc->txq);
317 if (now >= rc->backoff_deadline) {
320 rc->backoff = MIN(60, MAX(1, 2 * rc->backoff));
321 VLOG_WARN("%s: waiting %d seconds before reconnect\n",
322 rc->name, rc->backoff);
324 rc->backoff_deadline = now + rc->backoff;