ovs-brcompatd: Delete write-only variable.
[openvswitch] / lib / cfm.c
index 1626514b60a859657617a255a5c020b535a83d0a..9d9b1bd086b595135a5c432efbd2b76bd07d7682 100644 (file)
--- a/lib/cfm.c
+++ b/lib/cfm.c
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "dynamic-string.h"
 #include "flow.h"
 #include "hash.h"
 #include "hmap.h"
@@ -43,6 +44,9 @@ struct cfm_internal {
 
     long long ccm_sent;    /* The time we last sent a CCM. */
     long long fault_check; /* The time we last checked for faults. */
+
+    struct hmap x_remote_mps;   /* Unexpected remote MPs. */
+    struct hmap x_remote_maids; /* Unexpected remote MAIDs. */
 };
 
 static int
@@ -78,7 +82,7 @@ ms_to_ccm_interval(int interval_ms)
 }
 
 static struct cfm_internal *
-cfm_to_internal(struct cfm *cfm)
+cfm_to_internal(const struct cfm *cfm)
 {
     return CONTAINER_OF(cfm, struct cfm_internal, cfm);
 }
@@ -110,6 +114,21 @@ lookup_remote_mp(const struct hmap *hmap, uint16_t mpid)
     return NULL;
 }
 
+static struct remote_maid *
+lookup_remote_maid(const struct hmap *hmap, uint8_t maid[CCM_MAID_LEN])
+{
+    uint32_t hash;
+    struct remote_maid *rmaid;
+
+    hash = hash_bytes(maid, CCM_MAID_LEN, 0);
+    HMAP_FOR_EACH_WITH_HASH (rmaid, node, hash, hmap) {
+        if (memcmp(rmaid->maid, maid, CCM_MAID_LEN) == 0) {
+            return rmaid;
+        }
+    }
+    return NULL;
+}
+
 /* Allocates a 'cfm' object.  This object should have its 'mpid', 'maid',
  * 'eth_src', and 'interval' filled out.  When changes are made to the 'cfm'
  * object, cfm_configure should be called before using it. */
@@ -123,14 +142,15 @@ cfm_create(void)
     cfm  = &cfmi->cfm;
 
     hmap_init(&cfm->remote_mps);
-    hmap_init(&cfm->x_remote_mps);
-    hmap_init(&cfm->x_remote_maids);
+    hmap_init(&cfmi->x_remote_mps);
+    hmap_init(&cfmi->x_remote_maids);
     return cfm;
 }
 
 void
 cfm_destroy(struct cfm *cfm)
 {
+    struct cfm_internal *cfmi = cfm_to_internal(cfm);
     struct remote_mp *rmp, *rmp_next;
     struct remote_maid *rmaid, *rmaid_next;
 
@@ -143,20 +163,20 @@ cfm_destroy(struct cfm *cfm)
         free(rmp);
     }
 
-    HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->x_remote_mps) {
-        hmap_remove(&cfm->x_remote_mps, &rmp->node);
+    HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfmi->x_remote_mps) {
+        hmap_remove(&cfmi->x_remote_mps, &rmp->node);
         free(rmp);
     }
 
-    HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfm->x_remote_maids) {
-        hmap_remove(&cfm->x_remote_maids, &rmaid->node);
+    HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfmi->x_remote_maids) {
+        hmap_remove(&cfmi->x_remote_maids, &rmaid->node);
         free(rmaid);
     }
 
     hmap_destroy(&cfm->remote_mps);
-    hmap_destroy(&cfm->x_remote_mps);
-    hmap_destroy(&cfm->x_remote_maids);
-    free(cfm_to_internal(cfm));
+    hmap_destroy(&cfmi->x_remote_mps);
+    hmap_destroy(&cfmi->x_remote_maids);
+    free(cfmi);
 }
 
 /* Should be run periodically to update fault statistics messages. */
@@ -173,9 +193,8 @@ cfm_run(struct cfm *cfm)
      * MPs at once making this quite a bit simpler.
      *
      * According to the specification we should check when (ccm_interval_ms *
-     * 3.5)ms have passed.  We changed the multiplier to 4 to avoid messy
-     * floating point arithmetic and add a bit of wiggle room. */
-    if (now >= cfmi->fault_check + cfmi->ccm_interval_ms * 4) {
+     * 3.5)ms have passed. */
+    if (now >= cfmi->fault_check + (cfmi->ccm_interval_ms * 7) / 2) {
         bool fault;
         struct remote_mp *rmp, *rmp_next;
         struct remote_maid *rmaid, *rmaid_next;
@@ -187,22 +206,22 @@ cfm_run(struct cfm *cfm)
             fault      = rmp->fault || fault;
         }
 
-        HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->x_remote_mps) {
+        HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfmi->x_remote_mps) {
             if (cfmi->fault_check > rmp->recv_time) {
-                hmap_remove(&cfm->x_remote_mps, &rmp->node);
+                hmap_remove(&cfmi->x_remote_mps, &rmp->node);
                 free(rmp);
             }
         }
 
-        HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfm->x_remote_maids) {
+        HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfmi->x_remote_maids) {
             if (cfmi->fault_check > rmaid->recv_time) {
-                hmap_remove(&cfm->x_remote_maids, &rmaid->node);
+                hmap_remove(&cfmi->x_remote_maids, &rmaid->node);
                 free(rmaid);
             }
         }
 
-        fault = (fault || !hmap_is_empty(&cfm->x_remote_mps)
-                 || !hmap_is_empty(&cfm->x_remote_maids));
+        fault = (fault || !hmap_is_empty(&cfmi->x_remote_mps)
+                 || !hmap_is_empty(&cfmi->x_remote_maids));
 
         cfm->fault        = fault;
         cfmi->fault_check = now;
