[hipl-commit] [tiny] Rev 3675: Refactored HEARTBEAT module.

  • From: Tim Just <tim.just@xxxxxxxxxxxxxx>
  • To: hipl-commit@xxxxxxxxxxxxx
  • Date: Sat, 13 Mar 2010 15:48:07 +0200

Committer: Tim Just <tim.just@xxxxxxxxxxxxxx>
Date: Sat Mar 13 14:46:21 2010 +0100
Revision: 3675
Revision-id: tim.just@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Branch nick: tiny

Log:
  Refactored HEARTBEAT  module.
  
  Concept
  -------
  - Send periodic ICMP messages to all associated peers (HEARTBEATs)
  - When a HEARTBEAT response is received, calculate roundtrip time and
    maintain statistics.
    
  If an UPDATE module exists:
  - Register a HEARTBEAT counter to the host association database.
  - Increment this counter by 1, every time a HEARTBEAT is sent.
  - Reset the counter (set to 0), if a HEARTBEAT response is received.
  - If counter reaches the threshold value, trigger an UPDATE.
  
  
  For now, there is an (explicit) dependency between HEARTBEAT and UPDATE.
  This will be solved soon.

Modified:
  M  hipd/maintenance.c
  M  lib/core/state.h
  M  lib/modularization/lmod.c
  M  modules/heartbeat/hipd/heartbeat.c
  M  modules/heartbeat/hipd/heartbeat.h
  M  modules/heartbeat/module_info.xml
  M  modules/update/hipd/update.c
  M  modules/update/hipd/update.h

=== modified file 'hipd/maintenance.c'
--- hipd/maintenance.c  2010-03-13 10:30:42 +0000
+++ hipd/maintenance.c  2010-03-13 13:46:21 +0000
@@ -329,12 +329,9 @@
     uint32_t rcvd_heartbeats = 0;
     uint64_t rtt             = 0;
     double avg               = 0.0, std_dev = 0.0;
-#if 0
-    u_int32_t rtt            = 0, usecs = 0, secs = 0, square = 0;
-    u_int32_t sum1           = 0, sum2 = 0;
-#endif
     char hit[INET6_ADDRSTRLEN];
     hip_ha_t *entry          = NULL;
+    uint8_t *heartbeat_counter = NULL;
 
     hip_in6_ntop(src, hit);
 
@@ -352,8 +349,11 @@
     calc_statistics(&entry->heartbeats_statistics, &rcvd_heartbeats, NULL, 
NULL, &avg,
                     &std_dev, STATS_IN_MSECS);
 
