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 | /* |
---|
32 | * This program takes a series of traces and bpf filters and outputs how many |
---|
33 | * bytes/packets |
---|
34 | */ |
---|
35 | |
---|
36 | #include <stdio.h> |
---|
37 | #include <stdlib.h> |
---|
38 | #include <assert.h> |
---|
39 | #include <string.h> |
---|
40 | #include <sys/time.h> |
---|
41 | #include <sys/types.h> |
---|
42 | #include <time.h> |
---|
43 | |
---|
44 | #include <netinet/in.h> |
---|
45 | #include <netinet/in_systm.h> |
---|
46 | #include <netinet/tcp.h> |
---|
47 | #include <netinet/ip.h> |
---|
48 | #include <netinet/ip_icmp.h> |
---|
49 | #include <arpa/inet.h> |
---|
50 | #include <sys/socket.h> |
---|
51 | #include <getopt.h> |
---|
52 | #include <inttypes.h> |
---|
53 | #include <signal.h> |
---|
54 | |
---|
55 | #include "libtrace_parallel.h" |
---|
56 | #include "lt_inttypes.h" |
---|
57 | #include "data-struct/vector.h" |
---|
58 | #include "data-struct/message_queue.h" |
---|
59 | #include "combiners.h" |
---|
60 | #include <pthread.h> |
---|
61 | |
---|
62 | struct libtrace_t *trace = NULL; |
---|
63 | |
---|
64 | static void cleanup_signal(int signal) |
---|
65 | { |
---|
66 | static int s = 0; |
---|
67 | (void)signal; |
---|
68 | // trace_interrupt(); |
---|
69 | // trace_pstop isn't really signal safe because its got lots of locks in it |
---|
70 | trace_pstop(trace); |
---|
71 | /*if (s == 0) { |
---|
72 | if (trace_ppause(trace) == -1) |
---|
73 | trace_perror(trace, "Pause failed"); |
---|
74 | } |
---|
75 | else { |
---|
76 | if (trace_pstart(trace, NULL, NULL, NULL) == -1) |
---|
77 | trace_perror(trace, "Start failed"); |
---|
78 | }*/ |
---|
79 | s = !s; |
---|
80 | } |
---|
81 | |
---|
82 | struct filter_t { |
---|
83 | char *expr; |
---|
84 | struct libtrace_filter_t *filter; |
---|
85 | uint64_t count; |
---|
86 | uint64_t bytes; |
---|
87 | } *filters = NULL; |
---|
88 | int filter_count=0; |
---|
89 | volatile uint64_t totcount = 0; |
---|
90 | volatile uint64_t totbytes = 0; |
---|
91 | |
---|
92 | |
---|
93 | typedef struct global_blob { |
---|
94 | uint64_t * totcount; |
---|
95 | uint64_t * totbytes; |
---|
96 | } global_blob_t; |
---|
97 | |
---|
98 | typedef struct statistics { |
---|
99 | uint64_t count; |
---|
100 | uint64_t bytes; |
---|
101 | } statistics_t; |
---|
102 | |
---|
103 | |
---|
104 | //libtrace_message_t mesg |
---|
105 | static void* per_packet(libtrace_t *trace, libtrace_thread_t *t, |
---|
106 | int mesg, libtrace_generic_t data, |
---|
107 | libtrace_thread_t *sender UNUSED) |
---|
108 | { |
---|
109 | // Using first entry as total and those after for filter counts |
---|
110 | static __thread statistics_t * results = NULL; |
---|
111 | int i, wlen; |
---|
112 | libtrace_stat_t *stats; |
---|
113 | |
---|
114 | |
---|
115 | // printf ("%d.%06d READ #%"PRIu64"\n", tv.tv_sec, tv.tv_usec, trace_packet_get(packet)); |
---|
116 | switch (mesg) { |
---|
117 | case MESSAGE_PACKET: |
---|
118 | wlen = trace_get_wire_length(data.pkt); |
---|
119 | for(i=0;i<filter_count;++i) { |
---|
120 | if (filters[i].filter == NULL) |
---|
121 | continue; |
---|
122 | if(trace_apply_filter(filters[i].filter,data.pkt) > 0) { |
---|
123 | results[i+1].count++; |
---|
124 | results[i+1].bytes+=wlen; |
---|
125 | } |
---|
126 | if (trace_is_err(trace)) { |
---|
127 | trace_perror(trace, "trace_apply_filter"); |
---|
128 | fprintf(stderr, "Removing filter from filterlist\n"); |
---|
129 | // XXX might be a problem across threads below |
---|
130 | filters[i].filter = NULL; |
---|
131 | } |
---|
132 | } |
---|
133 | results[0].count++; |
---|
134 | results[0].bytes +=wlen; |
---|
135 | return data.pkt; |
---|
136 | case MESSAGE_STOPPING: |
---|
137 | stats = trace_create_statistics(); |
---|
138 | trace_get_thread_statistics(trace, t, stats); |
---|
139 | trace_print_statistics(stats, stderr, NULL); |
---|
140 | free(stats); |
---|
141 | trace_publish_result(trace, t, 0, (libtrace_generic_t){.ptr = results}, RESULT_NORMAL); // Only ever using a single key 0 |
---|
142 | //fprintf(stderr, "tracestats_parallel:\t Stopping thread - publishing results\n"); |
---|
143 | break; |
---|
144 | case MESSAGE_STARTING: |
---|
145 | results = calloc(1, sizeof(statistics_t) * (filter_count + 1)); |
---|
146 | break; |
---|
147 | case MESSAGE_DO_PAUSE: |
---|
148 | assert(!"GOT Asked to pause!!!\n"); |
---|
149 | break; |
---|
150 | case MESSAGE_PAUSING: |
---|
151 | //fprintf(stderr, "tracestats_parallel:\t pausing thread\n"); |
---|
152 | break; |
---|
153 | case MESSAGE_RESUMING: |
---|
154 | //fprintf(stderr, "tracestats_parallel:\t resuming thread\n"); |
---|
155 | break; |
---|
156 | } |
---|
157 | return NULL; |
---|
158 | } |
---|
159 | |
---|
160 | static void report_result(libtrace_t *trace UNUSED, int mesg, |
---|
161 | libtrace_generic_t data, |
---|
162 | libtrace_thread_t *sender UNUSED) { |
---|
163 | static uint64_t count=0, bytes=0; |
---|
164 | int i; |
---|
165 | libtrace_stat_t *stats; |
---|
166 | |
---|
167 | switch (mesg) { |
---|
168 | case MESSAGE_RESULT: |
---|
169 | /* Get the results from each core and sum 'em up */ |
---|
170 | assert(libtrace_result_get_key(data.res) == 0); |
---|
171 | statistics_t * res = libtrace_result_get_value(data.res).ptr; |
---|
172 | count += res[0].count; |
---|
173 | bytes += res[0].bytes; |
---|
174 | for (i = 0; i < filter_count; i++) { |
---|
175 | filters[i].count += res[i+1].count; |
---|
176 | filters[i].bytes += res[i+1].bytes; |
---|
177 | } |
---|
178 | free(res); |
---|
179 | break; |
---|
180 | case MESSAGE_STOPPING: |
---|
181 | stats = trace_get_statistics(trace, NULL); |
---|
182 | printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%"); |
---|
183 | for(i=0;i<filter_count;++i) { |
---|
184 | printf("%30s:\t%12"PRIu64"\t%12"PRIu64"\t%7.03f\n",filters[i].expr,filters[i].count,filters[i].bytes,filters[i].count*100.0/count); |
---|
185 | filters[i].bytes=0; |
---|
186 | filters[i].count=0; |
---|
187 | } |
---|
188 | if (stats->received_valid) |
---|
189 | fprintf(stderr,"%30s:\t%12" PRIu64"\n", |
---|
190 | "Input packets", stats->received); |
---|
191 | if (stats->filtered_valid) |
---|
192 | fprintf(stderr,"%30s:\t%12" PRIu64"\n", |
---|
193 | "Filtered packets", stats->filtered); |
---|
194 | if (stats->dropped_valid) |
---|
195 | fprintf(stderr,"%30s:\t%12" PRIu64"\n", |
---|
196 | "Dropped packets",stats->dropped); |
---|
197 | if (stats->accepted_valid) |
---|
198 | fprintf(stderr,"%30s:\t%12" PRIu64 "\n", |
---|
199 | "Accepted packets", stats->accepted); |
---|
200 | if (stats->errors_valid) |
---|
201 | fprintf(stderr,"%30s:\t%12" PRIu64 "\n", |
---|
202 | "Erred packets", stats->errors); |
---|
203 | printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes); |
---|
204 | totcount+=count; |
---|
205 | totbytes+=bytes; |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | static uint64_t rand_hash(libtrace_packet_t * pkt UNUSED, void *data UNUSED) { |
---|
210 | return rand(); |
---|
211 | } |
---|
212 | |
---|
213 | static uint64_t bad_hash(libtrace_packet_t * pkt UNUSED, void *data UNUSED) { |
---|
214 | return 0; |
---|
215 | } |
---|
216 | |
---|
217 | struct user_configuration uc; |
---|
218 | |
---|
219 | |
---|
220 | /* Process a trace, counting packets that match filter(s) */ |
---|
221 | static void run_trace(char *uri) |
---|
222 | { |
---|
223 | |
---|
224 | fprintf(stderr,"%s:\n",uri); |
---|
225 | |
---|
226 | trace = trace_create(uri); |
---|
227 | |
---|
228 | if (trace_is_err(trace)) { |
---|
229 | trace_perror(trace,"Failed to create trace"); |
---|
230 | return; |
---|
231 | } |
---|
232 | |
---|
233 | int option = 2; |
---|
234 | //option = 10000; |
---|
235 | //trace_set_hasher(trace, HASHER_CUSTOM, &rand_hash, NULL); |
---|
236 | //trace_parallel_config(trace, TRACE_OPTION_SET_PERPKT_THREAD_COUNT, &option); |
---|
237 | trace_parallel_config(trace, TRACE_OPTION_SET_CONFIG, &uc); |
---|
238 | trace_set_combiner(trace, &combiner_ordered, (libtrace_generic_t){0}); |
---|
239 | |
---|
240 | //trace_parallel_config(trace, TRACE_OPTION_SET_MAPPER_BUFFER_SIZE, &option); |
---|
241 | |
---|
242 | /* OPTIONALLY SETUP CORES HERE BUT WE DON'T CARE ABOUT THAT YET XXX */ |
---|
243 | |
---|
244 | /*if (trace_start(trace)==-1) { |
---|
245 | trace_perror(trace,"Failed to start trace"); |
---|
246 | return; |
---|
247 | }*/ |
---|
248 | global_blob_t blob; |
---|
249 | |
---|
250 | |
---|
251 | if (trace_pstart(trace, (void *)&blob, &per_packet, report_result)==-1) { |
---|
252 | trace_perror(trace,"Failed to start trace"); |
---|
253 | return; |
---|
254 | } |
---|
255 | |
---|
256 | // Wait for all threads to stop |
---|
257 | trace_join(trace); |
---|
258 | |
---|
259 | //map_pair_iterator_t * results = NULL; |
---|
260 | //trace_get_results(trace, &results); |
---|
261 | |
---|
262 | //if (results != NULL) { |
---|
263 | // reduce(trace, global_blob, results); |
---|
264 | //} |
---|
265 | if (trace_is_err(trace)) |
---|
266 | trace_perror(trace,"%s",uri); |
---|
267 | |
---|
268 | trace_destroy(trace); |
---|
269 | } |
---|
270 | |
---|
271 | static void usage(char *argv0) |
---|
272 | { |
---|
273 | fprintf(stderr,"Usage: %s [-H|--libtrace-help] [--filter|-f bpf ]... libtraceuri...\n",argv0); |
---|
274 | } |
---|
275 | |
---|
276 | int main(int argc, char *argv[]) { |
---|
277 | |
---|
278 | int i; |
---|
279 | struct sigaction sigact; |
---|
280 | ZERO_USER_CONFIG(uc); |
---|
281 | while(1) { |
---|
282 | int option_index; |
---|
283 | struct option long_options[] = { |
---|
284 | { "filter", 1, 0, 'f' }, |
---|
285 | { "libtrace-help", 0, 0, 'H' }, |
---|
286 | { "config", 1, 0, 'u' }, |
---|
287 | { "config-file", 1, 0, 'U' }, |
---|
288 | { NULL, 0, 0, 0 }, |
---|
289 | }; |
---|
290 | |
---|
291 | int c=getopt_long(argc, argv, "f:Hu:U:", |
---|
292 | long_options, &option_index); |
---|
293 | |
---|
294 | if (c==-1) |
---|
295 | break; |
---|
296 | |
---|
297 | switch (c) { |
---|
298 | case 'f': |
---|
299 | ++filter_count; |
---|
300 | filters=realloc(filters,filter_count*sizeof(struct filter_t)); |
---|
301 | filters[filter_count-1].expr=strdup(optarg); |
---|
302 | filters[filter_count-1].filter=trace_create_filter(optarg); |
---|
303 | filters[filter_count-1].count=0; |
---|
304 | filters[filter_count-1].bytes=0; |
---|
305 | break; |
---|
306 | case 'H': |
---|
307 | trace_help(); |
---|
308 | exit(1); |
---|
309 | break; |
---|
310 | case 'u': |
---|
311 | parse_user_config(&uc, optarg); |
---|
312 | break; |
---|
313 | case 'U':; |
---|
314 | FILE * f = fopen(optarg, "r"); |
---|
315 | if (f != NULL) { |
---|
316 | parse_user_config_file(&uc, f); |
---|
317 | } else { |
---|
318 | perror("Failed to open configuration file\n"); |
---|
319 | usage(argv[0]); |
---|
320 | } |
---|
321 | break; |
---|
322 | default: |
---|
323 | fprintf(stderr,"Unknown option: %c\n",c); |
---|
324 | usage(argv[0]); |
---|
325 | return 1; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | sigact.sa_handler = cleanup_signal; |
---|
330 | sigemptyset(&sigact.sa_mask); |
---|
331 | sigact.sa_flags = SA_RESTART; |
---|
332 | |
---|
333 | sigaction(SIGINT, &sigact, NULL); |
---|
334 | sigaction(SIGTERM, &sigact, NULL); |
---|
335 | |
---|
336 | for(i=optind;i<argc;++i) { |
---|
337 | run_trace(argv[i]); |
---|
338 | } |
---|
339 | if (optind+1<argc) { |
---|
340 | printf("Grand total:\n"); |
---|
341 | printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes); |
---|
342 | } |
---|
343 | |
---|
344 | return 0; |
---|
345 | } |
---|