Fix typo in comment.
[openvswitch] / lib / rconn.c
index 6bc371ad523468cc79b2b66ac1e394c5e07c6a25..c1c2748c3550d3ca1f2615f75bc91dfe41d6d259 100644 (file)
@@ -41,6 +41,7 @@
 #include "buffer.h"
 #include "poll-loop.h"
 #include "ofp-print.h"
+#include "sat-math.h"
 #include "timeval.h"
 #include "util.h"
 #include "vconn.h"
@@ -89,9 +90,15 @@ struct rconn {
     time_t backoff_deadline;
     time_t last_received;
     time_t last_connected;
-
     unsigned int packets_sent;
 
+    /* These values are simply for statistics reporting, not used directly by
+     * anything internal to the rconn (or the secchan for that matter). */
+    unsigned int packets_received;
+    unsigned int n_attempted_connections, n_successful_connections;
+    time_t creation_time;
+    unsigned long int total_time_connected;
+
     /* If we can't connect to the peer, it could be for any number of reasons.
      * Usually, one would assume it is because the peer is not running or
      * because the network is partitioned.  But it could also be because the
@@ -109,8 +116,6 @@ struct rconn {
     int probe_interval;         /* Secs of inactivity before sending probe. */
 };
 
-static unsigned int sat_add(unsigned int x, unsigned int y);
-static unsigned int sat_mul(unsigned int x, unsigned int y);
 static unsigned int elapsed_in_this_state(const struct rconn *);
 static unsigned int timeout(const struct rconn *);
 static bool timed_out(const struct rconn *);
@@ -173,6 +178,12 @@ rconn_create(int probe_interval, int max_backoff)
 
     rc->packets_sent = 0;
 
+    rc->packets_received = 0;
+    rc->n_attempted_connections = 0;
+    rc->n_successful_connections = 0;
+    rc->creation_time = time_now();
+    rc->total_time_connected = 0;
+
     rc->questionable_connectivity = false;
     rc->last_questioned = time_now();
 
@@ -253,6 +264,7 @@ reconnect(struct rconn *rc)
     int retval;
 
     VLOG_WARN("%s: connecting...", rc->name);