-    _HIP_DEBUG("Reset heartbeat timer to trigger UPDATE\n");
-    entry->update_trigger_on_heartbeat_counter = 0;
+    heartbeat_counter = lmod_get_state_item(entry->hip_modular_state,
+                                            "heartbeat_update");
+
+    *heartbeat_counter = 0;
+    HIP_DEBUG("heartbeat_counter: %d\n", *heartbeat_counter);
 
     HIP_DEBUG("\nHeartbeat from %s, RTT %.6f ms,\n%.6f ms mean, "
               "%.6f ms std dev, packets sent %d recv %d lost %d\n",

=== modified file 'lib/core/state.h'
--- lib/core/state.h    2010-03-11 11:21:47 +0000
+++ lib/core/state.h    2010-03-13 13:46:21 +0000
@@ -381,7 +381,6 @@
     /** Counters of heartbeats (ICMPv6s) */
     int                                        heartbeats_sent;
     statistics_data_t                          heartbeats_statistics;
-    int                                        
update_trigger_on_heartbeat_counter;
 
     struct timeval                             bex_start;
     struct timeval                             bex_end;

=== modified file 'lib/modularization/lmod.c'
--- lib/modularization/lmod.c   2010-03-11 11:21:47 +0000
+++ lib/modularization/lmod.c   2010-03-13 13:46:21 +0000
@@ -146,8 +146,8 @@
  *          Error   = -1
  */
 int lmod_add_state_item(struct modular_state *state,
-                       void *state_item,
-                       const char *item_name)
+                        void *state_item,
+                        const char *item_name)
 {
 
     /* Check if identifier already exists */

=== modified file 'modules/heartbeat/hipd/heartbeat.c'
--- modules/heartbeat/hipd/heartbeat.c  2010-03-13 10:59:53 +0000
+++ modules/heartbeat/hipd/heartbeat.c  2010-03-13 13:46:21 +0000
@@ -1,3 +1,27 @@
+/**
+ * @file
+ * This file defines HEARTBEAT functionality for the HIP daemon.
+ *
+ * Concept
+ * -------
+ * - Send periodic ICMP messages to all associated peers (HEARTBEATs)
+ * - When a HEARTBEAT response is received, calculate roundtrip time and
+ *   maintain statistics.
+ *
+ * If an UPDATE module exists:
+ * - Register a HEARTBEAT counter to the host association database.
+ * - Increment this counter by 1, every time a HEARTBEAT is sent.
+ * - Reset the counter (set to 0), if a HEARTBEAT response is received.
+ * - If counter reaches the threshold value, trigger an UPDATE.
+ *
+ * Distributed under <a 
href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>.
+ *
+ * @author Miika Komu
+ * @author Samu Varjonen
+ * @author Rene Hummen
+ * @author Tim Just
+ */
+
 /* required for s6_addr32 */
 #define _BSD_SOURCE
 
@@ -9,10 +33,13 @@
 #include "hipd/hip_socket.h"
 #include "hipd/modularization.h"
 
+#include "modules/update/hipd/update.h"
+
 #define HIP_HEARTBEAT_INTERVAL 20
 
 int hip_icmp_sock;
-int heartbeat_counter = HIP_HEARTBEAT_INTERVAL;
+static int heartbeat_counter = HIP_HEARTBEAT_INTERVAL;
+static const int hip_heartbeat_trigger_update_threshold = 5;
 
 /**
  * This function sends ICMPv6 echo with timestamp to dsthit
@@ -32,8 +59,8 @@
     u_char *icmp_pkt       = NULL;
     struct msghdr mhdr;
     struct iovec iov[1];
-    struct cmsghdr *chdr;
-    struct inet6_pktinfo *pkti;
+    struct cmsghdr *chdr = NULL;
+    struct inet6_pktinfo *pkti = NULL;
     struct timeval tval;
 
     HIP_IFEL(!entry, 0, "No entry\n");
@@ -120,59 +147,6 @@
     return err;
 }
 
-static int hip_heartbeat_handle_icmp_sock(struct hip_packet_context *ctx)
-{
-    int err = 0;
-
-    HIP_IFEL(hip_icmp_recvmsg(hip_icmp_sock), -1,
-             "Failed to recvmsg from ICMPv6\n");
-
-out_err:
-    return err;
-}
-
-static int hip_heartbeat_maintenance(void)
-{
-    /* Check if the heartbeats should be sent */
-    if (heartbeat_counter < 1) {
-        hip_for_each_ha(hip_send_heartbeat, &hip_icmp_sock);
-        heartbeat_counter = HIP_HEARTBEAT_INTERVAL;
-    } else {
-        heartbeat_counter--;
-    }
-
-    return 0;
-}
-
-/**
- * This function goes through the HA database and sends an icmp echo to all of 
them
- *
- * @param socket to send with
- *
- * @return 0 on success negative on error
- */
-int hip_send_heartbeat(hip_ha_t *entry, void *opaq)
-{
-    int err     = 0;
-    int *sockfd = (int *) opaq;
-
-    if (entry->state == HIP_STATE_ESTABLISHED) {
-        if (entry->outbound_sa_count > 0) {
-            _HIP_DEBUG("list_for_each_safe\n");
-            HIP_IFEL(hip_send_icmp(*sockfd, entry), 0,
-                     "Error sending heartbeat, ignore\n");
-        } else {
-            /* This can occur when ESP transform is not negotiated
-             * with e.g. a HIP Relay or Rendezvous server */
-            HIP_DEBUG("No SA, sending NOTIFY instead of ICMPv6\n");
-            err = hip_nat_send_keep_alive(entry, NULL);
-        }
-    }
-
-out_err:
-    return err;
-}
-
 /**
  * This function receives ICMPv6 msgs (heartbeats)
  *
@@ -182,7 +156,7 @@
  *
  * @note see RFC2292
  */
-int hip_icmp_recvmsg(int sockfd)
+static int hip_icmp_recvmsg(int sockfd)
 {
     int err = 0, ret = 0, identifier = 0;
     struct msghdr mhdr;
@@ -299,6 +273,98 @@
     return err;
 }
 
