source: lib/format_dag25.c @ 43c00e5

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 43c00e5 was 43c00e5, checked in by Shane Alcock <salcock@…>, 12 years ago
  • Updated documentation and licensing on DAG 2.5 format module
  • Also removed a few variables from the output format data that were not being used for anything important
  • Property mode set to 100644
File size: 36.1 KB
Line 
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 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33
34#define _GNU_SOURCE
35
36#include "config.h"
37#include "common.h"
38#include "libtrace.h"
39#include "libtrace_int.h"
40#include "format_helper.h"
41#include "format_erf.h"
42
43#include <assert.h>
44#include <errno.h>
45#include <fcntl.h>
46#include <stdio.h>
47#include <string.h>
48#include <stdlib.h>
49
50#include <sys/mman.h>
51/* XXX: Windows doesn't have pthreads, but this code doesn't compile under
52 * Windows anyway so we'll worry about this more later :] */
53#include <pthread.h>
54
55
56#ifdef WIN32
57#  include <io.h>
58#  include <share.h>
59#  define PATH_MAX _MAX_PATH
60#  define snprintf sprintf_s
61#else
62#  include <netdb.h>
63#  ifndef PATH_MAX
64#       define PATH_MAX 4096
65#  endif
66#  include <sys/ioctl.h>
67#endif
68
69/* This format deals with DAG cards that are using drivers from the 2.5 version
70 * onwards, including 3.X.
71 *
72 * DAG is a LIVE capture format.
73 *
74 * This format does support writing, provided the DAG card that you are using
75 * has transmit (Tx) support. Additionally, packets read using this format
76 * are in the ERF format, so can easily be written as ERF traces without
77 * losing any data.
78 */
79
80
81#define DATA(x) ((struct dag_format_data_t *)x->format_data)
82#define DATA_OUT(x) ((struct dag_format_data_out_t *)x->format_data)
83
84#define FORMAT_DATA DATA(libtrace)
85#define FORMAT_DATA_OUT DATA_OUT(libtrace)
86
87#define DUCK FORMAT_DATA->duck
88static struct libtrace_format_t dag;
89
90/* A DAG device - a DAG device can support multiple streams (and therefore
91 * multiple input traces) so each trace needs to refer to a device */
92struct dag_dev_t {
93        char * dev_name;                /* Device name */
94        int fd;                         /* File descriptor */
95        uint16_t ref_count;             /* Number of input / output traces
96                                           that are using this device */
97        struct dag_dev_t *prev;         /* Pointer to the previous device in
98                                           the device list */
99        struct dag_dev_t *next;         /* Pointer to the next device in the
100                                           device list */
101};
102
103/* "Global" data that is stored for each DAG output trace */
104struct dag_format_data_out_t {
105        /* The DAG device being used for writing */
106        struct dag_dev_t *device;
107        /* The DAG stream that is being written on */
108        unsigned int dagstream;
109        /* Boolean flag indicating whether the stream is currently attached */
110        int stream_attached;
111        /* The amount of data waiting to be transmitted, in bytes */
112        uint64_t waiting;
113        /* A buffer to hold the data to be transmittted */
114        uint8_t *txbuffer;
115};
116
117/* "Global" data that is stored for each DAG input trace */
118struct dag_format_data_t {
119
120        /* Data required for regular DUCK reporting */
121        struct {
122                /* Timestamp of the last DUCK report */
123                uint32_t last_duck;
124                /* The number of seconds between each DUCK report */
125                uint32_t duck_freq;
126                /* Timestamp of the last packet read from the DAG card */
127                uint32_t last_pkt;
128                /* Dummy trace to ensure DUCK packets are dealt with using the
129                 * DUCK format functions */
130                libtrace_t *dummy_duck;
131        } duck;
132
133        /* The DAG device that we are reading from */
134        struct dag_dev_t *device;
135        /* The DAG stream that we are reading from */
136        unsigned int dagstream;
137        /* Boolean flag indicating whether the stream is currently attached */
138        int stream_attached;
139        /* Pointer to the first unread byte in the DAG memory hole */
140        uint8_t *bottom;
141        /* Pointer to the last unread byte in the DAG memory hole */
142        uint8_t *top;
143        /* The amount of data processed thus far from the bottom pointer */
144        uint32_t processed;
145        /* The number of packets that have been dropped */
146        uint64_t drops;
147};
148
149/* To be thread-safe, we're going to need a mutex for operating on the list
150 * of DAG devices */
151pthread_mutex_t open_dag_mutex;
152
153/* The list of DAG devices that have been opened by libtrace.
154 *
155 * We can only open each DAG device once, but we might want to read from
156 * multiple streams. Therefore, we need to maintain a list of devices that we
157 * have opened (with ref counts!) so that we don't try to open a device too
158 * many times or close a device that we're still using */
159struct dag_dev_t *open_dags = NULL;
160
161/* Returns the amount of padding between the ERF header and the start of the
162 * captured packet data */
163static int dag_get_padding(const libtrace_packet_t *packet)
164{
165        /* ERF Ethernet records have a 2 byte padding before the packet itself
166         * so that the IP header is aligned on a 32 bit boundary.
167         */
168        if (packet->trace->format->type==TRACE_FORMAT_ERF) {
169                dag_record_t *erfptr = (dag_record_t *)packet->header;
170                switch(erfptr->type) {
171                        case TYPE_ETH:
172                        case TYPE_DSM_COLOR_ETH:
173                                return 2;
174                        default:                return 0;
175                }
176        }
177        else {
178                switch(trace_get_link_type(packet)) {
179                        case TRACE_TYPE_ETH:    return 2;
180                        default:                return 0;
181                }
182        }
183}
184
185/* Attempts to determine if the given filename refers to a DAG device */
186static int dag_probe_filename(const char *filename)
187{
188        struct stat statbuf;
189        /* Can we stat the file? */
190        if (stat(filename, &statbuf) != 0) {
191                return 0;
192        }
193        /* Is it a character device? */
194        if (!S_ISCHR(statbuf.st_mode)) {
195                return 0;
196        }
197        /* Yeah, it's probably us. */
198        return 1;
199}
200
201/* Initialises the DAG output data structure */
202static void dag_init_format_out_data(libtrace_out_t *libtrace) {
203        libtrace->format_data = (struct dag_format_data_out_t *) malloc(sizeof(struct dag_format_data_out_t));
204        // no DUCK on output
205        FORMAT_DATA_OUT->stream_attached = 0;
206        FORMAT_DATA_OUT->device = NULL;
207        FORMAT_DATA_OUT->dagstream = 0;
208        FORMAT_DATA_OUT->waiting = 0;
209
210}
211
212/* Initialises the DAG input data structure */
213static void dag_init_format_data(libtrace_t *libtrace) {
214        libtrace->format_data = (struct dag_format_data_t *)
215                malloc(sizeof(struct dag_format_data_t));
216        DUCK.last_duck = 0;
217        DUCK.duck_freq = 0;
218        DUCK.last_pkt = 0;
219        DUCK.dummy_duck = NULL;
220        FORMAT_DATA->stream_attached = 0;
221        FORMAT_DATA->drops = 0;
222        FORMAT_DATA->device = NULL;
223        FORMAT_DATA->dagstream = 0;
224        FORMAT_DATA->processed = 0;
225        FORMAT_DATA->bottom = NULL;
226        FORMAT_DATA->top = NULL;
227}
228
229/* Determines if there is already an entry for the given DAG device in the
230 * device list and increments the reference count for that device, if found.
231 *
232 * NOTE: This function assumes the open_dag_mutex is held by the caller */
233static struct dag_dev_t *dag_find_open_device(char *dev_name) {
234        struct dag_dev_t *dag_dev;
235
236        dag_dev = open_dags;
237
238        /* XXX: Not exactly zippy, but how often are we going to be dealing
239         * with multiple dag cards? */
240        while (dag_dev != NULL) {
241                if (strcmp(dag_dev->dev_name, dev_name) == 0) {
242                        dag_dev->ref_count ++;
243                        return dag_dev;
244
245                }
246                dag_dev = dag_dev->next;
247        }
248        return NULL;
249
250
251}
252
253/* Closes a DAG device and removes it from the device list.
254 *
255 * Attempting to close a DAG device that has a non-zero reference count will
256 * cause an assertion failure!
257 *
258 * NOTE: This function assumes the open_dag_mutex is held by the caller */
259static void dag_close_device(struct dag_dev_t *dev) {
260        /* Need to remove from the device list */
261
262        assert(dev->ref_count == 0);
263
264        if (dev->prev == NULL) {
265                open_dags = dev->next;
266                if (dev->next)
267                        dev->next->prev = NULL;
268        } else {
269                dev->prev->next = dev->next;
270                if (dev->next)
271                        dev->next->prev = dev->prev;
272        }
273
274        dag_close(dev->fd);
275        if (dev->dev_name)
276        free(dev->dev_name);
277        free(dev);
278}
279
280
281/* Opens a new DAG device for writing and adds it to the DAG device list
282 *
283 * NOTE: this function should only be called when opening a DAG device for
284 * writing - there is little practical difference between this and the
285 * function below that covers the reading case, but we need the output trace
286 * object to report errors properly so the two functions take slightly
287 * different arguments. This is really lame and there should be a much better
288 * way of doing this.
289 *
290 * NOTE: This function assumes the open_dag_mutex is held by the caller
291 */
292static struct dag_dev_t *dag_open_output_device(libtrace_out_t *libtrace, char *dev_name) {
293        struct stat buf;
294        int fd;
295        struct dag_dev_t *new_dev;
296
297        /* Make sure the device exists */
298        if (stat(dev_name, &buf) == -1) {
299                trace_set_err_out(libtrace,errno,"stat(%s)",dev_name);
300                return NULL;
301}
302
303        /* Make sure it is the appropriate type of device */
304        if (S_ISCHR(buf.st_mode)) {
305                /* Try opening the DAG device */
306                if((fd = dag_open(dev_name)) < 0) {
307                        trace_set_err_out(libtrace,errno,"Cannot open DAG %s",
308                                        dev_name);
309                        return NULL;
310                }
311        } else {
312                trace_set_err_out(libtrace,errno,"Not a valid dag device: %s",
313                                dev_name);
314                return NULL;
315        }
316
317        /* Add the device to our device list - it is just a doubly linked
318         * list with no inherent ordering; just tack the new one on the front
319         */
320        new_dev = (struct dag_dev_t *)malloc(sizeof(struct dag_dev_t));
321        new_dev->fd = fd;
322        new_dev->dev_name = dev_name;
323        new_dev->ref_count = 1;
324
325        new_dev->prev = NULL;
326        new_dev->next = open_dags;
327        if (open_dags)
328                open_dags->prev = new_dev;
329
330        open_dags = new_dev;
331
332        return new_dev;
333}
334
335/* Opens a new DAG device for reading and adds it to the DAG device list
336 *
337 * NOTE: this function should only be called when opening a DAG device for
338 * reading - there is little practical difference between this and the
339 * function above that covers the writing case, but we need the input trace
340 * object to report errors properly so the two functions take slightly
341 * different arguments. This is really lame and there should be a much better
342 * way of doing this.
343 *
344 * NOTE: This function assumes the open_dag_mutex is held by the caller */
345static struct dag_dev_t *dag_open_device(libtrace_t *libtrace, char *dev_name) {
346        struct stat buf;
347        int fd;
348        struct dag_dev_t *new_dev;
349
350        /* Make sure the device exists */
351        if (stat(dev_name, &buf) == -1) {
352                trace_set_err(libtrace,errno,"stat(%s)",dev_name);
353                return NULL;
354        }
355
356        /* Make sure it is the appropriate type of device */
357        if (S_ISCHR(buf.st_mode)) {
358                /* Try opening the DAG device */
359                if((fd = dag_open(dev_name)) < 0) {
360                        trace_set_err(libtrace,errno,"Cannot open DAG %s",
361                                        dev_name);
362                        return NULL;
363                }
364        } else {
365                trace_set_err(libtrace,errno,"Not a valid dag device: %s",
366                                dev_name);
367                return NULL;
368        }
369
370        /* Add the device to our device list - it is just a doubly linked
371         * list with no inherent ordering; just tack the new one on the front
372         */
373        new_dev = (struct dag_dev_t *)malloc(sizeof(struct dag_dev_t));
374        new_dev->fd = fd;
375        new_dev->dev_name = dev_name;
376        new_dev->ref_count = 1;
377
378        new_dev->prev = NULL;
379        new_dev->next = open_dags;
380        if (open_dags)
381                open_dags->prev = new_dev;
382
383        open_dags = new_dev;
384
385        return new_dev;
386}
387
388/* Creates and initialises a DAG output trace */
389static int dag_init_output(libtrace_out_t *libtrace) {
390        char *dag_dev_name = NULL;
391        char *scan = NULL;
392        struct dag_dev_t *dag_device = NULL;
393        int stream = 1;
394       
395        /* XXX I don't know if this is important or not, but this function
396         * isn't present in all of the driver releases that this code is
397         * supposed to support! */
398        /*
399        unsigned long wake_time;
400        dagutil_sleep_get_wake_time(&wake_time,0);
401        */
402
403        dag_init_format_out_data(libtrace);
404        /* Grab the mutex while we're likely to be messing with the device
405         * list */
406        pthread_mutex_lock(&open_dag_mutex);
407       
408        /* Specific streams are signified using a comma in the libtrace URI,
409         * e.g. dag:/dev/dag0,1 refers to stream 1 on the dag0 device.
410         *
411         * If no stream is specified, we will write using stream 1 */
412        if ((scan = strchr(libtrace->uridata,',')) == NULL) {
413                dag_dev_name = strdup(libtrace->uridata);
414        } else {
415                dag_dev_name = (char *)strndup(libtrace->uridata,
416                                (size_t)(scan - libtrace->uridata));
417                stream = atoi(++scan);
418        }
419        FORMAT_DATA_OUT->dagstream = stream;
420
421        /* See if our DAG device is already open */
422        dag_device = dag_find_open_device(dag_dev_name);
423
424        if (dag_device == NULL) {
425                /* Device not yet opened - open it ourselves */
426                dag_device = dag_open_output_device(libtrace, dag_dev_name);
427        } else {
428                /* Otherwise, just use the existing one */
429                free(dag_dev_name);
430                dag_dev_name = NULL;
431        }
432
433        /* Make sure we have successfully opened a DAG device */
434        if (dag_device == NULL) {
435                if (dag_dev_name) {
436                        free(dag_dev_name);
437                }
438                pthread_mutex_unlock(&open_dag_mutex);
439                return -1;
440        }
441
442        FORMAT_DATA_OUT->device = dag_device;
443        pthread_mutex_unlock(&open_dag_mutex);
444        return 0;
445}
446
447/* Creates and initialises a DAG input trace */
448static int dag_init_input(libtrace_t *libtrace) {
449        char *dag_dev_name = NULL;
450        char *scan = NULL;
451        int stream = 0;
452        struct dag_dev_t *dag_device = NULL;
453
454        dag_init_format_data(libtrace);
455        /* Grab the mutex while we're likely to be messing with the device
456         * list */
457        pthread_mutex_lock(&open_dag_mutex);
458       
459       
460        /* Specific streams are signified using a comma in the libtrace URI,
461         * e.g. dag:/dev/dag0,2 refers to stream 2 on the dag0 device.
462         *
463         * If no stream is specified, we will read from stream 0 */
464        if ((scan = strchr(libtrace->uridata,',')) == NULL) {
465                dag_dev_name = strdup(libtrace->uridata);
466        } else {
467                dag_dev_name = (char *)strndup(libtrace->uridata,
468                                (size_t)(scan - libtrace->uridata));
469                stream = atoi(++scan);
470        }
471
472        FORMAT_DATA->dagstream = stream;
473
474        /* See if our DAG device is already open */
475        dag_device = dag_find_open_device(dag_dev_name);
476
477        if (dag_device == NULL) {
478                /* Device not yet opened - open it ourselves */
479                dag_device = dag_open_device(libtrace, dag_dev_name);
480        } else {
481                /* Otherwise, just use the existing one */
482                free(dag_dev_name);
483                dag_dev_name = NULL;
484        }
485
486        /* Make sure we have successfully opened a DAG device */
487        if (dag_device == NULL) {
488                if (dag_dev_name)
489                        free(dag_dev_name);
490                dag_dev_name = NULL;
491                pthread_mutex_unlock(&open_dag_mutex);
492                return -1;
493        }
494
495        FORMAT_DATA->device = dag_device;
496
497        pthread_mutex_unlock(&open_dag_mutex);
498        return 0;
499}
500
501/* Configures a DAG input trace */
502static int dag_config_input(libtrace_t *libtrace, trace_option_t option,
503                                void *data) {
504        char conf_str[4096];
505        switch(option) {
506                case TRACE_OPTION_META_FREQ:
507                        /* This option is used to specify the frequency of DUCK
508                         * updates */
509                        DUCK.duck_freq = *(int *)data;
510                        return 0;
511                case TRACE_OPTION_SNAPLEN:
512                        /* Tell the card our new snap length */
513                        snprintf(conf_str, 4096, "varlen slen=%i", *(int *)data);
514                        if (dag_configure(FORMAT_DATA->device->fd,
515                                                conf_str) != 0) {
516                                trace_set_err(libtrace, errno, "Failed to configure snaplen on DAG card: %s", libtrace->uridata);
517                                return -1;
518                        }
519                        return 0;
520                case TRACE_OPTION_PROMISC:
521                        /* DAG already operates in a promisc fashion */
522                        return -1;
523                case TRACE_OPTION_FILTER:
524                        /* We don't yet support pushing filters into DAG
525                         * cards */
526                        return -1;
527                case TRACE_OPTION_EVENT_REALTIME:
528                        /* Live capture is always going to be realtime */
529                        return -1;
530        }
531        return -1;
532}
533
534/* Starts a DAG output trace */
535static int dag_start_output(libtrace_out_t *libtrace) {
536        struct timeval zero, nopoll;
537        uint8_t *top, *bottom;
538        top = bottom = NULL;
539
540        zero.tv_sec = 0;
541        zero.tv_usec = 0;
542        nopoll = zero;
543
544        /* Attach and start the DAG stream */
545
546        if (dag_attach_stream(FORMAT_DATA_OUT->device->fd,
547                        FORMAT_DATA_OUT->dagstream, 0, 1048576) < 0) {
548                trace_set_err_out(libtrace, errno, "Cannot attach DAG stream");
549                return -1;
550        }
551
552        if (dag_start_stream(FORMAT_DATA_OUT->device->fd,
553                        FORMAT_DATA_OUT->dagstream) < 0) {
554                trace_set_err_out(libtrace, errno, "Cannot start DAG stream");
555                return -1;
556        }
557        FORMAT_DATA_OUT->stream_attached = 1;
558
559        /* We don't want the dag card to do any sleeping */
560
561        dag_set_stream_poll(FORMAT_DATA_OUT->device->fd,
562                        FORMAT_DATA_OUT->dagstream, 0, &zero,
563                        &nopoll);
564
565        return 0;
566}
567
568/* Starts a DAG input trace */
569static int dag_start_input(libtrace_t *libtrace) {
570        struct timeval zero, nopoll;
571        uint8_t *top, *bottom;
572        uint8_t diff = 0;
573        top = bottom = NULL;
574
575        zero.tv_sec = 0;
576        zero.tv_usec = 0;
577        nopoll = zero;
578
579        /* Attach and start the DAG stream */
580        if (dag_attach_stream(FORMAT_DATA->device->fd,
581                                FORMAT_DATA->dagstream, 0, 0) < 0) {
582                trace_set_err(libtrace, errno, "Cannot attach DAG stream");
583                return -1;
584        }
585
586        if (dag_start_stream(FORMAT_DATA->device->fd,
587                                FORMAT_DATA->dagstream) < 0) {
588                trace_set_err(libtrace, errno, "Cannot start DAG stream");
589                return -1;
590        }
591        FORMAT_DATA->stream_attached = 1;
592       
593        /* We don't want the dag card to do any sleeping */
594        dag_set_stream_poll(FORMAT_DATA->device->fd,
595                                FORMAT_DATA->dagstream, 0, &zero,
596                                &nopoll);
597
598        /* Should probably flush the memory hole now */
599        do {
600                top = dag_advance_stream(FORMAT_DATA->device->fd,
601                                        FORMAT_DATA->dagstream,
602                                        &bottom);
603                assert(top && bottom);
604                diff = top - bottom;
605                bottom -= diff;
606        } while (diff != 0);
607        FORMAT_DATA->top = NULL;
608        FORMAT_DATA->bottom = NULL;
609        FORMAT_DATA->processed = 0;
610        FORMAT_DATA->drops = 0;
611
612        return 0;
613}
614
615/* Pauses a DAG output trace */
616static int dag_pause_output(libtrace_out_t *libtrace) {
617
618        /* Stop and detach the stream */
619        if (dag_stop_stream(FORMAT_DATA_OUT->device->fd,
620                        FORMAT_DATA_OUT->dagstream) < 0) {
621                trace_set_err_out(libtrace, errno, "Could not stop DAG stream");
622                return -1;
623        }
624        if (dag_detach_stream(FORMAT_DATA_OUT->device->fd,
625                        FORMAT_DATA_OUT->dagstream) < 0) {
626                trace_set_err_out(libtrace, errno, "Could not detach DAG stream");
627                return -1;
628        }
629        FORMAT_DATA_OUT->stream_attached = 0;
630        return 0;
631}
632
633/* Pauses a DAG input trace */
634static int dag_pause_input(libtrace_t *libtrace) {
635
636        /* Stop and detach the stream */
637        if (dag_stop_stream(FORMAT_DATA->device->fd,
638                                FORMAT_DATA->dagstream) < 0) {
639                trace_set_err(libtrace, errno, "Could not stop DAG stream");
640                return -1;
641        }
642        if (dag_detach_stream(FORMAT_DATA->device->fd,
643                                FORMAT_DATA->dagstream) < 0) {
644                trace_set_err(libtrace, errno, "Could not detach DAG stream");
645                return -1;
646        }
647        FORMAT_DATA->stream_attached = 0;
648        return 0;
649}
650
651/* Closes a DAG input trace */
652static int dag_fin_input(libtrace_t *libtrace) {
653        /* Need the lock, since we're going to be handling the device list */
654        pthread_mutex_lock(&open_dag_mutex);
655       
656        /* Detach the stream if we are not paused */
657        if (FORMAT_DATA->stream_attached)
658                dag_pause_input(libtrace);
659        FORMAT_DATA->device->ref_count --;
660
661        /* Close the DAG device if there are no more references to it */
662        if (FORMAT_DATA->device->ref_count == 0)
663                dag_close_device(FORMAT_DATA->device);
664        if (DUCK.dummy_duck)
665                trace_destroy_dead(DUCK.dummy_duck);
666        free(libtrace->format_data);
667        pthread_mutex_unlock(&open_dag_mutex);
668        return 0; /* success */
669}
670
671/* Closes a DAG output trace */
672static int dag_fin_output(libtrace_out_t *libtrace) {
673       
674        /* Commit any outstanding traffic in the txbuffer */
675        if (FORMAT_DATA_OUT->waiting) {
676                dag_tx_stream_commit_bytes(FORMAT_DATA_OUT->device->fd, FORMAT_DATA_OUT->dagstream,
677                                FORMAT_DATA_OUT->waiting );
678        }
679
680        /* Wait until the buffer is nearly clear before exiting the program,
681         * as we will lose packets otherwise */
682        dag_tx_get_stream_space(FORMAT_DATA_OUT->device->fd,
683                        FORMAT_DATA_OUT->dagstream,
684                        dag_get_stream_buffer_size(FORMAT_DATA_OUT->device->fd,
685                                        FORMAT_DATA_OUT->dagstream) - 8
686                        );
687
688        /* Need the lock, since we're going to be handling the device list */
689        pthread_mutex_lock(&open_dag_mutex);
690
691        /* Detach the stream if we are not paused */
692        if (FORMAT_DATA_OUT->stream_attached)
693                dag_pause_output(libtrace);
694        FORMAT_DATA_OUT->device->ref_count --;
695
696        /* Close the DAG device if there are no more references to it */
697        if (FORMAT_DATA_OUT->device->ref_count == 0)
698                dag_close_device(FORMAT_DATA_OUT->device);
699        free(libtrace->format_data);
700        pthread_mutex_unlock(&open_dag_mutex);
701        return 0; /* success */
702}
703
704/* Extracts DUCK information from the DAG card and produces a DUCK packet */
705static int dag_get_duckinfo(libtrace_t *libtrace,
706                                libtrace_packet_t *packet) {
707
708        /* Allocate memory for the DUCK data */
709        if (packet->buf_control == TRACE_CTRL_EXTERNAL ||
710                        !packet->buffer) {
711                packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE);
712                packet->buf_control = TRACE_CTRL_PACKET;
713                if (!packet->buffer) {
714                        trace_set_err(libtrace, errno,
715                                        "Cannot allocate packet buffer");
716                        return -1;
717                }
718        }
719
720        /* DUCK doesn't have a format header */
721        packet->header = 0;
722        packet->payload = packet->buffer;
723
724        /* No need to check if we can get DUCK or not - we're modern
725         * enough so just grab the DUCK info */
726        if ((ioctl(FORMAT_DATA->device->fd, DAGIOCDUCK,
727                                        (duckinf_t *)packet->payload) < 0)) {
728                trace_set_err(libtrace, errno, "Error using DAGIOCDUCK");
729                return -1;
730        }
731
732        packet->type = TRACE_RT_DUCK_2_5;
733
734        /* Set the packet's tracce to point at a DUCK trace, so that the
735         * DUCK format functions will be called on the packet rather than the
736         * DAG ones */
737        if (!DUCK.dummy_duck)
738                DUCK.dummy_duck = trace_create_dead("rt:localhost:3434");
739        packet->trace = DUCK.dummy_duck;
740        return sizeof(duckinf_t);
741}
742
743/* Determines the amount of data available to read from the DAG card */
744static int dag_available(libtrace_t *libtrace) {
745        uint32_t diff = FORMAT_DATA->top - FORMAT_DATA->bottom;
746
747        /* If we've processed more than 4MB of data since we last called
748         * dag_advance_stream, then we should call it again to allow the
749         * space occupied by that 4MB to be released */
750        if (diff >= dag_record_size && FORMAT_DATA->processed < 4 * 1024 * 1024)
751                return diff;
752       
753        /* Update the top and bottom pointers */
754        FORMAT_DATA->top = dag_advance_stream(FORMAT_DATA->device->fd,
755                        FORMAT_DATA->dagstream,
756                        &(FORMAT_DATA->bottom));
757       
758        if (FORMAT_DATA->top == NULL) {
759                trace_set_err(libtrace, errno, "dag_advance_stream failed!");
760                return -1;
761        }
762        FORMAT_DATA->processed = 0;
763        diff = FORMAT_DATA->top - FORMAT_DATA->bottom;
764        return diff;
765}
766
767/* Returns a pointer to the start of the next complete ERF record */
768static dag_record_t *dag_get_record(libtrace_t *libtrace) {
769        dag_record_t *erfptr = NULL;
770        uint16_t size;
771        erfptr = (dag_record_t *)FORMAT_DATA->bottom;
772        if (!erfptr)
773                return NULL;
774        size = ntohs(erfptr->rlen);
775        assert( size >= dag_record_size );
776        /* Make certain we have the full packet available */
777        if (size > (FORMAT_DATA->top - FORMAT_DATA->bottom))
778                return NULL;
779        FORMAT_DATA->bottom += size;
780        FORMAT_DATA->processed += size;
781        return erfptr;
782}
783
784/* Converts a buffer containing a recently read DAG packet record into a
785 * libtrace packet */
786static int dag_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet,
787                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) {
788
789        dag_record_t *erfptr;
790       
791        /* If the packet previously owned a buffer that is not the buffer
792         * that contains the new packet data, we're going to need to free the
793         * old one to avoid memory leaks */
794        if (packet->buffer != buffer &&
795                        packet->buf_control == TRACE_CTRL_PACKET) {
796                free(packet->buffer);
797        }
798
799        /* Set the buffer owner appropriately */
800        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) {
801                packet->buf_control = TRACE_CTRL_PACKET;
802        } else
803                packet->buf_control = TRACE_CTRL_EXTERNAL;
804
805        /* Update the packet pointers and type appropriately */
806        erfptr = (dag_record_t *)buffer;
807        packet->buffer = erfptr;
808        packet->header = erfptr;
809        packet->type = rt_type;
810
811        if (erfptr->flags.rxerror == 1) {
812                /* rxerror means the payload is corrupt - drop the payload
813                 * by tweaking rlen */
814                packet->payload = NULL;
815                erfptr->rlen = htons(erf_get_framing_length(packet));
816        } else {
817                packet->payload = (char*)packet->buffer
818                        + erf_get_framing_length(packet);
819        }
820
821        if (libtrace->format_data == NULL) {
822                dag_init_format_data(libtrace);
823        }
824
825        /* Update the dropped packets counter */
826
827        /* No loss counter for DSM coloured records - have to use
828         * some other API */
829        if (erfptr->type == TYPE_DSM_COLOR_ETH) {
830                /* TODO */
831        } else {
832                /* Use the ERF loss counter */
833                DATA(libtrace)->drops += ntohs(erfptr->lctr);
834        }
835
836        return 0;
837}
838
839/*
840 * dag_write_packet() at this stage attempts to improve tx performance
841 * by delaying sending a dag_tx_stream_commit_bytes() until a threshold
842 * has been met. I observed approximately 270% performance increase
843 * through this relatively naive tweak. No optimisation of buffer sizes
844 * was attempted.
845 */
846
847/* Pushes an ERF record onto the transmit stream */
848static int dag_dump_packet(libtrace_out_t *libtrace,
849                dag_record_t *erfptr, unsigned int pad, void *buffer) {
850        int size;
851
852        /*
853         * If we've got 0 bytes waiting in the txqueue, assume that we haven't
854         * requested any space yet, and request some, storing the pointer at
855         * FORMAT_DATA_OUT->txbuffer.
856         *
857         * The amount to request is slightly magical at the moment - it's
858         * 16Mebibytes + 128 kibibytes to ensure that we can copy a packet into
859         * the buffer and handle overruns.
860         */
861        if (FORMAT_DATA_OUT->waiting == 0) {
862                FORMAT_DATA_OUT->txbuffer = dag_tx_get_stream_space(FORMAT_DATA_OUT->device->fd,
863                                FORMAT_DATA_OUT->dagstream, 16908288);
864        }
865
866        /*
867         * Copy the header separately to the body, as we can't guarantee they
868         * are in contiguous memory
869         */
870        memcpy(FORMAT_DATA_OUT->txbuffer + FORMAT_DATA_OUT->waiting,erfptr,(dag_record_size + pad));
871        FORMAT_DATA_OUT->waiting += (dag_record_size + pad);
872
873
874
875        /*
876         * Copy our incoming packet into the outgoing buffer, and increment
877         * our waiting count
878         */
879        size = ntohs(erfptr->rlen)-(dag_record_size + pad);
880        memcpy(FORMAT_DATA_OUT->txbuffer + FORMAT_DATA_OUT->waiting,buffer,size);
881        FORMAT_DATA_OUT->waiting += size;
882
883        /*
884         * If our output buffer has more than 16 Mebibytes in it, commit those
885         * bytes and reset the waiting count to 0.
886         * Note: dag_fin_output will also call dag_tx_stream_commit_bytes() in
887         * case there is still data in the buffer at program exit.
888         */
889
890        if (FORMAT_DATA_OUT->waiting >= 16*1024*1024) {
891                FORMAT_DATA_OUT->txbuffer = dag_tx_stream_commit_bytes(FORMAT_DATA_OUT->device->fd, FORMAT_DATA_OUT->dagstream,
892                        FORMAT_DATA_OUT->waiting );
893                FORMAT_DATA_OUT->waiting = 0;
894        }
895
896        return size + pad + dag_record_size;
897
898}
899
900/* Attempts to determine a suitable ERF type for a given packet. Returns true
901 * if one is found, false otherwise */
902static bool find_compatible_linktype(libtrace_out_t *libtrace,
903                                libtrace_packet_t *packet, char *type)
904{
905         // Keep trying to simplify the packet until we can find
906         //something we can do with it
907
908        do {
909                *type=libtrace_to_erf_type(trace_get_link_type(packet));
910
911                // Success
912                if (*type != (char)-1)
913                        return true;
914
915                if (!demote_packet(packet)) {
916                        trace_set_err_out(libtrace,
917                                        TRACE_ERR_NO_CONVERSION,
918                                        "No erf type for packet (%i)",
919                                        trace_get_link_type(packet));
920                        return false;
921                }
922
923        } while(1);
924
925        return true;
926}
927
928/* Writes a packet to the provided DAG output trace */
929static int dag_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) {
930        /*
931         * This is heavily borrowed from erf_write_packet(). Yes, CnP coding
932         * sucks, sorry about that.
933         */
934        unsigned int pad = 0;
935        int numbytes;
936        void *payload = packet->payload;
937        dag_record_t *header = (dag_record_t *)packet->header;
938        char erf_type = 0;
939
940        if(!packet->header) {
941                /* No header, probably an RT packet. Lifted from
942                 * erf_write_packet(). */
943                return -1;
944        }
945
946        pad = dag_get_padding(packet);
947
948        /*
949         * If the payload is null, adjust the rlen. Discussion of this is
950         * attached to erf_write_packet()
951         */
952        if (payload == NULL) {
953                header->rlen = htons(dag_record_size + pad);
954        }
955
956        if (packet->type == TRACE_RT_DATA_ERF) {
957                numbytes = dag_dump_packet(libtrace,
958                                header,
959                                pad,
960                                payload
961                                );
962
963        } else {
964                /* Build up a new packet header from the existing header */
965
966                /* Simplify the packet first - if we can't do this, break
967                 * early */
968                if (!find_compatible_linktype(libtrace,packet,&erf_type))
969                        return -1;
970
971                dag_record_t erfhdr;
972
973                erfhdr.ts = bswap_host_to_le64(trace_get_erf_timestamp(packet));
974                payload=packet->payload;
975                pad = dag_get_padding(packet);
976
977                /* Flags. Can't do this */
978                memset(&erfhdr.flags,1,sizeof(erfhdr.flags));
979                if (trace_get_direction(packet)!=~0U)
980                        erfhdr.flags.iface = trace_get_direction(packet);
981
982                erfhdr.type = erf_type;
983
984                /* Packet length (rlen includes format overhead) */
985                assert(trace_get_capture_length(packet)>0
986                                && trace_get_capture_length(packet)<=65536);
987                assert(erf_get_framing_length(packet)>0
988                                && trace_get_framing_length(packet)<=65536);
989                assert(trace_get_capture_length(packet)+erf_get_framing_length(packet)>0
990                      &&trace_get_capture_length(packet)+erf_get_framing_length(packet)<=65536);
991
992                erfhdr.rlen = htons(trace_get_capture_length(packet)
993                        + erf_get_framing_length(packet));
994
995
996                /* Loss counter. Can't do this */
997                erfhdr.lctr = 0;
998                /* Wire length, does not include padding! */
999                erfhdr.wlen = htons(trace_get_wire_length(packet));
1000
1001                /* Write it out */
1002                numbytes = dag_dump_packet(libtrace,
1003                                &erfhdr,
1004                                pad,
1005                                payload);
1006
1007        }
1008
1009        return numbytes;
1010}
1011
1012/* Reads the next available packet from a DAG card, in a BLOCKING fashion
1013 *
1014 * If DUCK reporting is enabled, the packet returned may be a DUCK update
1015 */
1016static int dag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
1017        int size = 0;
1018        struct timeval tv;
1019        dag_record_t *erfptr = NULL;
1020        int numbytes = 0;
1021        uint32_t flags = 0;
1022
1023        /* Check if we're due for a DUCK report */
1024        if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq &&
1025                        DUCK.duck_freq != 0) {
1026                size = dag_get_duckinfo(libtrace, packet);
1027                DUCK.last_duck = DUCK.last_pkt;
1028                if (size != 0) {
1029                        return size;
1030                }
1031                /* No DUCK support, so don't waste our time anymore */
1032                DUCK.duck_freq = 0;
1033        }
1034
1035        /* Don't let anyone try to free our DAG memory hole! */
1036        flags |= TRACE_PREP_DO_NOT_OWN_BUFFER;
1037
1038        /* If the packet buffer is currently owned by libtrace, free it so
1039         * that we can set the packet to point into the DAG memory hole */
1040        if (packet->buf_control == TRACE_CTRL_PACKET) {
1041                free(packet->buffer);
1042                packet->buffer = 0;
1043        }
1044
1045        /* Grab a full ERF record */
1046        do {
1047                numbytes = dag_available(libtrace);
1048                if (numbytes < 0)
1049                        return numbytes;
1050                if (numbytes < dag_record_size)
1051                        /* Block until we see a packet */
1052                        continue;
1053                erfptr = dag_get_record(libtrace);
1054        } while (erfptr == NULL);
1055
1056        /* Prepare the libtrace packet */
1057        if (dag_prepare_packet(libtrace, packet, erfptr, TRACE_RT_DATA_ERF,
1058                                flags))
1059                return -1;
1060
1061        /* Update the DUCK timer */
1062        tv = trace_get_timeval(packet);
1063        DUCK.last_pkt = tv.tv_sec;
1064
1065        return packet->payload ? htons(erfptr->rlen) :
1066                                erf_get_framing_length(packet);
1067}
1068
1069/* Attempts to read a packet from a DAG card in a NON-BLOCKING fashion. If a
1070 * packet is available, we will return a packet event. Otherwise we will
1071 * return a SLEEP event (as we cannot select on the DAG file descriptor).
1072 */
1073static libtrace_eventobj_t trace_event_dag(libtrace_t *trace,
1074                                        libtrace_packet_t *packet) {
1075        libtrace_eventobj_t event = {0,0,0.0,0};
1076        dag_record_t *erfptr = NULL;
1077        int numbytes;
1078        uint32_t flags = 0;
1079
1080        do {
1081                erfptr = NULL;
1082                numbytes = 0;
1083       
1084                /* Need to call dag_available so that the top pointer will get
1085                 * updated, otherwise we'll never see any data! */
1086                numbytes = dag_available(trace);
1087
1088                /* May as well not bother calling dag_get_record if
1089                 * dag_available suggests that there's no data */
1090                if (numbytes != 0)
1091                        erfptr = dag_get_record(trace);
1092                if (erfptr == NULL) {
1093                        /* No packet available - sleep for a very short time */
1094                        event.type = TRACE_EVENT_SLEEP;
1095                        event.seconds = 0.0001;
1096                        break;
1097                }
1098                if (dag_prepare_packet(trace, packet, erfptr, 
1099                                        TRACE_RT_DATA_ERF, flags)) {
1100                        event.type = TRACE_EVENT_TERMINATE;
1101                        break;
1102                }
1103
1104
1105                event.size = trace_get_capture_length(packet) + 
1106                                trace_get_framing_length(packet);
1107               
1108                /* XXX trace_read_packet() normally applies the following
1109                 * config options for us, but this function is called via
1110                 * trace_event() so we have to do it ourselves */
1111
1112                if (trace->filter) {
1113                        if (trace_apply_filter(trace->filter, packet)) {
1114                                event.type = TRACE_EVENT_PACKET;
1115                        } else {
1116                                /* This packet isn't useful so we want to
1117                                 * immediately see if there is another suitable
1118                                 * one - we definitely DO NOT want to return
1119                                 * a sleep event in this case, like we used to
1120                                 * do! */
1121                                continue;
1122                        }
1123                } else {
1124                        event.type = TRACE_EVENT_PACKET;
1125                }
1126
1127                if (trace->snaplen > 0) {
1128                        trace_set_capture_length(packet, trace->snaplen);
1129                }
1130                break;
1131        } while (1);
1132
1133        return event;
1134}
1135
1136/* Gets the number of dropped packets */
1137static uint64_t dag_get_dropped_packets(libtrace_t *trace) {
1138        if (trace->format_data == NULL)
1139                return (uint64_t)-1;
1140        return DATA(trace)->drops;
1141}
1142
1143/* Prints some semi-useful help text about the DAG format module */
1144static void dag_help(void) {
1145        printf("dag format module: $Revision$\n");
1146        printf("Supported input URIs:\n");
1147        printf("\tdag:/dev/dagn\n");
1148        printf("\n");
1149        printf("\te.g.: dag:/dev/dag0\n");
1150        printf("\n");
1151        printf("Supported output URIs:\n");
1152        printf("\tnone\n");
1153        printf("\n");
1154}
1155
1156static struct libtrace_format_t dag = {
1157        "dag",
1158        "$Id$",
1159        TRACE_FORMAT_ERF,
1160        dag_probe_filename,             /* probe filename */
1161        NULL,                           /* probe magic */
1162        dag_init_input,                 /* init_input */
1163        dag_config_input,               /* config_input */
1164        dag_start_input,                /* start_input */
1165        dag_pause_input,                /* pause_input */
1166        dag_init_output,                /* init_output */
1167        NULL,                           /* config_output */
1168        dag_start_output,               /* start_output */
1169        dag_fin_input,                  /* fin_input */
1170        dag_fin_output,                 /* fin_output */
1171        dag_read_packet,                /* read_packet */
1172        dag_prepare_packet,             /* prepare_packet */
1173        NULL,                           /* fin_packet */
1174        dag_write_packet,               /* write_packet */
1175        erf_get_link_type,              /* get_link_type */
1176        erf_get_direction,              /* get_direction */
1177        erf_set_direction,              /* set_direction */
1178        erf_get_erf_timestamp,          /* get_erf_timestamp */
1179        NULL,                           /* get_timeval */
1180        NULL,                           /* get_seconds */
1181        NULL,                           /* get_timespec */
1182        NULL,                           /* seek_erf */
1183        NULL,                           /* seek_timeval */
1184        NULL,                           /* seek_seconds */
1185        erf_get_capture_length,         /* get_capture_length */
1186        erf_get_wire_length,            /* get_wire_length */
1187        erf_get_framing_length,         /* get_framing_length */
1188        erf_set_capture_length,         /* set_capture_length */
1189        NULL,                           /* get_received_packets */
1190        NULL,                           /* get_filtered_packets */
1191        dag_get_dropped_packets,        /* get_dropped_packets */
1192        NULL,                           /* get_captured_packets */
1193        NULL,                           /* get_fd */
1194        trace_event_dag,                /* trace_event */
1195        dag_help,                       /* help */
1196        NULL                            /* next pointer */
1197};
1198
1199void dag_constructor(void) {
1200        register_format(&dag);
1201}
Note: See TracBrowser for help on using the repository browser.