2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "reconnect.h"
23 #include "poll-loop.h"
26 VLOG_DEFINE_THIS_MODULE(reconnect);
30 STATE(BACKOFF, 1 << 1) \
31 STATE(CONNECTING, 1 << 3) \
32 STATE(ACTIVE, 1 << 4) \
34 STATE(RECONNECT, 1 << 6) \
35 STATE(LISTENING, 1 << 7)
37 #define STATE(NAME, VALUE) S_##NAME = VALUE,
43 is_connected_state(enum state state)
45 return (state & (S_ACTIVE | S_IDLE)) != 0;
55 enum vlog_level info; /* Used for informational messages. */
59 long long int state_entered;
61 long long int last_received;
62 long long int last_connected;
63 long long int last_disconnected;
64 unsigned int max_tries;
66 /* These values are simply for statistics reporting, not otherwise used
67 * directly by anything internal. */
68 long long int creation_time;
69 unsigned int n_attempted_connections, n_successful_connections;
70 unsigned int total_connected_duration;
74 static void reconnect_transition__(struct reconnect *, long long int now,
76 static long long int reconnect_deadline__(const struct reconnect *);
77 static bool reconnect_may_retry(struct reconnect *);
80 reconnect_state_name__(enum state state)
83 #define STATE(NAME, VALUE) case S_##NAME: return #NAME;
90 /* Creates and returns a new reconnect FSM with default settings. The FSM is
91 * initially disabled. The caller will likely want to call reconnect_enable()
92 * and reconnect_set_name() on the returned object. */
94 reconnect_create(long long int now)
96 struct reconnect *fsm = xzalloc(sizeof *fsm);
98 fsm->name = xstrdup("void");
99 fsm->min_backoff = RECONNECT_DEFAULT_MIN_BACKOFF;
100 fsm->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
101 fsm->probe_interval = RECONNECT_DEFAULT_PROBE_INTERVAL;
102 fsm->passive = false;
103 fsm->info = VLL_INFO;
106 fsm->state_entered = now;
108 fsm->last_received = now;
109 fsm->last_connected = LLONG_MAX;
110 fsm->last_disconnected = LLONG_MAX;
111 fsm->max_tries = UINT_MAX;
112 fsm->creation_time = now;
119 reconnect_destroy(struct reconnect *fsm)
127 /* If 'quiet' is true, 'fsm' will log informational messages at level VLL_DBG,
128 * by default keeping them out of log files. This is appropriate if the
129 * connection is one that is expected to be short-lived, so that the log
130 * messages are merely distracting.
132 * If 'quiet' is false, 'fsm' logs informational messages at level VLL_INFO.
133 * This is the default.
135 * This setting has no effect on the log level of debugging, warning, or error
138 reconnect_set_quiet(struct reconnect *fsm, bool quiet)
140 fsm->info = quiet ? VLL_DBG : VLL_INFO;
143 /* Returns 'fsm''s name. */
145 reconnect_get_name(const struct reconnect *fsm)
150 /* Sets 'fsm''s name to 'name'. If 'name' is null, then "void" is used
153 * The name set for 'fsm' is used in log messages. */
155 reconnect_set_name(struct reconnect *fsm, const char *name)
158 fsm->name = xstrdup(name ? name : "void");
161 /* Return the minimum number of milliseconds to back off between consecutive
162 * connection attempts. The default is RECONNECT_DEFAULT_MIN_BACKOFF. */
164 reconnect_get_min_backoff(const struct reconnect *fsm)
166 return fsm->min_backoff;
169 /* Return the maximum number of milliseconds to back off between consecutive
170 * connection attempts. The default is RECONNECT_DEFAULT_MAX_BACKOFF. */
172 reconnect_get_max_backoff(const struct reconnect *fsm)
174 return fsm->max_backoff;
177 /* Returns the "probe interval" for 'fsm' in milliseconds. If this is zero, it
178 * disables the connection keepalive feature. If it is nonzero, then if the
179 * interval passes while 'fsm' is connected and without reconnect_received()
180 * being called for 'fsm', reconnect_run() returns RECONNECT_PROBE. If the
181 * interval passes again without reconnect_received() being called,
182 * reconnect_run() returns RECONNECT_DISCONNECT for 'fsm'. */
184 reconnect_get_probe_interval(const struct reconnect *fsm)
186 return fsm->probe_interval;
189 /* Limits the maximum number of times that 'fsm' will ask the client to try to
190 * reconnect to 'max_tries'. UINT_MAX (the default) means an unlimited number
193 * After the number of tries has expired, the 'fsm' will disable itself
194 * instead of backing off and retrying. */
196 reconnect_set_max_tries(struct reconnect *fsm, unsigned int max_tries)
198 fsm->max_tries = max_tries;
201 /* Returns the current remaining number of connection attempts, UINT_MAX if
202 * the number is unlimited. */
204 reconnect_get_max_tries(struct reconnect *fsm)
206 return fsm->max_tries;
209 /* Configures the backoff parameters for 'fsm'. 'min_backoff' is the minimum
210 * number of milliseconds, and 'max_backoff' is the maximum, between connection
213 * 'min_backoff' must be at least 1000, and 'max_backoff' must be greater than
214 * or equal to 'min_backoff'.
216 * Pass 0 for 'min_backoff' or 'max_backoff' or both to use the defaults. */
218 reconnect_set_backoff(struct reconnect *fsm, int min_backoff, int max_backoff)
220 fsm->min_backoff = MAX(min_backoff, 1000);
221 fsm->max_backoff = (max_backoff
222 ? MAX(max_backoff, 1000)
223 : RECONNECT_DEFAULT_MAX_BACKOFF);
224 if (fsm->min_backoff > fsm->max_backoff) {
225 fsm->max_backoff = fsm->min_backoff;
228 if (fsm->state == S_BACKOFF && fsm->backoff > max_backoff) {
229 fsm->backoff = max_backoff;
233 /* Sets the "probe interval" for 'fsm' to 'probe_interval', in milliseconds.
234 * If this is zero, it disables the connection keepalive feature. If it is
235 * nonzero, then if the interval passes while 'fsm' is connected and without
236 * reconnect_received() being called for 'fsm', reconnect_run() returns
237 * RECONNECT_PROBE. If the interval passes again without reconnect_received()
238 * being called, reconnect_run() returns RECONNECT_DISCONNECT for 'fsm'.
240 * If 'probe_interval' is nonzero, then it will be forced to a value of at
243 reconnect_set_probe_interval(struct reconnect *fsm, int probe_interval)
245 fsm->probe_interval = probe_interval ? MAX(1000, probe_interval) : 0;
248 /* Returns true if 'fsm' is in passive mode, false if 'fsm' is in active mode
251 reconnect_is_passive(const struct reconnect *fsm)
256 /* Configures 'fsm' for active or passive mode. In active mode (the default),
257 * the FSM is attempting to connect to a remote host. In passive mode, the FSM
258 * is listening for connections from a remote host. */
260 reconnect_set_passive(struct reconnect *fsm, bool passive, long long int now)
262 if (fsm->passive != passive) {
263 fsm->passive = passive;
266 ? fsm->state & (S_CONNECTING | S_RECONNECT)
267 : fsm->state == S_LISTENING && reconnect_may_retry(fsm)) {
268 reconnect_transition__(fsm, now, S_BACKOFF);
274 /* Returns true if 'fsm' has been enabled with reconnect_enable(). Calling
275 * another function that indicates a change in connection state, such as
276 * reconnect_disconnected() or reconnect_force_reconnect(), will also enable
277 * a reconnect FSM. */
279 reconnect_is_enabled(const struct reconnect *fsm)
281 return fsm->state != S_VOID;
284 /* If 'fsm' is disabled (the default for newly created FSMs), enables it, so
285 * that the next call to reconnect_run() for 'fsm' will return
288 * If 'fsm' is not disabled, this function has no effect. */
290 reconnect_enable(struct reconnect *fsm, long long int now)
292 if (fsm->state == S_VOID && reconnect_may_retry(fsm)) {
293 reconnect_transition__(fsm, now, S_BACKOFF);
298 /* Disables 'fsm'. Until 'fsm' is enabled again, reconnect_run() will always
301 reconnect_disable(struct reconnect *fsm, long long int now)
303 if (fsm->state != S_VOID) {
304 reconnect_transition__(fsm, now, S_VOID);
308 /* If 'fsm' is enabled and currently connected (or attempting to connect),
309 * forces reconnect_run() for 'fsm' to return RECONNECT_DISCONNECT the next
310 * time it is called, which should cause the client to drop the connection (or
311 * attempt), back off, and then reconnect. */
313 reconnect_force_reconnect(struct reconnect *fsm, long long int now)
315 if (fsm->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) {
316 reconnect_transition__(fsm, now, S_RECONNECT);
320 /* Tell 'fsm' that the connection dropped or that a connection attempt failed.
321 * 'error' specifies the reason: a positive value represents an errno value,
322 * EOF indicates that the connection was closed by the peer (e.g. read()
323 * returned 0), and 0 indicates no specific error.
325 * The FSM will back off, then reconnect. */
327 reconnect_disconnected(struct reconnect *fsm, long long int now, int error)
329 if (!(fsm->state & (S_BACKOFF | S_VOID))) {
330 /* Report what happened. */
331 if (fsm->state & (S_ACTIVE | S_IDLE)) {
333 VLOG_WARN("%s: connection dropped (%s)",
334 fsm->name, strerror(error));
335 } else if (error == EOF) {
336 VLOG(fsm->info, "%s: connection closed by peer", fsm->name);
338 VLOG(fsm->info, "%s: connection dropped", fsm->name);
340 } else if (fsm->state == S_LISTENING) {
342 VLOG_WARN("%s: error listening for connections (%s)",
343 fsm->name, strerror(error));
345 VLOG(fsm->info, "%s: error listening for connections",
349 const char *type = fsm->passive ? "listen" : "connection";
351 VLOG_WARN("%s: %s attempt failed (%s)",
352 fsm->name, type, strerror(error));
354 VLOG(fsm->info, "%s: %s attempt timed out", fsm->name, type);
358 if (fsm->state & (S_ACTIVE | S_IDLE)) {
359 fsm->last_disconnected = now;
362 if (fsm->state & (S_ACTIVE | S_IDLE)
363 && (fsm->last_received - fsm->last_connected >= fsm->backoff
365 fsm->backoff = fsm->passive ? 0 : fsm->min_backoff;
367 if (fsm->backoff < fsm->min_backoff) {
368 fsm->backoff = fsm->min_backoff;
369 } else if (fsm->backoff >= fsm->max_backoff / 2) {
370 fsm->backoff = fsm->max_backoff;
375 VLOG(fsm->info, "%s: waiting %.3g seconds before trying to "
376 "listen again", fsm->name, fsm->backoff / 1000.0);
378 VLOG(fsm->info, "%s: waiting %.3g seconds before reconnect",
379 fsm->name, fsm->backoff / 1000.0);
383 reconnect_transition__(fsm, now,
384 reconnect_may_retry(fsm) ? S_BACKOFF : S_VOID);
388 /* Tell 'fsm' that a connection or listening attempt is in progress.
390 * The FSM will start a timer, after which the connection or listening attempt
391 * will be aborted (by returning RECONNECT_DISCONNECT from
392 * reconnect_run()). */
394 reconnect_connecting(struct reconnect *fsm, long long int now)
396 if (fsm->state != S_CONNECTING) {
398 VLOG(fsm->info, "%s: listening...", fsm->name);
400 VLOG(fsm->info, "%s: connecting...", fsm->name);
402 reconnect_transition__(fsm, now, S_CONNECTING);
406 /* Tell 'fsm' that the client is listening for connection attempts. This state
407 * last indefinitely until the client reports some change.
409 * The natural progression from this state is for the client to report that a
410 * connection has been accepted or is in progress of being accepted, by calling
411 * reconnect_connecting() or reconnect_connected().
413 * The client may also report that listening failed (e.g. accept() returned an
414 * unexpected error such as ENOMEM) by calling reconnect_listen_error(), in
415 * which case the FSM will back off and eventually return RECONNECT_CONNECT
416 * from reconnect_run() to tell the client to try listening again. */
418 reconnect_listening(struct reconnect *fsm, long long int now)
420 if (fsm->state != S_LISTENING) {
421 VLOG(fsm->info, "%s: listening...", fsm->name);
422 reconnect_transition__(fsm, now, S_LISTENING);
426 /* Tell 'fsm' that the client's attempt to accept a connection failed
427 * (e.g. accept() returned an unexpected error such as ENOMEM).
429 * If the FSM is currently listening (reconnect_listening() was called), it
430 * will back off and eventually return RECONNECT_CONNECT from reconnect_run()
431 * to tell the client to try listening again. If there is an active
432 * connection, this will be delayed until that connection drops. */
434 reconnect_listen_error(struct reconnect *fsm, long long int now, int error)
436 if (fsm->state == S_LISTENING) {
437 reconnect_disconnected(fsm, now, error);
441 /* Tell 'fsm' that the connection was successful.
443 * The FSM will start the probe interval timer, which is reset by
444 * reconnect_received(). If the timer expires, a probe will be sent (by
445 * returning RECONNECT_PROBE from reconnect_run()). If the timer expires
446 * again without being reset, the connection will be aborted (by returning
447 * RECONNECT_DISCONNECT from reconnect_run()). */
449 reconnect_connected(struct reconnect *fsm, long long int now)
451 if (!is_connected_state(fsm->state)) {
452 reconnect_connecting(fsm, now);
454 VLOG(fsm->info, "%s: connected", fsm->name);
455 reconnect_transition__(fsm, now, S_ACTIVE);
456 fsm->last_connected = now;
460 /* Tell 'fsm' that the connection attempt failed.
462 * The FSM will back off and attempt to reconnect. */
464 reconnect_connect_failed(struct reconnect *fsm, long long int now, int error)
466 reconnect_connecting(fsm, now);
467 reconnect_disconnected(fsm, now, error);
470 /* Tell 'fsm' that some data was received. This resets the probe interval
471 * timer, so that the connection is known not to be idle. */
473 reconnect_received(struct reconnect *fsm, long long int now)
475 if (fsm->state != S_ACTIVE) {
476 reconnect_transition__(fsm, now, S_ACTIVE);
478 fsm->last_received = now;
482 reconnect_transition__(struct reconnect *fsm, long long int now,
485 if (fsm->state == S_CONNECTING) {
486 fsm->n_attempted_connections++;
487 if (state == S_ACTIVE) {
488 fsm->n_successful_connections++;
491 if (is_connected_state(fsm->state) != is_connected_state(state)) {
492 if (is_connected_state(fsm->state)) {
493 fsm->total_connected_duration += now - fsm->last_connected;
498 VLOG_DBG("%s: entering %s", fsm->name, reconnect_state_name__(state));
500 fsm->state_entered = now;
504 reconnect_deadline__(const struct reconnect *fsm)
506 assert(fsm->state_entered != LLONG_MIN);
507 switch (fsm->state) {
513 return fsm->state_entered + fsm->backoff;
516 return fsm->state_entered + MAX(1000, fsm->backoff);
519 if (fsm->probe_interval) {
520 long long int base = MAX(fsm->last_received, fsm->state_entered);
521 return base + fsm->probe_interval;
526 return fsm->state_entered + fsm->probe_interval;
529 return fsm->state_entered;
535 /* Assesses whether any action should be taken on 'fsm'. The return value is
538 * - 0: The client need not take any action.
540 * - Active client, RECONNECT_CONNECT: The client should start a connection
541 * attempt and indicate this by calling reconnect_connecting(). If the
542 * connection attempt has definitely succeeded, it should call
543 * reconnect_connected(). If the connection attempt has definitely
544 * failed, it should call reconnect_connect_failed().
546 * The FSM is smart enough to back off correctly after successful
547 * connections that quickly abort, so it is OK to call
548 * reconnect_connected() after a low-level successful connection
549 * (e.g. connect()) even if the connection might soon abort due to a
550 * failure at a high-level (e.g. SSL negotiation failure).
552 * - Passive client, RECONNECT_CONNECT: The client should try to listen for
553 * a connection, if it is not already listening. It should call
554 * reconnect_listening() if successful, otherwise reconnect_connecting()
555 * or reconnected_connect_failed() if the attempt is in progress or
556 * definitely failed, respectively.
558 * A listening passive client should constantly attempt to accept a new
559 * connection and report an accepted connection with
560 * reconnect_connected().
562 * - RECONNECT_DISCONNECT: The client should abort the current connection
563 * or connection attempt or listen attempt and call
564 * reconnect_disconnected() or reconnect_connect_failed() to indicate it.
566 * - RECONNECT_PROBE: The client should send some kind of request to the
567 * peer that will elicit a response, to ensure that the connection is
568 * indeed in working order. (This will only be returned if the "probe
569 * interval" is nonzero--see reconnect_set_probe_interval()).
571 enum reconnect_action
572 reconnect_run(struct reconnect *fsm, long long int now)
574 if (now >= reconnect_deadline__(fsm)) {
575 switch (fsm->state) {
580 return RECONNECT_CONNECT;
583 return RECONNECT_DISCONNECT;
586 VLOG_DBG("%s: idle %lld ms, sending inactivity probe", fsm->name,
587 now - MAX(fsm->last_received, fsm->state_entered));
588 reconnect_transition__(fsm, now, S_IDLE);
589 return RECONNECT_PROBE;
592 VLOG_ERR("%s: no response to inactivity probe after %.3g "
593 "seconds, disconnecting",
594 fsm->name, (now - fsm->state_entered) / 1000.0);
595 return RECONNECT_DISCONNECT;
598 return RECONNECT_DISCONNECT;
610 /* Causes the next call to poll_block() to wake up when reconnect_run() should
611 * be called on 'fsm'. */
613 reconnect_wait(struct reconnect *fsm, long long int now)
615 int timeout = reconnect_timeout(fsm, now);
617 poll_timer_wait(timeout);
621 /* Returns the number of milliseconds after which reconnect_run() should be
622 * called on 'fsm' if nothing else notable happens in the meantime, or a
623 * negative number if this is currently unnecessary. */
625 reconnect_timeout(struct reconnect *fsm, long long int now)
627 long long int deadline = reconnect_deadline__(fsm);
628 if (deadline != LLONG_MAX) {
629 long long int remaining = deadline - now;
630 return MAX(0, MIN(INT_MAX, remaining));
635 /* Returns true if 'fsm' is currently believed to be connected, that is, if
636 * reconnect_connected() was called more recently than any call to
637 * reconnect_connect_failed() or reconnect_disconnected() or
638 * reconnect_disable(), and false otherwise. */
640 reconnect_is_connected(const struct reconnect *fsm)
642 return is_connected_state(fsm->state);
645 /* Returns the number of milliseconds since 'fsm' last successfully connected
646 * to its peer (even if it has since disconnected). Returns UINT_MAX if never
649 reconnect_get_last_connect_elapsed(const struct reconnect *fsm,
652 return fsm->last_connected == LLONG_MAX ? UINT_MAX
653 : now - fsm->last_connected;
656 /* Returns the number of milliseconds since 'fsm' last disconnected
657 * from its peer (even if it has since reconnected). Returns UINT_MAX if never
660 reconnect_get_last_disconnect_elapsed(const struct reconnect *fsm,
663 return fsm->last_disconnected == LLONG_MAX ? UINT_MAX
664 : now - fsm->last_disconnected;
667 /* Copies various statistics for 'fsm' into '*stats'. */
669 reconnect_get_stats(const struct reconnect *fsm, long long int now,
670 struct reconnect_stats *stats)
672 stats->creation_time = fsm->creation_time;
673 stats->last_received = fsm->last_received;
674 stats->last_connected = fsm->last_connected;
675 stats->last_disconnected = fsm->last_disconnected;
676 stats->backoff = fsm->backoff;
677 stats->seqno = fsm->seqno;
678 stats->is_connected = reconnect_is_connected(fsm);
679 stats->msec_since_connect
680 = reconnect_get_last_connect_elapsed(fsm, now);
681 stats->msec_since_disconnect
682 = reconnect_get_last_disconnect_elapsed(fsm, now);
683 stats->total_connected_duration = fsm->total_connected_duration
684 + (is_connected_state(fsm->state)
685 ? reconnect_get_last_connect_elapsed(fsm, now) : 0);
686 stats->n_attempted_connections = fsm->n_attempted_connections;
687 stats->n_successful_connections = fsm->n_successful_connections;
688 stats->state = reconnect_state_name__(fsm->state);
689 stats->state_elapsed = now - fsm->state_entered;
693 reconnect_may_retry(struct reconnect *fsm)
695 bool may_retry = fsm->max_tries > 0;
696 if (may_retry && fsm->max_tries != UINT_MAX) {