Add grouping session functionality for load balancing and failover
[l2tpns.git] / cluster.c
index cf65972..4b85f67 100644 (file)
--- a/cluster.c
+++ b/cluster.c
@@ -16,6 +16,7 @@
 #include <malloc.h>
 #include <errno.h>
 #include <libcli.h>
+#include <linux/rtnetlink.h>
 
 #include "l2tpns.h"
 #include "cluster.h"
@@ -709,6 +710,7 @@ void cluster_check_master(void)
                increment_counter(&session[i].cout, &session[i].cout_wrap, sess_local[i].cout);
                session[i].cin_delta += sess_local[i].cin;
                session[i].cout_delta += sess_local[i].cout;
+               session[i].coutgrp_delta += sess_local[i].cout;
 
                session[i].pin += sess_local[i].pin;
                session[i].pout += sess_local[i].pout;
@@ -1018,54 +1020,27 @@ static int type_changed(int type, int id)
 {
        int i;
 
-       for (i = 0 ; i < config->cluster_num_changes ; ++i)
-               if ( cluster_changes[i].id == id &&
-                       cluster_changes[i].type == type)
-                       return 0;       // Already marked for change.
-
-       cluster_changes[i].type = type;
-       cluster_changes[i].id = id;
-       ++config->cluster_num_changes;
-
-       if (config->cluster_num_changes > MAX_CHANGES)
-               cluster_heartbeat(); // flush now
-
-       return 1;
-}
-
-// The deleted session, must be before the new session
-int cluster_listinvert_session(int sidnew, int sidtodel)
-{
-       int i, inew = 0;
-
        for (i = 0 ; i < config->cluster_num_changes ; ++i)
        {
-               if ( cluster_changes[i].id == sidtodel && cluster_changes[i].type == C_CSESSION)
-                       return 0;       // Deleted session already before the new session.
-
-               if ( cluster_changes[i].id == sidnew && cluster_changes[i].type == C_CSESSION)
+               if ( cluster_changes[i].id == id && cluster_changes[i].type == type)
                {
-                       if (session[i].tunnel != T_FREE)
-                               inew = i;
-                       else
-                               return 0;       // This a free session no invert.
-
+                       // Already marked for change, remove it
+                       --config->cluster_num_changes;
+                       memmove(&cluster_changes[i],
+                                       &cluster_changes[i+1],
+                                       (config->cluster_num_changes - i) * sizeof(cluster_changes[i]));
                        break;
                }
        }
 
-       for ( ; i < config->cluster_num_changes ; ++i)
-       {
-               if ( cluster_changes[i].id == sidtodel && cluster_changes[i].type == C_CSESSION)
-               {
-                       // Reverse position
-                       cluster_changes[i].id = sidnew;
-                       cluster_changes[inew].id = sidtodel;
-                       return 1;
-               }
-       }
+       cluster_changes[config->cluster_num_changes].type = type;
+       cluster_changes[config->cluster_num_changes].id = id;
+       ++config->cluster_num_changes;
 
-       return 0;
+       if (config->cluster_num_changes > MAX_CHANGES)
+               cluster_heartbeat(); // flush now
+
+       return 1;
 }
 
 // A particular session has been changed!
@@ -1290,6 +1265,7 @@ static int cluster_handle_bytes(uint8_t *data, int size)
 
                session[b->sid].cin_delta += b->cin;
                session[b->sid].cout_delta += b->cout;
+               session[b->sid].coutgrp_delta += b->cout;
 
                if (b->cin)
                        session[b->sid].last_packet = session[b->sid].last_data = time_now;