1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id$ |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | /* This program takes a series of traces and bpf filters and outputs how many |
---|
32 | * bytes/packets every time interval |
---|
33 | */ |
---|
34 | |
---|
35 | #include <stdio.h> |
---|
36 | #include <stdlib.h> |
---|
37 | #include <assert.h> |
---|
38 | #include <string.h> |
---|
39 | #include <sys/time.h> |
---|
40 | #include <sys/types.h> |
---|
41 | #include <time.h> |
---|
42 | |
---|
43 | #include <netinet/in.h> |
---|
44 | #include <netinet/in_systm.h> |
---|
45 | #include <netinet/tcp.h> |
---|
46 | #include <netinet/ip.h> |
---|
47 | #include <netinet/ip_icmp.h> |
---|
48 | #include <arpa/inet.h> |
---|
49 | #include <sys/socket.h> |
---|
50 | #include <getopt.h> |
---|
51 | #include <inttypes.h> |
---|
52 | #include <lt_inttypes.h> |
---|
53 | |
---|
54 | #include "libtrace.h" |
---|
55 | #include "output.h" |
---|
56 | #include "rt_protocol.h" |
---|
57 | #include "dagformat.h" |
---|
58 | |
---|
59 | #include "data-struct/vector.h" |
---|
60 | #include "data-struct/message_queue.h" |
---|
61 | |
---|
62 | #ifndef UINT32_MAX |
---|
63 | #define UINT32_MAX 0xffffffffU |
---|
64 | #endif |
---|
65 | |
---|
66 | #define DEFAULT_OUTPUT_FMT "txt" |
---|
67 | |
---|
68 | struct libtrace_t *trace; |
---|
69 | char *output_format=NULL; |
---|
70 | |
---|
71 | int merge_inputs = 0; |
---|
72 | |
---|
73 | struct filter_t { |
---|
74 | char *expr; |
---|
75 | struct libtrace_filter_t *filter; |
---|
76 | uint64_t count; |
---|
77 | uint64_t bytes; |
---|
78 | } *filters = NULL; |
---|
79 | int filter_count=0; |
---|
80 | uint64_t totcount; |
---|
81 | uint64_t totbytes; |
---|
82 | |
---|
83 | uint64_t packet_count=UINT64_MAX; |
---|
84 | double packet_interval=UINT32_MAX; |
---|
85 | |
---|
86 | |
---|
87 | struct output_data_t *output = NULL; |
---|
88 | |
---|
89 | static void report_results(double ts,uint64_t count,uint64_t bytes) |
---|
90 | { |
---|
91 | int i=0; |
---|
92 | output_set_data_time(output,0,ts); |
---|
93 | output_set_data_int(output,1,count); |
---|
94 | output_set_data_int(output,2,bytes); |
---|
95 | for(i=0;i<filter_count;++i) { |
---|
96 | output_set_data_int(output,i*2+3,filters[i].count); |
---|
97 | output_set_data_int(output,i*2+4,filters[i].bytes); |
---|
98 | filters[i].count=filters[i].bytes=0; |
---|
99 | } |
---|
100 | output_flush_row(output); |
---|
101 | } |
---|
102 | |
---|
103 | static void create_output(char *title) { |
---|
104 | int i; |
---|
105 | |
---|
106 | output=output_init(title,output_format?output_format:DEFAULT_OUTPUT_FMT); |
---|
107 | if (!output) { |
---|
108 | fprintf(stderr,"Failed to create output file\n"); |
---|
109 | return; |
---|
110 | } |
---|
111 | output_add_column(output,"ts"); |
---|
112 | output_add_column(output,"packets"); |
---|
113 | output_add_column(output,"bytes"); |
---|
114 | for(i=0;i<filter_count;++i) { |
---|
115 | char buff[1024]; |
---|
116 | snprintf(buff,sizeof(buff),"%s packets",filters[i].expr); |
---|
117 | output_add_column(output,buff); |
---|
118 | snprintf(buff,sizeof(buff),"%s bytes",filters[i].expr); |
---|
119 | output_add_column(output,buff); |
---|
120 | } |
---|
121 | output_flush_headings(output); |
---|
122 | |
---|
123 | } |
---|
124 | |
---|
125 | uint64_t count; |
---|
126 | uint64_t bytes; |
---|
127 | |
---|
128 | typedef struct statistic { |
---|
129 | uint64_t count; |
---|
130 | uint64_t bytes; |
---|
131 | } statistic_t; |
---|
132 | |
---|
133 | typedef struct result { |
---|
134 | struct statistic total; |
---|
135 | struct statistic filters[0]; |
---|
136 | } result_t; |
---|
137 | |
---|
138 | |
---|
139 | static int reduce(libtrace_t* trace, void* global_blob, uint64_t *last_ts) |
---|
140 | { |
---|
141 | int i,j; |
---|
142 | //uint64_t count=0, bytes=0; |
---|
143 | static uint64_t ts = 0; |
---|
144 | libtrace_vector_t results; |
---|
145 | libtrace_vector_init(&results, sizeof(libtrace_result_t)); |
---|
146 | trace_get_results(trace, &results); |
---|
147 | //uint64_t packets; |
---|
148 | |
---|
149 | /* Get the results from each core and sum 'em up */ |
---|
150 | for (i = 0 ; i < libtrace_vector_get_size(&results) ; i++) { |
---|
151 | libtrace_result_t result; |
---|
152 | |
---|
153 | assert(libtrace_vector_get(&results, i, (void *) &result) == 1); |
---|
154 | ts = libtrace_result_get_key(&result); |
---|
155 | if (*last_ts == 0) |
---|
156 | *last_ts = ts; |
---|
157 | |
---|
158 | result_t * res = libtrace_result_get_value(&result); |
---|
159 | static result_t * last_res = NULL; |
---|
160 | // Memory manager might falsely trigger this |
---|
161 | assert(res != last_res); |
---|
162 | last_res = res; |
---|
163 | //printf("Perpkt published %"PRIu64" - c=%"PRIu64"\n", ts, res->total.count); |
---|
164 | while (*last_ts < ts) { |
---|
165 | report_results((double) *last_ts * (double) packet_interval, count, bytes); |
---|
166 | count = 0; |
---|
167 | bytes = 0; |
---|
168 | for (j = 0; j < filter_count; j++) |
---|
169 | filters[j].count = filters[j].bytes = 0; |
---|
170 | (*last_ts)++; |
---|
171 | } |
---|
172 | |
---|
173 | count += res->total.count; |
---|
174 | bytes += res->total.bytes; |
---|
175 | for (j = 0; j < filter_count; j++) { |
---|
176 | filters[j].count += res->filters[j].count; |
---|
177 | filters[j].bytes += res->filters[j].bytes; |
---|
178 | } |
---|
179 | free(res); |
---|
180 | } |
---|
181 | // Done with these results - Free internally and externally |
---|
182 | libtrace_vector_destroy(&results); |
---|
183 | |
---|
184 | return 0; |
---|
185 | } |
---|
186 | |
---|
187 | typedef struct timestamp_sync { |
---|
188 | int64_t difference_usecs; |
---|
189 | uint64_t first_interval_number; |
---|
190 | } timestamp_sync_t; |
---|
191 | |
---|
192 | static void* per_packet(libtrace_t *trace, libtrace_packet_t *pkt, |
---|
193 | libtrace_message_t *mesg, |
---|
194 | libtrace_thread_t *t) |
---|
195 | { |
---|
196 | int i; |
---|
197 | static __thread uint64_t last_ts = 0, ts = 0; |
---|
198 | static __thread result_t * results = NULL; |
---|
199 | |
---|
200 | // Unsure when we would hit this case but the old code had it, I |
---|
201 | // guess we should keep it |
---|
202 | if (pkt && trace_get_packet_buffer(pkt,NULL,NULL) != NULL) { |
---|
203 | //fprintf(stderr, "Got packet t=%x\n", t); |
---|
204 | ts = trace_get_seconds(pkt) / packet_interval; |
---|
205 | if (last_ts == 0) |
---|
206 | last_ts = ts; |
---|
207 | |
---|
208 | while (packet_interval != UINT64_MAX && last_ts<ts) { |
---|
209 | // Publish and make a new one new |
---|
210 | //fprintf(stderr, "Publishing result %"PRIu64"\n", last_ts); |
---|
211 | trace_publish_result(trace, t, (uint64_t) last_ts, results, RESULT_NORMAL); |
---|
212 | trace_post_reporter(trace); |
---|
213 | results = calloc(1, sizeof(result_t) + sizeof(statistic_t) * filter_count); |
---|
214 | last_ts++; |
---|
215 | } |
---|
216 | |
---|
217 | for(i=0;i<filter_count;++i) { |
---|
218 | if(trace_apply_filter(filters[i].filter, pkt)) { |
---|
219 | results->filters[i].count++; |
---|
220 | results->filters[i].bytes+=trace_get_wire_length(pkt); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | results->total.count++; |
---|
225 | results->total.bytes +=trace_get_wire_length(pkt); |
---|
226 | /*if (count >= packet_count) { |
---|
227 | report_results(ts,count,bytes); |
---|
228 | count=0; |
---|
229 | bytes=0; |
---|
230 | }*/ // TODO what was happening here doesn't match up with any of the documentations!!! |
---|
231 | } |
---|
232 | |
---|
233 | if (mesg) { |
---|
234 | // printf ("%d.%06d READ #%"PRIu64"\n", tv.tv_sec, tv.tv_usec, trace_packet_get(packet)); |
---|
235 | switch (mesg->code) { |
---|
236 | case MESSAGE_STARTING: |
---|
237 | results = calloc(1, sizeof(result_t) + sizeof(statistic_t) * filter_count); |
---|
238 | break; |
---|
239 | case MESSAGE_STOPPING: |
---|
240 | // Should we always post this? |
---|
241 | if (results->total.count) { |
---|
242 | trace_publish_result(trace, t, (uint64_t) last_ts, results, RESULT_NORMAL); |
---|
243 | trace_post_reporter(trace); |
---|
244 | results = NULL; |
---|
245 | } |
---|
246 | break; |
---|
247 | case MESSAGE_TICK: |
---|
248 | { |
---|
249 | int64_t offset; |
---|
250 | struct timeval *tv, tv_real; |
---|
251 | libtrace_packet_t *first_packet = NULL; |
---|
252 | retrive_first_packet(trace, &first_packet, &tv); |
---|
253 | if (first_packet != NULL) { |
---|
254 | // So figure out our running offset |
---|
255 | tv_real = trace_get_timeval(first_packet); |
---|
256 | offset = tv_to_usec(tv) - tv_to_usec(&tv_real); |
---|
257 | // Get time of day and do this stuff |
---|
258 | uint64_t next_update_time; |
---|
259 | next_update_time = (last_ts*packet_interval + packet_interval) * 1000000 + offset; |
---|
260 | if (next_update_time <= mesg->additional.uint64) { |
---|
261 | //fprintf(stderr, "Got a tick and publishing early!!\n"); |
---|
262 | trace_publish_result(trace, t, (uint64_t) last_ts, results, RESULT_NORMAL); |
---|
263 | trace_post_reporter(trace); |
---|
264 | results = calloc(1, sizeof(result_t) + sizeof(statistic_t) * filter_count); |
---|
265 | last_ts++; |
---|
266 | } else { |
---|
267 | //fprintf(stderr, "Got a tick but no publish ...\n"); |
---|
268 | } |
---|
269 | } else { |
---|
270 | //fprintf(stderr, "Got a tick but no packets seen yet!!!\n"); |
---|
271 | } |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | return pkt; |
---|
276 | } |
---|
277 | |
---|
278 | static uint64_t bad_hash(const libtrace_packet_t * pkt, void *data) { |
---|
279 | return 0; |
---|
280 | } |
---|
281 | |
---|
282 | /* Process a trace, counting packets that match filter(s) */ |
---|
283 | static void run_trace(char *uri) |
---|
284 | { |
---|
285 | int j; |
---|
286 | uint64_t last_ts = 0; |
---|
287 | |
---|
288 | if (!merge_inputs) |
---|
289 | create_output(uri); |
---|
290 | |
---|
291 | if (output == NULL) |
---|
292 | return; |
---|
293 | |
---|
294 | trace = trace_create(uri); |
---|
295 | if (trace_is_err(trace)) { |
---|
296 | trace_perror(trace,"trace_create"); |
---|
297 | trace_destroy(trace); |
---|
298 | if (!merge_inputs) |
---|
299 | output_destroy(output); |
---|
300 | return; |
---|
301 | } |
---|
302 | /* |
---|
303 | if (trace_start(trace)==-1) { |
---|
304 | trace_perror(trace,"trace_start"); |
---|
305 | trace_destroy(trace); |
---|
306 | if (!merge_inputs) |
---|
307 | output_destroy(output); |
---|
308 | return; |
---|
309 | }*/ |
---|
310 | int i = 1; |
---|
311 | trace_parallel_config(trace, TRACE_OPTION_ORDERED, &i); |
---|
312 | /* trace_parallel_config(trace, TRACE_OPTION_TRACETIME, &i); */ |
---|
313 | //trace_set_hasher(trace, HASHER_CUSTOM, &bad_hash, NULL); |
---|
314 | |
---|
315 | if (trace_get_information(trace)->live) { |
---|
316 | i = (int) (packet_interval * 1000); // Every interval send a tick |
---|
317 | trace_parallel_config(trace, TRACE_OPTION_TICK_INTERVAL, &i); |
---|
318 | } |
---|
319 | |
---|
320 | if (trace_pstart(trace, NULL, &per_packet, NULL)==-1) { |
---|
321 | trace_perror(trace,"Failed to start trace"); |
---|
322 | trace_destroy(trace); |
---|
323 | if (!merge_inputs) |
---|
324 | output_destroy(output); |
---|
325 | return; |
---|
326 | } |
---|
327 | |
---|
328 | |
---|
329 | // reduce |
---|
330 | while (!trace_finished(trace)) { |
---|
331 | // Read messages |
---|
332 | libtrace_message_t message; |
---|
333 | |
---|
334 | // We just release and do work currently, maybe if something |
---|
335 | // interesting comes through we'd deal with that |
---|
336 | libtrace_thread_get_message(trace, &message); |
---|
337 | |
---|
338 | while (libtrace_thread_try_get_message(trace, &message) != LIBTRACE_MQ_FAILED) { } |
---|
339 | reduce(trace, NULL, &last_ts); |
---|
340 | } |
---|
341 | |
---|
342 | // Wait for all threads to stop |
---|
343 | trace_join(trace); |
---|
344 | |
---|
345 | reduce(trace, NULL, &last_ts); |
---|
346 | // Flush the last one out |
---|
347 | report_results((double) last_ts * (double) packet_interval, count, bytes); |
---|
348 | //count = 0; |
---|
349 | //bytes = 0; |
---|
350 | for (j = 0; j < filter_count; j++) |
---|
351 | filters[j].count = filters[j].bytes = 0; |
---|
352 | (last_ts)++; |
---|
353 | |
---|
354 | if (trace_is_err(trace)) |
---|
355 | trace_perror(trace,"%s",uri); |
---|
356 | |
---|
357 | trace_destroy(trace); |
---|
358 | |
---|
359 | if (!merge_inputs) |
---|
360 | output_destroy(output); |
---|
361 | |
---|
362 | } |
---|
363 | // TODO Decide what to do with -c option |
---|
364 | static void usage(char *argv0) |
---|
365 | { |
---|
366 | fprintf(stderr,"Usage:\n" |
---|
367 | "%s flags libtraceuri [libtraceuri...]\n" |
---|
368 | "-i --interval=seconds Duration of reporting interval in seconds\n" |
---|
369 | "-c --count=packets Exit after count packets received\n" |
---|
370 | "-o --output-format=txt|csv|html|png Reporting output format\n" |
---|
371 | "-f --filter=bpf Apply BPF filter. Can be specified multiple times\n" |
---|
372 | "-m --merge-inputs Do not create separate outputs for each input trace\n" |
---|
373 | "-H --libtrace-help Print libtrace runtime documentation\n" |
---|
374 | ,argv0); |
---|
375 | } |
---|
376 | |
---|
377 | int main(int argc, char *argv[]) { |
---|
378 | |
---|
379 | int i; |
---|
380 | |
---|
381 | while(1) { |
---|
382 | int option_index; |
---|
383 | struct option long_options[] = { |
---|
384 | { "filter", 1, 0, 'f' }, |
---|
385 | { "interval", 1, 0, 'i' }, |
---|
386 | { "count", 1, 0, 'c' }, |
---|
387 | { "output-format", 1, 0, 'o' }, |
---|
388 | { "libtrace-help", 0, 0, 'H' }, |
---|
389 | { "merge-inputs", 0, 0, 'm' }, |
---|
390 | { NULL, 0, 0, 0 }, |
---|
391 | }; |
---|
392 | |
---|
393 | int c=getopt_long(argc, argv, "c:f:i:o:Hm", |
---|
394 | long_options, &option_index); |
---|
395 | |
---|
396 | if (c==-1) |
---|
397 | break; |
---|
398 | |
---|
399 | switch (c) { |
---|
400 | case 'f': |
---|
401 | ++filter_count; |
---|
402 | filters=realloc(filters,filter_count*sizeof(struct filter_t)); |
---|
403 | filters[filter_count-1].expr=strdup(optarg); |
---|
404 | filters[filter_count-1].filter=trace_create_filter(optarg); |
---|
405 | filters[filter_count-1].count=0; |
---|
406 | filters[filter_count-1].bytes=0; |
---|
407 | break; |
---|
408 | case 'i': |
---|
409 | packet_interval=atof(optarg); |
---|
410 | break; |
---|
411 | case 'c': |
---|
412 | packet_count=atoi(optarg); |
---|
413 | break; |
---|
414 | case 'o': |
---|
415 | if (output_format) free(output_format); |
---|
416 | output_format=strdup(optarg); |
---|
417 | break; |
---|
418 | case 'm': |
---|
419 | merge_inputs = 1; |
---|
420 | break; |
---|
421 | case 'H': |
---|
422 | trace_help(); |
---|
423 | exit(1); |
---|
424 | break; |
---|
425 | default: |
---|
426 | fprintf(stderr,"Unknown option: %c\n",c); |
---|
427 | usage(argv[0]); |
---|
428 | return 1; |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | if (packet_count == UINT64_MAX && packet_interval == UINT32_MAX) { |
---|
433 | packet_interval = 300; /* every 5 minutes */ |
---|
434 | } |
---|
435 | |
---|
436 | if (optind >= argc) |
---|
437 | return 0; |
---|
438 | |
---|
439 | if (output_format) |
---|
440 | fprintf(stderr,"output format: '%s'\n",output_format); |
---|
441 | else |
---|
442 | fprintf(stderr,"output format: '%s'\n", DEFAULT_OUTPUT_FMT); |
---|
443 | |
---|
444 | |
---|
445 | if (merge_inputs) { |
---|
446 | /* If we're merging the inputs, we only want to create all |
---|
447 | * the column headers etc. once rather than doing them once |
---|
448 | * per trace */ |
---|
449 | |
---|
450 | /* This is going to "name" the output based on the first |
---|
451 | * provided URI - admittedly not ideal */ |
---|
452 | create_output(argv[optind]); |
---|
453 | if (output == NULL) |
---|
454 | return 0; |
---|
455 | } |
---|
456 | |
---|
457 | for(i=optind;i<argc;++i) { |
---|
458 | run_trace(argv[i]); |
---|
459 | } |
---|
460 | |
---|
461 | if (merge_inputs) { |
---|
462 | /* Clean up after ourselves */ |
---|
463 | output_destroy(output); |
---|
464 | } |
---|
465 | |
---|
466 | |
---|
467 | return 0; |
---|
468 | } |
---|