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 | /* This file contains the common functions used by both the ring and int |
---|
28 | * formats. |
---|
29 | * |
---|
30 | * Typically these deal with the socket descriptor or common conversions. |
---|
31 | */ |
---|
32 | |
---|
33 | #include "config.h" |
---|
34 | #include "libtrace.h" |
---|
35 | #include "libtrace_int.h" |
---|
36 | #include "format_helper.h" |
---|
37 | #include "libtrace_arphrd.h" |
---|
38 | #include <stdlib.h> |
---|
39 | #include <errno.h> |
---|
40 | #include <unistd.h> |
---|
41 | #include <string.h> |
---|
42 | #include <assert.h> |
---|
43 | |
---|
44 | #ifdef HAVE_INTTYPES_H |
---|
45 | # include <inttypes.h> |
---|
46 | #else |
---|
47 | # error "Can't find inttypes.h" |
---|
48 | #endif |
---|
49 | |
---|
50 | #include "format_linux_common.h" |
---|
51 | |
---|
52 | #ifdef HAVE_NETPACKET_PACKET_H |
---|
53 | |
---|
54 | int linuxcommon_probe_filename(const char *filename) |
---|
55 | { |
---|
56 | /* Is this an interface? */ |
---|
57 | return (if_nametoindex(filename) != 0); |
---|
58 | } |
---|
59 | |
---|
60 | /* Compiles a libtrace BPF filter for use with a linux native socket */ |
---|
61 | static int linuxnative_configure_bpf(libtrace_t *libtrace, |
---|
62 | libtrace_filter_t *filter) { |
---|
63 | #if defined(HAVE_LIBPCAP) && defined(HAVE_BPF) |
---|
64 | struct ifreq ifr; |
---|
65 | unsigned int arphrd; |
---|
66 | libtrace_dlt_t dlt; |
---|
67 | libtrace_filter_t *f; |
---|
68 | int sock; |
---|
69 | pcap_t *pcap; |
---|
70 | |
---|
71 | /* Take a copy of the filter object as it was passed in */ |
---|
72 | f = (libtrace_filter_t *) malloc(sizeof(libtrace_filter_t)); |
---|
73 | memcpy(f, filter, sizeof(libtrace_filter_t)); |
---|
74 | |
---|
75 | /* If we are passed a filter with "flag" set to zero, then we must |
---|
76 | * compile the filterstring before continuing. This involves |
---|
77 | * determining the linktype, passing the filterstring to libpcap to |
---|
78 | * compile, and saving the result for trace_start() to push into the |
---|
79 | * kernel. |
---|
80 | * If flag is set to one, then the filter was probably generated using |
---|
81 | * trace_create_filter_from_bytecode() and so we don't need to do |
---|
82 | * anything (we've just copied it above). |
---|
83 | */ |
---|
84 | if (f->flag == 0) { |
---|
85 | sock = socket(PF_INET, SOCK_STREAM, 0); |
---|
86 | memset(&ifr, 0, sizeof(struct ifreq)); |
---|
87 | strncpy(ifr.ifr_name, libtrace->uridata, IF_NAMESIZE); |
---|
88 | if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0) { |
---|
89 | perror("Can't get HWADDR for interface"); |
---|
90 | return -1; |
---|
91 | } |
---|
92 | close(sock); |
---|
93 | |
---|
94 | arphrd = ifr.ifr_hwaddr.sa_family; |
---|
95 | dlt = libtrace_to_pcap_dlt(arphrd_type_to_libtrace(arphrd)); |
---|
96 | |
---|
97 | pcap = pcap_open_dead(dlt, |
---|
98 | FORMAT_DATA->snaplen); |
---|
99 | |
---|
100 | if (pcap_compile(pcap, &f->filter, f->filterstring, 0, 0) == -1) { |
---|
101 | /* Filter didn't compile, set flag to 0 so we can |
---|
102 | * detect this when trace_start() is called and |
---|
103 | * produce a useful error |
---|
104 | */ |
---|
105 | f->flag = 0; |
---|
106 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
107 | "Failed to compile BPF filter (%s): %s", |
---|
108 | f->filterstring, pcap_geterr(pcap)); |
---|
109 | } else { |
---|
110 | /* Set the "flag" to indicate that the filterstring |
---|
111 | * has been compiled |
---|
112 | */ |
---|
113 | f->flag = 1; |
---|
114 | } |
---|
115 | |
---|
116 | pcap_close(pcap); |
---|
117 | |
---|
118 | } |
---|
119 | |
---|
120 | if (FORMAT_DATA->filter != NULL) |
---|
121 | free(FORMAT_DATA->filter); |
---|
122 | |
---|
123 | FORMAT_DATA->filter = f; |
---|
124 | |
---|
125 | return 0; |
---|
126 | #else |
---|
127 | return -1; |
---|
128 | #endif |
---|
129 | } |
---|
130 | |
---|
131 | int linuxcommon_config_input(libtrace_t *libtrace, |
---|
132 | trace_option_t option, |
---|
133 | void *data) |
---|
134 | { |
---|
135 | switch(option) { |
---|
136 | case TRACE_OPTION_SNAPLEN: |
---|
137 | FORMAT_DATA->snaplen=*(int*)data; |
---|
138 | return 0; |
---|
139 | case TRACE_OPTION_PROMISC: |
---|
140 | FORMAT_DATA->promisc=*(int*)data; |
---|
141 | return 0; |
---|
142 | case TRACE_OPTION_FILTER: |
---|
143 | return linuxnative_configure_bpf(libtrace, |
---|
144 | (libtrace_filter_t *) data); |
---|
145 | case TRACE_OPTION_HASHER: |
---|
146 | switch (*((enum hasher_types *)data)) { |
---|
147 | case HASHER_BALANCE: |
---|
148 | // Do fanout |
---|
149 | FORMAT_DATA->fanout_flags = PACKET_FANOUT_LB; |
---|
150 | // Or we could balance to the CPU |
---|
151 | return 0; |
---|
152 | case HASHER_BIDIRECTIONAL: |
---|
153 | case HASHER_UNIDIRECTIONAL: |
---|
154 | FORMAT_DATA->fanout_flags = PACKET_FANOUT_HASH; |
---|
155 | return 0; |
---|
156 | case HASHER_CUSTOM: |
---|
157 | return -1; |
---|
158 | } |
---|
159 | break; |
---|
160 | case TRACE_OPTION_META_FREQ: |
---|
161 | /* No meta-data for this format */ |
---|
162 | break; |
---|
163 | case TRACE_OPTION_EVENT_REALTIME: |
---|
164 | /* Live captures are always going to be in trace time */ |
---|
165 | break; |
---|
166 | case TRACE_OPTION_REPLAY_SPEEDUP: |
---|
167 | break; |
---|
168 | /* Avoid default: so that future options will cause a warning |
---|
169 | * here to remind us to implement it, or flag it as |
---|
170 | * unimplementable |
---|
171 | */ |
---|
172 | } |
---|
173 | |
---|
174 | /* Don't set an error - trace_config will try to deal with the |
---|
175 | * option and will set an error if it fails */ |
---|
176 | return -1; |
---|
177 | } |
---|
178 | |
---|
179 | int linuxcommon_init_input(libtrace_t *libtrace) |
---|
180 | { |
---|
181 | struct linux_per_stream_t stream_data = ZERO_LINUX_STREAM; |
---|
182 | |
---|
183 | libtrace->format_data = (struct linux_format_data_t *) |
---|
184 | malloc(sizeof(struct linux_format_data_t)); |
---|
185 | assert(libtrace->format_data != NULL); |
---|
186 | |
---|
187 | FORMAT_DATA->per_stream = |
---|
188 | libtrace_list_init(sizeof(stream_data)); |
---|
189 | assert(FORMAT_DATA->per_stream != NULL); |
---|
190 | |
---|
191 | libtrace_list_push_back(FORMAT_DATA->per_stream, &stream_data); |
---|
192 | |
---|
193 | FORMAT_DATA->promisc = -1; |
---|
194 | FORMAT_DATA->snaplen = LIBTRACE_PACKET_BUFSIZE; |
---|
195 | FORMAT_DATA->filter = NULL; |
---|
196 | FORMAT_DATA->stats_valid = 0; |
---|
197 | FORMAT_DATA->stats.tp_drops = 0; |
---|
198 | FORMAT_DATA->stats.tp_packets = 0; |
---|
199 | FORMAT_DATA->max_order = MAX_ORDER; |
---|
200 | FORMAT_DATA->fanout_flags = PACKET_FANOUT_LB; |
---|
201 | /* Some examples use pid for the group however that would limit a single |
---|
202 | * application to use only int/ring format, instead using rand */ |
---|
203 | FORMAT_DATA->fanout_group = (uint16_t) rand(); |
---|
204 | return 0; |
---|
205 | } |
---|
206 | |
---|
207 | int linuxcommon_init_output(libtrace_out_t *libtrace) |
---|
208 | { |
---|
209 | libtrace->format_data = (struct linux_format_data_out_t*) |
---|
210 | malloc(sizeof(struct linux_format_data_out_t)); |
---|
211 | assert(libtrace->format_data != NULL); |
---|
212 | |
---|
213 | FORMAT_DATA_OUT->fd = -1; |
---|
214 | FORMAT_DATA_OUT->tx_ring = NULL; |
---|
215 | FORMAT_DATA_OUT->txring_offset = 0; |
---|
216 | FORMAT_DATA_OUT->queue = 0; |
---|
217 | FORMAT_DATA_OUT->max_order = MAX_ORDER; |
---|
218 | return 0; |
---|
219 | } |
---|
220 | |
---|
221 | /* Close an input stream, this is safe to be called part way through |
---|
222 | * initilisation as a cleanup function assuming streams were set to |
---|
223 | * ZERO_LINUX_STREAM to begin with. |
---|
224 | * |
---|
225 | * This works correctly with both int and ring |
---|
226 | */ |
---|
227 | void linuxcommon_close_input_stream(libtrace_t *libtrace, |
---|
228 | struct linux_per_stream_t *stream) { |
---|
229 | if (stream->fd != -1) |
---|
230 | close(stream->fd); |
---|
231 | stream->fd = -1; |
---|
232 | if (stream->rx_ring != MAP_FAILED) |
---|
233 | munmap(stream->rx_ring, |
---|
234 | stream->req.tp_block_size * |
---|
235 | stream->req.tp_block_nr); |
---|
236 | stream->rx_ring = MAP_FAILED; |
---|
237 | stream->rxring_offset = 0; |
---|
238 | FORMAT_DATA->dev_stats.if_name[0] = 0; |
---|
239 | } |
---|
240 | |
---|
241 | #define REPEAT_16(x) x x x x x x x x x x x x x x x x |
---|
242 | #define xstr(s) str(s) |
---|
243 | #define str(s) #s |
---|
244 | |
---|
245 | /* These don't typically reset however an interface does exist to reset them */ |
---|
246 | static int linuxcommon_get_dev_statistics(libtrace_t *libtrace, struct linux_dev_stats *stats) { |
---|
247 | FILE *file; |
---|
248 | char line[1024]; |
---|
249 | struct linux_dev_stats tmp_stats; |
---|
250 | |
---|
251 | file = fopen("/proc/net/dev","r"); |
---|
252 | if (file == NULL) { |
---|
253 | return -1; |
---|
254 | } |
---|
255 | |
---|
256 | /* Skip 2 header lines */ |
---|
257 | if (fgets(line, sizeof(line), file) == NULL) { |
---|
258 | fclose(file); |
---|
259 | return -1; |
---|
260 | } |
---|
261 | |
---|
262 | if (fgets(line, sizeof(line), file) == NULL) { |
---|
263 | fclose(file); |
---|
264 | return -1; |
---|
265 | } |
---|
266 | |
---|
267 | while (!(feof(file)||ferror(file))) { |
---|
268 | int tot; |
---|
269 | if (fgets(line, sizeof(line), file) == NULL) |
---|
270 | break; |
---|
271 | |
---|
272 | tot = sscanf(line, " %"xstr(IF_NAMESIZE)"[^:]:" REPEAT_16(" %"SCNd64), |
---|
273 | tmp_stats.if_name, |
---|
274 | &tmp_stats.rx_bytes, |
---|
275 | &tmp_stats.rx_packets, |
---|
276 | &tmp_stats.rx_errors, |
---|
277 | &tmp_stats.rx_drops, |
---|
278 | &tmp_stats.rx_fifo, |
---|
279 | &tmp_stats.rx_frame, |
---|
280 | &tmp_stats.rx_compressed, |
---|
281 | &tmp_stats.rx_multicast, |
---|
282 | &tmp_stats.tx_bytes, |
---|
283 | &tmp_stats.tx_packets, |
---|
284 | &tmp_stats.tx_errors, |
---|
285 | &tmp_stats.tx_drops, |
---|
286 | &tmp_stats.tx_fifo, |
---|
287 | &tmp_stats.tx_colls, |
---|
288 | &tmp_stats.tx_carrier, |
---|
289 | &tmp_stats.tx_compressed); |
---|
290 | if (tot != 17) |
---|
291 | continue; |
---|
292 | if (strncmp(tmp_stats.if_name, libtrace->uridata, IF_NAMESIZE) == 0) { |
---|
293 | *stats = tmp_stats; |
---|
294 | fclose(file); |
---|
295 | return 0; |
---|
296 | } |
---|
297 | } |
---|
298 | fclose(file); |
---|
299 | return -1; |
---|
300 | } |
---|
301 | |
---|
302 | /* Start an input stream |
---|
303 | * - Opens the file descriptor |
---|
304 | * - Sets promiscuous correctly |
---|
305 | * - Sets socket option |
---|
306 | * - Add BPF filter |
---|
307 | * |
---|
308 | * The output is ready for int directly, for ring the conversion to ring still |
---|
309 | * needs to take place. |
---|
310 | */ |
---|
311 | int linuxcommon_start_input_stream(libtrace_t *libtrace, |
---|
312 | struct linux_per_stream_t *stream) |
---|
313 | { |
---|
314 | struct sockaddr_ll addr; |
---|
315 | const int one = 1; |
---|
316 | memset(&addr,0,sizeof(addr)); |
---|
317 | libtrace_filter_t *filter = FORMAT_DATA->filter; |
---|
318 | |
---|
319 | stream->last_timestamp = 0; |
---|
320 | |
---|
321 | /* Create a raw socket for reading packets on */ |
---|
322 | stream->fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); |
---|
323 | if (stream->fd==-1) { |
---|
324 | trace_set_err(libtrace, errno, "Could not create raw socket"); |
---|
325 | return -1; |
---|
326 | } |
---|
327 | |
---|
328 | /* Bind to the capture interface */ |
---|
329 | addr.sll_family = AF_PACKET; |
---|
330 | addr.sll_protocol = htons(ETH_P_ALL); |
---|
331 | if (strlen(libtrace->uridata)) { |
---|
332 | addr.sll_ifindex = if_nametoindex(libtrace->uridata); |
---|
333 | if (addr.sll_ifindex == 0) { |
---|
334 | linuxcommon_close_input_stream(libtrace, stream); |
---|
335 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
336 | "Failed to find interface %s", |
---|
337 | libtrace->uridata); |
---|
338 | return -1; |
---|
339 | } |
---|
340 | } else { |
---|
341 | addr.sll_ifindex = 0; |
---|
342 | } |
---|
343 | if (bind(stream->fd, |
---|
344 | (struct sockaddr*)&addr, |
---|
345 | (socklen_t)sizeof(addr))==-1) { |
---|
346 | linuxcommon_close_input_stream(libtrace, stream); |
---|
347 | trace_set_err(libtrace, errno, |
---|
348 | "Failed to bind to interface %s", |
---|
349 | libtrace->uridata); |
---|
350 | return -1; |
---|
351 | } |
---|
352 | |
---|
353 | /* If promisc hasn't been specified, set it to "true" if we're |
---|
354 | * capturing on one interface, or "false" if we're capturing on |
---|
355 | * all interfaces. |
---|
356 | */ |
---|
357 | if (FORMAT_DATA->promisc==-1) { |
---|
358 | if (addr.sll_ifindex!=0) |
---|
359 | FORMAT_DATA->promisc=1; |
---|
360 | else |
---|
361 | FORMAT_DATA->promisc=0; |
---|
362 | } |
---|
363 | |
---|
364 | /* Enable promiscuous mode, if requested */ |
---|
365 | if (FORMAT_DATA->promisc) { |
---|
366 | struct packet_mreq mreq; |
---|
367 | socklen_t socklen = sizeof(mreq); |
---|
368 | memset(&mreq,0,sizeof(mreq)); |
---|
369 | mreq.mr_ifindex = addr.sll_ifindex; |
---|
370 | mreq.mr_type = PACKET_MR_PROMISC; |
---|
371 | if (setsockopt(stream->fd, |
---|
372 | SOL_PACKET, |
---|
373 | PACKET_ADD_MEMBERSHIP, |
---|
374 | &mreq, |
---|
375 | socklen)==-1) { |
---|
376 | perror("setsockopt(PROMISC)"); |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | /* Set the timestamp option on the socket - aim for the most detailed |
---|
381 | * clock resolution possible */ |
---|
382 | #ifdef SO_TIMESTAMPNS |
---|
383 | if (setsockopt(stream->fd, |
---|
384 | SOL_SOCKET, |
---|
385 | SO_TIMESTAMPNS, |
---|
386 | &one, |
---|
387 | (socklen_t)sizeof(one))!=-1) { |
---|
388 | FORMAT_DATA->timestamptype = TS_TIMESPEC; |
---|
389 | } |
---|
390 | else |
---|
391 | /* DANGER: This is a dangling else to only do the next setsockopt() |
---|
392 | * if we fail the first! */ |
---|
393 | #endif |
---|
394 | if (setsockopt(stream->fd, |
---|
395 | SOL_SOCKET, |
---|
396 | SO_TIMESTAMP, |
---|
397 | &one, |
---|
398 | (socklen_t)sizeof(one))!=-1) { |
---|
399 | FORMAT_DATA->timestamptype = TS_TIMEVAL; |
---|
400 | } |
---|
401 | else |
---|
402 | FORMAT_DATA->timestamptype = TS_NONE; |
---|
403 | |
---|
404 | /* Push BPF filter into the kernel. At this stage we can safely assume |
---|
405 | * that the filterstring has been compiled, or the filter was supplied |
---|
406 | * pre-compiled. |
---|
407 | */ |
---|
408 | #ifdef HAVE_BPF |
---|
409 | if (filter != NULL) { |
---|
410 | /* Check if the filter was successfully compiled. If not, |
---|
411 | * it is probably a bad filter and we should return an error |
---|
412 | * before the caller tries to read any packets */ |
---|
413 | if (filter->flag == 0) { |
---|
414 | linuxcommon_close_input_stream(libtrace, stream); |
---|
415 | trace_set_err(libtrace, TRACE_ERR_BAD_FILTER, |
---|
416 | "Cannot attach a bad filter to %s", |
---|
417 | libtrace->uridata); |
---|
418 | return -1; |
---|
419 | } |
---|
420 | |
---|
421 | if (setsockopt(stream->fd, |
---|
422 | SOL_SOCKET, |
---|
423 | SO_ATTACH_FILTER, |
---|
424 | &filter->filter, |
---|
425 | sizeof(filter->filter)) == -1) { |
---|
426 | perror("setsockopt(SO_ATTACH_FILTER)"); |
---|
427 | } |
---|
428 | } |
---|
429 | #endif |
---|
430 | |
---|
431 | /* Consume any buffered packets that were received before the socket |
---|
432 | * was properly setup, including those which missed the filter and |
---|
433 | * bind()ing to an interface. |
---|
434 | * |
---|
435 | * If packet rate is high this could therotically hang forever. 4K |
---|
436 | * should be a large enough limit. |
---|
437 | */ |
---|
438 | int count = 0; |
---|
439 | void *buf = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); |
---|
440 | while(count < 4096 && |
---|
441 | recv(stream->fd, |
---|
442 | buf, |
---|
443 | (size_t)LIBTRACE_PACKET_BUFSIZE, |
---|
444 | MSG_DONTWAIT) != -1) { count++; } |
---|
445 | free(buf); |
---|
446 | |
---|
447 | /* Mark that the stats are valid and apply an offset */ |
---|
448 | FORMAT_DATA->stats_valid = 1; |
---|
449 | /* Offset by number we ate for each stream and reset stats after pause */ |
---|
450 | FORMAT_DATA->stats.tp_packets = -count; |
---|
451 | FORMAT_DATA->stats.tp_drops = 0; |
---|
452 | |
---|
453 | if (linuxcommon_get_dev_statistics(libtrace, &FORMAT_DATA->dev_stats) != 0) { |
---|
454 | /* Mark this as bad */ |
---|
455 | FORMAT_DATA->dev_stats.if_name[0] = 0; |
---|
456 | } |
---|
457 | |
---|
458 | return 0; |
---|
459 | } |
---|
460 | |
---|
461 | int linuxcommon_pause_input(libtrace_t *libtrace) |
---|
462 | { |
---|
463 | size_t i; |
---|
464 | |
---|
465 | /* Stop and detach each stream */ |
---|
466 | for (i = 0; i < libtrace_list_get_size(FORMAT_DATA->per_stream); ++i) { |
---|
467 | struct linux_per_stream_t *stream; |
---|
468 | stream = libtrace_list_get_index(FORMAT_DATA->per_stream, i)->data; |
---|
469 | linuxcommon_close_input_stream(libtrace, stream); |
---|
470 | } |
---|
471 | |
---|
472 | return 0; |
---|
473 | } |
---|
474 | |
---|
475 | int linuxcommon_fin_input(libtrace_t *libtrace) |
---|
476 | { |
---|
477 | if (libtrace->format_data) { |
---|
478 | if (FORMAT_DATA->filter != NULL) |
---|
479 | free(FORMAT_DATA->filter); |
---|
480 | |
---|
481 | if (FORMAT_DATA->per_stream) |
---|
482 | libtrace_list_deinit(FORMAT_DATA->per_stream); |
---|
483 | |
---|
484 | free(libtrace->format_data); |
---|
485 | } |
---|
486 | |
---|
487 | return 0; |
---|
488 | } |
---|
489 | |
---|
490 | int linuxcommon_pregister_thread(libtrace_t *libtrace, |
---|
491 | libtrace_thread_t *t, |
---|
492 | bool reading) { |
---|
493 | if (reading) { |
---|
494 | /* XXX TODO remove this oneday make sure hasher thread still works */ |
---|
495 | struct linux_per_stream_t *stream; |
---|
496 | stream = libtrace_list_get_index(FORMAT_DATA->per_stream, |
---|
497 | t->perpkt_num)->data; |
---|
498 | t->format_data = stream; |
---|
499 | if (!stream) { |
---|
500 | /* This should never happen and indicates an |
---|
501 | * internal libtrace bug */ |
---|
502 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
503 | "Failed to attached thread %d to a stream", |
---|
504 | t->perpkt_num); |
---|
505 | return -1; |
---|
506 | } |
---|
507 | } |
---|
508 | return 0; |
---|
509 | } |
---|
510 | |
---|
511 | /* These counters reset with each read */ |
---|
512 | static void linuxcommon_update_socket_statistics(libtrace_t *libtrace) { |
---|
513 | struct tpacket_stats stats; |
---|
514 | size_t i; |
---|
515 | socklen_t len = sizeof(stats); |
---|
516 | |
---|
517 | for (i = 0; i < libtrace_list_get_size(FORMAT_DATA->per_stream); ++i) { |
---|
518 | struct linux_per_stream_t *stream; |
---|
519 | stream = libtrace_list_get_index(FORMAT_DATA->per_stream, i)->data; |
---|
520 | if (stream->fd != -1) { |
---|
521 | if (getsockopt(stream->fd, |
---|
522 | SOL_PACKET, |
---|
523 | PACKET_STATISTICS, |
---|
524 | &stats, |
---|
525 | &len) == 0) { |
---|
526 | if (FORMAT_DATA->stats_valid==0) { |
---|
527 | FORMAT_DATA->stats.tp_drops = stats.tp_drops; |
---|
528 | FORMAT_DATA->stats.tp_packets = stats.tp_packets; |
---|
529 | FORMAT_DATA->stats_valid = 1; |
---|
530 | } else { |
---|
531 | FORMAT_DATA->stats.tp_drops += stats.tp_drops; |
---|
532 | FORMAT_DATA->stats.tp_packets += stats.tp_packets; |
---|
533 | } |
---|
534 | } else { |
---|
535 | perror("getsockopt PACKET_STATISTICS failed"); |
---|
536 | } |
---|
537 | } |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | #define DEV_DIFF(x) (dev_stats.x - FORMAT_DATA->dev_stats.x) |
---|
542 | /* Note these statistics come from two different sources, the socket itself and |
---|
543 | * the linux device. As such this means it is highly likely that their is some |
---|
544 | * margin of error in the returned statistics, we perform basic sanitising so |
---|
545 | * that these are not too noticable. |
---|
546 | */ |
---|
547 | void linuxcommon_get_statistics(libtrace_t *libtrace, libtrace_stat_t *stat) { |
---|
548 | struct linux_dev_stats dev_stats; |
---|
549 | if (libtrace->format_data == NULL) |
---|
550 | return; |
---|
551 | /* Do we need to consider the case after the trace is closed? */ |
---|
552 | if (FORMAT_DATA_FIRST->fd == -1) { |
---|
553 | /* This is probably a 'dead' trace so obviously we can't query |
---|
554 | * the socket for capture counts, can we? */ |
---|
555 | return; |
---|
556 | } |
---|
557 | |
---|
558 | dev_stats.if_name[0] = 0; /* This will be set if we retrive valid stats */ |
---|
559 | /* Do we have starting stats to compare to? */ |
---|
560 | if (FORMAT_DATA->dev_stats.if_name[0] != 0) { |
---|
561 | linuxcommon_get_dev_statistics(libtrace, &dev_stats); |
---|
562 | } |
---|
563 | linuxcommon_update_socket_statistics(libtrace); |
---|
564 | |
---|
565 | /* filtered count == dev received - socket received */ |
---|
566 | if (FORMAT_DATA->filter != NULL && |
---|
567 | FORMAT_DATA->stats_valid && |
---|
568 | dev_stats.if_name[0]) { |
---|
569 | uint64_t filtered = DEV_DIFF(rx_packets) - |
---|
570 | FORMAT_DATA->stats.tp_packets; |
---|
571 | /* Check the value is sane, due to timing it could be below 0 */ |
---|
572 | if (filtered < UINT64_MAX - 100000) { |
---|
573 | stat->filtered += filtered; |
---|
574 | } |
---|
575 | } |
---|
576 | |
---|
577 | /* dropped count == socket dropped + dev dropped */ |
---|
578 | if (FORMAT_DATA->stats_valid) { |
---|
579 | stat->dropped_valid = 1; |
---|
580 | stat->dropped = FORMAT_DATA->stats.tp_drops; |
---|
581 | if (dev_stats.if_name[0]) { |
---|
582 | stat->dropped += DEV_DIFF(rx_drops); |
---|
583 | } |
---|
584 | } |
---|
585 | |
---|
586 | /* received count - All good packets even those dropped or filtered */ |
---|
587 | if (dev_stats.if_name[0]) { |
---|
588 | stat->received_valid = 1; |
---|
589 | stat->received = DEV_DIFF(rx_packets) + DEV_DIFF(rx_drops); |
---|
590 | } |
---|
591 | |
---|
592 | /* captured count - received and but not dropped */ |
---|
593 | if (dev_stats.if_name[0] && FORMAT_DATA->stats_valid) { |
---|
594 | stat->captured_valid = 1; |
---|
595 | stat->captured = DEV_DIFF(rx_packets) - FORMAT_DATA->stats.tp_drops; |
---|
596 | } |
---|
597 | |
---|
598 | /* errors */ |
---|
599 | if (dev_stats.if_name[0]) { |
---|
600 | stat->errors_valid = 1; |
---|
601 | stat->errors = DEV_DIFF(rx_errors); |
---|
602 | } |
---|
603 | |
---|
604 | } |
---|
605 | |
---|
606 | int linuxcommon_get_fd(const libtrace_t *libtrace) { |
---|
607 | if (libtrace->format_data == NULL) |
---|
608 | return -1; |
---|
609 | return FORMAT_DATA_FIRST->fd; |
---|
610 | } |
---|
611 | |
---|
612 | int linuxcommon_pstart_input(libtrace_t *libtrace, |
---|
613 | int (*start_stream)(libtrace_t *, struct linux_per_stream_t*)) { |
---|
614 | int i = 0; |
---|
615 | int tot = libtrace->perpkt_thread_count; |
---|
616 | int iserror = 0; |
---|
617 | struct linux_per_stream_t empty_stream = ZERO_LINUX_STREAM; |
---|
618 | |
---|
619 | for (i = 0; i < tot; ++i) |
---|
620 | { |
---|
621 | struct linux_per_stream_t *stream; |
---|
622 | /* Add storage for another stream */ |
---|
623 | if (libtrace_list_get_size(FORMAT_DATA->per_stream) <= (size_t) i) |
---|
624 | libtrace_list_push_back(FORMAT_DATA->per_stream, &empty_stream); |
---|
625 | |
---|
626 | stream = libtrace_list_get_index(FORMAT_DATA->per_stream, i)->data; |
---|
627 | if (start_stream(libtrace, stream) != 0) { |
---|
628 | iserror = 1; |
---|
629 | break; |
---|
630 | } |
---|
631 | if (linuxcommon_to_packet_fanout(libtrace, stream) != 0) |
---|
632 | { |
---|
633 | iserror = 1; |
---|
634 | close(stream->fd); |
---|
635 | stream->fd = -1; |
---|
636 | break; |
---|
637 | } |
---|
638 | } |
---|
639 | |
---|
640 | if (iserror) { |
---|
641 | /* Free those that succeeded */ |
---|
642 | for (i = i - 1; i >= 0; i--) { |
---|
643 | struct linux_per_stream_t *stream; |
---|
644 | stream = libtrace_list_get_index(FORMAT_DATA->per_stream, i)->data; |
---|
645 | linuxcommon_close_input_stream(libtrace, stream); |
---|
646 | } |
---|
647 | return -1; |
---|
648 | } |
---|
649 | |
---|
650 | return 0; |
---|
651 | } |
---|
652 | |
---|
653 | #else /* HAVE_NETPACKET_PACKET_H */ |
---|
654 | |
---|
655 | /* No NETPACKET - So this format is not live */ |
---|
656 | void linuxcommon_get_statistics(libtrace_t *libtrace UNUSED, |
---|
657 | libtrace_stat_t *stat UNUSED) { |
---|
658 | return; |
---|
659 | } |
---|
660 | |
---|
661 | #endif /* HAVE_NETPACKET_PACKET_H */ |
---|
662 | |
---|