source: lib/format_dag25.c @ 81c2da8

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 81c2da8 was 81c2da8, checked in by Perry Lorier <perry@…>, 11 years ago

Suggestions for improving user feedback when opening a dag card

  • Property mode set to 100644
File size: 36.6 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        /* See Config_Status_API_Programming_Guide.pdf from the Endace Dag Documentation */
498        /* Check kBooleanAttributeActive is true -- no point capturing on an interface that's disabled */
499        /* Check kBooleanAttributeFault is false */
500        /* Check kBooleanAttributeLocalFault is false */
501        /* Check kBooleanAttributeLock is true ? */
502        /* Check kBooleanAttributePeerLink ? */
503
504        /* Set kBooleanAttributePromisc/kBooleanPromiscuousMode based on libtrace promisc attribute?*/
505        /* Set kUint32AttributeSnapLength to the snaplength */
506
507        pthread_mutex_unlock(&open_dag_mutex);
508        return 0;
509}
510
511/* Configures a DAG input trace */
512static int dag_config_input(libtrace_t *libtrace, trace_option_t option,
513                                void *data) {
514        char conf_str[4096];
515        switch(option) {
516                case TRACE_OPTION_META_FREQ:
517                        /* This option is used to specify the frequency of DUCK
518                         * updates */
519                        DUCK.duck_freq = *(int *)data;
520                        return 0;
521                case TRACE_OPTION_SNAPLEN:
522                        /* Tell the card our new snap length */
523                        snprintf(conf_str, 4096, "varlen slen=%i", *(int *)data);
524                        if (dag_configure(FORMAT_DATA->device->fd,
525                                                conf_str) != 0) {
526                                trace_set_err(libtrace, errno, "Failed to configure snaplen on DAG card: %s", libtrace->uridata);
527                                return -1;
528                        }
529                        return 0;
530                case TRACE_OPTION_PROMISC:
531                        /* DAG already operates in a promisc fashion */
532                        return -1;
533                case TRACE_OPTION_FILTER:
534                        /* We don't yet support pushing filters into DAG
535                         * cards */
536                        return -1;
537                case TRACE_OPTION_EVENT_REALTIME:
538                        /* Live capture is always going to be realtime */
539                        return -1;
540        }
541        return -1;
542}
543
544/* Starts a DAG output trace */
545static int dag_start_output(libtrace_out_t *libtrace) {
546        struct timeval zero, nopoll;
547        uint8_t *top, *bottom;
548        top = bottom = NULL;
549
550        zero.tv_sec = 0;
551        zero.tv_usec = 0;
552        nopoll = zero;
553
554        /* Attach and start the DAG stream */
555
556        if (dag_attach_stream(FORMAT_DATA_OUT->device->fd,
557                        FORMAT_DATA_OUT->dagstream, 0, 1048576) < 0) {
558                trace_set_err_out(libtrace, errno, "Cannot attach DAG stream");
559                return -1;
560        }
561
562        if (dag_start_stream(FORMAT_DATA_OUT->device->fd,
563                        FORMAT_DATA_OUT->dagstream) < 0) {
564                trace_set_err_out(libtrace, errno, "Cannot start DAG stream");
565                return -1;
566        }
567        FORMAT_DATA_OUT->stream_attached = 1;
568
569        /* We don't want the dag card to do any sleeping */
570
571        dag_set_stream_poll(FORMAT_DATA_OUT->device->fd,
572                        FORMAT_DATA_OUT->dagstream, 0, &zero,
573                        &nopoll);
574
575        return 0;
576}
577
578/* Starts a DAG input trace */
579static int dag_start_input(libtrace_t *libtrace) {
580        struct timeval zero, nopoll;
581        uint8_t *top, *bottom;
582        uint8_t diff = 0;
583        top = bottom = NULL;
584
585        zero.tv_sec = 0;
586        zero.tv_usec = 0;
587        nopoll = zero;
588
589        /* Attach and start the DAG stream */
590        if (dag_attach_stream(FORMAT_DATA->device->fd,
591                                FORMAT_DATA->dagstream, 0, 0) < 0) {
592                trace_set_err(libtrace, errno, "Cannot attach DAG stream");
593                return -1;
594        }
595
596        if (dag_start_stream(FORMAT_DATA->device->fd,
597                                FORMAT_DATA->dagstream) < 0) {
598                trace_set_err(libtrace, errno, "Cannot start DAG stream");
599                return -1;
600        }
601        FORMAT_DATA->stream_attached = 1;
602       
603        /* We don't want the dag card to do any sleeping */
604        dag_set_stream_poll(FORMAT_DATA->device->fd,
605                                FORMAT_DATA->dagstream, 0, &zero,
606                                &nopoll);
607
608        /* Should probably flush the memory hole now */
609        do {
610                top = dag_advance_stream(FORMAT_DATA->device->fd,
611                                        FORMAT_DATA->dagstream,
612                                        &bottom);
613                assert(top && bottom);
614                diff = top - bottom;
615                bottom -= diff;
616        } while (diff != 0);
617        FORMAT_DATA->top = NULL;
618        FORMAT_DATA->bottom = NULL;
619        FORMAT_DATA->processed = 0;
620        FORMAT_DATA->drops = 0;
621
622        return 0;
623}
624
625/* Pauses a DAG output trace */
626static int dag_pause_output(libtrace_out_t *libtrace) {
627
628        /* Stop and detach the stream */
629        if (dag_stop_stream(FORMAT_DATA_OUT->device->fd,
630                        FORMAT_DATA_OUT->dagstream) < 0) {
631                trace_set_err_out(libtrace, errno, "Could not stop DAG stream");
632                return -1;
633        }
634        if (dag_detach_stream(FORMAT_DATA_OUT->device->fd,
635                        FORMAT_DATA_OUT->dagstream) < 0) {
636                trace_set_err_out(libtrace, errno, "Could not detach DAG stream");
637                return -1;
638        }
639        FORMAT_DATA_OUT->stream_attached = 0;
640        return 0;
641}
642
643/* Pauses a DAG input trace */
644static int dag_pause_input(libtrace_t *libtrace) {
645
646        /* Stop and detach the stream */
647        if (dag_stop_stream(FORMAT_DATA->device->fd,
648                                FORMAT_DATA->dagstream) < 0) {
649                trace_set_err(libtrace, errno, "Could not stop DAG stream");
650                return -1;
651        }
652        if (dag_detach_stream(FORMAT_DATA->device->fd,
653                                FORMAT_DATA->dagstream) < 0) {
654                trace_set_err(libtrace, errno, "Could not detach DAG stream");
655                return -1;
656        }
657        FORMAT_DATA->stream_attached = 0;
658        return 0;
659}
660
661/* Closes a DAG input trace */
662static int dag_fin_input(libtrace_t *libtrace) {
663        /* Need the lock, since we're going to be handling the device list */
664        pthread_mutex_lock(&open_dag_mutex);
665       
666        /* Detach the stream if we are not paused */
667        if (FORMAT_DATA->stream_attached)
668                dag_pause_input(libtrace);
669        FORMAT_DATA->device->ref_count --;
670
671        /* Close the DAG device if there are no more references to it */
672        if (FORMAT_DATA->device->ref_count == 0)
673                dag_close_device(FORMAT_DATA->device);
674        if (DUCK.dummy_duck)
675                trace_destroy_dead(DUCK.dummy_duck);
676        free(libtrace->format_data);
677        pthread_mutex_unlock(&open_dag_mutex);
678        return 0; /* success */
679}
680
681/* Closes a DAG output trace */
682static int dag_fin_output(libtrace_out_t *libtrace) {
683       
684        /* Commit any outstanding traffic in the txbuffer */
685        if (FORMAT_DATA_OUT->waiting) {
686                dag_tx_stream_commit_bytes(FORMAT_DATA_OUT->device->fd, FORMAT_DATA_OUT->dagstream,
687                                FORMAT_DATA_OUT->waiting );
688        }
689
690        /* Wait until the buffer is nearly clear before exiting the program,
691         * as we will lose packets otherwise */
692        dag_tx_get_stream_space(FORMAT_DATA_OUT->device->fd,
693                        FORMAT_DATA_OUT->dagstream,
694                        dag_get_stream_buffer_size(FORMAT_DATA_OUT->device->fd,
695                                        FORMAT_DATA_OUT->dagstream) - 8
696                        );
697
698        /* Need the lock, since we're going to be handling the device list */
699        pthread_mutex_lock(&open_dag_mutex);
700
701        /* Detach the stream if we are not paused */
702        if (FORMAT_DATA_OUT->stream_attached)
703                dag_pause_output(libtrace);
704        FORMAT_DATA_OUT->device->ref_count --;
705
706        /* Close the DAG device if there are no more references to it */
707        if (FORMAT_DATA_OUT->device->ref_count == 0)
708                dag_close_device(FORMAT_DATA_OUT->device);
709        free(libtrace->format_data);
710        pthread_mutex_unlock(&open_dag_mutex);
711        return 0; /* success */
712}
713
714/* Extracts DUCK information from the DAG card and produces a DUCK packet */
715static int dag_get_duckinfo(libtrace_t *libtrace,
716                                libtrace_packet_t *packet) {
717
718        /* Allocate memory for the DUCK data */
719        if (packet->buf_control == TRACE_CTRL_EXTERNAL ||
720                        !packet->buffer) {
721                packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE);
722                packet->buf_control = TRACE_CTRL_PACKET;
723                if (!packet->buffer) {
724                        trace_set_err(libtrace, errno,
725                                        "Cannot allocate packet buffer");
726                        return -1;
727                }
728        }
729
730        /* DUCK doesn't have a format header */
731        packet->header = 0;
732        packet->payload = packet->buffer;
733
734        /* No need to check if we can get DUCK or not - we're modern
735         * enough so just grab the DUCK info */
736        if ((ioctl(FORMAT_DATA->device->fd, DAGIOCDUCK,
737                                        (duckinf_t *)packet->payload) < 0)) {
738                trace_set_err(libtrace, errno, "Error using DAGIOCDUCK");
739                return -1;
740        }
741
742        packet->type = TRACE_RT_DUCK_2_5;
743
744        /* Set the packet's tracce to point at a DUCK trace, so that the
745         * DUCK format functions will be called on the packet rather than the
746         * DAG ones */
747        if (!DUCK.dummy_duck)
748                DUCK.dummy_duck = trace_create_dead("rt:localhost:3434");
749        packet->trace = DUCK.dummy_duck;
750        return sizeof(duckinf_t);
751}
752
753/* Determines the amount of data available to read from the DAG card */
754static int dag_available(libtrace_t *libtrace) {
755        uint32_t diff = FORMAT_DATA->top - FORMAT_DATA->bottom;
756
757        /* If we've processed more than 4MB of data since we last called
758         * dag_advance_stream, then we should call it again to allow the
759         * space occupied by that 4MB to be released */
760        if (diff >= dag_record_size && FORMAT_DATA->processed < 4 * 1024 * 1024)
761                return diff;
762       
763        /* Update the top and bottom pointers */
764        FORMAT_DATA->top = dag_advance_stream(FORMAT_DATA->device->fd,
765                        FORMAT_DATA->dagstream,
766                        &(FORMAT_DATA->bottom));
767       
768        if (FORMAT_DATA->top == NULL) {
769                trace_set_err(libtrace, errno, "dag_advance_stream failed!");
770                return -1;
771        }
772        FORMAT_DATA->processed = 0;
773        diff = FORMAT_DATA->top - FORMAT_DATA->bottom;
774        return diff;
775}
776
777/* Returns a pointer to the start of the next complete ERF record */
778static dag_record_t *dag_get_record(libtrace_t *libtrace) {
779        dag_record_t *erfptr = NULL;
780        uint16_t size;
781        erfptr = (dag_record_t *)FORMAT_DATA->bottom;
782        if (!erfptr)
783                return NULL;
784        size = ntohs(erfptr->rlen);
785        assert( size >= dag_record_size );
786        /* Make certain we have the full packet available */
787        if (size > (FORMAT_DATA->top - FORMAT_DATA->bottom))
788                return NULL;
789        FORMAT_DATA->bottom += size;
790        FORMAT_DATA->processed += size;
791        return erfptr;
792}
793
794/* Converts a buffer containing a recently read DAG packet record into a
795 * libtrace packet */
796static int dag_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet,
797                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) {
798
799        dag_record_t *erfptr;
800       
801        /* If the packet previously owned a buffer that is not the buffer
802         * that contains the new packet data, we're going to need to free the
803         * old one to avoid memory leaks */
804        if (packet->buffer != buffer &&
805                        packet->buf_control == TRACE_CTRL_PACKET) {
806                free(packet->buffer);
807        }
808
809        /* Set the buffer owner appropriately */
810        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) {
811                packet->buf_control = TRACE_CTRL_PACKET;
812        } else
813                packet->buf_control = TRACE_CTRL_EXTERNAL;
814
815        /* Update the packet pointers and type appropriately */
816        erfptr = (dag_record_t *)buffer;
817        packet->buffer = erfptr;
818        packet->header = erfptr;
819        packet->type = rt_type;
820
821        if (erfptr->flags.rxerror == 1) {
822                /* rxerror means the payload is corrupt - drop the payload
823                 * by tweaking rlen */
824                packet->payload = NULL;
825                erfptr->rlen = htons(erf_get_framing_length(packet));
826        } else {
827                packet->payload = (char*)packet->buffer
828                        + erf_get_framing_length(packet);
829        }
830
831        if (libtrace->format_data == NULL) {
832                dag_init_format_data(libtrace);
833        }
834
835        /* Update the dropped packets counter */
836
837        /* No loss counter for DSM coloured records - have to use
838         * some other API */
839        if (erfptr->type == TYPE_DSM_COLOR_ETH) {
840                /* TODO */
841        } else {
842                /* Use the ERF loss counter */
843                DATA(libtrace)->drops += ntohs(erfptr->lctr);
844        }
845
846        return 0;
847}
848
849/*
850 * dag_write_packet() at this stage attempts to improve tx performance
851 * by delaying sending a dag_tx_stream_commit_bytes() until a threshold
852 * has been met. I observed approximately 270% performance increase
853 * through this relatively naive tweak. No optimisation of buffer sizes
854 * was attempted.
855 */
856
857/* Pushes an ERF record onto the transmit stream */
858static int dag_dump_packet(libtrace_out_t *libtrace,
859                dag_record_t *erfptr, unsigned int pad, void *buffer) {
860        int size;
861
862        /*
863         * If we've got 0 bytes waiting in the txqueue, assume that we haven't
864         * requested any space yet, and request some, storing the pointer at
865         * FORMAT_DATA_OUT->txbuffer.
866         *
867         * The amount to request is slightly magical at the moment - it's
868         * 16Mebibytes + 128 kibibytes to ensure that we can copy a packet into
869         * the buffer and handle overruns.
870         */
871        if (FORMAT_DATA_OUT->waiting == 0) {
872                FORMAT_DATA_OUT->txbuffer = dag_tx_get_stream_space(FORMAT_DATA_OUT->device->fd,
873                                FORMAT_DATA_OUT->dagstream, 16908288);
874        }
875
876        /*
877         * Copy the header separately to the body, as we can't guarantee they
878         * are in contiguous memory
879         */
880        memcpy(FORMAT_DATA_OUT->txbuffer + FORMAT_DATA_OUT->waiting,erfptr,(dag_record_size + pad));
881        FORMAT_DATA_OUT->waiting += (dag_record_size + pad);
882
883
884
885        /*
886         * Copy our incoming packet into the outgoing buffer, and increment
887         * our waiting count
888         */
889        size = ntohs(erfptr->rlen)-(dag_record_size + pad);
890        memcpy(FORMAT_DATA_OUT->txbuffer + FORMAT_DATA_OUT->waiting,buffer,size);
891        FORMAT_DATA_OUT->waiting += size;
892
893        /*
894         * If our output buffer has more than 16 Mebibytes in it, commit those
895         * bytes and reset the waiting count to 0.
896         * Note: dag_fin_output will also call dag_tx_stream_commit_bytes() in
897         * case there is still data in the buffer at program exit.
898         */
899
900        if (FORMAT_DATA_OUT->waiting >= 16*1024*1024) {
901                FORMAT_DATA_OUT->txbuffer = dag_tx_stream_commit_bytes(FORMAT_DATA_OUT->device->fd, FORMAT_DATA_OUT->dagstream,
902                        FORMAT_DATA_OUT->waiting );
903                FORMAT_DATA_OUT->waiting = 0;
904        }
905
906        return size + pad + dag_record_size;
907
908}
909
910/* Attempts to determine a suitable ERF type for a given packet. Returns true
911 * if one is found, false otherwise */
912static bool find_compatible_linktype(libtrace_out_t *libtrace,
913                                libtrace_packet_t *packet, char *type)
914{
915         // Keep trying to simplify the packet until we can find
916         //something we can do with it
917
918        do {
919                *type=libtrace_to_erf_type(trace_get_link_type(packet));
920
921                // Success
922                if (*type != (char)-1)
923                        return true;
924
925                if (!demote_packet(packet)) {
926                        trace_set_err_out(libtrace,
927                                        TRACE_ERR_NO_CONVERSION,
928                                        "No erf type for packet (%i)",
929                                        trace_get_link_type(packet));
930                        return false;
931                }
932
933        } while(1);
934
935        return true;
936}
937
938/* Writes a packet to the provided DAG output trace */
939static int dag_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) {
940        /*
941         * This is heavily borrowed from erf_write_packet(). Yes, CnP coding
942         * sucks, sorry about that.
943         */
944        unsigned int pad = 0;
945        int numbytes;
946        void *payload = packet->payload;
947        dag_record_t *header = (dag_record_t *)packet->header;
948        char erf_type = 0;
949
950        if(!packet->header) {
951                /* No header, probably an RT packet. Lifted from
952                 * erf_write_packet(). */
953                return -1;
954        }
955
956        pad = dag_get_padding(packet);
957
958        /*
959         * If the payload is null, adjust the rlen. Discussion of this is
960         * attached to erf_write_packet()
961         */
962        if (payload == NULL) {
963                header->rlen = htons(dag_record_size + pad);
964        }
965
966        if (packet->type == TRACE_RT_DATA_ERF) {
967                numbytes = dag_dump_packet(libtrace,
968                                header,
969                                pad,
970                                payload
971                                );
972
973        } else {
974                /* Build up a new packet header from the existing header */
975
976                /* Simplify the packet first - if we can't do this, break
977                 * early */
978                if (!find_compatible_linktype(libtrace,packet,&erf_type))
979                        return -1;
980
981                dag_record_t erfhdr;
982
983                erfhdr.ts = bswap_host_to_le64(trace_get_erf_timestamp(packet));
984                payload=packet->payload;
985                pad = dag_get_padding(packet);
986
987                /* Flags. Can't do this */
988                memset(&erfhdr.flags,1,sizeof(erfhdr.flags));
989                if (trace_get_direction(packet)!=~0U)
990                        erfhdr.flags.iface = trace_get_direction(packet);
991
992                erfhdr.type = erf_type;
993
994                /* Packet length (rlen includes format overhead) */
995                assert(trace_get_capture_length(packet)>0
996                                && trace_get_capture_length(packet)<=65536);
997                assert(erf_get_framing_length(packet)>0
998                                && trace_get_framing_length(packet)<=65536);
999                assert(trace_get_capture_length(packet)+erf_get_framing_length(packet)>0
1000                      &&trace_get_capture_length(packet)+erf_get_framing_length(packet)<=65536);
1001
1002                erfhdr.rlen = htons(trace_get_capture_length(packet)
1003                        + erf_get_framing_length(packet));
1004
1005
1006                /* Loss counter. Can't do this */
1007                erfhdr.lctr = 0;
1008                /* Wire length, does not include padding! */
1009                erfhdr.wlen = htons(trace_get_wire_length(packet));
1010
1011                /* Write it out */
1012                numbytes = dag_dump_packet(libtrace,
1013                                &erfhdr,
1014                                pad,
1015                                payload);
1016
1017        }
1018
1019        return numbytes;
1020}
1021
1022/* Reads the next available packet from a DAG card, in a BLOCKING fashion
1023 *
1024 * If DUCK reporting is enabled, the packet returned may be a DUCK update
1025 */
1026static int dag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
1027        int size = 0;
1028        struct timeval tv;
1029        dag_record_t *erfptr = NULL;
1030        int numbytes = 0;
1031        uint32_t flags = 0;
1032
1033        /* Check if we're due for a DUCK report */
1034        if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq &&
1035                        DUCK.duck_freq != 0) {
1036                size = dag_get_duckinfo(libtrace, packet);
1037                DUCK.last_duck = DUCK.last_pkt;
1038                if (size != 0) {
1039                        return size;
1040                }
1041                /* No DUCK support, so don't waste our time anymore */
1042                DUCK.duck_freq = 0;
1043        }
1044
1045        /* Don't let anyone try to free our DAG memory hole! */
1046        flags |= TRACE_PREP_DO_NOT_OWN_BUFFER;
1047
1048        /* If the packet buffer is currently owned by libtrace, free it so
1049         * that we can set the packet to point into the DAG memory hole */
1050        if (packet->buf_control == TRACE_CTRL_PACKET) {
1051                free(packet->buffer);
1052                packet->buffer = 0;
1053        }
1054
1055        /* Grab a full ERF record */
1056        do {
1057                numbytes = dag_available(libtrace);
1058                if (numbytes < 0)
1059                        return numbytes;
1060                if (numbytes < dag_record_size)
1061                        /* Block until we see a packet */
1062                        continue;
1063                erfptr = dag_get_record(libtrace);
1064        } while (erfptr == NULL);
1065
1066        /* Prepare the libtrace packet */
1067        if (dag_prepare_packet(libtrace, packet, erfptr, TRACE_RT_DATA_ERF,
1068                                flags))
1069                return -1;
1070
1071        /* Update the DUCK timer */
1072        tv = trace_get_timeval(packet);
1073        DUCK.last_pkt = tv.tv_sec;
1074
1075        return packet->payload ? htons(erfptr->rlen) :
1076                                erf_get_framing_length(packet);
1077}
1078
1079/* Attempts to read a packet from a DAG card in a NON-BLOCKING fashion. If a
1080 * packet is available, we will return a packet event. Otherwise we will
1081 * return a SLEEP event (as we cannot select on the DAG file descriptor).
1082 */
1083static libtrace_eventobj_t trace_event_dag(libtrace_t *trace,
1084                                        libtrace_packet_t *packet) {
1085        libtrace_eventobj_t event = {0,0,0.0,0};
1086        dag_record_t *erfptr = NULL;
1087        int numbytes;
1088        uint32_t flags = 0;
1089
1090        do {
1091                erfptr = NULL;
1092                numbytes = 0;
1093       
1094                /* Need to call dag_available so that the top pointer will get
1095                 * updated, otherwise we'll never see any data! */
1096                numbytes = dag_available(trace);
1097
1098                /* May as well not bother calling dag_get_record if
1099                 * dag_available suggests that there's no data */
1100                if (numbytes != 0)
1101                        erfptr = dag_get_record(trace);
1102                if (erfptr == NULL) {
1103                        /* No packet available - sleep for a very short time */
1104                        event.type = TRACE_EVENT_SLEEP;
1105                        event.seconds = 0.0001;
1106                        break;
1107                }
1108                if (dag_prepare_packet(trace, packet, erfptr, 
1109                                        TRACE_RT_DATA_ERF, flags)) {
1110                        event.type = TRACE_EVENT_TERMINATE;
1111                        break;
1112                }
1113
1114
1115                event.size = trace_get_capture_length(packet) + 
1116                                trace_get_framing_length(packet);
1117               
1118                /* XXX trace_read_packet() normally applies the following
1119                 * config options for us, but this function is called via
1120                 * trace_event() so we have to do it ourselves */
1121
1122                if (trace->filter) {
1123                        if (trace_apply_filter(trace->filter, packet)) {
1124                                event.type = TRACE_EVENT_PACKET;
1125                        } else {
1126                                /* This packet isn't useful so we want to
1127                                 * immediately see if there is another suitable
1128                                 * one - we definitely DO NOT want to return
1129                                 * a sleep event in this case, like we used to
1130                                 * do! */
1131                                continue;
1132                        }
1133                } else {
1134                        event.type = TRACE_EVENT_PACKET;
1135                }
1136
1137                if (trace->snaplen > 0) {
1138                        trace_set_capture_length(packet, trace->snaplen);
1139                }
1140                break;
1141        } while (1);
1142
1143        return event;
1144}
1145
1146/* Gets the number of dropped packets */
1147static uint64_t dag_get_dropped_packets(libtrace_t *trace) {
1148        if (trace->format_data == NULL)
1149                return (uint64_t)-1;
1150        return DATA(trace)->drops;
1151}
1152
1153/* Prints some semi-useful help text about the DAG format module */
1154static void dag_help(void) {
1155        printf("dag format module: $Revision$\n");
1156        printf("Supported input URIs:\n");
1157        printf("\tdag:/dev/dagn\n");
1158        printf("\n");
1159        printf("\te.g.: dag:/dev/dag0\n");
1160        printf("\n");
1161        printf("Supported output URIs:\n");
1162        printf("\tnone\n");
1163        printf("\n");
1164}
1165
1166static struct libtrace_format_t dag = {
1167        "dag",
1168        "$Id$",
1169        TRACE_FORMAT_ERF,
1170        dag_probe_filename,             /* probe filename */
1171        NULL,                           /* probe magic */
1172        dag_init_input,                 /* init_input */
1173        dag_config_input,               /* config_input */
1174        dag_start_input,                /* start_input */
1175        dag_pause_input,                /* pause_input */
1176        dag_init_output,                /* init_output */
1177        NULL,                           /* config_output */
1178        dag_start_output,               /* start_output */
1179        dag_fin_input,                  /* fin_input */
1180        dag_fin_output,                 /* fin_output */
1181        dag_read_packet,                /* read_packet */
1182        dag_prepare_packet,             /* prepare_packet */
1183        NULL,                           /* fin_packet */
1184        dag_write_packet,               /* write_packet */
1185        erf_get_link_type,              /* get_link_type */
1186        erf_get_direction,              /* get_direction */
1187        erf_set_direction,              /* set_direction */
1188        erf_get_erf_timestamp,          /* get_erf_timestamp */
1189        NULL,                           /* get_timeval */
1190        NULL,                           /* get_seconds */
1191        NULL,                           /* get_timespec */
1192        NULL,                           /* seek_erf */
1193        NULL,                           /* seek_timeval */
1194        NULL,                           /* seek_seconds */
1195        erf_get_capture_length,         /* get_capture_length */
1196        erf_get_wire_length,            /* get_wire_length */
1197        erf_get_framing_length,         /* get_framing_length */
1198        erf_set_capture_length,         /* set_capture_length */
1199        NULL,                           /* get_received_packets */
1200        NULL,                           /* get_filtered_packets */
1201        dag_get_dropped_packets,        /* get_dropped_packets */
1202        NULL,                           /* get_captured_packets */
1203        NULL,                           /* get_fd */
1204        trace_event_dag,                /* trace_event */
1205        dag_help,                       /* help */
1206        NULL                            /* next pointer */
1207};
1208
1209void dag_constructor(void) {
1210        register_format(&dag);
1211}
Note: See TracBrowser for help on using the repository browser.