1 /* See the DRL-LICENSE file for this file's software license. */
4 * Defines the standard "perfect" flow accounting table.
8 #ifndef _STANDARD_ACCOUNTING_H_
9 #define _STANDARD_ACCOUNTING_H_
13 /** The number of hash buckets in the table. */
14 #define FLOW_HASH_SIZE 1024
16 /** The number of seconds after which a flow is considered to be inactive.
17 * Inactive flows will be removed from the table during the next call to the
18 * cleanup function. */
19 #define FLOW_IDLE_TIME 10
21 /** Representation of a flow in a standard table. */
22 typedef struct std_flow {
23 /* Flow information. */
25 /** The rate of the flow in the current estimate interval. */
28 /** The rate of the flow in the previous estimate interval. */
31 /** The number of bytes this flow has sent since the last update. */
34 /** The time at which this flow was last updated. */
35 struct timeval last_update;
37 /** The time at which the most recent packet in this flow was received. */
40 /* Identification information. */
42 /** The flow's source IP address. */
45 /** The flow's destination IP address. */
48 /** The flow's source port. */
51 /** The flow's destination port. */
54 /** The flow's protocol. This corresponds to the protocol field of the IP
60 /** Pointer to the next flow in the hash list. */
61 struct std_flow *nexth;
63 /** Pointers to the next flow in the linked list. */
64 struct std_flow *next;
66 /** Pointers to the previous flow in the linked list. */
67 struct std_flow *prev;
72 * The "table" that stores the flows. It's constructed of two main pieces.
74 * The first is an array of hash buckets. Flows are classified into buckets
75 * by hashing the flow's values (key flow) using the table's hash function.
76 * Flows are chained together as a list in each bucket to deal with collisions.
78 * The second is a simple doubly linked list containing every flow in the table.
80 struct std_flow_table {
82 /** Pointer to the common flow information for the identity that owns this
83 * sampled flow table. This is updated with aggregate information. */
84 common_accounting_t *common;
86 /* Table structures. */
88 /** Hash buckets - each is a list of flows. */
89 struct std_flow *flows[FLOW_HASH_SIZE];
91 /** The head of the linked list of flows. */
92 struct std_flow *flows_head;
94 /** The tail of the linked list of flows. */
95 struct std_flow *flows_tail;
97 /** Function pointer to the function that will yield the hash of a
100 uint32_t (*hash_function)(const key_flow *key);
104 /** The type standard_flow_table is really a pointer to a struct
106 typedef struct std_flow_table *standard_flow_table;
109 * Creates a new table that will use the specified hash function.
111 * Returns the new table or NULL on failure.
113 standard_flow_table standard_table_create(uint32_t (*hash_function)(const key_flow *key), common_accounting_t *common);
116 * Destroys the specified table.
118 void standard_table_destroy(standard_flow_table table);
121 * Looks for a flow that matches the given key in the specified table. If a
122 * matching flow is not found, this function will allocate a new flow for the
125 * Returns a pointer to the flow that matches the key or NULL if there is no
126 * matching flow and a new flow couldn't be allocated.
128 standard_flow *standard_table_lookup(standard_flow_table table, const key_flow *key);
131 * Updates the state of the table given a newly acquired packet.
133 * Returns 1 if the flow is in the table. 0 otherwise (indicating that memory
134 * could not be allocated to add a new flow to the table for the given key.
136 int standard_table_sample(standard_flow_table table, const key_flow *key);
139 * Cleans the table by removing flow entires for any flow that hasn't seen a
140 * new packet in the interval specified by FLOW_IDLE_TIME seconds.
142 int standard_table_cleanup(standard_flow_table table);
145 * Updates the rate information for all flows in the table according to the
146 * specified current time and EWMA weight.
148 void standard_table_update_flows(standard_flow_table table, struct timeval now, double ewma_weight);
150 #endif /* _STANDARD_ACCOUNTING_H_ */