source: lib/format_dag24.c @ 2193905

develop
Last change on this file since 2193905 was 2193905, checked in by Jacob Van Walraven <jcv9@…>, 2 years ago

Apply changes required for pull request #81

  • Property mode set to 100644
File size: 18.7 KB
Line 
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#define _GNU_SOURCE
27
28#include "config.h"
29#include "common.h"
30#include "libtrace.h"
31#include "libtrace_int.h"
32#include "format_helper.h"
33#include "format_erf.h"
34
35#include <errno.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <string.h>
39#include <stdlib.h>
40#include <sys/stat.h>
41
42#include <sys/mman.h>
43#ifdef WIN32
44#  include <io.h>
45#  include <share.h>
46#  define PATH_MAX _MAX_PATH
47#  define snprintf sprintf_s
48#else
49#  include <netdb.h>
50#  ifndef PATH_MAX
51#       define PATH_MAX 4096
52#  endif
53#  include <sys/ioctl.h>
54#endif
55
56/* This format deals with DAG cards that are using drivers from the 2.4.X
57 * versions.
58 *
59 * DAG is a LIVE capture format.
60 *
61 * We do not support writing using this format, as transmit support was not
62 * added until a subsequent version of the DAG software (see format_dag25.c).
63 * Instead, you should write the packets read using this format as ERF traces.
64 */
65
66static struct libtrace_format_t dag;
67
68#define DATA(x) ((struct dag_format_data_t *)x->format_data)
69#define DUCK DATA(libtrace)->duck
70#define FORMAT_DATA DATA(libtrace)
71
72/* "Global" data that is stored for each DAG input trace */
73struct dag_format_data_t {
74
75        /* Data required for regular DUCK reporting */
76        struct {
77                /* Timestamp of the last DUCK report */
78                uint32_t last_duck;
79                /* The number of seconds between each DUCK report */
80                uint32_t duck_freq;
81                /* Timestamp of the last packet read from the DAG card */
82                uint32_t last_pkt;
83                /* Dummy trace to ensure DUCK packets are dealt with using
84                 * the DUCK format functions */
85                libtrace_t *dummy_duck;
86        } duck; 
87       
88        /* File descriptor for the DAG card */
89        int fd;
90        /* Pointer to DAG memory hole */
91        void *buf;
92        /* Difference between the top and bottom pointers in the DAG memory
93         * hole, i.e. the amount of available data to read */
94        uint32_t diff;
95        /* The amount of data read thus far from the start of the bottom
96         * pointer */
97        uint32_t offset;
98        /* The offset for the first unread byte in the DAG memory hole */
99        uint32_t bottom;
100        /* The offset for the last unread byte in the DAG memory hole */
101        uint32_t top;
102        /* The number of packets that have been dropped */
103        uint64_t drops;
104};
105
106/* Determines if a given filename refers to a DAG device */
107static void dag_probe_filename(const char *filename) 
108{
109        struct stat statbuf;
110        /* Can we stat the file? */
111        if (stat(filename, &statbuf) != 0) {
112                return 0;
113        }
114        /* Is it a character device? */
115        if (!S_ISCHR(statbuf.st_mode)) {
116                return 0;
117        }
118        /* Yeah, it's probably us. */
119        return 1;
120}
121
122/* Initialises the DAG "global" variables */
123static void dag_init_format_data(libtrace_t *libtrace) {
124        libtrace->format_data = (struct dag_format_data_t *)
125                malloc(sizeof(struct dag_format_data_t));
126
127        DUCK.last_duck = 0;
128        DUCK.duck_freq = 0;
129        DUCK.last_pkt = 0;
130        DUCK.dummy_duck = NULL;
131        FORMAT_DATA->drops = 0;
132        FORMAT_DATA->top = 0;
133        FORMAT_DATA->bottom = 0;
134        FORMAT_DATA->buf = NULL;
135        FORMAT_DATA->fd = -1;
136        FORMAT_DATA->offset = 0;
137        FORMAT_DATA->diff = 0;
138}
139
140/* Determines how much data is available for reading on the DAG card and
141 * updates the various offsets accordingly */
142static int dag_available(libtrace_t *libtrace) {
143
144        if (FORMAT_DATA->diff > 0)
145                return FORMAT_DATA->diff;
146
147        FORMAT_DATA->bottom = FORMAT_DATA->top;
148        FORMAT_DATA->top = dag_offset(
149                        FORMAT_DATA->fd,
150                        &(FORMAT_DATA->bottom),
151                        DAGF_NONBLOCK);
152        FORMAT_DATA->diff = FORMAT_DATA->top - FORMAT_DATA->bottom;
153        FORMAT_DATA->offset = 0;
154        return FORMAT_DATA->diff;
155}
156
157/* Initialises a DAG input trace */
158static int dag_init_input(libtrace_t *libtrace) {
159        struct stat buf;
160        char *dag_dev_name = NULL;
161        char *scan = NULL;
162
163        /* Since DAG 2.5 has been changed to support a slightly different URI
164         * format, it's probably a good idea to deal with URIs specified in
165         * such a fashion even if we just end up ignoring the stream number */
166        if ((scan = strchr(libtrace->uridata,',')) == NULL) {
167                dag_dev_name = strdup(libtrace->uridata);
168        } else {
169                dag_dev_name = (char *)strndup(libtrace->uridata,
170                                (size_t)(scan - libtrace->uridata));
171        }
172
173
174        /* Make sure a DAG device with the right name exists */ 
175        if (stat(dag_dev_name, &buf) == -1) {
176                trace_set_err(libtrace,errno,"stat(%s)",dag_dev_name);
177                free(dag_dev_name);
178                return -1;
179        }
180
181        dag_init_format_data(libtrace);
182        if (S_ISCHR(buf.st_mode)) {
183                /* DEVICE */
184                if((FORMAT_DATA->fd = dag_open(dag_dev_name)) < 0) {
185                        trace_set_err(libtrace,errno,"Cannot open DAG %s",
186                                        dag_dev_name);
187                        free(dag_dev_name);
188                        return -1;
189                }
190
191                /* Memory-map ourselves a pointer to the DAG memory hole */
192                if((FORMAT_DATA->buf = (void *)dag_mmap(FORMAT_DATA->fd)) == MAP_FAILED) {
193                        trace_set_err(libtrace,errno,"Cannot mmap DAG %s",
194                                        dag_dev_name);
195                        free(dag_dev_name);
196                        return -1;
197                }
198        } else {
199                trace_set_err(libtrace,errno,"Not a valid dag device: %s",
200                                dag_dev_name);
201                free(dag_dev_name);
202                return -1;
203        }
204
205        free(dag_dev_name);
206
207        return 0;
208}
209
210/* Configures a DAG input trace */
211static int dag_config_input(libtrace_t *libtrace, trace_option_t option,
212                                void *data) {
213        switch(option) {
214                case TRACE_OPTION_META_FREQ:
215                        /* We use this option to specify the frequency of
216                         * DUCK updates */
217                        DUCK.duck_freq = *(int *)data;
218                        return 0;
219                case TRACE_OPTION_SNAPLEN:
220                        /* Surely we can set this?? Fall through for now*/
221                        return -1;
222                case TRACE_OPTION_PROMISC:
223                        /* DAG already operates in a promisc fashion */
224                        return -1;
225                case TRACE_OPTION_FILTER:
226                        /* Cards that use the older drivers don't do
227                         * filtering */
228                        return -1;
229                case TRACE_OPTION_EVENT_REALTIME:
230                        /* Live capture is always going to be realtime */
231                        return -1;
232        }
233        return -1;
234}
235
236/* Starts a DAG input trace */
237static int dag_start_input(libtrace_t *libtrace) {     
238        if(dag_start(FORMAT_DATA->fd) < 0) {
239                trace_set_err(libtrace,errno,"Cannot start DAG %s",
240                                libtrace->uridata);
241                return -1;
242        }
243
244        /* Flush the memory hole */
245        while(dag_available(libtrace) != 0)
246                FORMAT_DATA->diff = 0;
247        FORMAT_DATA->drops = 0;
248        return 0;
249}
250
251/* Pauses a DAG input trace */
252static int dag_pause_input(libtrace_t *libtrace) {
253        dag_stop(FORMAT_DATA->fd);
254        return 0;
255}
256
257/* Destroys a DAG input trace */
258static int dag_fin_input(libtrace_t *libtrace) {
259        dag_close(FORMAT_DATA->fd);
260        if (DUCK.dummy_duck)
261                trace_destroy_dead(DUCK.dummy_duck);
262        free(libtrace->format_data);
263        return 0; /* success */
264}
265
266/* Extracts DUCK information from the DAG card and produces a DUCK packet */
267static int dag_get_duckinfo(libtrace_t *libtrace,
268                                libtrace_packet_t *packet) {
269        dag_inf lt_dag_inf;
270
271        /* Allocate memory for the DUCK data */
272        if (packet->buf_control == TRACE_CTRL_EXTERNAL ||
273                        !packet->buffer) {
274                packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE);
275                packet->buf_control = TRACE_CTRL_PACKET;
276                if (!packet->buffer) {
277                        trace_set_err(libtrace, errno,
278                                        "Cannot allocate packet buffer");
279                        return -1;
280                }
281        }
282
283        /* DUCK doesn't actually have a format header, as such */
284        packet->header = 0;
285        packet->payload = packet->buffer;
286
287        /* Check that the DAG card supports DUCK */
288        if ((ioctl(FORMAT_DATA->fd, DAG_IOINF, &lt_dag_inf) < 0)) {
289                trace_set_err(libtrace, errno,
290                                "Error using DAG_IOINF");
291                return -1;
292        }
293        if (!IsDUCK(&lt_dag_inf)) {
294                printf("WARNING: %s does not have modern clock support - No DUCK information will be gathered\n", libtrace->uridata);
295                return 0;
296        }
297
298        /* Get the DUCK information from the card */
299        if ((ioctl(FORMAT_DATA->fd, DAG_IOGETDUCK, (duck_inf *)packet->payload)
300                                < 0)) {
301                trace_set_err(libtrace, errno, "Error using DAG_IOGETDUCK");
302                return -1;
303        }
304
305        /* Set the type */
306        packet->type = TRACE_RT_DUCK_2_4;
307
308        /* Set the packet's trace to point at a DUCK trace, so that the
309         * DUCK format functions will be called on the packet rather than the
310         * DAG ones */
311        if (!DUCK.dummy_duck)
312                DUCK.dummy_duck = trace_create_dead("duck:dummy");
313        packet->trace = DUCK.dummy_duck;
314        return sizeof(duck_inf);
315}
316
317/* Reads the next ERF record from the DAG memory hole */
318static dag_record_t *dag_get_record(libtrace_t *libtrace) {
319        dag_record_t *erfptr = NULL;
320        uint16_t size;
321        erfptr = (dag_record_t *) ((char *)FORMAT_DATA->buf + 
322                        (FORMAT_DATA->bottom + FORMAT_DATA->offset));
323
324        if (!erfptr)
325                return NULL;
326        size = ntohs(erfptr->rlen);
327        if (size < dag_record_size) {
328                fprintf(stderr, "DAG2.4 rlen is invalid (rlen = %u, must be at least %u)\n",
329                        size, dag_record_size);
330                return NULL;
331        }
332        FORMAT_DATA->offset += size;
333        FORMAT_DATA->diff -= size;
334        return erfptr;
335}
336
337/* Converts a buffer containing a recently read DAG packet record into a
338 * libtrace packet */
339static int dag_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet,
340                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) {
341
342        dag_record_t *erfptr;
343        /* If the packet previously owned a buffer that is not the buffer
344         * that contains the new packet data, we're going to need to free the
345         * old one to avoid memory leaks */
346        if (packet->buffer != buffer &&
347                        packet->buf_control == TRACE_CTRL_PACKET) {
348                free(packet->buffer);
349        }
350
351        /* Set the buffer owner appropriately */
352        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) {
353                packet->buf_control = TRACE_CTRL_PACKET;
354        } else 
355                packet->buf_control = TRACE_CTRL_EXTERNAL;
356       
357        /* Update packet pointers and type appropriately */
358        erfptr = (dag_record_t *)buffer;
359        packet->buffer = erfptr;
360        packet->header = erfptr;
361        packet->type = rt_type;
362
363        if (erfptr->flags.rxerror == 1) {
364                /* rxerror means the payload is corrupt - drop the payload
365                 * by tweaking rlen */
366                packet->payload = NULL;
367                erfptr->rlen = htons(erf_get_framing_length(packet));
368        } else {
369                packet->payload = (char*)packet->buffer
370                        + erf_get_framing_length(packet);
371        }
372
373        if (libtrace->format_data == NULL) {
374                dag_init_format_data(libtrace);
375        }
376
377        /* Update the dropped packets counter, using the value of the ERF
378         * loss counter */
379        DATA(libtrace)->drops += ntohs(erfptr->lctr);
380
381        return 0;
382
383}
384
385/* Reads the next available packet from a DAG card, in a BLOCKING fashion
386 *
387 * If DUCK reporting is enabled, the packet returned may be a DUCK update */
388static int dag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
389        int numbytes;
390        int size = 0;
391        uint32_t flags = 0;
392        struct timeval tv;
393        dag_record_t *erfptr = NULL;
394
395        /* Check if we're due for a DUCK report */
396        if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq &&
397                        DUCK.duck_freq != 0) {
398                size = dag_get_duckinfo(libtrace, packet);
399                DUCK.last_duck = DUCK.last_pkt;
400                if (size != 0) {
401                        return size;
402                }
403                /* No DUCK support, so don't waste our time anymore */
404                DUCK.duck_freq = 0;
405        }
406
407        /* Don't let anyone try to free our DAG memory hole */
408        flags |= TRACE_PREP_DO_NOT_OWN_BUFFER;
409       
410        /* If the packet buffer is currently owned by libtrace, free it so
411         * that we can set the packet to point into the DAG memory hole */
412        if (packet->buf_control == TRACE_CTRL_PACKET) {
413                packet->buf_control = TRACE_CTRL_EXTERNAL;
414                free(packet->buffer);
415                packet->buffer = 0;
416        }
417
418        /* Grab a full ERF record */
419        do {
420                numbytes = dag_available(libtrace);
421                if (numbytes < 0)
422                        return numbytes;
423                if (numbytes == 0)
424                        continue;
425                erfptr = dag_get_record(libtrace);
426        } while (erfptr == NULL);
427       
428        /* Prepare the libtrace packet */
429        if (dag_prepare_packet(libtrace, packet, erfptr, TRACE_RT_DATA_ERF, 
430                                flags))
431                return -1;
432       
433        /* Update the DUCK timer */
434        tv = trace_get_timeval(packet);
435        DUCK.last_pkt = tv.tv_sec;
436        return packet->payload ? htons(erfptr->rlen) : erf_get_framing_length(packet);
437}
438
439/* Attempts to read a packet from a DAG card in a NON-BLOCKING fashion. If
440 * a packet is available, we will return a packet event. Otherwise we will
441 * return a SLEEP event (as we cannot select on the DAG file descriptor).
442 */
443static libtrace_eventobj_t trace_event_dag(libtrace_t *trace,
444                                        libtrace_packet_t *packet) {
445        libtrace_eventobj_t event = {0,0,0.0,0};
446        int data;
447
448        do {
449                data = dag_available(trace);
450
451                /* If no data is available, drop out and return a sleep event */
452                if (data <= 0)
453                        break;
454
455                /* Data is available, so we can call the blocking read because
456                 * we know that we will get a packet straight away */
457                event.size = dag_read_packet(trace,packet);
458                //DATA(trace)->dag.diff -= event.size;
459               
460                /* XXX trace_read_packet() normally applies the following
461                 * config options for us, but this function is called via
462                 * trace_event() so we have to do it ourselves */
463
464                /* Check that the packet matches any pre-existing filter */
465                if (trace->filter) {
466                        if (trace_apply_filter(trace->filter, packet)) {
467                                event.type = TRACE_EVENT_PACKET;
468                        } else {
469                                /* Do not sleep - try to read another packet */
470                                trace->filtered_packets ++;
471                                continue;
472                        }
473                } else {
474                        event.type = TRACE_EVENT_PACKET;
475                }
476
477                /* If the user has specified a snap length, apply that too */
478                if (trace->snaplen > 0) {
479                        trace_set_capture_length(packet, trace->snaplen);
480                }
481                trace->accepted_packets ++;
482                return event;
483        } while (1);
484
485        event.type = TRACE_EVENT_SLEEP;
486        event.seconds = 0.0001;
487        event.size = 0;
488        return event;
489}
490
491/* Gets the number of dropped packets */
492static uint64_t dag_get_dropped_packets(libtrace_t *trace)
493{
494        if (!trace->format_data)
495                return (uint64_t)-1;
496        return DATA(trace)->drops;
497}
498
499/* Prints some semi-useful help text about the DAG format module */
500static void dag_help(void) {
501        printf("dag format module: $Revision: 1715 $\n");
502        printf("Supported input URIs:\n");
503        printf("\tdag:/dev/dagn\n");
504        printf("\n");
505        printf("\te.g.: dag:/dev/dag0\n");
506        printf("\n");
507        printf("Supported output URIs:\n");
508        printf("\tnone\n");
509        printf("\n");
510}
511
512static struct libtrace_format_t dag = {
513        "dag",
514        "$Id$",
515        TRACE_FORMAT_ERF,
516        dag_probe_filename,             /* probe filename */
517        NULL,                           /* probe magic */
518        dag_init_input,                 /* init_input */
519        dag_config_input,               /* config_input */
520        dag_start_input,                /* start_input */
521        dag_pause_input,                /* pause_input */
522        NULL,                           /* init_output */
523        NULL,                           /* config_output */
524        NULL,                           /* start_output */
525        dag_fin_input,                  /* fin_input */
526        NULL,                           /* fin_output */
527        dag_read_packet,                /* read_packet */
528        dag_prepare_packet,             /* prepare_packet */
529        NULL,                           /* fin_packet */
530        NULL,                           /* write_packet */
531        NULL,                           /* flush_output */
532        erf_get_link_type,              /* get_link_type */
533        erf_get_direction,              /* get_direction */
534        erf_set_direction,              /* set_direction */
535        erf_get_erf_timestamp,          /* get_erf_timestamp */
536        NULL,                           /* get_timeval */
537        NULL,                           /* get_timespec */
538        NULL,                           /* get_seconds */
539        NULL,                           /* seek_erf */
540        NULL,                           /* seek_timeval */
541        NULL,                           /* seek_seconds */
542        erf_get_capture_length,         /* get_capture_length */
543        erf_get_wire_length,            /* get_wire_length */
544        erf_get_framing_length,         /* get_framing_length */
545        erf_set_capture_length,         /* set_capture_length */
546        NULL,                           /* get_received_packets */
547        NULL,                           /* get_filtered_packets */
548        dag_get_dropped_packets,        /* get_dropped_packets */
549        NULL,                           /* get_statistics */
550        NULL,                           /* get_fd */
551        trace_event_dag,                /* trace_event */
552        dag_help,                       /* help */
553        NULL,                            /* next pointer */
554    NON_PARALLEL(true)
555};
556
557void dag_constructor(void) {
558        register_format(&dag);
559}
Note: See TracBrowser for help on using the repository browser.