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 printf("Malloc returned null.\n");
79 printlog(LOG_CRITICAL, "ALLOC: Malloc returned NULL.\n");
83 memset(flow, 0, sizeof(standard_flow));
84 flow->protocol = key->protocol;
85 flow->source_ip = key->source_ip;
86 flow->dest_ip = key->dest_ip;
87 flow->source_port = key->source_port;
88 flow->dest_port = key->dest_port;
89 flow->last_packet = key->packet_time;
90 gettimeofday(&flow->last_update, NULL);
92 /* Add the flow to the hash list. */
93 flow->nexth = table->flows[hash];
94 table->flows[hash] = flow;
96 /* Add the flow to the linked list. */
97 if (table->flows_tail) {
98 flow->prev = table->flows_tail;
99 table->flows_tail->next = flow;
100 table->flows_tail = flow;
102 table->flows_head = table->flows_tail = flow;
103 /* next and prev are already null due to memset above. */
106 src.s_addr = ntohl(flow->source_ip);
107 dst.s_addr = ntohl(flow->dest_ip);
108 strcpy(sip, inet_ntoa(src));
109 strcpy(dip, inet_ntoa(dst));
110 printlog(LOG_DEBUG, "ALLOC:%s:%hd -> %s:%hd\n", sip,
111 flow->source_port, dip, flow->dest_port);
117 int standard_table_sample(standard_flow_table table, const key_flow *key) {
120 assert(table != NULL);
121 assert(table->common != NULL);
123 /* Update aggregate. */
124 table->common->bytes_since += key->packet_size;
127 flow = standard_table_lookup(table, key);
132 flow->bytes_since += key->packet_size;
133 flow->last_packet = key->packet_time;
138 void standard_table_remove(standard_flow_table table, standard_flow *flow) {
141 standard_flow *current, *prev;
145 /* Remove the flow from the hash list. */
146 key.source_ip = flow->source_ip;
147 key.dest_ip = flow->dest_ip;
148 key.source_port = flow->source_port;
149 key.dest_port = flow->dest_port;
150 key.protocol = flow->protocol;
152 hash = table->hash_function(&key);
154 assert(table->flows[hash]);
156 if (table->flows[hash] == flow) {
157 /* It's the head of the hash list. */
158 table->flows[hash] = flow->nexth;
160 prev = table->flows[hash];
161 current = table->flows[hash]->nexth;
163 while (current != NULL) {
164 if (current == flow) {
165 prev->nexth = flow->nexth;
169 current = current->next;
173 assert(current != NULL);
176 /* Remove the flow from the linked list. */
177 if (flow->prev == NULL && flow->next == NULL) {
178 /* It's the head, tail, and only element of the list. */
179 assert(table->flows_head == flow);
180 assert(table->flows_tail == flow);
182 table->flows_head = NULL;
183 table->flows_tail = NULL;
184 } else if (flow->prev == NULL) {
185 /* It's the head of the list. */
186 assert(table->flows_head == flow);
188 table->flows_head = flow->next;
190 if (table->flows_head != NULL) {
191 table->flows_head->prev = NULL;
193 } else if (flow->next == NULL) {
194 /* It's the tail of the list. */
195 assert(table->flows_tail == flow);
197 table->flows_tail = flow->prev;
199 table->flows_tail->next = NULL;
201 /* Not the head or tail of the list. */
202 assert(table->flows_head != flow);
204 flow->prev->next = flow->next;
206 if (flow->next != NULL) {
207 flow->next->prev = flow->prev;
211 memset(flow, 0, sizeof(standard_flow));
217 int standard_table_cleanup(standard_flow_table table) {
218 standard_flow *current = table->flows_head;
219 standard_flow *remove;
220 time_t now = time(NULL);
222 while (current != NULL) {
223 if (current->last_packet + FLOW_IDLE_TIME <= now) {
224 /* Flow hasn't received a packet in the time limit - kill it. */
226 current = current->next;
228 standard_table_remove(table, remove);
230 current = current->next;
237 void standard_table_update_flows(standard_flow_table table, struct timeval now, double ewma_weight) {
238 uint32_t maxflowrate = 0;
240 double unweighted_rate;
241 standard_flow *current;
242 struct in_addr src, dst;
243 char sip[22], dip[22];
245 time_delta = timeval_subtract(now, table->common->last_update);
247 if (time_delta <= 0) {
250 unweighted_rate = table->common->bytes_since / time_delta;
253 table->common->last_inst_rate = table->common->inst_rate;
254 table->common->inst_rate = unweighted_rate;
256 table->common->last_rate = table->common->rate;
258 /* If the rate is zero, then we don't know anything yet. Don't apply EWMA
260 if (table->common->rate == 0) {
261 table->common->rate = unweighted_rate;
263 table->common->rate = table->common->rate * ewma_weight +
264 unweighted_rate * (1 - ewma_weight);
267 table->common->bytes_since = 0;
268 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 //printf("%d, ", current->rate);
300 src.s_addr = ntohl(current->source_ip);
301 dst.s_addr = ntohl(current->dest_ip);
302 strcpy(sip, inet_ntoa(src));
303 strcpy(dip, inet_ntoa(dst));
304 printlog(LOG_DEBUG, "FLOW: (%p) %s:%d -> %s:%d at %d\n", current,
305 sip, current->source_port,
306 dip, current->dest_port,
311 printlog(LOG_DEBUG, "FLOW:--\n--\n");
313 table->common->max_flow_rate = maxflowrate;