Importing all of DRL, including ulogd and all of its files.
[distributedratelimiting.git] / drl / common_accounting.h
diff --git a/drl/common_accounting.h b/drl/common_accounting.h
new file mode 100644 (file)
index 0000000..d656d8a
--- /dev/null
@@ -0,0 +1,83 @@
+/* See the DRL-LICENSE file for this file's software license. */
+
+/** Defines a structure that is common to all of the flow accounting
+ * mechanisms.  This includes the aggregate rate and the time it was last
+ * updated. */
+
+#ifndef _COMMON_ACCOUNTING_H_
+#define _COMMON_ACCOUNTING_H_
+
+#include <inttypes.h>
+#include <sys/time.h>
+#include <time.h>
+
+/** Representation of a flow that is to be used as a key into the table. */
+typedef struct key_flow {
+
+    /* Identification information.  Some or all of this information is used
+     * by the accounting table hash functions to identify flows. */
+
+    /** The source IP address. */
+    uint32_t source_ip;
+
+    /** The destination IP address. */
+    uint32_t dest_ip;
+
+    /** The source TCP/UDP port. */
+    uint16_t source_port;
+
+    /** The destination TCP/UDP port. */
+    uint16_t dest_port;
+
+    /** The value of the IP header protocol field. */
+    uint8_t protocol;
+
+    /* 
+     * Packet information.  This is not used by the accounting tables for
+     * hashing, but is useful when sampling a new packet.
+     */
+
+    /** The size of the packet (in bytes). */
+    uint16_t packet_size;
+
+    /** The time at which the packet was sent. */
+    time_t packet_time;
+
+} key_flow;
+
+/** 
+ * This structure is used to store information common to all flow accounting
+ * mechanisms.  It includes the aggregate rate information that will be read
+ * by the estimation and allocation functions.
+ */
+typedef struct {
+    /** The best estimate of the rate for the current estimate interval.  This
+     * includes EWMA smoothing. */
+    uint32_t rate;
+
+    /** The best estimate of the rate for the previous estimate inteval.  This
+     * includes EWMA smoothing. */
+    uint32_t last_rate;
+
+    /** The instantaneous rate for the current estimate interval.*/
+    uint32_t inst_rate;
+
+    /** The instantaneous rate for the previous estimate interval.*/
+    uint32_t last_inst_rate;
+
+    /** The rate of the largest flow being tracked by the table. */
+    uint32_t max_flow_rate;
+
+    /** The time at which this structure was last updated. */
+    struct timeval last_update;
+
+    /** The number of bytes sent since the last_update time. */
+    uint32_t bytes_since;
+
+} common_accounting_t;
+
+/** Determines the difference between two timeval structs (in seconds, with
+ * fractions). */
+#define timeval_subtract(big, small) ((double) (big.tv_sec - small.tv_sec) + (big.tv_usec - small.tv_usec) / 1000000.0)
+
+#endif  /* _COMMON_ACCOUNTING_H_ */