1 /* See the DRL-LICENSE file for this file's software license. */
4 * Defines the multiple-interval "perfect" flow accounting table.
8 #ifndef _MULTIPLE_ACCOUNTING_H_
9 #define _MULTIPLE_ACCOUNTING_H_
13 //FIXME: Update comments on structure variables
15 /** The number of hash buckets in the table. */
16 #define MUL_FLOW_HASH_SIZE 1024
18 /** The number of seconds after which a flow is considered to be inactive.
19 * Inactive flows will be removed from the table during the next call to the
20 * cleanup function. */
21 #define MUL_FLOW_IDLE_TIME 15
23 #define MUL_INTERVAL_COUNT 10
25 typedef struct mul_interval {
26 /** The number of bytes this flow has sent since the last update to this
27 * interval structure. */
30 /** The time at which this interval was last updated. */
31 struct timeval last_update;
33 /** The time at which the most recent packet in this flow was received
34 * in this interval. */
39 struct mul_interval *next;
42 /** Representation of a flow in a multiple-interval table. */
43 typedef struct mul_flow {
44 /* Flow information. */
46 /** The rate of the flow in the current estimate interval. */
49 /** The rate of the flow in the previous estimate interval. */
54 interval *current_interval;
58 /* Identification information. */
60 /** The flow's source IP address. */
63 /** The flow's destination IP address. */
66 /** The flow's source port. */
69 /** The flow's destination port. */
72 /** The flow's protocol. This corresponds to the protocol field of the IP
78 /** Pointer to the next flow in the hash list. */
79 struct mul_flow *nexth;
81 /** Pointers to the next flow in the linked list. */
82 struct mul_flow *next;
84 /** Pointers to the previous flow in the linked list. */
85 struct mul_flow *prev;
90 * The "table" that stores the flows. It's constructed of two main pieces.
92 * The first is an array of hash buckets. Flows are classified into buckets
93 * by hashing the flow's values (key flow) using the table's hash function.
94 * Flows are chained together as a list in each bucket to deal with collisions.
96 * The second is a simple doubly linked list containing every flow in the table.
98 struct mul_flow_table {
100 /** Pointer to the common flow information for the identity that owns this
101 * sampled flow table. This is updated with aggregate information. */
102 common_accounting_t *common;
104 uint32_t interval_count;
106 interval *current_interval;
110 /* Table structures. */
112 /** Hash buckets - each is a list of flows. */
113 struct mul_flow *flows[MUL_FLOW_HASH_SIZE];
115 /** The head of the linked list of flows. */
116 struct mul_flow *flows_head;
118 /** The tail of the linked list of flows. */
119 struct mul_flow *flows_tail;
121 /** Function pointer to the function that will yield the hash of a
124 uint32_t (*hash_function)(const key_flow *key);
128 /** The type multiple_flow_table is really a pointer to a struct
130 typedef struct mul_flow_table *multiple_flow_table;
133 * Creates a new table that will use the specified hash function.
135 * Returns the new table or NULL on failure.
137 multiple_flow_table multiple_table_create(uint32_t (*hash_function)(const key_flow *key), uint32_t interval_count, common_accounting_t *common);
140 * Destroys the specified table.
142 void multiple_table_destroy(multiple_flow_table table);
145 * Looks for a flow that matches the given key in the specified table. If a
146 * matching flow is not found, this function will allocate a new flow for the
149 * Returns a pointer to the flow that matches the key or NULL if there is no
150 * matching flow and a new flow couldn't be allocated.
152 multiple_flow *multiple_table_lookup(multiple_flow_table table, const key_flow *key);
155 * Updates the state of the table given a newly acquired packet.
157 * Returns 1 if the flow is in the table. 0 otherwise (indicating that memory
158 * could not be allocated to add a new flow to the table for the given key.
160 int multiple_table_sample(multiple_flow_table table, const key_flow *key);
163 * Cleans the table by removing flow entires for any flow that hasn't seen a
164 * new packet in the interval specified by MUL_FLOW_IDLE_TIME seconds.
166 int multiple_table_cleanup(multiple_flow_table table);
169 * Updates the rate information for all flows in the table according to the
170 * specified current time and EWMA weight.
172 void multiple_table_update_flows(multiple_flow_table table, struct timeval now, double ewma_weight);
174 #endif /* _MULTIPLE_ACCOUNTING_H_ */