cfm: Don't report unexpected remote endpoints.
[openvswitch] / lib / cfm.c
index 12cf3e96a79c21ab850534d02391555775d3bba4..d0f3ee05c4251d720e464c8ff0d5de57870355f9 100644 (file)
--- a/lib/cfm.c
+++ b/lib/cfm.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Nicira Networks.
+ * Copyright (c) 2010, 2011 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -33,7 +33,6 @@
 VLOG_DEFINE_THIS_MODULE(cfm);
 
 #define CCM_OPCODE 1              /* CFM message opcode meaning CCM. */
-#define DEST_ADDR  0x0180C2000030 /* Destination for MD level 0 CCMs. */
 
 struct cfm_internal {
     struct cfm cfm;
@@ -44,6 +43,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
@@ -111,34 +113,19 @@ lookup_remote_mp(const struct hmap *hmap, uint16_t mpid)
     return NULL;
 }
 
-static struct ofpbuf *
-compose_ccm(struct cfm_internal *cfmi)
+static struct remote_maid *
+lookup_remote_maid(const struct hmap *hmap, uint8_t maid[CCM_MAID_LEN])
 {
-    struct ccm *ccm;
-    struct ofpbuf *packet;
-    struct eth_header *eth;
-
-    packet = xzalloc(sizeof *packet);
-
-    ofpbuf_init(packet, ETH_HEADER_LEN + CCM_LEN + 2);
-
-    ofpbuf_reserve(packet, 2);
-
-    eth = ofpbuf_put_zeros(packet, ETH_HEADER_LEN);
-    ccm = ofpbuf_put_zeros(packet, CCM_LEN);
+    uint32_t hash;
+    struct remote_maid *rmaid;
 
-    eth_addr_from_uint64(DEST_ADDR, eth->eth_dst);
-    memcpy(eth->eth_src, cfmi->cfm.eth_src, sizeof eth->eth_src);
-    eth->eth_type = htons(ETH_TYPE_CFM);
-
-    ccm->mdlevel_version = 0;
-    ccm->opcode          = CCM_OPCODE;
-    ccm->tlv_offset      = 70;
-    ccm->seq             = htonl(++cfmi->seq);
-    ccm->mpid            = htons(cfmi->cfm.mpid);
-    ccm->flags           = cfmi->ccm_interval;
-    memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid);
-    return packet;
+    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',
@@ -154,14 +141,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;
 
@@ -174,26 +162,24 @@ 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 and generate CCM
- * messages.  If necessary, returns a packet which the caller is responsible
- * for sending, un-initing, and deallocating.  Otherwise returns NULL. */
-struct ofpbuf *
+/* Should be run periodically to update fault statistics messages. */
+void
 cfm_run(struct cfm *cfm)
 {
     long long now = time_msec();
@@ -220,33 +206,54 @@ 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;
     }
+}
 
-    if (now >= cfmi->ccm_sent + cfmi->ccm_interval_ms) {
-        cfmi->ccm_sent = now;
-        return compose_ccm(cfmi);
-    }
+/* Should be run periodically to check if the CFM module has a CCM message it
+ * wishes to send. */
+bool
+cfm_should_send_ccm(struct cfm *cfm)
+{
+    struct cfm_internal *cfmi = cfm_to_internal(cfm);
 
-    return NULL;
+    return time_msec() >= cfmi->ccm_sent + cfmi->ccm_interval_ms;
+}
+
+/* Composes a CCM message into 'ccm'.  Messages generated with this function
+ * should be sent whenever cfm_should_send_ccm() indicates. */
+void
+cfm_compose_ccm(struct cfm *cfm, struct ccm *ccm)
+{
+    struct cfm_internal *cfmi = cfm_to_internal(cfm);
+
+    cfmi->ccm_sent = time_msec();
+
+    ccm->mdlevel_version = 0;
+    ccm->opcode = CCM_OPCODE;
+    ccm->tlv_offset = 70;
+    ccm->seq = htonl(++cfmi->seq);
+    ccm->mpid = htons(cfmi->cfm.mpid);
+    ccm->flags = cfmi->ccm_interval;
+    memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid);
 }
 
 void
@@ -286,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;
@@ -302,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;
@@ -372,7 +380,7 @@ bool
 cfm_should_process_flow(const struct flow *flow)
 {
     return (ntohs(flow->dl_type) == ETH_TYPE_CFM
-            && eth_addr_to_uint64(flow->dl_dst) == DEST_ADDR);
+            && eth_addr_equals(flow->dl_dst, eth_addr_ccm));
 }
 
 /* Updates internal statistics relevant to packet 'p'.  Should be called on
@@ -404,41 +412,42 @@ 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);
-
-        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;
-            }
+        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;
 
-        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;
-    }
+        rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
 
-    ccm_mpid     = ntohs(ccm->mpid);
-    ccm_seq      = ntohl(ccm->seq);
-    ccm_interval = ccm->flags & 0x7;
+        if (!rmp) {
+            rmp = lookup_remote_mp(&cfmi->x_remote_mps, ccm_mpid);
+        }
 
-    rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
+        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;
+        }
 
-    if (!rmp) {
-        rmp = lookup_remote_mp(&cfm->x_remote_mps, ccm_mpid);
-    }
+        rmp->recv_time = time_msec();
+        rmp->fault = ccm_interval != cfmi->ccm_interval;
 
-    if (!rmp) {
-        rmp       = xzalloc(sizeof *rmp);
-        rmp->mpid = ccm_mpid;
-        hmap_insert(&cfm->x_remote_mps, &rmp->node, hash_mpid(ccm_mpid));
+        if (rmp->fault) {
+            cfm->fault = true;
+        }
     }
-
-    rmp->recv_time = time_msec();
-    rmp->fault     = ccm_interval != cfmi->ccm_interval;
 }