merge from multibind
[l2tpns.git] / grpsess.c
index a26c489..bfc7d8d 100644 (file)
--- a/grpsess.c
+++ b/grpsess.c
@@ -22,7 +22,7 @@ union grp_iphash {
        union grp_iphash *idx;
 } grp_ip_hash[256];                    // Mapping from IP address to group structures.
 
-static groupidt gnextgrpid = 0;
+groupidt gnextgrpid = 0;
 
 // Find gruop by IP, < 1 for not found
 //
@@ -483,7 +483,7 @@ static void grp_uncache_ipsession(groupidt g, sessionidt s)
 // return the next session can be used on the group
 sessionidt grp_getnextsession(groupidt g, in_addr_t ip)
 {
-       sessionidt s = 0, s2 = 0, s3 = 0, smax = 0;
+       sessionidt s = 0, s2 = 0, s3 = 0;
        int i;
        uint32_t ltime_changed = 0, mintxrate = 0xFFFFFFFF, maxtxrate = 0;
        uint32_t txrate;
@@ -491,43 +491,25 @@ sessionidt grp_getnextsession(groupidt g, in_addr_t ip)
        if (g >= MAXGROUPE)
                return 0;
 
-       if (grpsession[g].time_changed < config->grp_txrate_average_time)
-       {
-               if ((s = sessionbyip(ip)))
-               {
-                       if ( (session[s].ppp.phase > Establish) &&
-                                (time_now - session[s].last_packet <= (config->echo_timeout + 1)) )
-                       {
-                               return s;
-                       }
-                       s = 0;
-               }
-       }
-       else
+       if (grpsession[g].time_changed >= config->grp_txrate_average_time)
        {
                // recalculation txrate
                ltime_changed = grpsession[g].time_changed;
                grpsession[g].time_changed = 0;
-               s = 0;
                for (i = 0; i < grpsession[g].nbsession; i++)
                {
                        if ((s2 = grpsession[g].sesslist[i].sid))
                        {
-                               s3 = s2;
-
                                grpsession[g].sesslist[i].tx_rate = session[s2].coutgrp_delta/ltime_changed;
                                session[s2].coutgrp_delta = 0;
 
-                               //LOG(3, s2, session[s2].tunnel, "TX Rate: %d session weight: %d\n",
-                               //      grpsession[g].sesslist[i].tx_rate, grpsession[g].sesslist[i].weight);
-
                                txrate = grpsession[g].sesslist[i].tx_rate/grpsession[g].sesslist[i].weight;
                                if (txrate < mintxrate)
                                {
                                        if ( session[s2].ppp.phase > Establish &&
                                                (time_now - session[s2].last_packet <= (config->echo_timeout + 1)) )
                                        {
-                                               s = s2;
+                                               grpsession[g].smin = s2;
                                                mintxrate = txrate;
                                        }
                                }
@@ -537,16 +519,29 @@ sessionidt grp_getnextsession(groupidt g, in_addr_t ip)
                                        if ( session[s2].ppp.phase > Establish &&
                                        (time_now - session[s2].last_packet <= (config->echo_timeout + 1)) )
                                        {
-                                               smax = s2;
+                                               grpsession[g].smax = s2;
                                                maxtxrate = txrate;
                                        }
                                }
                        }
                }
+       }
 
-               if (smax && (maxtxrate != mintxrate))
+       if ((s = sessionbyip(ip)))
+       {
+               if (s == grpsession[g].smax)
+               {
+                       s = grpsession[g].smin;
+                       grpsession[g].smax = 0;
+               }
+               else if ( (session[s].ppp.phase > Establish) &&
+                        (time_now - session[s].last_packet <= (config->echo_timeout + 1)) )
                {
-                       grp_uncache_ipsession(g, smax);
+                       return s;
+               }
+               else
+               {
+                       s = 0;
                }
        }
 
@@ -564,7 +559,6 @@ sessionidt grp_getnextsession(groupidt g, in_addr_t ip)
                        (time_now - session[s2].last_packet <= (config->echo_timeout + 1)))
                {
                        s = s2;
-                       //LOG(3, s, session[s].tunnel, "New random session\n");
                }
                else
                {