Merge "master" branch into "db".
[sliver-openvswitch.git] / lib / reconnect.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "reconnect.h"
19
20 #include <assert.h>
21 #include <stdlib.h>
22
23 #include "poll-loop.h"
24
25 #define THIS_MODULE VLM_reconnect
26 #include "vlog.h"
27
28 #define STATES                                  \
29     STATE(VOID, 1 << 0)                         \
30     STATE(BACKOFF, 1 << 1)                      \
31     STATE(CONNECTING, 1 << 2)                   \
32     STATE(ACTIVE, 1 << 3)                       \
33     STATE(IDLE, 1 << 4)                         \
34     STATE(RECONNECT, 1 << 5)
35 enum state {
36 #define STATE(NAME, VALUE) S_##NAME = VALUE,
37     STATES
38 #undef STATE
39 };
40
41 static bool
42 is_connected_state(enum state state)
43 {
44     return (state & (S_ACTIVE | S_IDLE)) != 0;
45 }
46
47 struct reconnect {
48     /* Configuration. */
49     char *name;
50     int min_backoff;
51     int max_backoff;
52     int probe_interval;
53
54     /* State. */
55     enum state state;
56     long long int state_entered;
57     int backoff;
58     long long int last_received;
59     long long int last_connected;
60
61     /* These values are simply for statistics reporting, not otherwise used
62      * directly by anything internal. */
63     long long int creation_time;
64     unsigned int n_attempted_connections, n_successful_connections;
65     unsigned int total_connected_duration;
66     unsigned int seqno;
67 };
68
69 static void reconnect_transition__(struct reconnect *, long long int now,
70                                    enum state state);
71 static long long int reconnect_deadline__(const struct reconnect *);
72
73 static const char *
74 reconnect_state_name__(enum state state)
75 {
76     switch (state) {
77 #define STATE(NAME, VALUE) case S_##NAME: return #NAME;
78         STATES
79 #undef STATE
80     }
81     return "***ERROR***";
82 }
83
84 /* Creates and returns a new reconnect FSM with default settings.  The FSM is
85  * initially disabled.  The caller will likely want to call reconnect_enable()
86  * and reconnect_set_name() on the returned object. */
87 struct reconnect *
88 reconnect_create(long long int now)
89 {
90     struct reconnect *fsm = xzalloc(sizeof *fsm);
91
92     fsm->name = xstrdup("void");
93     fsm->min_backoff = 1000;
94     fsm->max_backoff = 8000;
95     fsm->probe_interval = 5000;
96
97     fsm->state = S_VOID;
98     fsm->state_entered = now;
99     fsm->backoff = 0;
100     fsm->last_received = now;
101     fsm->last_connected = now;
102     fsm->creation_time = now;
103
104     return fsm;
105 }
106
107 /* Frees 'fsm'. */
108 void
109 reconnect_destroy(struct reconnect *fsm)
110 {
111     if (fsm) {
112         free(fsm->name);
113         free(fsm);
114     }
115 }
116
117 /* Returns 'fsm''s name. */
118 const char *
119 reconnect_get_name(const struct reconnect *fsm)
120 {
121     return fsm->name;
122 }
123
124 /* Sets 'fsm''s name to 'name'.  If 'name' is null, then "void" is used
125  * instead.
126  *
127  * The name set for 'fsm' is used in log messages. */
128 void
129 reconnect_set_name(struct reconnect *fsm, const char *name)
130 {
131     free(fsm->name);
132     fsm->name = xstrdup(name ? name : "void");
133 }
134
135 /* Return the minimum number of milliseconds to back off between consecutive
136  * connection attempts.  The default is 1000 ms. */
137 int
138 reconnect_get_min_backoff(const struct reconnect *fsm)
139 {
140     return fsm->min_backoff;
141 }
142
143 /* Return the maximum number of milliseconds to back off between consecutive
144  * connection attempts.  The default is 8000 ms. */
145 int
146 reconnect_get_max_backoff(const struct reconnect *fsm)
147 {
148     return fsm->max_backoff;
149 }
150
151 /* Returns the "probe interval" for 'fsm' in milliseconds.  If this is zero, it
152  * disables the connection keepalive feature.  If it is nonzero, then if the
153  * interval passes while 'fsm' is connected and without reconnect_received()
154  * being called for 'fsm', reconnect_run() returns RECONNECT_PROBE.  If the
155  * interval passes again without reconnect_received() being called,
156  * reconnect_run() returns RECONNECT_DISCONNECT for 'fsm'. */
157 int
158 reconnect_get_probe_interval(const struct reconnect *fsm)
159 {
160     return fsm->probe_interval;
161 }
162
163 /* Configures the backoff parameters for 'fsm'.  'min_backoff' is the minimum
164  * number of milliseconds, and 'max_backoff' is the maximum, between connection
165  * attempts.
166  *
167  * 'min_backoff' must be at least 1000, and 'max_backoff' must be greater than
168  * or equal to 'min_backoff'. */
169 void
170 reconnect_set_backoff(struct reconnect *fsm, int min_backoff, int max_backoff)
171 {
172     fsm->min_backoff = MAX(min_backoff, 1000);
173     fsm->max_backoff = max_backoff ? MAX(max_backoff, 1000) : 8000;
174     if (fsm->min_backoff > fsm->max_backoff) {
175         fsm->max_backoff = fsm->min_backoff;
176     }
177
178     if (fsm->state == S_BACKOFF && fsm->backoff > max_backoff) {
179         fsm->backoff = max_backoff;
180     }
181 }
182
183 /* Sets the "probe interval" for 'fsm' to 'probe_interval', in milliseconds.
184  * If this is zero, it disables the connection keepalive feature.  If it is
185  * nonzero, then if the interval passes while 'fsm' is connected and without
186  * reconnect_received() being called for 'fsm', reconnect_run() returns
187  * RECONNECT_PROBE.  If the interval passes again without reconnect_received()
188  * being called, reconnect_run() returns RECONNECT_DISCONNECT for 'fsm'.
189  *
190  * If 'probe_interval' is nonzero, then it will be forced to a value of at
191  * least 1000 ms. */
192 void
193 reconnect_set_probe_interval(struct reconnect *fsm, int probe_interval)
194 {
195     fsm->probe_interval = probe_interval ? MAX(1000, probe_interval) : 0;
196 }
197
198 /* Returns true if 'fsm' has been enabled with reconnect_enable().  Calling
199  * another function that indicates a change in connection state, such as
200  * reconnect_disconnected() or reconnect_force_reconnect(), will also enable
201  * a reconnect FSM. */
202 bool
203 reconnect_is_enabled(const struct reconnect *fsm)
204 {
205     return fsm->state != S_VOID;
206 }
207
208 /* If 'fsm' is disabled (the default for newly created FSMs), enables it, so
209  * that the next call to reconnect_run() for 'fsm' will return
210  * RECONNECT_CONNECT.
211  *
212  * If 'fsm' is not disabled, this function has no effect. */
213 void
214 reconnect_enable(struct reconnect *fsm, long long int now)
215 {
216     if (fsm->state == S_VOID) {
217         reconnect_transition__(fsm, now, S_BACKOFF);
218         fsm->backoff = 0;
219     }
220 }
221
222 /* Disables 'fsm'.  Until 'fsm' is enabled again, reconnect_run() will always
223  * return 0. */
224 void
225 reconnect_disable(struct reconnect *fsm, long long int now)
226 {
227     if (fsm->state != S_VOID) {
228         reconnect_transition__(fsm, now, S_VOID);
229     }
230 }
231
232 /* If 'fsm' is enabled and currently connected (or attempting to connect),
233  * forces reconnect_run() for 'fsm' to return RECONNECT_DISCONNECT the next
234  * time it is called, which should cause the client to drop the connection (or
235  * attempt), back off, and then reconnect. */
236 void
237 reconnect_force_reconnect(struct reconnect *fsm, long long int now)
238 {
239     if (fsm->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) {
240         reconnect_transition__(fsm, now, S_RECONNECT);
241     }
242 }
243
244 /* Tell 'fsm' that the connection dropped or that a connection attempt failed.
245  * 'error' specifies the reason: a positive value represents an errno value,
246  * EOF indicates that the connection was closed by the peer (e.g. read()
247  * returned 0), and 0 indicates no specific error.
248  *
249  * The FSM will back off, then reconnect. */
250 void
251 reconnect_disconnected(struct reconnect *fsm, long long int now, int error)
252 {
253     if (fsm->state != S_BACKOFF) {
254         /* Report what happened. */
255         if (fsm->state & (S_ACTIVE | S_IDLE)) {
256             if (error > 0) {
257                 VLOG_WARN("%s: connection dropped (%s)",
258                           fsm->name, strerror(error));
259             } else if (error == EOF) {
260                 VLOG_INFO("%s: connection closed by peer", fsm->name);
261             } else {
262                 VLOG_INFO("%s: connection dropped", fsm->name);
263             }
264         } else {
265             if (error > 0) {
266                 VLOG_WARN("%s: connection attempt failed (%s)",
267                           fsm->name, strerror(error));
268             } else {
269                 VLOG_INFO("%s: connection attempt timed out", fsm->name);
270             }
271         }
272
273         /* Back off. */
274         if (fsm->state & (S_ACTIVE | S_IDLE)
275             && fsm->last_received - fsm->last_connected >= fsm->backoff) {
276             fsm->backoff = fsm->min_backoff;
277         } else {
278             if (fsm->backoff < fsm->min_backoff) {
279                 fsm->backoff = fsm->min_backoff;
280             } else if (fsm->backoff >= fsm->max_backoff / 2) {
281                 fsm->backoff = fsm->max_backoff;
282             } else {
283                 fsm->backoff *= 2;
284             }
285             VLOG_INFO("%s: waiting %.3g seconds before reconnect\n",
286                       fsm->name, fsm->backoff / 1000.0);
287         }
288         reconnect_transition__(fsm, now, S_BACKOFF);
289     }
290 }
291
292 /* Tell 'fsm' that a connection attempt is in progress.
293  *
294  * The FSM will start a timer, after which the connection attempt will be
295  * aborted (by returning RECONNECT_DISCONNECT from reconect_run()).  */
296 void
297 reconnect_connecting(struct reconnect *fsm, long long int now)
298 {
299     if (fsm->state != S_CONNECTING) {
300         VLOG_INFO("%s: connecting...", fsm->name);
301         reconnect_transition__(fsm, now, S_CONNECTING);
302     }
303 }
304
305 /* Tell 'fsm' that the connection was successful.
306  *
307  * The FSM will start the probe interval timer, which is reset by
308  * reconnect_received().  If the timer expires, a probe will be sent (by
309  * returning RECONNECT_PROBE from reconnect_run()).  If the timer expires
310  * again without being reset, the connection will be aborted (by returning
311  * RECONNECT_DISCONNECT from reconnect_run()). */
312 void
313 reconnect_connected(struct reconnect *fsm, long long int now)
314 {
315     if (!is_connected_state(fsm->state)) {
316         reconnect_connecting(fsm, now);
317
318         VLOG_INFO("%s: connected", fsm->name);
319         reconnect_transition__(fsm, now, S_ACTIVE);
320         fsm->last_connected = now;
321     }
322 }
323
324 /* Tell 'fsm' that the connection attempt failed.
325  *
326  * The FSM will back off and attempt to reconnect. */
327 void
328 reconnect_connect_failed(struct reconnect *fsm, long long int now, int error)
329 {
330     reconnect_connecting(fsm, now);
331     reconnect_disconnected(fsm, now, error);
332 }
333
334 /* Tell 'fsm' that some data was received.  This resets the probe interval
335  * timer, so that the connection is known not to be idle. */
336 void
337 reconnect_received(struct reconnect *fsm, long long int now)
338 {
339     if (fsm->state != S_ACTIVE) {
340         reconnect_transition__(fsm, now, S_ACTIVE);
341     }
342     fsm->last_received = now;
343 }
344
345 static void
346 reconnect_transition__(struct reconnect *fsm, long long int now,
347                        enum state state)
348 {
349     if (fsm->state == S_CONNECTING) {
350         fsm->n_attempted_connections++;
351         if (state == S_ACTIVE) {
352             fsm->n_successful_connections++;
353         }
354     }
355     if (is_connected_state(fsm->state) != is_connected_state(state)) {
356         if (is_connected_state(fsm->state)) {
357             fsm->total_connected_duration += now - fsm->last_connected;
358         }
359         fsm->seqno++;
360     }
361
362     VLOG_DBG("%s: entering %s", fsm->name, reconnect_state_name__(state));
363     fsm->state = state;
364     fsm->state_entered = now;
365 }
366
367 static long long int
368 reconnect_deadline__(const struct reconnect *fsm)
369 {
370     assert(fsm->state_entered != LLONG_MIN);
371     switch (fsm->state) {
372     case S_VOID:
373         return LLONG_MAX;
374
375     case S_BACKOFF:
376         return fsm->state_entered + fsm->backoff;
377
378     case S_CONNECTING:
379         return fsm->state_entered + MAX(1000, fsm->backoff);
380
381     case S_ACTIVE:
382         if (fsm->probe_interval) {
383             long long int base = MAX(fsm->last_received, fsm->state_entered);
384             return base + fsm->probe_interval;
385         }
386         return LLONG_MAX;
387
388     case S_IDLE:
389         return fsm->state_entered + fsm->probe_interval;
390
391     case S_RECONNECT:
392         return fsm->state_entered;
393     }
394
395     NOT_REACHED();
396 }
397
398 /* Assesses whether any action should be taken on 'fsm'.  The return value is
399  * one of:
400  *
401  *     - 0: The client need not take any action.
402  *
403  *     - RECONNECT_CONNECT: The client should start a connection attempt and
404  *       indicate this by calling reconnect_connecting().  If the connection
405  *       attempt has definitely succeeded, it should call
406  *       reconnect_connected().  If the connection attempt has definitely
407  *       failed, it should call reconnect_connect_failed().
408  *
409  *       The FSM is smart enough to back off correctly after successful
410  *       connections that quickly abort, so it is OK to call
411  *       reconnect_connected() after a low-level successful connection
412  *       (e.g. connect()) even if the connection might soon abort due to a
413  *       failure at a high-level (e.g. SSL negotiation failure).
414  *
415  *     - RECONNECT_DISCONNECT: The client should abort the current connection
416  *       or connection attempt and call reconnect_disconnected() or
417  *       reconnect_connect_failed() to indicate it.
418  *
419  *     - RECONNECT_PROBE: The client should send some kind of request to the
420  *       peer that will elicit a response, to ensure that the connection is
421  *       indeed in working order.  (This will only be returned if the "probe
422  *       interval" is nonzero--see reconnect_set_probe_interval()).
423  */
424 enum reconnect_action
425 reconnect_run(struct reconnect *fsm, long long int now)
426 {
427     if (now >= reconnect_deadline__(fsm)) {
428         switch (fsm->state) {
429         case S_VOID:
430             return 0;
431
432         case S_BACKOFF:
433             return RECONNECT_CONNECT;
434
435         case S_CONNECTING:
436             return RECONNECT_DISCONNECT;
437
438         case S_ACTIVE:
439             VLOG_DBG("%s: idle %lld ms, sending inactivity probe", fsm->name,
440                      now - MAX(fsm->last_received, fsm->state_entered));
441             reconnect_transition__(fsm, now, S_IDLE);
442             return RECONNECT_PROBE;
443
444         case S_IDLE:
445             VLOG_ERR("%s: no response to inactivity probe after %.3g "
446                      "seconds, disconnecting",
447                      fsm->name, (now - fsm->state_entered) / 1000.0);
448             return RECONNECT_DISCONNECT;
449
450         case S_RECONNECT:
451             return RECONNECT_DISCONNECT;
452         }
453
454         NOT_REACHED();
455     } else {
456         return fsm->state == S_CONNECTING ? RECONNECT_CONNECT : 0;
457     }
458 }
459
460 /* Causes the next call to poll_block() to wake up when reconnect_run() should
461  * be called on 'fsm'. */
462 void
463 reconnect_wait(struct reconnect *fsm, long long int now)
464 {
465     int timeout = reconnect_timeout(fsm, now);
466     if (timeout >= 0) {
467         poll_timer_wait(timeout);
468     }
469 }
470
471 /* Returns the number of milliseconds after which reconnect_run() should be
472  * called on 'fsm' if nothing else notable happens in the meantime, or a
473  * negative number if this is currently unnecessary. */
474 int
475 reconnect_timeout(struct reconnect *fsm, long long int now)
476 {
477     long long int deadline = reconnect_deadline__(fsm);
478     if (deadline != LLONG_MAX) {
479         long long int remaining = deadline - now;
480         return MAX(0, MIN(INT_MAX, remaining));
481     }
482     return -1;
483 }
484
485 /* Returns true if 'fsm' is currently believed to be connected, that is, if
486  * reconnect_connected() was called more recently than any call to
487  * reconnect_connect_failed() or reconnect_disconnected() or
488  * reconnect_disable(), and false otherwise.  */
489 bool
490 reconnect_is_connected(const struct reconnect *fsm)
491 {
492     return is_connected_state(fsm->state);
493 }
494
495 /* Returns the number of milliseconds for which 'fsm' has been continuously
496  * connected to its peer.  (If 'fsm' is not currently connected, this is 0.) */
497 unsigned int
498 reconnect_get_connection_duration(const struct reconnect *fsm,
499                                   long long int now)
500 {
501     return reconnect_is_connected(fsm) ? now - fsm->last_connected : 0;
502 }
503
504 /* Copies various statistics for 'fsm' into '*stats'. */
505 void
506 reconnect_get_stats(const struct reconnect *fsm, long long int now,
507                     struct reconnect_stats *stats)
508 {
509     stats->creation_time = fsm->creation_time;
510     stats->last_received = fsm->last_received;
511     stats->last_connected = fsm->last_connected;
512     stats->backoff = fsm->backoff;
513     stats->seqno = fsm->seqno;
514     stats->is_connected = reconnect_is_connected(fsm);
515     stats->current_connection_duration
516         = reconnect_get_connection_duration(fsm, now);
517     stats->total_connected_duration = (stats->current_connection_duration
518                                        + fsm->total_connected_duration);
519     stats->n_attempted_connections = fsm->n_attempted_connections;
520     stats->n_successful_connections = fsm->n_successful_connections;
521     stats->state = reconnect_state_name__(fsm->state);
522     stats->state_elapsed = now - fsm->state_entered;
523 }