1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, |
---|
5 | * New Zealand. |
---|
6 | * |
---|
7 | * Authors: Daniel Lawson |
---|
8 | * Perry Lorier |
---|
9 | * Shane Alcock |
---|
10 | * Richard Sanger |
---|
11 | * |
---|
12 | * All rights reserved. |
---|
13 | * |
---|
14 | * This code has been developed by the University of Waikato WAND |
---|
15 | * research group. For further information please see http://www.wand.net.nz/ |
---|
16 | * |
---|
17 | * libtrace is free software; you can redistribute it and/or modify |
---|
18 | * it under the terms of the GNU General Public License as published by |
---|
19 | * the Free Software Foundation; either version 2 of the License, or |
---|
20 | * (at your option) any later version. |
---|
21 | * |
---|
22 | * libtrace is distributed in the hope that it will be useful, |
---|
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
25 | * GNU General Public License for more details. |
---|
26 | * |
---|
27 | * You should have received a copy of the GNU General Public License |
---|
28 | * along with libtrace; if not, write to the Free Software |
---|
29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
30 | * |
---|
31 | * $Id$ |
---|
32 | * |
---|
33 | */ |
---|
34 | |
---|
35 | /* This format module deals with using the Linux Ring capture format (also |
---|
36 | * known as PACKET_MMAP). |
---|
37 | * |
---|
38 | * Linux Ring is a LIVE capture format. |
---|
39 | * |
---|
40 | * This format also supports writing which will write packets out to the |
---|
41 | * network as a form of packet replay. This should not be confused with the |
---|
42 | * RT protocol which is intended to transfer captured packet records between |
---|
43 | * RT-speaking programs. |
---|
44 | */ |
---|
45 | |
---|
46 | #include "config.h" |
---|
47 | #include "libtrace.h" |
---|
48 | #include "libtrace_int.h" |
---|
49 | #include "format_helper.h" |
---|
50 | #include "libtrace_arphrd.h" |
---|
51 | #include <stdlib.h> |
---|
52 | #include <errno.h> |
---|
53 | #include <unistd.h> |
---|
54 | #include <string.h> |
---|
55 | #include <assert.h> |
---|
56 | |
---|
57 | #ifdef HAVE_INTTYPES_H |
---|
58 | # include <inttypes.h> |
---|
59 | #else |
---|
60 | # error "Can't find inttypes.h" |
---|
61 | #endif |
---|
62 | |
---|
63 | #include "format_linux_common.h" |
---|
64 | |
---|
65 | /* Get the start of the captured data. I'm not sure if tp_mac (link layer) is |
---|
66 | * always guaranteed. If it's not there then just use tp_net. |
---|
67 | */ |
---|
68 | #define TP_TRACE_START(mac, net, hdrend) \ |
---|
69 | ((mac) > (hdrend) && (mac) < (net) ? (mac) : (net)) |
---|
70 | |
---|
71 | #ifdef HAVE_NETPACKET_PACKET_H |
---|
72 | /* Get current frame in the ring buffer*/ |
---|
73 | #define GET_CURRENT_BUFFER(stream) \ |
---|
74 | ((void *)stream->rx_ring + \ |
---|
75 | (stream->rxring_offset * \ |
---|
76 | stream->req.tp_frame_size)) |
---|
77 | |
---|
78 | /* Cached page size, the page size shouldn't be changing */ |
---|
79 | static int pagesize = 0; |
---|
80 | |
---|
81 | /* |
---|
82 | * Try figure out the best sizes for the ring buffer. Ensure that: |
---|
83 | * - max(Block_size) == page_size << max_order |
---|
84 | * - Frame_size == page_size << x (so that block_size%frame_size == 0) |
---|
85 | * This means that there will be no wasted space between blocks |
---|
86 | * - Frame_size < block_size |
---|
87 | * - Frame_size is as close as possible to LIBTRACE_PACKET_BUFSIZE, but not |
---|
88 | * bigger |
---|
89 | * - Frame_nr = Block_nr * (frames per block) |
---|
90 | * - CONF_RING_FRAMES is used a minimum number of frames to hold |
---|
91 | * - Calculates based on max_order and buf_min |
---|
92 | */ |
---|
93 | static void calculate_buffers(struct tpacket_req * req, int fd, char * uri, |
---|
94 | uint32_t max_order) |
---|
95 | { |
---|
96 | struct ifreq ifr; |
---|
97 | unsigned max_frame = LIBTRACE_PACKET_BUFSIZE; |
---|
98 | pagesize = getpagesize(); |
---|
99 | |
---|
100 | strcpy(ifr.ifr_name, uri); |
---|
101 | /* Don't bother trying to set frame size above mtu linux will drop |
---|
102 | * these anyway. |
---|
103 | * |
---|
104 | * Remember, that our frame also has to include a TPACKET header! |
---|
105 | */ |
---|
106 | if (ioctl(fd, SIOCGIFMTU, (caddr_t)&ifr) >= 0) |
---|
107 | max_frame = ifr.ifr_mtu + TPACKET_ALIGN(TPACKET2_HDRLEN); |
---|
108 | if (max_frame > LIBTRACE_PACKET_BUFSIZE) |
---|
109 | max_frame = LIBTRACE_PACKET_BUFSIZE; |
---|
110 | |
---|
111 | /* Calculate frame size */ |
---|
112 | req->tp_frame_size = pagesize; |
---|
113 | while (req->tp_frame_size < max_frame && |
---|
114 | req->tp_frame_size < LIBTRACE_PACKET_BUFSIZE) { |
---|
115 | req->tp_frame_size <<= 1; |
---|
116 | } |
---|
117 | if (req->tp_frame_size > LIBTRACE_PACKET_BUFSIZE) |
---|
118 | req->tp_frame_size >>= 1; |
---|
119 | |
---|
120 | /* Calculate block size */ |
---|
121 | req->tp_block_size = pagesize << max_order; |
---|
122 | /* If max order is too high this might become 0 */ |
---|
123 | if (req->tp_block_size == 0) { |
---|
124 | calculate_buffers(req, fd, uri, max_order-1); |
---|
125 | return; |
---|
126 | } |
---|
127 | do { |
---|
128 | req->tp_block_size >>= 1; |
---|
129 | } while ((CONF_RING_FRAMES * req->tp_frame_size) <= req->tp_block_size); |
---|
130 | req->tp_block_size <<= 1; |
---|
131 | |
---|
132 | /* Calculate number of blocks */ |
---|
133 | req->tp_block_nr = (CONF_RING_FRAMES * req->tp_frame_size) |
---|
134 | / req->tp_block_size; |
---|
135 | if((CONF_RING_FRAMES * req->tp_frame_size) % req->tp_block_size != 0) |
---|
136 | req->tp_block_nr++; |
---|
137 | |
---|
138 | /* Calculate packets such that we use all the space we have to |
---|
139 | * allocated */ |
---|
140 | req->tp_frame_nr = req->tp_block_nr * |
---|
141 | (req->tp_block_size / req->tp_frame_size); |
---|
142 | |
---|
143 | /* |
---|
144 | printf("MaxO 0x%x BS 0x%x BN 0x%x FS 0x%x FN 0x%x\n", |
---|
145 | max_order, |
---|
146 | req->tp_block_size, |
---|
147 | req->tp_block_nr, |
---|
148 | req->tp_frame_size, |
---|
149 | req->tp_frame_nr); |
---|
150 | */ |
---|
151 | |
---|
152 | /* In case we have some silly values*/ |
---|
153 | assert(req->tp_block_size); |
---|
154 | assert(req->tp_block_nr); |
---|
155 | assert(req->tp_frame_size); |
---|
156 | assert(req->tp_frame_nr); |
---|
157 | assert(req->tp_block_size % req->tp_frame_size == 0); |
---|
158 | } |
---|
159 | |
---|
160 | static inline int socket_to_packetmmap(char * uridata, int ring_type, |
---|
161 | int fd, |
---|
162 | struct tpacket_req * req, |
---|
163 | char ** ring_location, |
---|
164 | uint32_t *max_order, |
---|
165 | char *error) { |
---|
166 | int val; |
---|
167 | |
---|
168 | /* Switch to TPACKET header version 2, we only try support v2 because |
---|
169 | * v1 had problems with data type consistancy */ |
---|
170 | val = TPACKET_V2; |
---|
171 | if (setsockopt(fd, |
---|
172 | SOL_PACKET, |
---|
173 | PACKET_VERSION, |
---|
174 | &val, |
---|
175 | sizeof(val)) == -1) { |
---|
176 | strncpy(error, "TPACKET2 not supported", 2048); |
---|
177 | return -1; |
---|
178 | } |
---|
179 | |
---|
180 | /* Try switch to a ring buffer. If it fails we assume the the kernel |
---|
181 | * cannot allocate a block of that size, so decrease max_block and |
---|
182 | * retry. |
---|
183 | */ |
---|
184 | while(1) { |
---|
185 | if (*max_order <= 0) { |
---|
186 | strncpy(error, |
---|
187 | "Cannot allocate enough memory for ring buffer", |
---|
188 | 2048); |
---|
189 | return -1; |
---|
190 | } |
---|
191 | calculate_buffers(req, fd, uridata, *max_order); |
---|
192 | if (setsockopt(fd, |
---|
193 | SOL_PACKET, |
---|
194 | ring_type, |
---|
195 | req, |
---|
196 | sizeof(struct tpacket_req)) == -1) { |
---|
197 | if(errno == ENOMEM) { |
---|
198 | (*max_order)--; |
---|
199 | } else { |
---|
200 | strncpy(error, |
---|
201 | "Error setting the ring buffer size", |
---|
202 | 2048); |
---|
203 | return -1; |
---|
204 | } |
---|
205 | |
---|
206 | } else break; |
---|
207 | } |
---|
208 | |
---|
209 | /* Map the ring buffer into userspace */ |
---|
210 | *ring_location = mmap(NULL, |
---|
211 | req->tp_block_size * req->tp_block_nr, |
---|
212 | PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
---|
213 | if(*ring_location == MAP_FAILED) { |
---|
214 | strncpy(error, "Failed to map memory for ring buffer", 2048); |
---|
215 | return -1; |
---|
216 | } |
---|
217 | |
---|
218 | return 0; |
---|
219 | } |
---|
220 | |
---|
221 | /* Release a frame back to the kernel or free() if it's a malloc'd buffer |
---|
222 | */ |
---|
223 | inline static void ring_release_frame(libtrace_t *libtrace UNUSED, |
---|
224 | libtrace_packet_t *packet) |
---|
225 | { |
---|
226 | /* Free the old packet */ |
---|
227 | if(packet->buffer == NULL) |
---|
228 | return; |
---|
229 | |
---|
230 | if(packet->buf_control == TRACE_CTRL_PACKET){ |
---|
231 | free(packet->buffer); |
---|
232 | packet->buffer = NULL; |
---|
233 | } |
---|
234 | |
---|
235 | if(packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
236 | //struct linux_format_data_t *ftd = FORMAT_DATA; |
---|
237 | /* Check it's within our buffer first - consider the pause |
---|
238 | * resume case it might have already been free'd lets hope we |
---|
239 | * get another buffer */ |
---|
240 | // TODO: For now let any one free anything |
---|
241 | /*if(LIBTRACE_BETWEEN((char *) packet->buffer, |
---|
242 | (char *) ftd->rx_ring, |
---|
243 | ftd->rx_ring + |
---|
244 | ftd->req.tp_block_size * |
---|
245 | ftd->req.tp_block_nr)){*/ |
---|
246 | TO_TP_HDR2(packet->buffer)->tp_status = 0; |
---|
247 | packet->buffer = NULL; |
---|
248 | /*}*/ |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | static inline int linuxring_start_input_stream(libtrace_t *libtrace, |
---|
253 | struct linux_per_stream_t *stream) { |
---|
254 | char error[2048]; |
---|
255 | |
---|
256 | /* We set the socket up the same and then convert it to PACKET_MMAP */ |
---|
257 | if (linuxcommon_start_input_stream(libtrace, stream) < 0) |
---|
258 | return -1; |
---|
259 | |
---|
260 | strncpy(error, "No known error", 2048); |
---|
261 | |
---|
262 | /* Make it a packetmmap */ |
---|
263 | if(socket_to_packetmmap(libtrace->uridata, PACKET_RX_RING, |
---|
264 | stream->fd, |
---|
265 | &stream->req, |
---|
266 | &stream->rx_ring, |
---|
267 | &FORMAT_DATA->max_order, |
---|
268 | error) != 0) { |
---|
269 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
270 | "Initialisation of packet MMAP failed: %s", |
---|
271 | error); |
---|
272 | linuxcommon_close_input_stream(libtrace, stream); |
---|
273 | return -1; |
---|
274 | } |
---|
275 | |
---|
276 | return 0; |
---|
277 | } |
---|
278 | |
---|
279 | static int linuxring_start_input(libtrace_t *libtrace) |
---|
280 | { |
---|
281 | int ret = linuxring_start_input_stream(libtrace, FORMAT_DATA_FIRST); |
---|
282 | return ret; |
---|
283 | } |
---|
284 | |
---|
285 | static int linuxring_pstart_input(libtrace_t *libtrace) { |
---|
286 | return linuxcommon_pstart_input(libtrace, linuxring_start_input_stream); |
---|
287 | } |
---|
288 | |
---|
289 | static int linuxring_start_output(libtrace_out_t *libtrace) |
---|
290 | { |
---|
291 | char error[2048]; |
---|
292 | FORMAT_DATA_OUT->fd = socket(PF_PACKET, SOCK_RAW, 0); |
---|
293 | if (FORMAT_DATA_OUT->fd==-1) { |
---|
294 | free(FORMAT_DATA_OUT); |
---|
295 | trace_set_err_out(libtrace, errno, "Failed to create raw socket"); |
---|
296 | return -1; |
---|
297 | } |
---|
298 | |
---|
299 | /* Make it a packetmmap */ |
---|
300 | if(socket_to_packetmmap(libtrace->uridata, PACKET_TX_RING, |
---|
301 | FORMAT_DATA_OUT->fd, |
---|
302 | &FORMAT_DATA_OUT->req, |
---|
303 | &FORMAT_DATA_OUT->tx_ring, |
---|
304 | &FORMAT_DATA_OUT->max_order, |
---|
305 | error) != 0) { |
---|
306 | trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED, |
---|
307 | "Initialisation of packet MMAP failed: %s", |
---|
308 | error); |
---|
309 | close(FORMAT_DATA_OUT->fd); |
---|
310 | free(FORMAT_DATA_OUT); |
---|
311 | libtrace->format_data = NULL; |
---|
312 | return -1; |
---|
313 | } |
---|
314 | |
---|
315 | FORMAT_DATA_OUT->sock_hdr.sll_family = AF_PACKET; |
---|
316 | FORMAT_DATA_OUT->sock_hdr.sll_protocol = 0; |
---|
317 | FORMAT_DATA_OUT->sock_hdr.sll_ifindex = |
---|
318 | if_nametoindex(libtrace->uridata); |
---|
319 | FORMAT_DATA_OUT->sock_hdr.sll_hatype = 0; |
---|
320 | FORMAT_DATA_OUT->sock_hdr.sll_pkttype = 0; |
---|
321 | FORMAT_DATA_OUT->sock_hdr.sll_halen = 0; |
---|
322 | FORMAT_DATA_OUT->queue = 0; |
---|
323 | |
---|
324 | return 0; |
---|
325 | } |
---|
326 | |
---|
327 | static int linuxring_fin_output(libtrace_out_t *libtrace) |
---|
328 | { |
---|
329 | /* Make sure any remaining frames get sent */ |
---|
330 | sendto(FORMAT_DATA_OUT->fd, |
---|
331 | NULL, |
---|
332 | 0, |
---|
333 | 0, |
---|
334 | (void *) &FORMAT_DATA_OUT->sock_hdr, |
---|
335 | sizeof(FORMAT_DATA_OUT->sock_hdr)); |
---|
336 | |
---|
337 | /* Unmap our data area */ |
---|
338 | munmap(FORMAT_DATA_OUT->tx_ring, |
---|
339 | FORMAT_DATA_OUT->req.tp_block_size * |
---|
340 | FORMAT_DATA_OUT->req.tp_block_nr); |
---|
341 | |
---|
342 | /* Free the socket */ |
---|
343 | close(FORMAT_DATA_OUT->fd); |
---|
344 | FORMAT_DATA_OUT->fd=-1; |
---|
345 | free(libtrace->format_data); |
---|
346 | return 0; |
---|
347 | } |
---|
348 | #endif /* HAVE_NETPACKET_PACKET_H */ |
---|
349 | |
---|
350 | static libtrace_linktype_t |
---|
351 | linuxring_get_link_type(const struct libtrace_packet_t *packet) |
---|
352 | { |
---|
353 | uint16_t linktype = GET_SOCKADDR_HDR(packet->buffer)->sll_hatype; |
---|
354 | return linuxcommon_get_link_type(linktype); |
---|
355 | } |
---|
356 | |
---|
357 | static libtrace_direction_t |
---|
358 | linuxring_get_direction(const struct libtrace_packet_t *packet) { |
---|
359 | return linuxcommon_get_direction(GET_SOCKADDR_HDR(packet->buffer)-> |
---|
360 | sll_pkttype); |
---|
361 | } |
---|
362 | |
---|
363 | static libtrace_direction_t |
---|
364 | linuxring_set_direction(libtrace_packet_t *packet, |
---|
365 | libtrace_direction_t direction) { |
---|
366 | return linuxcommon_set_direction(GET_SOCKADDR_HDR(packet->buffer), direction); |
---|
367 | } |
---|
368 | |
---|
369 | static struct timeval linuxring_get_timeval(const libtrace_packet_t *packet) |
---|
370 | { |
---|
371 | struct timeval tv; |
---|
372 | tv.tv_sec = TO_TP_HDR2(packet->buffer)->tp_sec; |
---|
373 | tv.tv_usec = TO_TP_HDR2(packet->buffer)->tp_nsec / 1000; |
---|
374 | return tv; |
---|
375 | } |
---|
376 | |
---|
377 | static struct timespec linuxring_get_timespec(const libtrace_packet_t *packet) |
---|
378 | { |
---|
379 | struct timespec ts; |
---|
380 | ts.tv_sec = TO_TP_HDR2(packet->buffer)->tp_sec; |
---|
381 | ts.tv_nsec = TO_TP_HDR2(packet->buffer)->tp_nsec; |
---|
382 | return ts; |
---|
383 | } |
---|
384 | |
---|
385 | static int linuxring_get_capture_length(const libtrace_packet_t *packet) |
---|
386 | { |
---|
387 | return TO_TP_HDR2(packet->buffer)->tp_snaplen; |
---|
388 | } |
---|
389 | |
---|
390 | static int linuxring_get_wire_length(const libtrace_packet_t *packet) |
---|
391 | { |
---|
392 | int wirelen = TO_TP_HDR2(packet->buffer)->tp_len; |
---|
393 | |
---|
394 | /* Include the missing FCS */ |
---|
395 | if (trace_get_link_type(packet) == TRACE_TYPE_ETH) |
---|
396 | wirelen += 4; |
---|
397 | |
---|
398 | return wirelen; |
---|
399 | } |
---|
400 | |
---|
401 | static int linuxring_get_framing_length(const libtrace_packet_t *packet) |
---|
402 | { |
---|
403 | /* |
---|
404 | * Need to make frame_length + capture_length = complete capture length |
---|
405 | * so include alignment whitespace. So reverse calculate from packet. |
---|
406 | */ |
---|
407 | return (char *)packet->payload - (char *)packet->buffer; |
---|
408 | } |
---|
409 | |
---|
410 | static size_t linuxring_set_capture_length(libtrace_packet_t *packet, |
---|
411 | size_t size) |
---|
412 | { |
---|
413 | assert(packet); |
---|
414 | if (size > trace_get_capture_length(packet)) { |
---|
415 | /* We should avoid making a packet larger */ |
---|
416 | return trace_get_capture_length(packet); |
---|
417 | } |
---|
418 | |
---|
419 | /* Reset the cached capture length */ |
---|
420 | packet->capture_length = -1; |
---|
421 | |
---|
422 | TO_TP_HDR2(packet->buffer)->tp_snaplen = size; |
---|
423 | |
---|
424 | return trace_get_capture_length(packet); |
---|
425 | } |
---|
426 | |
---|
427 | static int linuxring_prepare_packet(libtrace_t *libtrace UNUSED, |
---|
428 | libtrace_packet_t *packet, void *buffer, |
---|
429 | libtrace_rt_types_t rt_type, uint32_t flags) |
---|
430 | { |
---|
431 | if (packet->buffer != buffer && |
---|
432 | packet->buf_control == TRACE_CTRL_PACKET) { |
---|
433 | free(packet->buffer); |
---|
434 | } |
---|
435 | |
---|
436 | if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) |
---|
437 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
438 | else |
---|
439 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
440 | |
---|
441 | |
---|
442 | packet->buffer = buffer; |
---|
443 | packet->header = buffer; |
---|
444 | packet->payload = (char *)buffer + |
---|
445 | TP_TRACE_START(TO_TP_HDR2(packet->header)->tp_mac, |
---|
446 | TO_TP_HDR2(packet->header)->tp_net, |
---|
447 | TPACKET2_HDRLEN); |
---|
448 | packet->type = rt_type; |
---|
449 | |
---|
450 | return 0; |
---|
451 | } |
---|
452 | |
---|
453 | #ifdef HAVE_NETPACKET_PACKET_H |
---|
454 | #define LIBTRACE_MIN(a,b) ((a)<(b) ? (a) : (b)) |
---|
455 | inline static int linuxring_read_stream(libtrace_t *libtrace, |
---|
456 | libtrace_packet_t *packet, |
---|
457 | struct linux_per_stream_t *stream, |
---|
458 | libtrace_message_queue_t *queue) { |
---|
459 | |
---|
460 | struct tpacket2_hdr *header; |
---|
461 | int ret; |
---|
462 | unsigned int snaplen; |
---|
463 | struct pollfd pollset[2]; |
---|
464 | |
---|
465 | ring_release_frame(libtrace, packet); |
---|
466 | |
---|
467 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
468 | packet->type = TRACE_RT_DATA_LINUX_RING; |
---|
469 | |
---|
470 | /* Fetch the current frame */ |
---|
471 | header = GET_CURRENT_BUFFER(stream); |
---|
472 | assert((((unsigned long) header) & (pagesize - 1)) == 0); |
---|
473 | |
---|
474 | /* TP_STATUS_USER means that we can use the frame. |
---|
475 | * When a slot does not have this flag set, the frame is not |
---|
476 | * ready for consumption. |
---|
477 | */ |
---|
478 | while (!(header->tp_status & TP_STATUS_USER)) { |
---|
479 | pollset[0].fd = stream->fd; |
---|
480 | pollset[0].events = POLLIN; |
---|
481 | pollset[0].revents = 0; |
---|
482 | if (queue) { |
---|
483 | pollset[1].fd = libtrace_message_queue_get_fd(queue); |
---|
484 | pollset[1].events = POLLIN; |
---|
485 | pollset[1].revents = 0; |
---|
486 | } |
---|
487 | /* Wait for more data or a message */ |
---|
488 | ret = poll(pollset, (queue ? 2 : 1), 500); |
---|
489 | if (ret > 0) { |
---|
490 | if (pollset[0].revents == POLLIN) |
---|
491 | continue; |
---|
492 | else if (queue && pollset[1].revents == POLLIN) |
---|
493 | return READ_MESSAGE; |
---|
494 | else if (queue && pollset[1].revents) { |
---|
495 | /* Internal error */ |
---|
496 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE, |
---|
497 | "Message queue error %d poll()", |
---|
498 | pollset[1].revents); |
---|
499 | return READ_ERROR; |
---|
500 | } else { |
---|
501 | /* Try get the error from the socket */ |
---|
502 | int err = ENETDOWN; |
---|
503 | socklen_t len = sizeof(err); |
---|
504 | getsockopt(stream->fd, SOL_SOCKET, SO_ERROR, |
---|
505 | &err, &len); |
---|
506 | trace_set_err(libtrace, err, |
---|
507 | "Socket error revents=%d poll()", |
---|
508 | pollset[0].revents); |
---|
509 | return READ_ERROR; |
---|
510 | } |
---|
511 | } else if (ret < 0) { |
---|
512 | if (errno != EINTR) { |
---|
513 | trace_set_err(libtrace,errno,"poll()"); |
---|
514 | return -1; |
---|
515 | } |
---|
516 | } else { |
---|
517 | /* Poll timed out - check if we should exit */ |
---|
518 | if (libtrace_halt) |
---|
519 | return 0; |
---|
520 | continue; |
---|
521 | } |
---|
522 | } |
---|
523 | |
---|
524 | packet->buffer = header; |
---|
525 | packet->trace = libtrace; |
---|
526 | |
---|
527 | /* If a snaplen was configured, automatically truncate the packet to |
---|
528 | * the desired length. |
---|
529 | */ |
---|
530 | snaplen=LIBTRACE_MIN( |
---|
531 | (int)LIBTRACE_PACKET_BUFSIZE-(int)sizeof(*header), |
---|
532 | (int)FORMAT_DATA->snaplen); |
---|
533 | |
---|
534 | TO_TP_HDR2(packet->buffer)->tp_snaplen = LIBTRACE_MIN((unsigned int)snaplen, TO_TP_HDR2(packet->buffer)->tp_len); |
---|
535 | |
---|
536 | /* Move to next buffer */ |
---|
537 | stream->rxring_offset++; |
---|
538 | stream->rxring_offset %= stream->req.tp_frame_nr; |
---|
539 | |
---|
540 | /* We just need to get prepare_packet to set all our packet pointers |
---|
541 | * appropriately */ |
---|
542 | if (linuxring_prepare_packet(libtrace, packet, packet->buffer, |
---|
543 | packet->type, 0)) |
---|
544 | return -1; |
---|
545 | return linuxring_get_framing_length(packet) + |
---|
546 | linuxring_get_capture_length(packet); |
---|
547 | |
---|
548 | } |
---|
549 | |
---|
550 | static int linuxring_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
551 | return linuxring_read_stream(libtrace, packet, FORMAT_DATA_FIRST, NULL); |
---|
552 | } |
---|
553 | |
---|
554 | static int linuxring_pread_packets(libtrace_t *libtrace, |
---|
555 | libtrace_thread_t *t, |
---|
556 | libtrace_packet_t *packets[], |
---|
557 | UNUSED size_t nb_packets) { |
---|
558 | /* For now just read one packet */ |
---|
559 | packets[0]->error = linuxring_read_stream(libtrace, packets[0], |
---|
560 | t->format_data, &t->messages); |
---|
561 | if (packets[0]->error >= 1) |
---|
562 | return 1; |
---|
563 | else |
---|
564 | return packets[0]->error; |
---|
565 | } |
---|
566 | |
---|
567 | /* Non-blocking read */ |
---|
568 | static libtrace_eventobj_t linuxring_event(libtrace_t *libtrace, |
---|
569 | libtrace_packet_t *packet) |
---|
570 | { |
---|
571 | struct tpacket2_hdr *header; |
---|
572 | libtrace_eventobj_t event = {0,0,0.0,0}; |
---|
573 | |
---|
574 | /* We must free the old packet, otherwise select() will instantly |
---|
575 | * return */ |
---|
576 | ring_release_frame(libtrace, packet); |
---|
577 | |
---|
578 | /* Fetch the current frame */ |
---|
579 | header = GET_CURRENT_BUFFER(FORMAT_DATA_FIRST); |
---|
580 | if (header->tp_status & TP_STATUS_USER) { |
---|
581 | /* We have a frame waiting */ |
---|
582 | event.size = trace_read_packet(libtrace, packet); |
---|
583 | event.type = TRACE_EVENT_PACKET; |
---|
584 | } else { |
---|
585 | /* Ok we don't have a packet waiting */ |
---|
586 | event.type = TRACE_EVENT_IOWAIT; |
---|
587 | event.fd = FORMAT_DATA_FIRST->fd; |
---|
588 | } |
---|
589 | |
---|
590 | return event; |
---|
591 | } |
---|
592 | |
---|
593 | /** |
---|
594 | * Free any resources being kept for this packet, Note: libtrace |
---|
595 | * will ensure all fields are zeroed correctly. |
---|
596 | */ |
---|
597 | static void linuxring_fin_packet(libtrace_packet_t *packet) |
---|
598 | { |
---|
599 | libtrace_t *libtrace = packet->trace; |
---|
600 | |
---|
601 | if (packet->buffer == NULL) |
---|
602 | return; |
---|
603 | assert(packet->trace); |
---|
604 | |
---|
605 | /* If we own the packet (i.e. it's not a copy), we need to free it */ |
---|
606 | if (packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
607 | /* Started should always match the existence of the rx_ring |
---|
608 | * in the parallel case still just check the first ring */ |
---|
609 | assert(!!FORMAT_DATA_FIRST->rx_ring == |
---|
610 | !!packet->trace->started); |
---|
611 | /* If we don't have a ring its already been destroyed */ |
---|
612 | if (FORMAT_DATA_FIRST->rx_ring != MAP_FAILED) |
---|
613 | ring_release_frame(packet->trace, packet); |
---|
614 | else |
---|
615 | packet->buffer = NULL; |
---|
616 | } |
---|
617 | } |
---|
618 | |
---|
619 | static int linuxring_write_packet(libtrace_out_t *libtrace, |
---|
620 | libtrace_packet_t *packet) |
---|
621 | { |
---|
622 | struct tpacket2_hdr *header; |
---|
623 | struct pollfd pollset; |
---|
624 | struct socket_addr; |
---|
625 | int ret; |
---|
626 | unsigned max_size; |
---|
627 | void * off; |
---|
628 | |
---|
629 | if (trace_get_link_type(packet) == TRACE_TYPE_NONDATA) |
---|
630 | return 0; |
---|
631 | |
---|
632 | max_size = FORMAT_DATA_OUT->req.tp_frame_size - |
---|
633 | TPACKET2_HDRLEN + sizeof(struct sockaddr_ll); |
---|
634 | |
---|
635 | header = (void *)FORMAT_DATA_OUT->tx_ring + |
---|
636 | (FORMAT_DATA_OUT->txring_offset * |
---|
637 | FORMAT_DATA_OUT->req.tp_frame_size); |
---|
638 | |
---|
639 | while(header->tp_status != TP_STATUS_AVAILABLE) { |
---|
640 | /* if none available: wait on more data */ |
---|
641 | pollset.fd = FORMAT_DATA_OUT->fd; |
---|
642 | pollset.events = POLLOUT; |
---|
643 | pollset.revents = 0; |
---|
644 | ret = poll(&pollset, 1, 1000); |
---|
645 | if (ret < 0 && errno != EINTR) { |
---|
646 | perror("poll"); |
---|
647 | return -1; |
---|
648 | } |
---|
649 | if(ret == 0) |
---|
650 | /* Timeout something has gone wrong - maybe the queue is |
---|
651 | * to large so try issue another send command |
---|
652 | */ |
---|
653 | ret = sendto(FORMAT_DATA_OUT->fd, |
---|
654 | NULL, |
---|
655 | 0, |
---|
656 | 0, |
---|
657 | (void *)&FORMAT_DATA_OUT->sock_hdr, |
---|
658 | sizeof(FORMAT_DATA_OUT->sock_hdr)); |
---|
659 | if (ret < 0) { |
---|
660 | trace_set_err_out(libtrace, errno, |
---|
661 | "sendto after timeout " |
---|
662 | "failed"); |
---|
663 | return -1; |
---|
664 | } |
---|
665 | } |
---|
666 | |
---|
667 | header->tp_len = trace_get_capture_length(packet); |
---|
668 | |
---|
669 | /* We cannot write the whole packet so just write part of it */ |
---|
670 | if (header->tp_len > max_size) |
---|
671 | header->tp_len = max_size; |
---|
672 | |
---|
673 | /* Fill packet - no sockaddr_ll in header when writing to the TX_RING */ |
---|
674 | off = ((void *)header) + (TPACKET2_HDRLEN - sizeof(struct sockaddr_ll)); |
---|
675 | memcpy(off, (char *)packet->payload, header->tp_len); |
---|
676 | |
---|
677 | /* 'Send it' and increase ring pointer to the next frame */ |
---|
678 | header->tp_status = TP_STATUS_SEND_REQUEST; |
---|
679 | FORMAT_DATA_OUT->txring_offset = (FORMAT_DATA_OUT->txring_offset + 1) % |
---|
680 | FORMAT_DATA_OUT->req.tp_frame_nr; |
---|
681 | |
---|
682 | /* Notify kernel there are frames to send */ |
---|
683 | FORMAT_DATA_OUT->queue ++; |
---|
684 | FORMAT_DATA_OUT->queue %= TX_MAX_QUEUE; |
---|
685 | if(FORMAT_DATA_OUT->queue == 0){ |
---|
686 | ret = sendto(FORMAT_DATA_OUT->fd, |
---|
687 | NULL, |
---|
688 | 0, |
---|
689 | MSG_DONTWAIT, |
---|
690 | (void *)&FORMAT_DATA_OUT->sock_hdr, |
---|
691 | sizeof(FORMAT_DATA_OUT->sock_hdr)); |
---|
692 | if (ret < 0) { |
---|
693 | trace_set_err_out(libtrace, errno, "sendto failed"); |
---|
694 | return -1; |
---|
695 | } |
---|
696 | } |
---|
697 | return header->tp_len; |
---|
698 | |
---|
699 | } |
---|
700 | |
---|
701 | static void linuxring_help(void) |
---|
702 | { |
---|
703 | printf("linuxring format module: $Revision: 1793 $\n"); |
---|
704 | printf("Supported input URIs:\n"); |
---|
705 | printf("\tring:eth0\n"); |
---|
706 | printf("\n"); |
---|
707 | printf("Supported output URIs:\n"); |
---|
708 | printf("\tring:eth0\n"); |
---|
709 | printf("\n"); |
---|
710 | return; |
---|
711 | } |
---|
712 | |
---|
713 | static struct libtrace_format_t linuxring = { |
---|
714 | "ring", |
---|
715 | "$Id$", |
---|
716 | TRACE_FORMAT_LINUX_RING, |
---|
717 | linuxcommon_probe_filename, /* probe filename */ |
---|
718 | NULL, /* probe magic */ |
---|
719 | linuxcommon_init_input, /* init_input */ |
---|
720 | linuxcommon_config_input, /* config_input */ |
---|
721 | linuxring_start_input, /* start_input */ |
---|
722 | linuxcommon_pause_input, /* pause_input */ |
---|
723 | linuxcommon_init_output, /* init_output */ |
---|
724 | NULL, /* config_output */ |
---|
725 | linuxring_start_output, /* start_ouput */ |
---|
726 | linuxcommon_fin_input, /* fin_input */ |
---|
727 | linuxring_fin_output, /* fin_output */ |
---|
728 | linuxring_read_packet, /* read_packet */ |
---|
729 | linuxring_prepare_packet, /* prepare_packet */ |
---|
730 | linuxring_fin_packet, /* fin_packet */ |
---|
731 | linuxring_write_packet, /* write_packet */ |
---|
732 | linuxring_get_link_type, /* get_link_type */ |
---|
733 | linuxring_get_direction, /* get_direction */ |
---|
734 | linuxring_set_direction, /* set_direction */ |
---|
735 | NULL, /* get_erf_timestamp */ |
---|
736 | linuxring_get_timeval, /* get_timeval */ |
---|
737 | linuxring_get_timespec, /* get_timespec */ |
---|
738 | NULL, /* get_seconds */ |
---|
739 | NULL, /* seek_erf */ |
---|
740 | NULL, /* seek_timeval */ |
---|
741 | NULL, /* seek_seconds */ |
---|
742 | linuxring_get_capture_length, /* get_capture_length */ |
---|
743 | linuxring_get_wire_length, /* get_wire_length */ |
---|
744 | linuxring_get_framing_length, /* get_framing_length */ |
---|
745 | linuxring_set_capture_length, /* set_capture_length */ |
---|
746 | NULL, /* get_received_packets */ |
---|
747 | NULL, /* get_filtered_packets */ |
---|
748 | NULL, /* get_dropped_packets */ |
---|
749 | linuxcommon_get_statistics, /* get_statistics */ |
---|
750 | linuxcommon_get_fd, /* get_fd */ |
---|
751 | linuxring_event, /* trace_event */ |
---|
752 | linuxring_help, /* help */ |
---|
753 | NULL, /* next pointer */ |
---|
754 | {true, -1}, /* Live, no thread limit */ |
---|
755 | linuxring_pstart_input, /* pstart_input */ |
---|
756 | linuxring_pread_packets, /* pread_packets */ |
---|
757 | linuxcommon_pause_input, /* ppause */ |
---|
758 | linuxcommon_fin_input, /* p_fin */ |
---|
759 | linuxcommon_pregister_thread, /* register thread */ |
---|
760 | NULL, /* unregister thread */ |
---|
761 | NULL /* get thread stats */ |
---|
762 | }; |
---|
763 | #else /* HAVE_NETPACKET_PACKET_H */ |
---|
764 | |
---|
765 | static void linuxring_help(void) |
---|
766 | { |
---|
767 | printf("linuxring format module: $Revision: 1793 $\n"); |
---|
768 | printf("Not supported on this host\n"); |
---|
769 | } |
---|
770 | |
---|
771 | static struct libtrace_format_t linuxring = { |
---|
772 | "ring", |
---|
773 | "$Id$", |
---|
774 | TRACE_FORMAT_LINUX_RING, |
---|
775 | NULL, /* probe filename */ |
---|
776 | NULL, /* probe magic */ |
---|
777 | NULL, /* init_input */ |
---|
778 | NULL, /* config_input */ |
---|
779 | NULL, /* start_input */ |
---|
780 | NULL, /* pause_input */ |
---|
781 | NULL, /* init_output */ |
---|
782 | NULL, /* config_output */ |
---|
783 | NULL, /* start_ouput */ |
---|
784 | NULL, /* fin_input */ |
---|
785 | NULL, /* fin_output */ |
---|
786 | NULL, /* read_packet */ |
---|
787 | linuxring_prepare_packet, /* prepare_packet */ |
---|
788 | NULL, /* fin_packet */ |
---|
789 | NULL, /* write_packet */ |
---|
790 | linuxring_get_link_type, /* get_link_type */ |
---|
791 | linuxring_get_direction, /* get_direction */ |
---|
792 | linuxring_set_direction, /* set_direction */ |
---|
793 | NULL, /* get_erf_timestamp */ |
---|
794 | linuxring_get_timeval, /* get_timeval */ |
---|
795 | linuxring_get_timespec, /* get_timespec */ |
---|
796 | NULL, /* get_seconds */ |
---|
797 | NULL, /* seek_erf */ |
---|
798 | NULL, /* seek_timeval */ |
---|
799 | NULL, /* seek_seconds */ |
---|
800 | linuxring_get_capture_length, /* get_capture_length */ |
---|
801 | linuxring_get_wire_length, /* get_wire_length */ |
---|
802 | linuxring_get_framing_length, /* get_framing_length */ |
---|
803 | linuxring_set_capture_length, /* set_capture_length */ |
---|
804 | NULL, /* get_received_packets */ |
---|
805 | NULL, /* get_filtered_packets */ |
---|
806 | NULL, /* get_dropped_packets */ |
---|
807 | linuxcommon_get_statistics, /* get_statistics */ |
---|
808 | NULL, /* get_fd */ |
---|
809 | NULL, /* trace_event */ |
---|
810 | linuxring_help, /* help */ |
---|
811 | NULL, /* next pointer */ |
---|
812 | NON_PARALLEL(true) |
---|
813 | }; |
---|
814 | #endif /* HAVE_NETPACKET_PACKET_H */ |
---|
815 | |
---|
816 | /* TODO: Figure out how to give this format preference over the linux native |
---|
817 | * formate if the user only specifies an interface */ |
---|
818 | void linuxring_constructor(void) |
---|
819 | { |
---|
820 | register_format(&linuxring); |
---|
821 | } |
---|