+static int hip_heartbeat_handle_icmp_sock(struct hip_packet_context *ctx)
+{
+    int err = 0;
+
+    HIP_IFEL(hip_icmp_recvmsg(hip_icmp_sock), -1,
+             "Failed to recvmsg from ICMPv6\n");
+
+out_err:
+    return err;
+}
+
+/**
+ * This function goes through the HA database and sends an icmp echo to all of 
them
+ *
+ * @param socket to send with
+ *
+ * @return 0 on success negative on error
+ */
+static int hip_send_heartbeat(hip_ha_t *hadb_entry, void *opaq)
+{
+    int err                                     = 0;
+    int *sockfd                                 = (int *) opaq;
+    uint8_t *heartbeat_counter                  = NULL;
+    hip_common_t *locator_msg                   = NULL;
+    struct hip_locator_info_addr_item *locators = NULL;
+
+    if ((hadb_entry->state == HIP_STATE_ESTABLISHED) &&
+        (hadb_entry->outbound_sa_count > 0)) {
+
+        HIP_IFEL(hip_send_icmp(*sockfd, hadb_entry), 0,
+                     "Error sending heartbeat, ignore\n");
+        heartbeat_counter = lmod_get_state_item(hadb_entry->hip_modular_state,
+                                                "heartbeat_update");
+
+        *heartbeat_counter = *heartbeat_counter + 1;
+        HIP_DEBUG("heartbeat_counter: %d\n", *heartbeat_counter);
+
+        if (*heartbeat_counter >= hip_heartbeat_trigger_update_threshold) {
+            HIP_DEBUG("HEARTBEAT counter reached threshold, trigger UPDATE\n");
+
+            HIP_IFEL(!(locator_msg = hip_msg_alloc()), -ENOMEM,
+                     "Out of memory while allocation memory for the packet\n");
+            HIP_IFE(hip_create_locators(locator_msg, &locators), -1);
+
+            HIP_IFEL(hip_send_locators_to_one_peer(NULL,
+                                                   hadb_entry,
+                                                   &hadb_entry->our_addr,
+                                                   &hadb_entry->peer_addr,
+                                                   locators,
+                                                   HIP_UPDATE_LOCATOR),
+                     -1, "Failed to trigger update\n");
+
+            *heartbeat_counter = 0;
+        }
+    }
+
+out_err:
+    if (locator_msg) {
+        free(locator_msg);
+    }
+
+    return err;
+}
+
+static int hip_heartbeat_maintenance(void)
+{
+    /* Check if the heartbeats should be sent */
+    if (heartbeat_counter < 1) {
+        hip_for_each_ha(hip_send_heartbeat, &hip_icmp_sock);
+        heartbeat_counter = HIP_HEARTBEAT_INTERVAL;
+    } else {
+        heartbeat_counter--;
+    }
+
+    return 0;
+}
+
+static int hip_heartbeat_update_init_state(struct modular_state *state)
+{
+    int err = 0;
+    uint8_t *heartbeat_counter = NULL;
+
+    HIP_IFEL(!(heartbeat_counter = malloc(sizeof(uint8_t))),
+                -1,
+                "Error on allocating memory for heartbeat_counter.\n");
+
+    err = lmod_add_state_item(state, heartbeat_counter, "heartbeat_update");
+
+out_err:
+    return err;
+}
+
 /**
  * Initialize icmpv6 socket.
  */
@@ -340,6 +406,11 @@
              -1,
              "Error on registration of hip_heartbeat_maintenance().\n");
 
+    /** @todo This should only be done, if the module UPDATE exists */
+    
HIP_IFEL(lmod_register_state_init_function(&hip_heartbeat_update_init_state),
+             -1,
+             "Error on registration of hip_heartbeat_update_init_state().\n");
+
 out_err:
     return err;
 }

=== modified file 'modules/heartbeat/hipd/heartbeat.h'
--- modules/heartbeat/hipd/heartbeat.h  2010-03-11 14:08:33 +0000
+++ modules/heartbeat/hipd/heartbeat.h  2010-03-13 13:46:21 +0000
@@ -1,10 +1,6 @@
 #ifndef HIP_HIPD_HEARTBEAT_H
 #define HIP_HIPD_HEARTBEAT_H
 
-#include "lib/core/state.h"
-
 int hip_heartbeat_init(void);
-int hip_send_heartbeat(hip_ha_t *entry, void *opaq);
-int hip_icmp_recvmsg(int sockfd);
 
 #endif /* HIP_HIPD_HEARTBEAT_H */

