#include <stdlib.h>
#include <string.h>
#include "coverage.h"
+#include "ofp-util.h"
#include "ofpbuf.h"
#include "openflow/openflow.h"
#include "poll-loop.h"
#include "timeval.h"
#include "util.h"
#include "vconn.h"
-
-#define THIS_MODULE VLM_rconn
#include "vlog.h"
+VLOG_DEFINE_THIS_MODULE(rconn)
+
#define STATES \
STATE(VOID, 1 << 0) \
STATE(BACKOFF, 1 << 1) \
time_t state_entered;
struct vconn *vconn;
- char *name;
+ char *name; /* Human-readable descriptive name. */
+ char *target; /* vconn name, passed to vconn_open(). */
bool reliable;
struct ovs_queue txq;
static unsigned int timeout(const struct rconn *);
static bool timed_out(const struct rconn *);
static void state_transition(struct rconn *, enum state);
-static void set_vconn_name(struct rconn *, const char *name);
+static void rconn_set_target__(struct rconn *,
+ const char *target, const char *name);
static int try_send(struct rconn *);
-static int reconnect(struct rconn *);
+static void reconnect(struct rconn *);
static void report_error(struct rconn *, int error);
static void disconnect(struct rconn *, int error);
static void flush_queue(struct rconn *);
static void copy_to_monitor(struct rconn *, const struct ofpbuf *);
static bool is_connected_state(enum state);
static bool is_admitted_msg(const struct ofpbuf *);
-
-/* Creates a new rconn, connects it (reliably) to 'name', and returns it. */
-struct rconn *
-rconn_new(const char *name, int inactivity_probe_interval, int max_backoff)
-{
- struct rconn *rc = rconn_create(inactivity_probe_interval, max_backoff);
- rconn_connect(rc, name);
- return rc;
-}
-
-/* Creates a new rconn, connects it (unreliably) to 'vconn', and returns it. */
-struct rconn *
-rconn_new_from_vconn(const char *name, struct vconn *vconn)
-{
- struct rconn *rc = rconn_create(60, 0);
- rconn_connect_unreliably(rc, name, vconn);
- return rc;
-}
+static bool rconn_logging_connection_attempts__(const struct rconn *);
/* Creates and returns a new rconn.
*
* 'max_backoff' is the maximum number of seconds between attempts to connect
* to the peer. The actual interval starts at 1 second and doubles on each
* failure until it reaches 'max_backoff'. If 0 is specified, the default of
- * 8 seconds is used. */
+ * 8 seconds is used.
+ *
+ * The new rconn is initially unconnected. Use rconn_connect() or
+ * rconn_connect_unreliably() to connect it. */
struct rconn *
rconn_create(int probe_interval, int max_backoff)
{
rc->vconn = NULL;
rc->name = xstrdup("void");
+ rc->target = xstrdup("void");
rc->reliable = false;
queue_init(&rc->txq);
return rc->probe_interval;
}
-int
-rconn_connect(struct rconn *rc, const char *name)
+/* Drops any existing connection on 'rc', then sets up 'rc' to connect to
+ * 'target' and reconnect as needed. 'target' should be a remote OpenFlow
+ * target in a form acceptable to vconn_open().
+ *
+ * If 'name' is nonnull, then it is used in log messages in place of 'target'.
+ * It should presumably give more information to a human reader than 'target',
+ * but it need not be acceptable to vconn_open(). */
+void
+rconn_connect(struct rconn *rc, const char *target, const char *name)
{
rconn_disconnect(rc);
- set_vconn_name(rc, name);
+ rconn_set_target__(rc, target, name);
rc->reliable = true;
- return reconnect(rc);
+ reconnect(rc);
}
+/* Drops any existing connection on 'rc', then configures 'rc' to use
+ * 'vconn'. If the connection on 'vconn' drops, 'rc' will not reconnect on it
+ * own.
+ *
+ * By default, the target obtained from vconn_get_name(vconn) is used in log
+ * messages. If 'name' is nonnull, then it is used instead. It should
+ * presumably give more information to a human reader than the target, but it
+ * need not be acceptable to vconn_open(). */
void
rconn_connect_unreliably(struct rconn *rc,
- const char *name, struct vconn *vconn)
+ struct vconn *vconn, const char *name)
{
assert(vconn != NULL);
rconn_disconnect(rc);
- set_vconn_name(rc, name);
+ rconn_set_target__(rc, vconn_get_name(vconn), name);
rc->reliable = false;
rc->vconn = vconn;
rc->last_connected = time_now();
vconn_close(rc->vconn);
rc->vconn = NULL;
}
- set_vconn_name(rc, "void");
+ rconn_set_target__(rc, "void", NULL);
rc->reliable = false;
rc->backoff = 0;
size_t i;
free(rc->name);
+ free(rc->target);
vconn_close(rc->vconn);
flush_queue(rc);
queue_destroy(&rc->txq);
/* Nothing to do. */
}
-static int
+static void
reconnect(struct rconn *rc)
{
int retval;
- VLOG_INFO("%s: connecting...", rc->name);
+ if (rconn_logging_connection_attempts__(rc)) {
+ VLOG_INFO("%s: connecting...", rc->name);
+ }
rc->n_attempted_connections++;
- retval = vconn_open(rc->name, OFP_VERSION, &rc->vconn);
+ retval = vconn_open(rc->target, OFP_VERSION, &rc->vconn);
if (!retval) {
rc->remote_ip = vconn_get_remote_ip(rc->vconn);
rc->local_ip = vconn_get_local_ip(rc->vconn);
rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
disconnect(rc, retval);
}
- return retval;
}
static unsigned int
state_transition(rc, S_ACTIVE);
rc->last_connected = rc->state_entered;
} else if (retval != EAGAIN) {
- VLOG_INFO("%s: connection failed (%s)", rc->name, strerror(retval));
+ if (rconn_logging_connection_attempts__(rc)) {
+ VLOG_INFO("%s: connection failed (%s)",
+ rc->name, strerror(retval));
+ }
disconnect(rc, retval);
} else if (timed_out(rc)) {
- VLOG_INFO("%s: connection timed out", rc->name);
+ if (rconn_logging_connection_attempts__(rc)) {
+ VLOG_INFO("%s: connection timed out", rc->name);
+ }
rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */
disconnect(rc, ETIMEDOUT);
}
timeo = timeout(rc);
if (timeo != UINT_MAX) {
- unsigned int expires = sat_add(rc->state_entered, timeo);
- unsigned int remaining = sat_sub(expires, time_now());
- poll_timer_wait(sat_mul(remaining, 1000));
+ long long int expires = sat_add(rc->state_entered, timeo);
+ poll_timer_wait_until(expires * 1000);
}
if ((rc->state & (S_ACTIVE | S_IDLE)) && rc->txq.n) {
}
}
-/* Returns 'rc''s name (the 'name' argument passed to rconn_new()). */
+/* Returns 'rc''s name. This is a name for human consumption, appropriate for
+ * use in log messages. It is not necessarily a name that may be passed
+ * directly to, e.g., vconn_open(). */
const char *
rconn_get_name(const struct rconn *rc)
{
return rc->name;
}
+/* Sets 'rc''s name to 'new_name'. */
+void
+rconn_set_name(struct rconn *rc, const char *new_name)
+{
+ free(rc->name);
+ rc->name = xstrdup(new_name);
+}
+
+/* Returns 'rc''s target. This is intended to be a string that may be passed
+ * directly to, e.g., vconn_open(). */
+const char *
+rconn_get_target(const struct rconn *rc)
+{
+ return rc->target;
+}
+
/* Returns true if 'rconn' is connected or in the process of reconnecting,
* false if 'rconn' is disconnected and will not reconnect on its own. */
bool
}
}
\f
-/* Set the name of the remote vconn to 'name' and clear out the cached IP
- * address and port information, since changing the name also likely changes
- * these values. */
+/* Set rc->target and rc->name to 'target' and 'name', respectively. If 'name'
+ * is null, 'target' is used.
+ *
+ * Also, clear out the cached IP address and port information, since changing
+ * the target also likely changes these values. */
static void
-set_vconn_name(struct rconn *rc, const char *name)
+rconn_set_target__(struct rconn *rc, const char *target, const char *name)
{
free(rc->name);
- rc->name = xstrdup(name);
+ rc->name = xstrdup(name ? name : target);
+ free(rc->target);
+ rc->target = xstrdup(target);
rc->local_ip = 0;
rc->remote_ip = 0;
rc->remote_port = 0;
if (now >= rc->backoff_deadline) {
rc->backoff = 1;
- } else {
- rc->backoff = MIN(rc->max_backoff, MAX(1, 2 * rc->backoff));
- VLOG_INFO("%s: waiting %d seconds before reconnect\n",
+ } else if (rc->backoff < rc->max_backoff / 2) {
+ rc->backoff = MAX(1, 2 * rc->backoff);
+ VLOG_INFO("%s: waiting %d seconds before reconnect",
rc->name, rc->backoff);
+ } else {
+ if (rconn_logging_connection_attempts__(rc)) {
+ VLOG_INFO("%s: continuing to retry connections in the "
+ "background but suppressing further logging",
+ rc->name);
+ }
+ rc->backoff = rc->max_backoff;
}
rc->backoff_deadline = now + rc->backoff;
state_transition(rc, S_BACKOFF);
(1u << OFPT_GET_CONFIG_REPLY) |
(1u << OFPT_SET_CONFIG)));
}
+
+/* Returns true if 'rc' is currently logging information about connection
+ * attempts, false if logging should be suppressed because 'rc' hasn't
+ * successuflly connected in too long. */
+static bool
+rconn_logging_connection_attempts__(const struct rconn *rc)
+{
+ return rc->backoff < rc->max_backoff;
+}