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.h" |
---|
56 | #include "lt_inttypes.h" |
---|
57 | #include "data-struct/vector.h" |
---|
58 | #include "data-struct/message_queue.h" |
---|
59 | #include <pthread.h> |
---|
60 | |
---|
61 | struct libtrace_t *trace; |
---|
62 | |
---|
63 | static void cleanup_signal(int signal) |
---|
64 | { |
---|
65 | (void)signal; |
---|
66 | //trace_interrupt(); |
---|
67 | // trace_pstop isn't really signal safe because its got lots of locks in it |
---|
68 | trace_pstop(trace); |
---|
69 | } |
---|
70 | |
---|
71 | struct filter_t { |
---|
72 | char *expr; |
---|
73 | struct libtrace_filter_t *filter; |
---|
74 | uint64_t count; |
---|
75 | uint64_t bytes; |
---|
76 | } *filters = NULL; |
---|
77 | int filter_count=0; |
---|
78 | volatile uint64_t totcount = 0; |
---|
79 | volatile uint64_t totbytes = 0; |
---|
80 | |
---|
81 | |
---|
82 | typedef struct global_blob { |
---|
83 | uint64_t * totcount; |
---|
84 | uint64_t * totbytes; |
---|
85 | } global_blob_t; |
---|
86 | |
---|
87 | typedef struct statistics { |
---|
88 | uint64_t count; |
---|
89 | uint64_t bytes; |
---|
90 | } statistics_t; |
---|
91 | |
---|
92 | |
---|
93 | static void* per_packet(libtrace_t *trace, libtrace_packet_t *pkt, libtrace_message_t *mesg, libtrace_thread_t *t) |
---|
94 | { |
---|
95 | // Using first entry as total and those after for filter counts |
---|
96 | static __thread statistics_t * results = NULL; |
---|
97 | int i; |
---|
98 | |
---|
99 | if (pkt) { |
---|
100 | int wlen = trace_get_wire_length(pkt); |
---|
101 | for(i=0;i<filter_count;++i) { |
---|
102 | if (filters[i].filter == NULL) |
---|
103 | continue; |
---|
104 | if(trace_apply_filter(filters[i].filter,pkt) > 0) { |
---|
105 | results[i+1].count++; |
---|
106 | results[i+1].bytes+=wlen; |
---|
107 | } |
---|
108 | if (trace_is_err(trace)) { |
---|
109 | trace_perror(trace, "trace_apply_filter"); |
---|
110 | fprintf(stderr, "Removing filter from filterlist\n"); |
---|
111 | // XXX might be a problem across threads below |
---|
112 | filters[i].filter = NULL; |
---|
113 | } |
---|
114 | } |
---|
115 | results[0].count++; |
---|
116 | results[0].bytes +=wlen; |
---|
117 | } |
---|
118 | if (mesg) { |
---|
119 | // printf ("%d.%06d READ #%"PRIu64"\n", tv.tv_sec, tv.tv_usec, trace_packet_get(packet)); |
---|
120 | switch (mesg->code) { |
---|
121 | case MESSAGE_STOPPED: |
---|
122 | trace_publish_result(trace, 0, results); // Only ever using a single key 0 |
---|
123 | fprintf(stderr, "Thread published resuslts WOWW \n"); |
---|
124 | break; |
---|
125 | case MESSAGE_STARTED: |
---|
126 | results = calloc(1, sizeof(statistics_t) * (filter_count + 1)); |
---|
127 | break; |
---|
128 | case MESSAGE_PAUSE: |
---|
129 | fprintf(stderr, "GOT Asked to pause ahh\n"); |
---|
130 | break; |
---|
131 | } |
---|
132 | } |
---|
133 | return pkt; |
---|
134 | } |
---|
135 | |
---|
136 | static int reduce(libtrace_t* trace, void* global_blob) |
---|
137 | { |
---|
138 | int i,j; |
---|
139 | uint64_t count=0, bytes=0; |
---|
140 | libtrace_vector_t results; |
---|
141 | libtrace_vector_init(&results, sizeof(libtrace_result_t)); |
---|
142 | trace_get_results(trace, &results); |
---|
143 | uint64_t packets; |
---|
144 | |
---|
145 | /* Get the results from each core and sum 'em up */ |
---|
146 | for (i = 0 ; i < libtrace_vector_get_size(&results) ; i++) { |
---|
147 | libtrace_result_t result; |
---|
148 | assert(libtrace_vector_get(&results, i, (void *) &result) == 1); |
---|
149 | assert(libtrace_result_get_key(&result) == 0); |
---|
150 | statistics_t * res = libtrace_result_get_value(&result); |
---|
151 | count += res[0].count; |
---|
152 | bytes += res[0].bytes; |
---|
153 | for (j = 0; j < filter_count; j++) { |
---|
154 | filters[j].count += res[j+1].count; |
---|
155 | filters[j].bytes += res[j+1].bytes; |
---|
156 | } |
---|
157 | free(res); |
---|
158 | } |
---|
159 | // Done with these results - Free internally and externally |
---|
160 | libtrace_vector_destroy(&results); |
---|
161 | |
---|
162 | printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%"); |
---|
163 | for(i=0;i<filter_count;++i) { |
---|
164 | 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); |
---|
165 | filters[i].bytes=0; |
---|
166 | filters[i].count=0; |
---|
167 | } |
---|
168 | packets=trace_get_received_packets(trace); |
---|
169 | if (packets!=UINT64_MAX) |
---|
170 | fprintf(stderr,"%30s:\t%12" PRIu64"\n", |
---|
171 | "Input packets", packets); |
---|
172 | packets=trace_get_filtered_packets(trace); |
---|
173 | if (packets!=UINT64_MAX) |
---|
174 | fprintf(stderr,"%30s:\t%12" PRIu64"\n", |
---|
175 | "Filtered packets", packets); |
---|
176 | packets=trace_get_dropped_packets(trace); |
---|
177 | if (packets!=UINT64_MAX) |
---|
178 | fprintf(stderr,"%30s:\t%12" PRIu64"\n", |
---|
179 | "Dropped packets",packets); |
---|
180 | packets=trace_get_accepted_packets(trace); |
---|
181 | if (packets!=UINT64_MAX) |
---|
182 | fprintf(stderr,"%30s:\t%12" PRIu64 "\n", |
---|
183 | "Accepted Packets",packets); |
---|
184 | printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes); |
---|
185 | totcount+=count; |
---|
186 | totbytes+=bytes; |
---|
187 | |
---|
188 | return 0; |
---|
189 | } |
---|
190 | |
---|
191 | static uint64_t rand_hash(libtrace_packet_t * pkt) { |
---|
192 | return rand(); |
---|
193 | } |
---|
194 | |
---|
195 | static uint64_t bad_hash(libtrace_packet_t * pkt) { |
---|
196 | return 0; |
---|
197 | } |
---|
198 | |
---|
199 | /* Process a trace, counting packets that match filter(s) */ |
---|
200 | static void run_trace(char *uri) |
---|
201 | { |
---|
202 | |
---|
203 | fprintf(stderr,"%s:\n",uri); |
---|
204 | |
---|
205 | trace = trace_create(uri); |
---|
206 | |
---|
207 | if (trace_is_err(trace)) { |
---|
208 | trace_perror(trace,"Failed to create trace"); |
---|
209 | return; |
---|
210 | } |
---|
211 | |
---|
212 | int option = 2; |
---|
213 | //option = 10000; |
---|
214 | //trace_parallel_config(trace, TRACE_OPTION_USE_DEDICATED_HASHER, &option); |
---|
215 | //trace_parallel_config(trace, TRACE_OPTION_USE_SLIDING_WINDOW_BUFFER, &option); |
---|
216 | option = 2; |
---|
217 | trace_parallel_config(trace, TRACE_OPTION_SET_PERPKT_THREAD_COUNT, &option); |
---|
218 | //trace_parallel_config(trace, TRACE_OPTION_SET_MAPPER_BUFFER_SIZE, &option); |
---|
219 | |
---|
220 | /* OPTIONALLY SETUP CORES HERE BUT WE DON'T CARE ABOUT THAT YET XXX */ |
---|
221 | |
---|
222 | /*if (trace_start(trace)==-1) { |
---|
223 | trace_perror(trace,"Failed to start trace"); |
---|
224 | return; |
---|
225 | }*/ |
---|
226 | global_blob_t blob; |
---|
227 | |
---|
228 | |
---|
229 | if (trace_pstart(trace, (void *)&blob, &per_packet, NULL)==-1) { |
---|
230 | trace_perror(trace,"Failed to start trace"); |
---|
231 | return; |
---|
232 | } |
---|
233 | |
---|
234 | // Wait for all threads to stop |
---|
235 | trace_join(trace); |
---|
236 | reduce(trace, NULL); |
---|
237 | |
---|
238 | //map_pair_iterator_t * results = NULL; |
---|
239 | //trace_get_results(trace, &results); |
---|
240 | |
---|
241 | //if (results != NULL) { |
---|
242 | // reduce(trace, global_blob, results); |
---|
243 | //} |
---|
244 | if (trace_is_err(trace)) |
---|
245 | trace_perror(trace,"%s",uri); |
---|
246 | |
---|
247 | print_contention_stats(trace); |
---|
248 | trace_destroy(trace); |
---|
249 | } |
---|
250 | |
---|
251 | static void usage(char *argv0) |
---|
252 | { |
---|
253 | fprintf(stderr,"Usage: %s [-H|--libtrace-help] [--filter|-f bpf ]... libtraceuri...\n",argv0); |
---|
254 | } |
---|
255 | |
---|
256 | int main(int argc, char *argv[]) { |
---|
257 | |
---|
258 | int i; |
---|
259 | struct sigaction sigact; |
---|
260 | |
---|
261 | while(1) { |
---|
262 | int option_index; |
---|
263 | struct option long_options[] = { |
---|
264 | { "filter", 1, 0, 'f' }, |
---|
265 | { "libtrace-help", 0, 0, 'H' }, |
---|
266 | { NULL, 0, 0, 0 }, |
---|
267 | }; |
---|
268 | |
---|
269 | int c=getopt_long(argc, argv, "f:H", |
---|
270 | long_options, &option_index); |
---|
271 | |
---|
272 | if (c==-1) |
---|
273 | break; |
---|
274 | |
---|
275 | switch (c) { |
---|
276 | case 'f': |
---|
277 | ++filter_count; |
---|
278 | filters=realloc(filters,filter_count*sizeof(struct filter_t)); |
---|
279 | filters[filter_count-1].expr=strdup(optarg); |
---|
280 | filters[filter_count-1].filter=trace_create_filter(optarg); |
---|
281 | filters[filter_count-1].count=0; |
---|
282 | filters[filter_count-1].bytes=0; |
---|
283 | break; |
---|
284 | case 'H': |
---|
285 | trace_help(); |
---|
286 | exit(1); |
---|
287 | break; |
---|
288 | default: |
---|
289 | fprintf(stderr,"Unknown option: %c\n",c); |
---|
290 | usage(argv[0]); |
---|
291 | return 1; |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | sigact.sa_handler = cleanup_signal; |
---|
296 | sigemptyset(&sigact.sa_mask); |
---|
297 | sigact.sa_flags = SA_RESTART; |
---|
298 | |
---|
299 | sigaction(SIGINT, &sigact, NULL); |
---|
300 | sigaction(SIGTERM, &sigact, NULL); |
---|
301 | |
---|
302 | for(i=optind;i<argc;++i) { |
---|
303 | run_trace(argv[i]); |
---|
304 | } |
---|
305 | if (optind+1<argc) { |
---|
306 | printf("Grand total:\n"); |
---|
307 | printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes); |
---|
308 | } |
---|
309 | |
---|
310 | return 0; |
---|
311 | } |
---|