=== modified file 'modules/heartbeat/module_info.xml'
--- modules/heartbeat/module_info.xml   2010-03-08 15:47:13 +0000
+++ modules/heartbeat/module_info.xml   2010-03-13 13:46:21 +0000
@@ -8,6 +8,10 @@
     developer=""
     bugaddress="hipl-users@xxxxxxxxxxxxx"
     webpage="http://infrahip.hiit.fi/";>
+    
+    <requires>
+        <module name="update" minversion="0.0.1" />
+    </requires>
 
     <!-- Mandatory: name, header_file, init_function, linkcommand -->
     <application

=== modified file 'modules/update/hipd/update.c'
--- modules/update/hipd/update.c        2010-03-13 10:30:42 +0000
+++ modules/update/hipd/update.c        2010-03-13 13:46:21 +0000
@@ -59,11 +59,10 @@
     uint32_t                     update_id_in;
 };
 
-int hip_trigger_update_on_heartbeat_failure =  0;
-int update_id_window_size                   = 50;
+const static int update_id_window_size = 50;
 
-static int hip_create_locators(hip_common_t *locator_msg,
-                               struct hip_locator_info_addr_item **locators)
+int hip_create_locators(hip_common_t *locator_msg,
+                        struct hip_locator_info_addr_item **locators)
 {
     int err = 0;
     struct hip_locator *loc = NULL;
@@ -760,10 +759,6 @@
         }
     }
 
-    if (hip_trigger_update_on_heartbeat_failure) {
-//        hip_for_each_ha(hip_handle_update_heartbeat_trigger, NULL);
-    }
-
     return err;
 }
 
@@ -1021,57 +1016,6 @@
              -1,
              "Error on registering UPDATE maintenance function.\n");
 
-    hip_trigger_update_on_heartbeat_failure = lmod_module_exists("heartbeat");
-
-out_err:
-    return err;
-}
-
-#if 0
-static int hip_handle_update_heartbeat_trigger(hip_ha_t *ha, void *unused)
-{
-    int err = 0;
-    hip_common_t *locator_msg = NULL;
-    struct hip_locator_info_addr_item *locators = NULL;
-
-    if (!(ha->hastate == HIP_HASTATE_HITOK &&
-          ha->state == HIP_STATE_ESTABLISHED &&
-          ha->disable_sas == 0)) {
-        goto out_err;
-    }
-
-    ha->update_trigger_on_heartbeat_counter++;
-    _HIP_DEBUG("Trigger count %d/%d\n", 
ha->update_trigger_on_heartbeat_counter,
-               HIP_ADDRESS_CHANGE_HB_COUNT_TRIGGER * hip_icmp_interval);
-
-    if (ha->update_trigger_on_heartbeat_counter <
-        HIP_ADDRESS_CHANGE_HB_COUNT_TRIGGER * hip_icmp_interval) {
-        goto out_err;
-    }
-
-    /* Time to try a handover because heart beats have been failing.
-     * Let's also reset to counter to avoid UPDATE looping in case e.g.
-     * there is just no connectivity at all. */
-    ha->update_trigger_on_heartbeat_counter = 0;
-
-    HIP_DEBUG("Hearbeat counter with ha expired, trigger UPDATE\n");
-
-    HIP_IFEL(!(locator_msg = hip_msg_alloc()), -ENOMEM,
-             "Out of memory while allocation memory for the packet\n");
-    HIP_IFE(hip_create_locators(locator_msg, &locators), -1);
-
-    HIP_IFEL(hip_send_locators_to_one_peer(NULL, ha, &ha->our_addr,
-                                           &ha->peer_addr, locators,
-                                           HIP_UPDATE_LOCATOR),
-             -1, "Failed to trigger update\n");
-
-    ha->update_trigger_on_heartbeat_counter = 0;
-
-out_err:
-    if (locator_msg) {
-        free(locator_msg);
-    }
-
-    return err;
-}
-#endif
+out_err:
+    return err;
+}

=== modified file 'modules/update/hipd/update.h'
--- modules/update/hipd/update.h        2010-03-13 10:04:42 +0000
+++ modules/update/hipd/update.h        2010-03-13 13:46:21 +0000
@@ -15,6 +15,9 @@
 #include "hipd/hadb.h"
 #include "hipd/modularization.h"
 
+int hip_create_locators(hip_common_t *locator_msg,
+                        struct hip_locator_info_addr_item **locators);
+
 int hip_send_locators_to_all_peers(void);
 
 int hip_send_locators_to_one_peer(hip_common_t *received_update_packet,

Other related posts:

  • » [hipl-commit] [tiny] Rev 3675: Refactored HEARTBEAT module. - Tim Just