+    rc->n_attempted_connections++;
     retval = vconn_open(rc->name, &rc->vconn);
     if (!retval) {
         rc->backoff_deadline = time_now() + rc->backoff;
@@ -290,6 +302,7 @@ run_CONNECTING(struct rconn *rc)
     int retval = vconn_connect(rc->vconn);
     if (!retval) {
         VLOG_WARN("%s: connected", rc->name);
+        rc->n_successful_connections++;
         if (vconn_is_passive(rc->vconn)) {
             error(0, "%s: passive vconn not supported", rc->name);
             state_transition(rc, S_VOID);
@@ -310,12 +323,18 @@ run_CONNECTING(struct rconn *rc)
 static void
 do_tx_work(struct rconn *rc)
 {
+    if (!rc->txq.n) {
+        return;
+    }
     while (rc->txq.n > 0) {
         int error = try_send(rc);
         if (error) {
             break;
         }
     }
+    if (!rc->txq.n) {
+        poll_immediate_wake();
+    }
 }
 
 static unsigned int
@@ -334,7 +353,7 @@ run_ACTIVE(struct rconn *rc)
 {
     if (timed_out(rc)) {
         unsigned int base = MAX(rc->last_received, rc->state_entered);
-        queue_push_tail(&rc->txq, make_echo_request());
+        rconn_send(rc, make_echo_request(), NULL);
         VLOG_DBG("%s: idle %u seconds, sending inactivity probe",
                  rc->name, (unsigned int) (time_now() - base));
         state_transition(rc, S_IDLE);
@@ -390,7 +409,9 @@ rconn_run_wait(struct rconn *rc)
 {
     unsigned int timeo = timeout(rc);
     if (timeo != UINT_MAX) {
-        poll_timer_wait(sat_mul(timeo, 1000));
+        unsigned int expires = sat_add(rc->state_entered, timeo);
+        unsigned int remaining = sat_sub(expires, time_now());
+        poll_timer_wait(sat_mul(remaining, 1000));
     }
 
     if ((rc->state & (S_ACTIVE | S_IDLE)) && rc->txq.n) {
@@ -409,6 +430,7 @@ rconn_recv(struct rconn *rc)
         int error = vconn_recv(rc->vconn, &buffer);
         if (!error) {
             rc->last_received = time_now();
+            rc->packets_received++;
             if (rc->state == S_IDLE) {
                 state_transition(rc, S_ACTIVE);
             }
@@ -446,7 +468,7 @@ rconn_recv_wait(struct rconn *rc)
 int
 rconn_send(struct rconn *rc, struct buffer *b, int *n_queued)
 {
-    if (rc->vconn) {
+    if (rconn_is_connected(rc)) {
         b->private = n_queued;
         if (n_queued) {
             ++*n_queued;
@@ -464,7 +486,7 @@ rconn_send(struct rconn *rc, struct buffer *b, int *n_queued)
 /* Sends 'b' on 'rc'.  Increments '*n_queued' while the packet is in flight; it
  * will be decremented when it has been sent (or discarded due to
  * disconnection).  Returns 0 if successful, EAGAIN if '*n_queued' is already
- * at least as large of 'queue_limit', or ENOTCONN if 'rc' is not currently
+ * at least as large as 'queue_limit', or ENOTCONN if 'rc' is not currently
  * connected.  Regardless of return value, 'b' is destroyed.
  *
  * Because 'b' may be sent (or discarded) before this function returns, the
@@ -547,6 +569,75 @@ rconn_is_connectivity_questionable(struct rconn *rconn)
     rconn->questionable_connectivity = false;
     return questionable;
 }
+
+/* Returns the total number of packets successfully received by the underlying
+ * vconn.  */
+unsigned int
+rconn_packets_received(const struct rconn *rc)
+{
+    return rc->packets_received;
+}
+
+/* Returns a string representing the internal state of 'rc'.  The caller must
+ * not modify or free the string. */
+const char *
+rconn_get_state(const struct rconn *rc)
+{
+    return state_name(rc->state);
+}
+
+/* Returns the number of connection attempts made by 'rc', including any
+ * ongoing attempt that has not yet succeeded or failed. */
+unsigned int
+rconn_get_attempted_connections(const struct rconn *rc)
+{
+    return rc->n_attempted_connections;
+}
+
+/* Returns the number of successful connection attempts made by 'rc'. */
+unsigned int
+rconn_get_successful_connections(const struct rconn *rc)
+{
+    return rc->n_successful_connections;
+}
+
+/* Returns the time at which the last successful connection was made by
+ * 'rc'. */
+time_t
+rconn_get_last_connection(const struct rconn *rc)
+{
+    return rc->last_connected;
+}
+
+/* Returns the time at which 'rc' was created. */
+time_t
+rconn_get_creation_time(const struct rconn *rc)
+{
+    return rc->creation_time;
+}
+
+/* Returns the approximate number of seconds that 'rc' has been connected. */
+unsigned long int
+rconn_get_total_time_connected(const struct rconn *rc)
+{
+    return (rc->total_time_connected
+            + (rconn_is_connected(rc) ? elapsed_in_this_state(rc) : 0));
+}
+
+/* Returns the current amount of backoff, in seconds.  This is the amount of
+ * time after which the rconn will transition from BACKOFF to CONNECTING. */
+int
+rconn_get_backoff(const struct rconn *rc)
+{
+    return rc->backoff;
+}
+
+/* Returns the number of seconds spent in this state so far. */
+unsigned int
+rconn_get_state_elapsed(const struct rconn *rc)
+{
+    return elapsed_in_this_state(rc);
+}
 \f
 /* Tries to send a packet from 'rc''s send buffer.  Returns 0 if successful,
  * otherwise a positive errno value. */
@@ -618,6 +709,9 @@ disconnect(struct rconn *rc, int error)
 static void
 flush_queue(struct rconn *rc)
 {
+    if (!rc->txq.n) {
+        return;
+    }
     while (rc->txq.n > 0) {
         struct buffer *b = queue_pop_head(&rc->txq);
         int *n_queued = b->private;
@@ -626,6 +720,7 @@ flush_queue(struct rconn *rc)
         }
         buffer_delete(b);
     }
+    poll_immediate_wake();
 }
 
 static unsigned int
@@ -655,24 +750,14 @@ timed_out(const struct rconn *rc)
 static void
 state_transition(struct rconn *rc, enum state state)
 {
+    if (rconn_is_connected(rc)) {
+        rc->total_time_connected += elapsed_in_this_state(rc);
+    }
     VLOG_DBG("%s: entering %s", rc->name, state_name(state));
     rc->state = state;
     rc->state_entered = time_now();
 }
 
-static unsigned int
-sat_add(unsigned int x, unsigned int y)
-{
-    return x + y >= x ? x + y : UINT_MAX;
-}
-
-static unsigned int
-sat_mul(unsigned int x, unsigned int y)
-{
-    assert(y);
-    return x <= UINT_MAX / y ? x * y : UINT_MAX;
-}
-
 static void
 question_connectivity(struct rconn *rc) 
 {