1 /* See the DRL-LICENSE file for this file's software license. */
6 #include <netinet/in.h>
12 #include <sys/types.h>
15 #include "common_accounting.h"
19 standard_flow_table standard_table_create(uint32_t (*hash_function)(const key_flow *key), common_accounting_t *common) {
20 standard_flow_table table = malloc(sizeof(struct std_flow_table));
26 memset(table, 0, sizeof(struct std_flow_table));
27 table->common = common;
28 table->hash_function = hash_function;
30 gettimeofday(&table->common->last_update, NULL);
35 void standard_table_destroy(standard_flow_table table) {
36 standard_flow *current, *next;
38 if ((current = table->flows_head)) {
39 while (current->next) {
50 /* Looks for the flow in the table. If the flow isn't there, it allocates a
52 standard_flow *standard_table_lookup(standard_flow_table table, const key_flow *key) {
55 struct in_addr src, dst;
56 char sip[22], dip[22];
62 hash = table->hash_function(key);
64 /* Find the flow, if it's there. */
65 for (flow = table->flows[hash]; flow; flow = flow->nexth) {
66 if (flow->source_ip == key->source_ip &&
67 flow->dest_ip == key->dest_ip &&
68 flow->source_port == key->source_port &&
69 flow->dest_port == key->dest_port &&
70 flow->protocol == key->protocol) {
76 flow = malloc(sizeof(standard_flow));
78 printlog(LOG_CRITICAL, "standard.c: Malloc returned NULL.\n");
82 memset(flow, 0, sizeof(standard_flow));
83 flow->protocol = key->protocol;
84 flow->source_ip = key->source_ip;
85 flow->dest_ip = key->dest_ip;
86 flow->source_port = key->source_port;
87 flow->dest_port = key->dest_port;
88 flow->last_packet = key->packet_time;
89 gettimeofday(&flow->last_update, NULL);
91 /* Add the flow to the hash list. */
92 flow->nexth = table->flows[hash];
93 table->flows[hash] = flow;
95 /* Add the flow to the linked list. */
96 if (table->flows_tail) {
97 flow->prev = table->flows_tail;
98 table->flows_tail->next = flow;
99 table->flows_tail = flow;
101 table->flows_head = table->flows_tail = flow;
102 /* next and prev are already null due to memset above. */
105 src.s_addr = ntohl(flow->source_ip);
106 dst.s_addr = ntohl(flow->dest_ip);
107 strcpy(sip, inet_ntoa(src));
108 strcpy(dip, inet_ntoa(dst));
109 printlog(LOG_DEBUG, "ALLOC:%s:%hd -> %s:%hd\n", sip,
110 flow->source_port, dip, flow->dest_port);
116 int standard_table_sample(standard_flow_table table, const key_flow *key) {
119 assert(table != NULL);
120 assert(table->common != NULL);
122 /* Update aggregate. */
123 table->common->bytes_since += key->packet_size;
126 flow = standard_table_lookup(table, key);
131 flow->bytes_since += key->packet_size;
132 flow->last_packet = key->packet_time;
137 void standard_table_remove(standard_flow_table table, standard_flow *flow) {
140 standard_flow *current, *prev;
144 /* Remove the flow from the hash list. */
145 key.source_ip = flow->source_ip;
146 key.dest_ip = flow->dest_ip;
147 key.source_port = flow->source_port;
148 key.dest_port = flow->dest_port;
149 key.protocol = flow->protocol;
151 hash = table->hash_function(&key);
153 assert(table->flows[hash]);
155 if (table->flows[hash] == flow) {
156 /* It's the head of the hash list. */
157 table->flows[hash] = flow->nexth;
159 prev = table->flows[hash];
160 current = table->flows[hash]->nexth;
162 while (current != NULL) {
163 if (current == flow) {
164 prev->nexth = flow->nexth;
168 current = current->next;
172 //assert(current != NULL);
173 if (current == NULL) {
174 printlog(LOG_WARN, "Flow %p disappeared?\n", current);
178 /* Remove the flow from the linked list. */
179 if (flow->prev == NULL && flow->next == NULL) {
180 /* It's the head, tail, and only element of the list. */
181 assert(table->flows_head == flow);
182 assert(table->flows_tail == flow);
184 table->flows_head = NULL;
185 table->flows_tail = NULL;
186 } else if (flow->prev == NULL) {
187 /* It's the head of the list. */
188 assert(table->flows_head == flow);
190 table->flows_head = flow->next;
192 if (table->flows_head != NULL) {
193 table->flows_head->prev = NULL;
195 } else if (flow->next == NULL) {
196 /* It's the tail of the list. */
197 assert(table->flows_tail == flow);
199 table->flows_tail = flow->prev;
201 table->flows_tail->next = NULL;
203 /* Not the head or tail of the list. */
204 assert(table->flows_head != flow);
206 flow->prev->next = flow->next;
208 if (flow->next != NULL) {
209 flow->next->prev = flow->prev;
213 memset(flow, 0, sizeof(standard_flow));
219 int standard_table_cleanup(standard_flow_table table) {
220 standard_flow *current = table->flows_head;
221 standard_flow *remove;
222 time_t now = time(NULL);
224 while (current != NULL) {
225 if (current->last_packet + FLOW_IDLE_TIME <= now) {
226 /* Flow hasn't received a packet in the time limit - kill it. */
228 current = current->next;
230 standard_table_remove(table, remove);
232 current = current->next;
239 void standard_table_update_flows(standard_flow_table table, struct timeval now, double ewma_weight) {
240 uint32_t maxflowrate = 0;
242 double unweighted_rate;
243 standard_flow *current;
244 struct in_addr src, dst;
245 char sip[22], dip[22];
247 time_delta = timeval_subtract(now, table->common->last_update);
249 if (time_delta <= 0) {
252 unweighted_rate = table->common->bytes_since / time_delta;
255 table->common->last_inst_rate = table->common->inst_rate;
256 table->common->inst_rate = unweighted_rate;
258 table->common->last_rate = table->common->rate;
260 /* If the rate is zero, then we don't know anything yet. Don't apply EWMA
262 if (table->common->rate == 0) {
263 table->common->rate = unweighted_rate;
265 table->common->rate = table->common->rate * ewma_weight +
266 unweighted_rate * (1 - ewma_weight);
269 table->common->bytes_since = 0;
270 table->common->last_update = now;
272 /* Update per-flow information. */
273 for (current = table->flows_head; current; current = current->next) {
274 time_delta = timeval_subtract(now, current->last_update);
276 if (time_delta <= 0) {
279 unweighted_rate = current->bytes_since / time_delta;
282 current->last_rate = current->rate;
284 if (current->rate == 0) {
285 current->rate = unweighted_rate;
287 current->rate = current->rate * ewma_weight +
288 unweighted_rate * (1 - ewma_weight);
291 current->bytes_since = 0;
292 current->last_update = now;
294 if (current->rate > maxflowrate) {
295 maxflowrate = current->rate;
298 src.s_addr = ntohl(current->source_ip);
299 dst.s_addr = ntohl(current->dest_ip);
300 strcpy(sip, inet_ntoa(src));
301 strcpy(dip, inet_ntoa(dst));
302 printlog(LOG_DEBUG, "FLOW: (%p) %s:%d -> %s:%d at %d\n", current,
303 sip, current->source_port,
304 dip, current->dest_port,
308 printlog(LOG_DEBUG, "FLOW:--\n--\n");
310 table->common->max_flow_rate = maxflowrate;