1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | /* |
---|
29 | * This program takes a series of traces and bpf filters and outputs how many |
---|
30 | * bytes/packets |
---|
31 | */ |
---|
32 | |
---|
33 | #include <stdio.h> |
---|
34 | #include <stdlib.h> |
---|
35 | #include <assert.h> |
---|
36 | #include <string.h> |
---|
37 | #include <sys/time.h> |
---|
38 | #include <sys/types.h> |
---|
39 | #include <time.h> |
---|
40 | |
---|
41 | #include <netinet/in.h> |
---|
42 | #include <netinet/in_systm.h> |
---|
43 | #include <netinet/tcp.h> |
---|
44 | #include <netinet/ip.h> |
---|
45 | #include <netinet/ip_icmp.h> |
---|
46 | #include <arpa/inet.h> |
---|
47 | #include <sys/socket.h> |
---|
48 | #include <getopt.h> |
---|
49 | #include <inttypes.h> |
---|
50 | #include <signal.h> |
---|
51 | |
---|
52 | #include "libtrace_parallel.h" |
---|
53 | #include "lt_inttypes.h" |
---|
54 | #include <pthread.h> |
---|
55 | |
---|
56 | struct libtrace_t *inptrace = NULL; |
---|
57 | |
---|
58 | static void cleanup_signal(int signal UNUSED) |
---|
59 | { |
---|
60 | if (inptrace) |
---|
61 | trace_pstop(inptrace); |
---|
62 | } |
---|
63 | |
---|
64 | struct filter_t { |
---|
65 | char *expr; |
---|
66 | struct libtrace_filter_t *filter; |
---|
67 | } *filters = NULL; |
---|
68 | |
---|
69 | int filter_count=0; |
---|
70 | |
---|
71 | |
---|
72 | typedef struct statistics { |
---|
73 | uint64_t count; |
---|
74 | uint64_t bytes; |
---|
75 | } statistics_t; |
---|
76 | |
---|
77 | volatile uint64_t totcount = 0; |
---|
78 | volatile uint64_t totbytes = 0; |
---|
79 | |
---|
80 | |
---|
81 | static void fn_result(libtrace_t *trace UNUSED, |
---|
82 | libtrace_thread_t *sender UNUSED, |
---|
83 | void *global UNUSED, void *tls, |
---|
84 | libtrace_result_t *result) { |
---|
85 | statistics_t *counters = (statistics_t *)tls; |
---|
86 | int i; |
---|
87 | |
---|
88 | assert(result->key == 0); |
---|
89 | statistics_t * res = result->value.ptr; |
---|
90 | counters[0].count += res[0].count; |
---|
91 | counters[0].bytes += res[0].bytes; |
---|
92 | |
---|
93 | for (i = 0; i < filter_count; i++) { |
---|
94 | counters[i+1].count += res[i+1].count; |
---|
95 | counters[i+1].bytes += res[i+1].bytes; |
---|
96 | } |
---|
97 | free(res); |
---|
98 | } |
---|
99 | |
---|
100 | static void fn_print_results(libtrace_t *trace, |
---|
101 | libtrace_thread_t *sender UNUSED, void *global UNUSED, |
---|
102 | void *tls) { |
---|
103 | |
---|
104 | statistics_t *counters = (statistics_t *)tls; |
---|
105 | libtrace_stat_t *stats = NULL; |
---|
106 | int i; |
---|
107 | double pct; |
---|
108 | |
---|
109 | stats = trace_get_statistics(trace, NULL); |
---|
110 | printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","% count"); |
---|
111 | for(i=0;i<filter_count;++i) { |
---|
112 | if (counters[0].count == 0) { |
---|
113 | pct = 0.0; |
---|
114 | } else { |
---|
115 | pct = counters[i+1].count*100.0/counters[0].count; |
---|
116 | } |
---|
117 | printf("%30s:\t%12"PRIu64"\t%12"PRIu64"\t%7.03f\n",filters[i].expr,counters[i+1].count,counters[i+1].bytes,pct); |
---|
118 | } |
---|
119 | if (stats->received_valid) |
---|
120 | fprintf(stderr,"%30s:\t%12" PRIu64"\n", |
---|
121 | "Input packets", stats->received); |
---|
122 | if (stats->filtered_valid) |
---|
123 | fprintf(stderr,"%30s:\t%12" PRIu64"\n", |
---|
124 | "Filtered packets", stats->filtered); |
---|
125 | if (stats->dropped_valid) |
---|
126 | fprintf(stderr,"%30s:\t%12" PRIu64"\n", |
---|
127 | "Dropped packets",stats->dropped); |
---|
128 | if (stats->accepted_valid) |
---|
129 | fprintf(stderr,"%30s:\t%12" PRIu64 "\n", |
---|
130 | "Accepted packets", stats->accepted); |
---|
131 | if (stats->errors_valid) |
---|
132 | fprintf(stderr,"%30s:\t%12" PRIu64 "\n", |
---|
133 | "Erred packets", stats->errors); |
---|
134 | printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",counters[0].count,counters[0].bytes); |
---|
135 | totcount+=counters[0].count; |
---|
136 | totbytes+=counters[0].bytes; |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | static void* fn_starting(libtrace_t *trace UNUSED, libtrace_thread_t *t UNUSED, void *global UNUSED) { |
---|
142 | /* Allocate space to hold a total count and one for each filter */ |
---|
143 | return calloc(1, sizeof(statistics_t) * (filter_count + 1)); |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | static void fn_stopping(libtrace_t *trace, libtrace_thread_t *t UNUSED, |
---|
148 | void *global UNUSED, void*tls) { |
---|
149 | statistics_t *results = (statistics_t *)tls; |
---|
150 | libtrace_generic_t gen; |
---|
151 | /* We only output one result per thread with the key 0 when the |
---|
152 | * trace is over. */ |
---|
153 | gen.ptr = results; |
---|
154 | trace_publish_result(trace, t, 0, gen, RESULT_USER); |
---|
155 | } |
---|
156 | |
---|
157 | static libtrace_packet_t* fn_packet(libtrace_t *trace, |
---|
158 | libtrace_thread_t *t UNUSED, |
---|
159 | void *global UNUSED, void*tls, libtrace_packet_t *pkt) { |
---|
160 | statistics_t *results = (statistics_t *)tls; |
---|
161 | int i, wlen; |
---|
162 | |
---|
163 | if (IS_LIBTRACE_META_PACKET(pkt)) |
---|
164 | return pkt; |
---|
165 | |
---|
166 | /* Apply filters to every packet note the result */ |
---|
167 | wlen = trace_get_wire_length(pkt); |
---|
168 | if (wlen == 0) { |
---|
169 | /* Don't count ERF provenance etc. */ |
---|
170 | return pkt; |
---|
171 | } |
---|
172 | for(i=0;i<filter_count;++i) { |
---|
173 | if (filters[i].filter == NULL) |
---|
174 | continue; |
---|
175 | if(trace_apply_filter(filters[i].filter,pkt) > 0) { |
---|
176 | results[i+1].count++; |
---|
177 | results[i+1].bytes+=wlen; |
---|
178 | } |
---|
179 | if (trace_is_err(trace)) { |
---|
180 | trace_perror(trace, "trace_apply_filter"); |
---|
181 | fprintf(stderr, "Removing filter from filterlist\n"); |
---|
182 | /* This is a race, but will be atomic */ |
---|
183 | filters[i].filter = NULL; |
---|
184 | } |
---|
185 | } |
---|
186 | results[0].count++; |
---|
187 | results[0].bytes +=wlen; |
---|
188 | return pkt; |
---|
189 | } |
---|
190 | |
---|
191 | /* Process a trace, counting packets that match filter(s) */ |
---|
192 | static void run_trace(char *uri, int threadcount) |
---|
193 | { |
---|
194 | |
---|
195 | fprintf(stderr,"%s:\n",uri); |
---|
196 | libtrace_callback_set_t *pktcbs, *rescbs; |
---|
197 | |
---|
198 | inptrace = trace_create(uri); |
---|
199 | |
---|
200 | if (trace_is_err(inptrace)) { |
---|
201 | trace_perror(inptrace,"Failed to create trace"); |
---|
202 | return; |
---|
203 | } |
---|
204 | |
---|
205 | pktcbs = trace_create_callback_set(); |
---|
206 | rescbs = trace_create_callback_set(); |
---|
207 | |
---|
208 | trace_set_packet_cb(pktcbs, fn_packet); |
---|
209 | trace_set_starting_cb(pktcbs, fn_starting); |
---|
210 | trace_set_stopping_cb(pktcbs, fn_stopping); |
---|
211 | trace_set_starting_cb(rescbs, fn_starting); |
---|
212 | trace_set_result_cb(rescbs, fn_result); |
---|
213 | trace_set_stopping_cb(rescbs, fn_print_results); |
---|
214 | |
---|
215 | if (threadcount != 0) |
---|
216 | trace_set_perpkt_threads(inptrace, threadcount); |
---|
217 | |
---|
218 | /* Start the trace as a parallel trace */ |
---|
219 | if (trace_pstart(inptrace, NULL, pktcbs, rescbs)==-1) { |
---|
220 | trace_perror(inptrace,"Failed to start trace"); |
---|
221 | return; |
---|
222 | } |
---|
223 | |
---|
224 | /* Wait for all threads to stop */ |
---|
225 | trace_join(inptrace); |
---|
226 | |
---|
227 | if (trace_is_err(inptrace)) |
---|
228 | trace_perror(inptrace,"%s",uri); |
---|
229 | |
---|
230 | trace_destroy(inptrace); |
---|
231 | trace_destroy_callback_set(pktcbs); |
---|
232 | trace_destroy_callback_set(rescbs); |
---|
233 | } |
---|
234 | |
---|
235 | static void usage(char *argv0) |
---|
236 | { |
---|
237 | fprintf(stderr,"Usage: %s [-h|--help] [--threads|-t threads] [--filter|-f bpf ]... libtraceuri...\n",argv0); |
---|
238 | } |
---|
239 | |
---|
240 | int main(int argc, char *argv[]) { |
---|
241 | |
---|
242 | int i; |
---|
243 | struct sigaction sigact; |
---|
244 | int threadcount = 1; |
---|
245 | |
---|
246 | while(1) { |
---|
247 | int option_index; |
---|
248 | struct option long_options[] = { |
---|
249 | { "filter", 1, 0, 'f' }, |
---|
250 | { "help", 0, 0, 'h' }, |
---|
251 | { "threads", 1, 0, 't' }, |
---|
252 | { NULL, 0, 0, 0 }, |
---|
253 | }; |
---|
254 | |
---|
255 | int c=getopt_long(argc, argv, "f:ht:", |
---|
256 | long_options, &option_index); |
---|
257 | |
---|
258 | if (c==-1) |
---|
259 | break; |
---|
260 | |
---|
261 | switch (c) { |
---|
262 | case 'f': |
---|
263 | ++filter_count; |
---|
264 | filters=realloc(filters,filter_count*sizeof(struct filter_t)); |
---|
265 | filters[filter_count-1].expr=strdup(optarg); |
---|
266 | filters[filter_count-1].filter=trace_create_filter(optarg); |
---|
267 | break; |
---|
268 | case 'h': |
---|
269 | usage(argv[0]); |
---|
270 | return 1; |
---|
271 | case 't': |
---|
272 | threadcount = atoi(optarg); |
---|
273 | if (threadcount <= 0) |
---|
274 | threadcount = 1; |
---|
275 | break; |
---|
276 | default: |
---|
277 | fprintf(stderr,"Unknown option: %c\n",c); |
---|
278 | usage(argv[0]); |
---|
279 | return 1; |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | sigact.sa_handler = cleanup_signal; |
---|
284 | sigemptyset(&sigact.sa_mask); |
---|
285 | sigact.sa_flags = SA_RESTART; |
---|
286 | |
---|
287 | sigaction(SIGINT, &sigact, NULL); |
---|
288 | sigaction(SIGTERM, &sigact, NULL); |
---|
289 | |
---|
290 | for(i=optind;i<argc;++i) { |
---|
291 | run_trace(argv[i], threadcount); |
---|
292 | } |
---|
293 | if (optind+1<argc) { |
---|
294 | printf("Grand total:\n"); |
---|
295 | printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes); |
---|
296 | } |
---|
297 | |
---|
298 | return 0; |
---|
299 | } |
---|