Moved segment configuration parsing to ha_sync_plugin
authorMartin Willi <martin@strongswan.org>
Tue, 22 Sep 2009 12:53:03 +0000 (14:53 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 7 Apr 2010 11:55:14 +0000 (13:55 +0200)
src/charon/plugins/ha_sync/ha_sync_plugin.c
src/charon/plugins/ha_sync/ha_sync_segments.c
src/charon/plugins/ha_sync/ha_sync_segments.h

index 63de6c5..2665b2f 100644 (file)
@@ -90,6 +90,29 @@ static void destroy(private_ha_sync_plugin_t *this)
        free(this);
 }
 
+/**
+ * Convert segment string to mask
+ */
+static segment_mask_t parse_active(char *active)
+{
+       enumerator_t *enumerator;
+       u_int segment;
+       segment_mask_t mask = 0;
+
+       enumerator = enumerator_create_token(active, ",", " ");
+       while (enumerator->enumerate(enumerator, &active))
+       {
+               segment = atoi(active);
+               if (segment > 0 && segment < SEGMENTS_MAX)
+               {
+                       mask |= SEGMENTS_BIT(segment);
+               }
+       }
+       enumerator->destroy(enumerator);
+
+       return mask;
+}
+
 /*
  * see header file
  */
@@ -97,6 +120,8 @@ plugin_t *plugin_create()
 {
        private_ha_sync_plugin_t *this;
        char *local, *remote, *secret;
+       segment_mask_t active;
+       u_int count;
        bool fifo;
 
        local = lib->settings->get_str(lib->settings,
@@ -107,6 +132,10 @@ plugin_t *plugin_create()
                                                                "charon.plugins.ha_sync.secret", NULL);
        fifo = lib->settings->get_bool(lib->settings,
                                                                "charon.plugins.ha_sync.fifo_interface", FALSE);
+       count = min(SEGMENTS_MAX, lib->settings->get_int(lib->settings,
+                                                               "charon.plugins.ha_sync.segment_count", 1));
+       active = parse_active(lib->settings->get_str(lib->settings,
+                                                               "charon.plugins.ha_sync.active_segments", "1"));
        if (!local || !remote)
        {
                DBG1(DBG_CFG, "HA sync config misses local/remote address");
@@ -125,7 +154,7 @@ plugin_t *plugin_create()
                free(this);
                return NULL;
        }
-       this->segments = ha_sync_segments_create(this->socket);
+       this->segments = ha_sync_segments_create(this->socket, count, active);
        if (secret)
        {
                this->tunnel = ha_sync_tunnel_create(secret, local, remote);
index 6d7faf3..81f6ace 100644 (file)
@@ -23,8 +23,6 @@ typedef u_int8_t u8;
 
 #include <linux/jhash.h>
 
-#define MAX_SEGMENTS 16
-
 typedef struct private_ha_sync_segments_t private_ha_sync_segments_t;
 
 /**
@@ -60,7 +58,7 @@ struct private_ha_sync_segments_t {
        /**
         * mask of active segments
         */
-       u_int16_t active;
+       segment_mask_t active;
 };
 
 /**
@@ -115,14 +113,6 @@ static void log_segments(private_ha_sync_segments_t *this, bool activated,
 }
 
 /**
- * Get the bit of the segment in the bitmask
- */
-static inline u_int16_t bit_of(u_int segment)
-{
-       return 0x01 << (segment - 1);
-}
-
-/**
  * Enable/Disable an an IKE_SA.
  */
 static void enable_disable(private_ha_sync_segments_t *this, u_int segment,
@@ -148,11 +138,11 @@ static void enable_disable(private_ha_sync_segments_t *this, u_int segment,
                {
                        if (enable)
                        {
-                               this->active |= bit_of(i);
+                               this->active |= SEGMENTS_BIT(i);
                        }
                        else
                        {
-                               this->active &= ~bit_of(i);
+                               this->active &= ~SEGMENTS_BIT(i);
                        }
                }
                enumerator = charon->ike_sa_manager->create_enumerator(charon->ike_sa_manager);
@@ -246,7 +236,7 @@ static void resync(private_ha_sync_segments_t *this, u_int segment)
        enumerator_t *enumerator;
        linked_list_t *list;
        ike_sa_id_t *id;
-       u_int16_t mask = bit_of(segment);
+       u_int16_t mask = SEGMENTS_BIT(segment);
 
        list = linked_list_create();
        this->lock->read_lock(this->lock);
@@ -309,12 +299,10 @@ static void destroy(private_ha_sync_segments_t *this)
 /**
  * See header
  */
-ha_sync_segments_t *ha_sync_segments_create(ha_sync_socket_t *socket)
+ha_sync_segments_t *ha_sync_segments_create(ha_sync_socket_t *socket,
+                                                                                       u_int count, segment_mask_t active)
 {
        private_ha_sync_segments_t *this = malloc_thing(private_ha_sync_segments_t);
-       enumerator_t *enumerator;
-       u_int segment;
-       char *str;
 
        this->public.activate = (void(*)(ha_sync_segments_t*, u_int segment,bool))activate;
        this->public.deactivate = (void(*)(ha_sync_segments_t*, u_int segment,bool))deactivate;
@@ -324,22 +312,8 @@ ha_sync_segments_t *ha_sync_segments_create(ha_sync_socket_t *socket)
        this->socket = socket;
        this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
        this->initval = 0;
-       this->active = 0;
-       this->segment_count = lib->settings->get_int(lib->settings,
-                                                               "charon.plugins.ha_sync.segment_count", 1);
-       this->segment_count = min(this->segment_count, MAX_SEGMENTS);
-       str = lib->settings->get_str(lib->settings,
-                                                               "charon.plugins.ha_sync.active_segments", "1");
-       enumerator = enumerator_create_token(str, ",", " ");
-       while (enumerator->enumerate(enumerator, &str))
-       {
-               segment = atoi(str);
-               if (segment > 0 && segment < MAX_SEGMENTS)
-               {
-                       this->active |= bit_of(segment);
-               }
-       }
-       enumerator->destroy(enumerator);
+       this->active = active;
+       this->segment_count = count;
 
        return &this->public;
 }
index e6effad..396d2ef 100644 (file)
 
 typedef struct ha_sync_segments_t ha_sync_segments_t;
 
+typedef u_int16_t segment_mask_t;
+
+/**
+ * maximum number of segments
+ */
+#define SEGMENTS_MAX (sizeof(segment_mask_t)*8)
+
+/**
+ * Get the bit in the mask of a segment
+ */
+#define SEGMENTS_BIT(segment) (0x01 << (segment - 1))
+
 /**
  * Segmentation of peers into active and passive.
  */
@@ -70,8 +82,11 @@ struct ha_sync_segments_t {
  * Create a ha_sync_segments instance.
  *
  * @param socket               socket to communicate segment (de-)activation
+ * @param count                        number of segments the cluster uses
+ * @param active               bit mask of initially active segments
  * @return                             segment object
  */
-ha_sync_segments_t *ha_sync_segments_create(ha_sync_socket_t *socket);
+ha_sync_segments_t *ha_sync_segments_create(ha_sync_socket_t *socket,
+                                                                                       u_int count, segment_mask_t active);
 
 #endif /* HA_SYNC_SEGMENTS_ @}*/