@@ -274,6 +293,7 @@ cfm_configure(struct cfm *cfm)
 void
 cfm_update_remote_mps(struct cfm *cfm, const uint16_t *mpids, size_t n_mpids)
 {
+    struct cfm_internal *cfmi = cfm_to_internal(cfm);
     size_t i;
     struct hmap new_rmps;
     struct remote_mp *rmp, *rmp_next;
@@ -290,8 +310,8 @@ cfm_update_remote_mps(struct cfm *cfm, const uint16_t *mpids, size_t n_mpids)
 
         if ((rmp = lookup_remote_mp(&cfm->remote_mps, mpid))) {
             hmap_remove(&cfm->remote_mps, &rmp->node);
-        } else if ((rmp = lookup_remote_mp(&cfm->x_remote_mps, mpid))) {
-            hmap_remove(&cfm->x_remote_mps, &rmp->node);
+        } else if ((rmp = lookup_remote_mp(&cfmi->x_remote_mps, mpid))) {
+            hmap_remove(&cfmi->x_remote_mps, &rmp->node);
         } else {
             rmp = xzalloc(sizeof *rmp);
             rmp->mpid = mpid;
@@ -392,42 +412,79 @@ cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
     }
 
     if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
-        uint32_t hash;
         struct remote_maid *rmaid;
 
-        hash = hash_bytes(ccm->maid, sizeof ccm->maid, 0);
+        rmaid = lookup_remote_maid(&cfmi->x_remote_maids, ccm->maid);
+        if (rmaid) {
+            rmaid->recv_time = time_msec();
+        } else {
+            rmaid = xzalloc(sizeof *rmaid);
+            rmaid->recv_time = time_msec();
+            memcpy(rmaid->maid, ccm->maid, sizeof rmaid->maid);
+            hmap_insert(&cfmi->x_remote_maids, &rmaid->node,
+                        hash_bytes(ccm->maid, CCM_MAID_LEN, 0));
+        }
+        cfm->fault = true;
+    } else {
+        ccm_mpid = ntohs(ccm->mpid);
+        ccm_seq = ntohl(ccm->seq);
+        ccm_interval = ccm->flags & 0x7;
+
+        rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
 
-        HMAP_FOR_EACH_IN_BUCKET (rmaid, node, hash, &cfm->x_remote_maids) {
-            if (memcmp(rmaid->maid, ccm->maid, sizeof rmaid->maid) == 0) {
-                rmaid->recv_time = time_msec();
-                return;
-            }
+        if (!rmp) {
+            rmp = lookup_remote_mp(&cfmi->x_remote_mps, ccm_mpid);
         }
 
-        rmaid            = xzalloc(sizeof *rmaid);
-        rmaid->recv_time = time_msec();
-        memcpy(rmaid->maid, ccm->maid, sizeof rmaid->maid);
-        hmap_insert(&cfm->x_remote_maids, &rmaid->node, hash);
-        return;
+        if (!rmp) {
+            rmp = xzalloc(sizeof *rmp);
+            rmp->mpid = ccm_mpid;
+            hmap_insert(&cfmi->x_remote_mps, &rmp->node, hash_mpid(ccm_mpid));
+            rmp->fault = true;
+        }
+
+        rmp->recv_time = time_msec();
+        rmp->fault = ccm_interval != cfmi->ccm_interval;
+
+        if (rmp->fault) {
+            cfm->fault = true;
+        }
     }
+}
 
-    ccm_mpid     = ntohs(ccm->mpid);
-    ccm_seq      = ntohl(ccm->seq);
-    ccm_interval = ccm->flags & 0x7;
+static void
+put_remote_mp(struct ds *ds, const char *header,
+              const struct remote_mp *rmp)
+{
+    ds_put_format(ds, "%s %"PRIu16": %s\n", header, rmp->mpid,
+                  rmp->fault ? "fault" : "");
+    ds_put_format(ds, "\ttime since CCM rx: %lldms\n",
+                  time_msec() - rmp->recv_time);
+}
 
-    rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
+void
+cfm_dump_ds(const struct cfm *cfm, struct ds *ds)
+{
+    const struct cfm_internal *cfmi = cfm_to_internal(cfm);
+    long long int now = time_msec();
+    struct remote_mp *rmp;
 
-    if (!rmp) {
-        rmp = lookup_remote_mp(&cfm->x_remote_mps, ccm_mpid);
-    }
+    ds_put_format(ds, "MPID %"PRIu16": %s\n", cfm->mpid,
+                  cfm->fault ? "fault" : "");
 
-    if (!rmp) {
-        rmp       = xzalloc(sizeof *rmp);
-        rmp->mpid = ccm_mpid;
-        hmap_insert(&cfm->x_remote_mps, &rmp->node, hash_mpid(ccm_mpid));
-        cfm->fault = true;
+    ds_put_format(ds, "\tinterval: %dms\n", cfmi->ccm_interval_ms);
+    ds_put_format(ds, "\ttime since CCM tx: %lldms\n", now - cfmi->ccm_sent);
+    ds_put_format(ds, "\ttime since fault check: %lldms\n",
+                  now - cfmi->fault_check);
+    ds_put_format(ds, "\tunexpected remote MAIDS: %s\n",
+                  !hmap_is_empty(&cfmi->x_remote_maids) ? "true" : "false");
+
+    ds_put_cstr(ds, "\n");
+    HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
+        put_remote_mp(ds, "Remote MPID", rmp);
     }
 
-    rmp->recv_time = time_msec();
-    rmp->fault     = ccm_interval != cfmi->ccm_interval;
+    HMAP_FOR_EACH (rmp, node, &cfmi->x_remote_mps) {
+        put_remote_mp(ds, "Unexpected Remote MPID", rmp);
+    }
 }