Added a handler for SIGUSR1, which toggles off/on the enforcement calls to tc.
[distributedratelimiting.git] / drl / standard.c
index cadafee..ba1fcc7 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;
         }
 
@@ -170,7 +169,10 @@ void standard_table_remove(standard_flow_table table, standard_flow *flow) {
             }
         }
 
-        assert(current != NULL);
+        //assert(current != NULL);
+        if (current == NULL) {
+            printlog(LOG_WARN, "Flow %p disappeared?\n", current);
+        }
     }
 
     /* Remove the flow from the linked list. */
@@ -242,6 +244,15 @@ void standard_table_update_flows(standard_flow_table table, struct timeval now,
     struct in_addr src, dst;
     char sip[22], dip[22];
 
+    /* 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);
 
     if (time_delta <= 0) {
@@ -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);
@@ -295,7 +304,27 @@ void standard_table_update_flows(standard_flow_table table, struct timeval now,
             maxflowrate = current->rate;
         }
 
-        //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,7 +336,10 @@ 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;