python: Honor zero probe interval in reconnect.py
[cascardo/ovs.git] / lib / reconnect.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 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 #include "vlog.h"
25
26 VLOG_DEFINE_THIS_MODULE(reconnect);
27
28 #define STATES                                  \
29     STATE(VOID, 1 << 0)                         \
30     STATE(BACKOFF, 1 << 1)                      \
31     STATE(CONNECTING, 1 << 3)          \
32     STATE(ACTIVE, 1 << 4)                       \
33     STATE(IDLE, 1 << 5)                         \
34     STATE(RECONNECT, 1 << 6)                    \
35     STATE(LISTENING, 1 << 7)
36 enum state {
37 #define STATE(NAME, VALUE) S_##NAME = VALUE,
38     STATES
39 #undef STATE
40 };
41
42 static bool
43 is_connected_state(enum state state)
44 {
45     return (state & (S_ACTIVE | S_IDLE)) != 0;
46 }
47
48 struct reconnect {
49     /* Configuration. */
50     char *name;
51     int min_backoff;
52     int max_backoff;
53     int probe_interval;
54     bool passive;
55     enum vlog_level info;       /* Used for informational messages. */
56
57     uint8_t dscp;
58
59     /* State. */
60     enum state state;
61     long long int state_entered;
62     int backoff;
63     long long int last_received;
64     long long int last_connected;
65     long long int last_disconnected;
66     unsigned int max_tries;
67
68     /* These values are simply for statistics reporting, not otherwise used
69      * directly by anything internal. */
70     long long int creation_time;
71     unsigned int n_attempted_connections, n_successful_connections;
72     unsigned int total_connected_duration;
73     unsigned int seqno;
74 };
75
76 static void reconnect_transition__(struct reconnect *, long long int now,
77                                    enum state state);
78 static long long int reconnect_deadline__(const struct reconnect *);
79 static bool reconnect_may_retry(struct reconnect *);
80
81 static const char *
82 reconnect_state_name__(enum state state)
83 {
84     switch (state) {
85 #define STATE(NAME, VALUE) case S_##NAME: return #NAME;
86         STATES
87 #undef STATE
88     }
89     return "***ERROR***";
90 }
91
92 /* Creates and returns a new reconnect FSM with default settings.  The FSM is
93  * initially disabled.  The caller will likely want to call reconnect_enable()
94  * and reconnect_set_name() on the returned object. */
95 struct reconnect *
96 reconnect_create(long long int now)
97 {
98     struct reconnect *fsm = xzalloc(sizeof *fsm);
99
100     fsm->name = xstrdup("void");
101     fsm->min_backoff = RECONNECT_DEFAULT_MIN_BACKOFF;
102     fsm->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
103     fsm->probe_interval = RECONNECT_DEFAULT_PROBE_INTERVAL;
104     fsm->passive = false;
105     fsm->info = VLL_INFO;
106
107     fsm->state = S_VOID;
108     fsm->state_entered = now;
109     fsm->backoff = 0;
110     fsm->last_received = now;
111     fsm->last_connected = LLONG_MAX;
112     fsm->last_disconnected = LLONG_MAX;
113     fsm->max_tries = UINT_MAX;
114     fsm->creation_time = now;
115
116     return fsm;
117 }
118
119 /* Frees 'fsm'. */
120 void
121 reconnect_destroy(struct reconnect *fsm)
122 {
123     if (fsm) {
124         free(fsm->name);
125         free(fsm);
126     }
127 }
128
129 /* If 'quiet' is true, 'fsm' will log informational messages at level VLL_DBG,
130  * by default keeping them out of log files.  This is appropriate if the
131  * connection is one that is expected to be short-lived, so that the log
132  * messages are merely distracting.
133  *
134  * If 'quiet' is false, 'fsm' logs informational messages at level VLL_INFO.
135  * This is the default.
136  *
137  * This setting has no effect on the log level of debugging, warning, or error
138  * messages. */
139 void
140 reconnect_set_quiet(struct reconnect *fsm, bool quiet)
141 {
142     fsm->info = quiet ? VLL_DBG : VLL_INFO;
143 }
144
145 /* Returns 'fsm''s name. */
146 const char *
147 reconnect_get_name(const struct reconnect *fsm)
148 {
149     return fsm->name;
150 }
151
152 /* Sets 'fsm''s name to 'name'.  If 'name' is null, then "void" is used
153  * instead.
154  *
155  * The name set for 'fsm' is used in log messages. */
156 void
157 reconnect_set_name(struct reconnect *fsm, const char *name)
158 {
159     free(fsm->name);
160     fsm->name = xstrdup(name ? name : "void");
161 }
162
163 /* Return the minimum number of milliseconds to back off between consecutive
164  * connection attempts.  The default is RECONNECT_DEFAULT_MIN_BACKOFF. */
165 int
166 reconnect_get_min_backoff(const struct reconnect *fsm)
167 {
168     return fsm->min_backoff;
169 }
170
171 /* Return the maximum number of milliseconds to back off between consecutive
172  * connection attempts.  The default is RECONNECT_DEFAULT_MAX_BACKOFF. */
173 int
174 reconnect_get_max_backoff(const struct reconnect *fsm)
175 {
176     return fsm->max_backoff;
177 }
178
179 /* Returns the "probe interval" for 'fsm' in milliseconds.  If this is zero, it
180  * disables the connection keepalive feature.  If it is nonzero, then if the
181  * interval passes while 'fsm' is connected and without reconnect_received()
182  * being called for 'fsm', reconnect_run() returns RECONNECT_PROBE.  If the
183  * interval passes again without reconnect_received() being called,
184  * reconnect_run() returns RECONNECT_DISCONNECT for 'fsm'. */
185 int
186 reconnect_get_probe_interval(const struct reconnect *fsm)
187 {
188     return fsm->probe_interval;
189 }
190
191 /* Returns the dscp value used for establishing the connection between the
192  * manager and the database. */
193 uint8_t
194 reconnect_get_dscp(const struct reconnect *fsm)
195 {
196     return fsm->dscp;
197 }
198
199 /* Limits the maximum number of times that 'fsm' will ask the client to try to
200  * reconnect to 'max_tries'.  UINT_MAX (the default) means an unlimited number
201  * of tries.
202  *
203  * After the number of tries has expired, the 'fsm' will disable itself
204  * instead of backing off and retrying. */
205 void
206 reconnect_set_max_tries(struct reconnect *fsm, unsigned int max_tries)
207 {
208     fsm->max_tries = max_tries;
209 }
210
211 /* Returns the current remaining number of connection attempts, UINT_MAX if
212  * the number is unlimited.  */
213 unsigned int
214 reconnect_get_max_tries(struct reconnect *fsm)
215 {
216     return fsm->max_tries;
217 }
218
219 /* Configures the backoff parameters for 'fsm'.  'min_backoff' is the minimum
220  * number of milliseconds, and 'max_backoff' is the maximum, between connection
221  * attempts.
222  *
223  * 'min_backoff' must be at least 1000, and 'max_backoff' must be greater than
224  * or equal to 'min_backoff'.
225  *
226  * Pass 0 for 'min_backoff' or 'max_backoff' or both to use the defaults. */
227 void
228 reconnect_set_backoff(struct reconnect *fsm, int min_backoff, int max_backoff)
229 {
230     fsm->min_backoff = MAX(min_backoff, 1000);
231     fsm->max_backoff = (max_backoff
232                         ? MAX(max_backoff, 1000)
233                         : RECONNECT_DEFAULT_MAX_BACKOFF);
234     if (fsm->min_backoff > fsm->max_backoff) {
235         fsm->max_backoff = fsm->min_backoff;
236     }
237
238     if (fsm->state == S_BACKOFF && fsm->backoff > max_backoff) {
239         fsm->backoff = max_backoff;
240     }
241 }
242
243 /* Sets the "probe interval" for 'fsm' to 'probe_interval', in milliseconds.
244  * If this is zero, it disables the connection keepalive feature.  If it is
245  * nonzero, then if the interval passes while 'fsm' is connected and without
246  * reconnect_received() being called for 'fsm', reconnect_run() returns
247  * RECONNECT_PROBE.  If the interval passes again without reconnect_received()
248  * being called, reconnect_run() returns RECONNECT_DISCONNECT for 'fsm'.
249  *
250  * If 'probe_interval' is nonzero, then it will be forced to a value of at
251  * least 1000 ms. */
252 void
253 reconnect_set_probe_interval(struct reconnect *fsm, int probe_interval)
254 {
255     fsm->probe_interval = probe_interval ? MAX(1000, probe_interval) : 0;
256 }
257
258 /* Sets the dscp value to be used for establishing a connection between the
259  * manager and the database. */
260 void
261 reconnect_set_dscp(struct reconnect *fsm, uint8_t dscp)
262 {
263     fsm->dscp = dscp;
264 }
265
266 /* Returns true if 'fsm' is in passive mode, false if 'fsm' is in active mode
267  * (the default). */
268 bool
269 reconnect_is_passive(const struct reconnect *fsm)
270 {
271     return fsm->passive;
272 }
273
274 /* Configures 'fsm' for active or passive mode.  In active mode (the default),
275  * the FSM is attempting to connect to a remote host.  In passive mode, the FSM
276  * is listening for connections from a remote host. */
277 void
278 reconnect_set_passive(struct reconnect *fsm, bool passive, long long int now)
279 {
280     if (fsm->passive != passive) {
281         fsm->passive = passive;
282
283         if (passive
284             ? fsm->state & (S_CONNECTING | S_RECONNECT)
285             : fsm->state == S_LISTENING && reconnect_may_retry(fsm)) {
286             reconnect_transition__(fsm, now, S_BACKOFF);
287             fsm->backoff = 0;
288         }
289     }
290 }
291
292 /* Returns true if 'fsm' has been enabled with reconnect_enable().  Calling
293  * another function that indicates a change in connection state, such as
294  * reconnect_disconnected() or reconnect_force_reconnect(), will also enable
295  * a reconnect FSM. */
296 bool
297 reconnect_is_enabled(const struct reconnect *fsm)
298 {
299     return fsm->state != S_VOID;
300 }
301
302 /* If 'fsm' is disabled (the default for newly created FSMs), enables it, so
303  * that the next call to reconnect_run() for 'fsm' will return
304  * RECONNECT_CONNECT.
305  *
306  * If 'fsm' is not disabled, this function has no effect. */
307 void
308 reconnect_enable(struct reconnect *fsm, long long int now)
309 {
310     if (fsm->state == S_VOID && reconnect_may_retry(fsm)) {
311         reconnect_transition__(fsm, now, S_BACKOFF);
312         fsm->backoff = 0;
313     }
314 }
315
316 /* Disables 'fsm'.  Until 'fsm' is enabled again, reconnect_run() will always
317  * return 0. */
318 void
319 reconnect_disable(struct reconnect *fsm, long long int now)
320 {
321     if (fsm->state != S_VOID) {
322         reconnect_transition__(fsm, now, S_VOID);
323     }
324 }
325
326 /* If 'fsm' is enabled and currently connected (or attempting to connect),
327  * forces reconnect_run() for 'fsm' to return RECONNECT_DISCONNECT the next
328  * time it is called, which should cause the client to drop the connection (or
329  * attempt), back off, and then reconnect. */
330 void
331 reconnect_force_reconnect(struct reconnect *fsm, long long int now)
332 {
333     if (fsm->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) {
334         reconnect_transition__(fsm, now, S_RECONNECT);
335     }
336 }
337
338 /* Tell 'fsm' that the connection dropped or that a connection attempt failed.
339  * 'error' specifies the reason: a positive value represents an errno value,
340  * EOF indicates that the connection was closed by the peer (e.g. read()
341  * returned 0), and 0 indicates no specific error.
342  *
343  * The FSM will back off, then reconnect. */
344 void
345 reconnect_disconnected(struct reconnect *fsm, long long int now, int error)
346 {
347     if (!(fsm->state & (S_BACKOFF | S_VOID))) {
348         /* Report what happened. */
349         if (fsm->state & (S_ACTIVE | S_IDLE)) {
350             if (error > 0) {
351                 VLOG_WARN("%s: connection dropped (%s)",
352                           fsm->name, strerror(error));
353             } else if (error == EOF) {
354                 VLOG(fsm->info, "%s: connection closed by peer", fsm->name);
355             } else {
356                 VLOG(fsm->info, "%s: connection dropped", fsm->name);
357             }
358         } else if (fsm->state == S_LISTENING) {
359             if (error > 0) {
360                 VLOG_WARN("%s: error listening for connections (%s)",
361                           fsm->name, strerror(error));
362             } else {
363                 VLOG(fsm->info, "%s: error listening for connections",
364                      fsm->name);
365             }
366         } else {
367             const char *type = fsm->passive ? "listen" : "connection";
368             if (error > 0) {
369                 VLOG_WARN("%s: %s attempt failed (%s)",
370                           fsm->name, type, strerror(error));
371             } else {
372                 VLOG(fsm->info, "%s: %s attempt timed out", fsm->name, type);
373             }
374         }
375
376         if (fsm->state & (S_ACTIVE | S_IDLE)) {
377             fsm->last_disconnected = now;
378         }
379         /* Back off. */
380         if (fsm->state & (S_ACTIVE | S_IDLE)
381              && (fsm->last_received - fsm->last_connected >= fsm->backoff
382                  || fsm->passive)) {
383             fsm->backoff = fsm->passive ? 0 : fsm->min_backoff;
384         } else {
385             if (fsm->backoff < fsm->min_backoff) {
386                 fsm->backoff = fsm->min_backoff;
387             } else if (fsm->backoff >= fsm->max_backoff / 2) {
388                 fsm->backoff = fsm->max_backoff;
389             } else {
390                 fsm->backoff *= 2;
391             }
392             if (fsm->passive) {
393                 VLOG(fsm->info, "%s: waiting %.3g seconds before trying to "
394                           "listen again", fsm->name, fsm->backoff / 1000.0);
395             } else {
396                 VLOG(fsm->info, "%s: waiting %.3g seconds before reconnect",
397                           fsm->name, fsm->backoff / 1000.0);
398             }
399         }
400
401         reconnect_transition__(fsm, now,
402                                reconnect_may_retry(fsm) ? S_BACKOFF : S_VOID);
403     }
404 }
405
406 /* Tell 'fsm' that a connection or listening attempt is in progress.
407  *
408  * The FSM will start a timer, after which the connection or listening attempt
409  * will be aborted (by returning RECONNECT_DISCONNECT from
410  * reconnect_run()).  */
411 void
412 reconnect_connecting(struct reconnect *fsm, long long int now)
413 {
414     if (fsm->state != S_CONNECTING) {
415         if (fsm->passive) {
416             VLOG(fsm->info, "%s: listening...", fsm->name);
417         } else {
418             VLOG(fsm->info, "%s: connecting...", fsm->name);
419         }
420         reconnect_transition__(fsm, now, S_CONNECTING);
421     }
422 }
423
424 /* Tell 'fsm' that the client is listening for connection attempts.  This state
425  * last indefinitely until the client reports some change.
426  *
427  * The natural progression from this state is for the client to report that a
428  * connection has been accepted or is in progress of being accepted, by calling
429  * reconnect_connecting() or reconnect_connected().
430  *
431  * The client may also report that listening failed (e.g. accept() returned an
432  * unexpected error such as ENOMEM) by calling reconnect_listen_error(), in
433  * which case the FSM will back off and eventually return RECONNECT_CONNECT
434  * from reconnect_run() to tell the client to try listening again. */
435 void
436 reconnect_listening(struct reconnect *fsm, long long int now)
437 {
438     if (fsm->state != S_LISTENING) {
439         VLOG(fsm->info, "%s: listening...", fsm->name);
440         reconnect_transition__(fsm, now, S_LISTENING);
441     }
442 }
443
444 /* Tell 'fsm' that the client's attempt to accept a connection failed
445  * (e.g. accept() returned an unexpected error such as ENOMEM).
446  *
447  * If the FSM is currently listening (reconnect_listening() was called), it
448  * will back off and eventually return RECONNECT_CONNECT from reconnect_run()
449  * to tell the client to try listening again.  If there is an active
450  * connection, this will be delayed until that connection drops. */
451 void
452 reconnect_listen_error(struct reconnect *fsm, long long int now, int error)
453 {
454     if (fsm->state == S_LISTENING) {
455         reconnect_disconnected(fsm, now, error);
456     }
457 }
458
459 /* Tell 'fsm' that the connection was successful.
460  *
461  * The FSM will start the probe interval timer, which is reset by
462  * reconnect_received().  If the timer expires, a probe will be sent (by
463  * returning RECONNECT_PROBE from reconnect_run()).  If the timer expires
464  * again without being reset, the connection will be aborted (by returning
465  * RECONNECT_DISCONNECT from reconnect_run()). */
466 void
467 reconnect_connected(struct reconnect *fsm, long long int now)
468 {
469     if (!is_connected_state(fsm->state)) {
470         reconnect_connecting(fsm, now);
471
472         VLOG(fsm->info, "%s: connected", fsm->name);
473         reconnect_transition__(fsm, now, S_ACTIVE);
474         fsm->last_connected = now;
475     }
476 }
477
478 /* Tell 'fsm' that the connection attempt failed.
479  *
480  * The FSM will back off and attempt to reconnect. */
481 void
482 reconnect_connect_failed(struct reconnect *fsm, long long int now, int error)
483 {
484     reconnect_connecting(fsm, now);
485     reconnect_disconnected(fsm, now, error);
486 }
487
488 /* Tell 'fsm' that some data was received.  This resets the probe interval
489  * timer, so that the connection is known not to be idle. */
490 void
491 reconnect_received(struct reconnect *fsm, long long int now)
492 {
493     if (fsm->state != S_ACTIVE) {
494         reconnect_transition__(fsm, now, S_ACTIVE);
495     }
496     fsm->last_received = now;
497 }
498
499 static void
500 reconnect_transition__(struct reconnect *fsm, long long int now,
501                        enum state state)
502 {
503     if (fsm->state == S_CONNECTING) {
504         fsm->n_attempted_connections++;
505         if (state == S_ACTIVE) {
506             fsm->n_successful_connections++;
507         }
508     }
509     if (is_connected_state(fsm->state) != is_connected_state(state)) {
510         if (is_connected_state(fsm->state)) {
511             fsm->total_connected_duration += now - fsm->last_connected;
512         }
513         fsm->seqno++;
514     }
515
516     VLOG_DBG("%s: entering %s", fsm->name, reconnect_state_name__(state));
517     fsm->state = state;
518     fsm->state_entered = now;
519 }
520
521 static long long int
522 reconnect_deadline__(const struct reconnect *fsm)
523 {
524     assert(fsm->state_entered != LLONG_MIN);
525     switch (fsm->state) {
526     case S_VOID:
527     case S_LISTENING:
528         return LLONG_MAX;
529
530     case S_BACKOFF:
531         return fsm->state_entered + fsm->backoff;
532
533     case S_CONNECTING:
534         return fsm->state_entered + MAX(1000, fsm->backoff);
535
536     case S_ACTIVE:
537         if (fsm->probe_interval) {
538             long long int base = MAX(fsm->last_received, fsm->state_entered);
539             return base + fsm->probe_interval;
540         }
541         return LLONG_MAX;
542
543     case S_IDLE:
544         if (fsm->probe_interval) {
545             return fsm->state_entered + fsm->probe_interval;
546         }
547         return LLONG_MAX;
548
549     case S_RECONNECT:
550         return fsm->state_entered;
551     }
552
553     NOT_REACHED();
554 }
555
556 /* Assesses whether any action should be taken on 'fsm'.  The return value is
557  * one of:
558  *
559  *     - 0: The client need not take any action.
560  *
561  *     - Active client, RECONNECT_CONNECT: The client should start a connection
562  *       attempt and indicate this by calling reconnect_connecting().  If the
563  *       connection attempt has definitely succeeded, it should call
564  *       reconnect_connected().  If the connection attempt has definitely
565  *       failed, it should call reconnect_connect_failed().
566  *
567  *       The FSM is smart enough to back off correctly after successful
568  *       connections that quickly abort, so it is OK to call
569  *       reconnect_connected() after a low-level successful connection
570  *       (e.g. connect()) even if the connection might soon abort due to a
571  *       failure at a high-level (e.g. SSL negotiation failure).
572  *
573  *     - Passive client, RECONNECT_CONNECT: The client should try to listen for
574  *       a connection, if it is not already listening.  It should call
575  *       reconnect_listening() if successful, otherwise reconnect_connecting()
576  *       or reconnected_connect_failed() if the attempt is in progress or
577  *       definitely failed, respectively.
578  *
579  *       A listening passive client should constantly attempt to accept a new
580  *       connection and report an accepted connection with
581  *       reconnect_connected().
582  *
583  *     - RECONNECT_DISCONNECT: The client should abort the current connection
584  *       or connection attempt or listen attempt and call
585  *       reconnect_disconnected() or reconnect_connect_failed() to indicate it.
586  *
587  *     - RECONNECT_PROBE: The client should send some kind of request to the
588  *       peer that will elicit a response, to ensure that the connection is
589  *       indeed in working order.  (This will only be returned if the "probe
590  *       interval" is nonzero--see reconnect_set_probe_interval()).
591  */
592 enum reconnect_action
593 reconnect_run(struct reconnect *fsm, long long int now)
594 {
595     if (now >= reconnect_deadline__(fsm)) {
596         switch (fsm->state) {
597         case S_VOID:
598             return 0;
599
600         case S_BACKOFF:
601             return RECONNECT_CONNECT;
602
603         case S_CONNECTING:
604             return RECONNECT_DISCONNECT;
605
606         case S_ACTIVE:
607             VLOG_DBG("%s: idle %lld ms, sending inactivity probe", fsm->name,
608                      now - MAX(fsm->last_received, fsm->state_entered));
609             reconnect_transition__(fsm, now, S_IDLE);
610             return RECONNECT_PROBE;
611
612         case S_IDLE:
613             VLOG_ERR("%s: no response to inactivity probe after %.3g "
614                      "seconds, disconnecting",
615                      fsm->name, (now - fsm->state_entered) / 1000.0);
616             return RECONNECT_DISCONNECT;
617
618         case S_RECONNECT:
619             return RECONNECT_DISCONNECT;
620
621         case S_LISTENING:
622             return 0;
623         }
624
625         NOT_REACHED();
626     } else {
627         return 0;
628     }
629 }
630
631 /* Causes the next call to poll_block() to wake up when reconnect_run() should
632  * be called on 'fsm'. */
633 void
634 reconnect_wait(struct reconnect *fsm, long long int now)
635 {
636     int timeout = reconnect_timeout(fsm, now);
637     if (timeout >= 0) {
638         poll_timer_wait(timeout);
639     }
640 }
641
642 /* Returns the number of milliseconds after which reconnect_run() should be
643  * called on 'fsm' if nothing else notable happens in the meantime, or a
644  * negative number if this is currently unnecessary. */
645 int
646 reconnect_timeout(struct reconnect *fsm, long long int now)
647 {
648     long long int deadline = reconnect_deadline__(fsm);
649     if (deadline != LLONG_MAX) {
650         long long int remaining = deadline - now;
651         return MAX(0, MIN(INT_MAX, remaining));
652     }
653     return -1;
654 }
655
656 /* Returns true if 'fsm' is currently believed to be connected, that is, if
657  * reconnect_connected() was called more recently than any call to
658  * reconnect_connect_failed() or reconnect_disconnected() or
659  * reconnect_disable(), and false otherwise.  */
660 bool
661 reconnect_is_connected(const struct reconnect *fsm)
662 {
663     return is_connected_state(fsm->state);
664 }
665
666 /* Returns the number of milliseconds since 'fsm' last successfully connected
667  * to its peer (even if it has since disconnected). Returns UINT_MAX if never
668  * connected. */
669 unsigned int
670 reconnect_get_last_connect_elapsed(const struct reconnect *fsm,
671                                    long long int now)
672 {
673     return fsm->last_connected == LLONG_MAX ? UINT_MAX
674         : now - fsm->last_connected;
675 }
676
677 /* Returns the number of milliseconds since 'fsm' last disconnected
678  * from its peer (even if it has since reconnected). Returns UINT_MAX if never
679  * disconnected. */
680 unsigned int
681 reconnect_get_last_disconnect_elapsed(const struct reconnect *fsm,
682                                       long long int now)
683 {
684     return fsm->last_disconnected == LLONG_MAX ? UINT_MAX
685         : now - fsm->last_disconnected;
686 }
687
688 /* Copies various statistics for 'fsm' into '*stats'. */
689 void
690 reconnect_get_stats(const struct reconnect *fsm, long long int now,
691                     struct reconnect_stats *stats)
692 {
693     stats->creation_time = fsm->creation_time;
694     stats->last_received = fsm->last_received;
695     stats->last_connected = fsm->last_connected;
696     stats->last_disconnected = fsm->last_disconnected;
697     stats->backoff = fsm->backoff;
698     stats->seqno = fsm->seqno;
699     stats->is_connected = reconnect_is_connected(fsm);
700     stats->msec_since_connect
701         = reconnect_get_last_connect_elapsed(fsm, now);
702     stats->msec_since_disconnect
703         = reconnect_get_last_disconnect_elapsed(fsm, now);
704     stats->total_connected_duration = fsm->total_connected_duration
705         + (is_connected_state(fsm->state)
706            ? reconnect_get_last_connect_elapsed(fsm, now) : 0);
707     stats->n_attempted_connections = fsm->n_attempted_connections;
708     stats->n_successful_connections = fsm->n_successful_connections;
709     stats->state = reconnect_state_name__(fsm->state);
710     stats->state_elapsed = now - fsm->state_entered;
711 }
712
713 static bool
714 reconnect_may_retry(struct reconnect *fsm)
715 {
716     bool may_retry = fsm->max_tries > 0;
717     if (may_retry && fsm->max_tries != UINT_MAX) {
718         fsm->max_tries--;
719     }
720     return may_retry;
721 }