Importing all of DRL, including ulogd and all of its files.
[distributedratelimiting.git] / drl / simple.c
diff --git a/drl/simple.c b/drl/simple.c
new file mode 100644 (file)
index 0000000..fe54abe
--- /dev/null
@@ -0,0 +1,81 @@
+/* See the DRL-LICENSE file for this file's software license. */
+
+#include <arpa/inet.h>
+#include <assert.h>
+#include <inttypes.h>
+#include <netinet/in.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <time.h>
+
+#include "common_accounting.h"
+#include "simple.h"
+#include "logging.h"
+
+simple_flow_table simple_table_create(common_accounting_t *common) {
+    simple_flow_table table = malloc(sizeof(struct sim_flow_table));
+
+    if (table == NULL) {
+        return NULL;
+    }
+
+    memset(table, 0, sizeof(struct sim_flow_table));
+    table->common = common;
+    table->hash_function = NULL;
+
+    gettimeofday(&table->common->last_update, NULL);
+
+    return table;
+}
+
+void simple_table_destroy(simple_flow_table table) {
+    free(table);
+}
+
+int simple_table_sample(simple_flow_table table, const key_flow *key) {
+    /* Update aggregate. */
+    table->common->bytes_since += key->packet_size;
+
+    return 0;
+}
+
+int simple_table_cleanup(simple_flow_table table) {
+    return 0;
+}
+
+void simple_table_update_flows(simple_flow_table table, struct timeval now, double ewma_weight) {
+    double time_delta;
+    double unweighted_rate;
+
+    time_delta = timeval_subtract(now, table->common->last_update);
+
+    if (time_delta <= 0) {
+        unweighted_rate = 0;
+    } else {
+        unweighted_rate = table->common->bytes_since / time_delta;
+    }
+
+    table->common->last_inst_rate = table->common->inst_rate;
+    table->common->inst_rate = unweighted_rate;
+
+    table->common->last_rate = table->common->rate;
+
+    /* If the rate is zero, then we don't know anything yet.  Don't apply EWMA
+     * in that case. */
+    if (table->common->rate == 0) {
+        table->common->rate = unweighted_rate;
+    } else {
+        table->common->rate = table->common->rate * ewma_weight +
+                              unweighted_rate * (1 - ewma_weight);
+    }
+
+    table->common->bytes_since = 0;
+    table->common->last_update = now;
+
+    /* We don't track flow rates in this table. (Not suitable for FPS) */
+    table->common->max_flow_rate = 0;
+}