Initialize rconn "connected" member.
[sliver-openvswitch.git] / lib / rconn.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
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:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
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
27  * SOFTWARE.
28  * 
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.
32  */
33
34 #include "rconn.h"
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "buffer.h"
40 #include "poll-loop.h"
41 #include "ofp-print.h"
42 #include "util.h"
43 #include "vconn.h"
44
45 #define THIS_MODULE VLM_rconn
46 #include "vlog.h"
47
48 /* A reliable connection to an OpenFlow switch or controller.
49  *
50  * See the large comment in rconn.h for more information. */
51 struct rconn {
52     bool reliable;
53     char *name;
54     struct vconn *vconn;
55     bool connected;
56     struct queue txq;
57     int txq_limit;
58     time_t backoff_deadline;
59     int backoff;
60 };
61
62 static struct rconn *create_rconn(const char *name, int txq_limit,
63                                   struct vconn *);
64 static int try_send(struct rconn *);
65 static void disconnect(struct rconn *, int error);
66
67 /* Creates and returns a new rconn that connects (and re-connects as necessary)
68  * to the vconn named 'name'.
69  *
70  * 'txq_limit' is the maximum length of the send queue, in packets. */
71 struct rconn *
72 rconn_new(const char *name, int txq_limit) 
73 {
74     return create_rconn(name, txq_limit, NULL);
75 }
76
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
79  * drops.
80  *
81  * 'txq_limit' is the maximum length of the send queue, in packets. */
82 struct rconn *
83 rconn_new_from_vconn(const char *name, int txq_limit, struct vconn *vconn)
84 {
85     assert(vconn != NULL);
86     return create_rconn(name, txq_limit, vconn);
87 }
88
89 /* Disconnects 'rc' and frees the underlying storage. */
90 void
91 rconn_destroy(struct rconn *rc)
92 {
93     if (rc) {
94         free(rc->name);
95         vconn_close(rc->vconn);
96         queue_destroy(&rc->txq);
97         free(rc);
98     }
99 }
100
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. */
104 void
105 rconn_run(struct rconn *rc)
106 {
107     if (!rc->vconn) {
108         if (rc->reliable && time(0) >= rc->backoff_deadline) {
109             int retval;
110
111             retval = vconn_open(rc->name, &rc->vconn);
112             if (!retval) {
113                 rc->backoff_deadline = time(0) + rc->backoff;
114                 rc->connected = false;
115             } else {
116                 VLOG_WARN("%s: connection failed (%s)",
117                           rc->name, strerror(retval)); 
118                 disconnect(rc, 0);
119             }
120         }
121     } else if (!rc->connected) {
122         int error = vconn_connect(rc->vconn);
123         if (!error) {
124             VLOG_WARN("%s: connected", rc->name);
125             if (vconn_is_passive(rc->vconn)) {
126                 fatal(0, "%s: passive vconn not supported in switch",
127                       rc->name);
128             }
129             rc->connected = true;
130         } else if (error != EAGAIN) {
131             VLOG_WARN("%s: connection failed (%s)", rc->name, strerror(error));
132             disconnect(rc, 0);
133         }
134     } else {
135         while (rc->txq.n > 0) {
136             int error = try_send(rc);
137             if (error == EAGAIN) {
138                 break;
139             } else if (error) {
140                 disconnect(rc, error);
141                 return;
142             }
143         }
144     }
145 }
146
147 /* Causes the next call to poll_block() to wake up when rconn_run() should be
148  * called on 'rc'. */
149 void
150 rconn_run_wait(struct rconn *rc) 
151 {
152     if (rc->vconn) {
153         if (rc->txq.n) {
154             vconn_wait(rc->vconn, WAIT_SEND);
155         }
156     } else {
157         poll_timer_wait((rc->backoff_deadline - time(0)) * 1000);
158     }
159 }
160
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()). */
164 struct buffer *
165 rconn_recv(struct rconn *rc)
166 {
167     if (rc->vconn && rc->connected) {
168         struct buffer *buffer;
169         int error = vconn_recv(rc->vconn, &buffer);
170         if (!error) {
171             return buffer;
172         } else if (error != EAGAIN) {
173             disconnect(rc, error); 
174         }
175     }
176     return NULL;
177 }
178
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'.  */
181 void
182 rconn_recv_wait(struct rconn *rc) 
183 {
184     if (rc->vconn) {
185         vconn_wait(rc->vconn, WAIT_RECV);
186     }
187 }
188
189 /* There is no rconn_send_wait() function: an rconn has a send queue that it
190  * takes care of sending if you call rconn_wait(), which will have the side
191  * effect of waking up poll_block(). */
192 int
193 rconn_send(struct rconn *rc, struct buffer *b) 
194 {
195     if (rc->vconn) {
196         if (rc->txq.n < rc->txq_limit) {
197             queue_push_tail(&rc->txq, b);
198             if (rc->txq.n == 1) {
199                 try_send(rc);
200             }
201             return 0;
202         } else {
203             return EAGAIN;
204         }
205     } else {
206         return ENOTCONN;
207     }
208 }
209
210 /* Returns 'rc''s name (the 'name' argument passed to rconn_new()). */
211 const char *
212 rconn_get_name(const struct rconn *rc) 
213 {
214     return rc->name;
215 }
216
217 /* Returns true if 'rconn' is connected or in the process of reconnecting,
218  * false if 'rconn' is disconnected and will not be reconnected. */
219 bool
220 rconn_is_alive(const struct rconn *rconn) 
221 {
222     return rconn->reliable || rconn->vconn;
223 }
224 \f
225 static struct rconn *
226 create_rconn(const char *name, int txq_limit, struct vconn *vconn)
227 {
228     struct rconn *rc = xmalloc(sizeof *rc);
229     assert(txq_limit > 0);
230     rc->reliable = vconn == NULL;
231     rc->connected = vconn != NULL;
232     rc->name = xstrdup(name);
233     rc->vconn = vconn;
234     queue_init(&rc->txq);
235     rc->txq_limit = txq_limit;
236     rc->backoff_deadline = 0;
237     rc->backoff = 0;
238     return rc;
239 }
240
241 /* Tries to send a packet from 'rc''s send buffer.  Returns 0 if successful,
242  * otherwise a positive errno value. */
243 static int
244 try_send(struct rconn *rc)
245 {
246     int retval = 0;
247     struct buffer *next = rc->txq.head->next;
248     retval = vconn_send(rc->vconn, rc->txq.head);
249     if (retval) {
250         return retval;
251     }
252     queue_advance_head(&rc->txq, next);
253     return 0;
254 }
255
256 /* Disconnects 'rc'.  'error' is used only for logging purposes.  If it is
257  * nonzero, then it should be EOF to indicate the connection was closed by the
258  * peer in a normal fashion or a positive errno value. */
259 static void
260 disconnect(struct rconn *rc, int error) 
261 {
262     time_t now = time(0);
263     
264     if (rc->vconn) {
265         if (error > 0) {
266             VLOG_WARN("%s: connection dropped (%s)",
267                       rc->name, strerror(error)); 
268         } else if (error == EOF) {
269             if (rc->reliable) {
270                 VLOG_WARN("%s: connection closed", rc->name);
271             }
272         } else {
273             VLOG_WARN("%s: connection dropped", rc->name); 
274         }
275         vconn_close(rc->vconn);
276         rc->vconn = NULL;
277         queue_clear(&rc->txq);
278     }
279
280     if (now >= rc->backoff_deadline) {
281         rc->backoff = 1;
282     } else {
283         rc->backoff = MIN(60, MAX(1, 2 * rc->backoff));
284         VLOG_WARN("%s: waiting %d seconds before reconnect\n",
285                   rc->name, rc->backoff);
286     }
287     rc->backoff_deadline = now + rc->backoff;
288 }