Lots of changes. In no particular order:
[distributedratelimiting.git] / drl / standard.c
index cadafee..9c996e3 100644 (file)
@@ -75,8 +75,7 @@ standard_flow *standard_table_lookup(standard_flow_table table, const key_flow *
     if (flow == NULL) {
         flow = malloc(sizeof(standard_flow));
         if (flow == NULL) {
-            printf("Malloc returned null.\n");
-            printlog(LOG_CRITICAL, "ALLOC: Malloc returned NULL.\n");
+            printlog(LOG_CRITICAL, "standard.c: Malloc returned NULL.\n");
             return NULL;
         }
 
@@ -87,7 +86,7 @@ standard_flow *standard_table_lookup(standard_flow_table table, const key_flow *
         flow->source_port = key->source_port;
         flow->dest_port = key->dest_port;
         flow->last_packet = key->packet_time;
-        gettimeofday(&flow->last_update, NULL);
+        flow->last_update = table->common->last_update;
 
         /* Add the flow to the hash list. */
         flow->nexth = table->flows[hash];
@@ -107,7 +106,7 @@ standard_flow *standard_table_lookup(standard_flow_table table, const key_flow *
         dst.s_addr = ntohl(flow->dest_ip);
         strcpy(sip, inet_ntoa(src));
         strcpy(dip, inet_ntoa(dst));
-        printlog(LOG_DEBUG, "ALLOC:%s:%hd -> %s:%hd\n", sip,
+        printlog(LOG_DEBUG, "ALLOC:%s:%hu -> %s:%hu\n", sip,
                 flow->source_port, dip, flow->dest_port);
     }
 
@@ -138,7 +137,6 @@ int standard_table_sample(standard_flow_table table, const key_flow *key) {
 void standard_table_remove(standard_flow_table table, standard_flow *flow) {
     key_flow key;
     uint32_t hash;
-    standard_flow *current, *prev;
 
     assert(flow);
 
@@ -157,19 +155,22 @@ void standard_table_remove(standard_flow_table table, standard_flow *flow) {
         /* It's the head of the hash list. */
         table->flows[hash] = flow->nexth;
     } else {
+        standard_flow *current, *prev;
+        
         prev = table->flows[hash];
-        current = table->flows[hash]->nexth;
 
-        while (current != NULL) {
+        for (current = table->flows[hash]->nexth; current; current = current->nexth) {
             if (current == flow) {
                 prev->nexth = flow->nexth;
                 break;
             } else {
                 prev = current;
-                current = current->next;
             }
         }
 
+        if (current == NULL) {
+            printlog(LOG_CRITICAL, "Flow %p disappeared?\n", flow);
+        }
         assert(current != NULL);
     }
 
@@ -220,7 +221,7 @@ int standard_table_cleanup(standard_flow_table table) {
     time_t now = time(NULL);
 
     while (current != NULL) {
-        if (current->last_packet + FLOW_IDLE_TIME <= now) {
+        if (current->last_packet + STD_FLOW_IDLE_TIME <= now) {
             /* Flow hasn't received a packet in the time limit - kill it. */
             remove = current;
             current = current->next;
@@ -241,6 +242,16 @@ void standard_table_update_flows(standard_flow_table table, struct timeval now,
     standard_flow *current;
     struct in_addr src, dst;
     char sip[22], dip[22];
+    key_flow largest_flow_info;
+
+    /* Reset statistics. */
+    table->common->num_flows = 0;
+    table->common->num_flows_5k = 0;
+    table->common->num_flows_10k = 0;
+    table->common->num_flows_20k = 0;
+    table->common->num_flows_50k = 0;
+    table->common->avg_rate = 0;
+    /* End statistics. */
 
     time_delta = timeval_subtract(now, table->common->last_update);
 
@@ -267,8 +278,6 @@ void standard_table_update_flows(standard_flow_table table, struct timeval now,
     table->common->bytes_since = 0;
     table->common->last_update = now;
 
-    //printf("Flows: ");
-
     /* Update per-flow information. */
     for (current = table->flows_head; current; current = current->next) {
         time_delta = timeval_subtract(now, current->last_update);
@@ -293,9 +302,34 @@ void standard_table_update_flows(standard_flow_table table, struct timeval now,
 
         if (current->rate > maxflowrate) {
             maxflowrate = current->rate;
+            largest_flow_info.source_ip = current->source_ip;
+            largest_flow_info.dest_ip = current->dest_ip;
+            largest_flow_info.source_port = current->source_port;
+            largest_flow_info.dest_port = current->dest_port;
+            largest_flow_info.protocol = current->protocol;
         }
 
-        //printf("%d, ", current->rate);
+        if (current->rate > 51200) {
+            table->common->num_flows_50k += 1;
+            table->common->num_flows_20k += 1;
+            table->common->num_flows_10k += 1;
+            table->common->num_flows_5k += 1;
+            table->common->num_flows += 1;
+        } else if (current->rate > 20480) {
+            table->common->num_flows_20k += 1;
+            table->common->num_flows_10k += 1;
+            table->common->num_flows_5k += 1;
+            table->common->num_flows += 1;
+        } else if (current->rate > 10240) {
+            table->common->num_flows_10k += 1;
+            table->common->num_flows_5k += 1;
+            table->common->num_flows += 1;
+        } else if (current->rate > 5120) {
+            table->common->num_flows_5k += 1;
+            table->common->num_flows += 1;
+        } else {
+            table->common->num_flows += 1;
+        }
 
         src.s_addr = ntohl(current->source_ip);
         dst.s_addr = ntohl(current->dest_ip);
@@ -307,8 +341,12 @@ void standard_table_update_flows(standard_flow_table table, struct timeval now,
                 current->rate);
     }
 
-    //printf("\n");
+    if (table->common->num_flows > 0) {
+        table->common->avg_rate = table->common->rate / table->common->num_flows;
+    }
+
     printlog(LOG_DEBUG, "FLOW:--\n--\n");
 
     table->common->max_flow_rate = maxflowrate;
+    table->common->max_flow_rate_flow_hash = table->hash_function(&largest_flow_info);
 }