source: lib/format_dag25.c @ 2193905

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

Apply changes required for pull request #81

  • Property mode set to 100644
File size: 46.7 KB
Line 
1/*
2 *
3 * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
4 * All rights reserved.
5 *
6 * This file is part of libtrace.
7 *
8 * This code has been developed by the University of Waikato WAND
9 * research group. For further information please see http://www.wand.net.nz/
10 *
11 * libtrace is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * libtrace is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 *
25 */
26#define _GNU_SOURCE
27
28#include "config.h"
29#include "common.h"
30#include "libtrace.h"
31#include "libtrace_int.h"
32#include "format_helper.h"
33#include "format_erf.h"
34
35#include <errno.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <string.h>
39#include <stdlib.h>
40#include <sys/stat.h>
41
42#include <sys/mman.h>
43/* XXX: Windows doesn't have pthreads, but this code doesn't compile under
44 * Windows anyway so we'll worry about this more later :] */
45#include <pthread.h>
46
47
48#ifdef HAVE_DAG_CONFIG_API_H
49#include <dag_config_api.h>
50#endif
51
52#ifdef WIN32
53#  include <io.h>
54#  include <share.h>
55#  define PATH_MAX _MAX_PATH
56#  define snprintf sprintf_s
57#else
58#  include <netdb.h>
59#  ifndef PATH_MAX
60#       define PATH_MAX 4096
61#  endif
62#  include <sys/ioctl.h>
63#endif
64
65/* This format deals with DAG cards that are using drivers from the 2.5 version
66 * onwards, including 3.X.
67 *
68 * DAG is a LIVE capture format.
69 *
70 * This format does support writing, provided the DAG card that you are using
71 * has transmit (Tx) support. Additionally, packets read using this format
72 * are in the ERF format, so can easily be written as ERF traces without
73 * losing any data.
74 */
75
76#define DATA(x) ((struct dag_format_data_t *)x->format_data)
77#define DATA_OUT(x) ((struct dag_format_data_out_t *)x->format_data)
78#define STREAM_DATA(x) ((struct dag_per_stream_t *)x->data)
79
80#define FORMAT_DATA DATA(libtrace)
81#define FORMAT_DATA_OUT DATA_OUT(libtrace)
82
83#define DUCK FORMAT_DATA->duck
84
85#define FORMAT_DATA_HEAD FORMAT_DATA->per_stream->head
86#define FORMAT_DATA_FIRST ((struct dag_per_stream_t *)FORMAT_DATA_HEAD->data)
87
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/* Data that is stored against each input stream */
118struct dag_per_stream_t {
119        /* DAG stream number */
120        uint16_t dagstream;
121        /* Pointer to the last unread byte in the DAG memory */
122        uint8_t *top;
123        /* Pointer to the first unread byte in the DAG memory */
124        uint8_t *bottom;
125        /* Amount of data processed from the bottom pointer */
126        uint32_t processed;
127        /* Number of packets seen by the stream */
128        uint64_t pkt_count;
129        /* Drop count for this particular stream */
130        uint64_t drops;
131        /* Boolean values to indicate if a particular interface has been seen
132         * or not. This is limited to four interfaces, which is enough to
133         * support all current DAG cards */
134        uint8_t seeninterface[4];
135};
136
137/* "Global" data that is stored for each DAG input trace */
138struct dag_format_data_t {
139        /* DAG device */
140        struct dag_dev_t *device;
141        /* Boolean flag indicating whether the trace is currently attached */
142        int stream_attached;
143        /* Data stored against each DAG input stream */
144        libtrace_list_t *per_stream;
145
146        /* Data required for regular DUCK reporting.
147         * We put this on a new cache line otherwise we have a lot of false
148         * sharing caused by updating the last_pkt.
149         * This should only ever be accessed by the first thread stream,
150         * that includes both read and write operations.
151         */
152        struct {
153                /* Timestamp of the last DUCK report */
154                uint32_t last_duck;
155                /* The number of seconds between each DUCK report */
156                uint32_t duck_freq;
157                /* Timestamp of the last packet read from the DAG card */
158                uint32_t last_pkt;
159                /* Dummy trace to ensure DUCK packets are dealt with using the
160                 * DUCK format functions */
161                libtrace_t *dummy_duck;
162        } duck ALIGN_STRUCT(CACHE_LINE_SIZE);
163};
164
165/* To be thread-safe, we're going to need a mutex for operating on the list
166 * of DAG devices */
167pthread_mutex_t open_dag_mutex;
168
169/* The list of DAG devices that have been opened by libtrace.
170 *
171 * We can only open each DAG device once, but we might want to read from
172 * multiple streams. Therefore, we need to maintain a list of devices that we
173 * have opened (with ref counts!) so that we don't try to open a device too
174 * many times or close a device that we're still using */
175struct dag_dev_t *open_dags = NULL;
176
177/* Returns the amount of padding between the ERF header and the start of the
178 * captured packet data */
179static int dag_get_padding(const libtrace_packet_t *packet)
180{
181        /* ERF Ethernet records have a 2 byte padding before the packet itself
182         * so that the IP header is aligned on a 32 bit boundary.
183         */
184        if (packet->trace->format->type==TRACE_FORMAT_ERF) {
185                dag_record_t *erfptr = (dag_record_t *)packet->header;
186                switch(erfptr->type) {
187                        case TYPE_ETH:
188                        case TYPE_COLOR_ETH:
189                        case TYPE_DSM_COLOR_ETH:
190                        case TYPE_COLOR_HASH_ETH:
191                                return 2;
192                        default:                return 0;
193                }
194        }
195        else {
196                switch(trace_get_link_type(packet)) {
197                        case TRACE_TYPE_ETH:    return 2;
198                        default:                return 0;
199                }
200        }
201}
202
203/* Attempts to determine if the given filename refers to a DAG device */
204static int dag_probe_filename(const char *filename)
205{
206        struct stat statbuf;
207        /* Can we stat the file? */
208        if (stat(filename, &statbuf) != 0) {
209                return 0;
210        }
211        /* Is it a character device? */
212        if (!S_ISCHR(statbuf.st_mode)) {
213                return 0;
214        }
215        /* Yeah, it's probably us. */
216        return 1;
217}
218
219/* Initialises the DAG output data structure */
220static void dag_init_format_out_data(libtrace_out_t *libtrace)
221{
222        libtrace->format_data = (struct dag_format_data_out_t *)
223                malloc(sizeof(struct dag_format_data_out_t));
224        // no DUCK on output
225        FORMAT_DATA_OUT->stream_attached = 0;
226        FORMAT_DATA_OUT->device = NULL;
227        FORMAT_DATA_OUT->dagstream = 0;
228        FORMAT_DATA_OUT->waiting = 0;
229
230}
231
232/* Initialises the DAG input data structure */
233static void dag_init_format_data(libtrace_t *libtrace)
234{
235        struct dag_per_stream_t stream_data;
236
237        libtrace->format_data = (struct dag_format_data_t *)
238                malloc(sizeof(struct dag_format_data_t));
239        DUCK.last_duck = 0;
240        DUCK.duck_freq = 0;
241        DUCK.last_pkt = 0;
242        DUCK.dummy_duck = NULL;
243
244        FORMAT_DATA->per_stream =
245                libtrace_list_init(sizeof(stream_data));
246        if (FORMAT_DATA->per_stream == NULL) {
247                trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
248                        "Unable to create list for stream in dag_init_format_data()");
249                return -1;
250        }
251
252        /* We'll start with just one instance of stream_data, and we'll
253         * add more later if we need them */
254        memset(&stream_data, 0, sizeof(stream_data));
255        libtrace_list_push_back(FORMAT_DATA->per_stream, &stream_data);
256}
257
258/* Determines if there is already an entry for the given DAG device in the
259 * device list and increments the reference count for that device, if found.
260 *
261 * NOTE: This function assumes the open_dag_mutex is held by the caller */
262static struct dag_dev_t *dag_find_open_device(char *dev_name)
263{
264        struct dag_dev_t *dag_dev;
265
266        dag_dev = open_dags;
267
268        /* XXX: Not exactly zippy, but how often are we going to be dealing
269         * with multiple dag cards? */
270        while (dag_dev != NULL) {
271                if (strcmp(dag_dev->dev_name, dev_name) == 0) {
272                        dag_dev->ref_count ++;
273                        return dag_dev;
274                }
275                dag_dev = dag_dev->next;
276        }
277        return NULL;
278}
279
280/* Closes a DAG device and removes it from the device list.
281 *
282 * Attempting to close a DAG device that has a non-zero reference count will
283 * cause an assertion failure!
284 *
285 * NOTE: This function assumes the open_dag_mutex is held by the caller */
286static void dag_close_device(struct dag_dev_t *dev)
287{
288        /* Need to remove from the device list */
289        if (dev->ref_count != 0) {
290                fprintf(stderr, "Cannot close DAG device with non zero reference in dag_close_device()\n");
291                return;
292        }
293
294        if (dev->prev == NULL) {
295                open_dags = dev->next;
296                if (dev->next)
297                        dev->next->prev = NULL;
298        } else {
299                dev->prev->next = dev->next;
300                if (dev->next)
301                        dev->next->prev = dev->prev;
302        }
303
304        dag_close(dev->fd);
305        free(dev);
306}
307
308
309/* Opens a new DAG device for writing and adds it to the DAG device list
310 *
311 * NOTE: this function should only be called when opening a DAG device for
312 * writing - there is little practical difference between this and the
313 * function below that covers the reading case, but we need the output trace
314 * object to report errors properly so the two functions take slightly
315 * different arguments. This is really lame and there should be a much better
316 * way of doing this.
317 *
318 * NOTE: This function assumes the open_dag_mutex is held by the caller
319 */
320static struct dag_dev_t *dag_open_output_device(libtrace_out_t *libtrace,
321                                                char *dev_name)
322{
323        struct stat buf;
324        int fd;
325        struct dag_dev_t *new_dev;
326
327        /* Make sure the device exists */
328        if (stat(dev_name, &buf) == -1) {
329                trace_set_err_out(libtrace,errno,"stat(%s)",dev_name);
330                return NULL;
331        }
332
333        /* Make sure it is the appropriate type of device */
334        if (S_ISCHR(buf.st_mode)) {
335                /* Try opening the DAG device */
336                if((fd = dag_open(dev_name)) < 0) {
337                        trace_set_err_out(libtrace,errno,"Cannot open DAG %s",
338                                        dev_name);
339                        return NULL;
340                }
341        } else {
342                trace_set_err_out(libtrace,errno,"Not a valid dag device: %s",
343                                dev_name);
344                return NULL;
345        }
346
347        /* Add the device to our device list - it is just a doubly linked
348         * list with no inherent ordering; just tack the new one on the front
349         */
350        new_dev = (struct dag_dev_t *)malloc(sizeof(struct dag_dev_t));
351        new_dev->fd = fd;
352        new_dev->dev_name = dev_name;
353        new_dev->ref_count = 1;
354
355        new_dev->prev = NULL;
356        new_dev->next = open_dags;
357        if (open_dags)
358                open_dags->prev = new_dev;
359
360        open_dags = new_dev;
361
362        return new_dev;
363}
364
365/* Opens a new DAG device for reading and adds it to the DAG device list
366 *
367 * NOTE: this function should only be called when opening a DAG device for
368 * reading - there is little practical difference between this and the
369 * function above that covers the writing case, but we need the input trace
370 * object to report errors properly so the two functions take slightly
371 * different arguments. This is really lame and there should be a much better
372 * way of doing this.
373 *
374 * NOTE: This function assumes the open_dag_mutex is held by the caller */
375static struct dag_dev_t *dag_open_device(libtrace_t *libtrace, char *dev_name) {
376        struct stat buf;
377        int fd;
378        struct dag_dev_t *new_dev;
379
380        /* Make sure the device exists */
381        if (stat(dev_name, &buf) == -1) {
382                trace_set_err(libtrace,errno,"stat(%s)",dev_name);
383                return NULL;
384        }
385
386        /* Make sure it is the appropriate type of device */
387        if (S_ISCHR(buf.st_mode)) {
388                /* Try opening the DAG device */
389                if((fd = dag_open(dev_name)) < 0) {
390                        trace_set_err(libtrace,errno,"Cannot open DAG %s",
391                                      dev_name);
392                        return NULL;
393                }
394        } else {
395                trace_set_err(libtrace,errno,"Not a valid dag device: %s",
396                              dev_name);
397                return NULL;
398        }
399
400        /* Add the device to our device list - it is just a doubly linked
401         * list with no inherent ordering; just tack the new one on the front
402         */
403        new_dev = (struct dag_dev_t *)malloc(sizeof(struct dag_dev_t));
404        new_dev->fd = fd;
405        new_dev->dev_name = dev_name;
406        new_dev->ref_count = 1;
407
408        new_dev->prev = NULL;
409        new_dev->next = open_dags;
410        if (open_dags)
411                open_dags->prev = new_dev;
412
413        open_dags = new_dev;
414
415        return new_dev;
416}
417
418/* Creates and initialises a DAG output trace */
419static int dag_init_output(libtrace_out_t *libtrace)
420{
421        /* Upon successful creation, the device name is stored against the
422         * device and free when it is free()d */
423        char *dag_dev_name = NULL;
424        char *scan = NULL;
425        struct dag_dev_t *dag_device = NULL;
426        int stream = 1;
427
428        /* XXX I don't know if this is important or not, but this function
429         * isn't present in all of the driver releases that this code is
430         * supposed to support! */
431        /*
432        unsigned long wake_time;
433        dagutil_sleep_get_wake_time(&wake_time,0);
434        */
435
436        dag_init_format_out_data(libtrace);
437        /* Grab the mutex while we're likely to be messing with the device
438         * list */
439        pthread_mutex_lock(&open_dag_mutex);
440
441        /* Specific streams are signified using a comma in the libtrace URI,
442         * e.g. dag:/dev/dag0,1 refers to stream 1 on the dag0 device.
443         *
444         * If no stream is specified, we will write using stream 1 */
445        if ((scan = strchr(libtrace->uridata,',')) == NULL) {
446                dag_dev_name = strdup(libtrace->uridata);
447        } else {
448                dag_dev_name = (char *)strndup(libtrace->uridata,
449                                (size_t)(scan - libtrace->uridata));
450                stream = atoi(++scan);
451        }
452        FORMAT_DATA_OUT->dagstream = stream;
453
454        /* See if our DAG device is already open */
455        dag_device = dag_find_open_device(dag_dev_name);
456
457        if (dag_device == NULL) {
458                /* Device not yet opened - open it ourselves */
459                dag_device = dag_open_output_device(libtrace, dag_dev_name);
460        } else {
461                /* Otherwise, just use the existing one */
462                free(dag_dev_name);
463                dag_dev_name = NULL;
464        }
465
466        /* Make sure we have successfully opened a DAG device */
467        if (dag_device == NULL) {
468                if (dag_dev_name) {
469                        free(dag_dev_name);
470                }
471                pthread_mutex_unlock(&open_dag_mutex);
472                return -1;
473        }
474
475        FORMAT_DATA_OUT->device = dag_device;
476        pthread_mutex_unlock(&open_dag_mutex);
477        return 0;
478}
479
480/* Creates and initialises a DAG input trace */
481static int dag_init_input(libtrace_t *libtrace) {
482        /* Upon successful creation, the device name is stored against the
483         * device and free when it is free()d */
484        char *dag_dev_name = NULL;
485        char *scan = NULL;
486        int stream = 0;
487        struct dag_dev_t *dag_device = NULL;
488
489        dag_init_format_data(libtrace);
490        /* Grab the mutex while we're likely to be messing with the device
491         * list */
492        pthread_mutex_lock(&open_dag_mutex);
493
494
495        /* DAG cards support multiple streams. In a single threaded capture,
496         * these are specified using a comma in the libtrace URI,
497         * e.g. dag:/dev/dag0,2 refers to stream 2 on the dag0 device.
498         *
499         * If no stream is specified, we will read from stream 0 with
500         * one thread
501         */
502        if ((scan = strchr(libtrace->uridata,',')) == NULL) {
503                dag_dev_name = strdup(libtrace->uridata);
504        } else {
505                dag_dev_name = (char *)strndup(libtrace->uridata,
506                                (size_t)(scan - libtrace->uridata));
507                stream = atoi(++scan);
508        }
509
510        FORMAT_DATA_FIRST->dagstream = stream;
511
512        /* See if our DAG device is already open */
513        dag_device = dag_find_open_device(dag_dev_name);
514
515        if (dag_device == NULL) {
516                /* Device not yet opened - open it ourselves */
517                dag_device = dag_open_device(libtrace, dag_dev_name);
518        } else {
519                /* Otherwise, just use the existing one */
520                free(dag_dev_name);
521                dag_dev_name = NULL;
522        }
523
524        /* Make sure we have successfully opened a DAG device */
525        if (dag_device == NULL) {
526                if (dag_dev_name)
527                        free(dag_dev_name);
528                dag_dev_name = NULL;
529                pthread_mutex_unlock(&open_dag_mutex);
530                trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to open DAG device %s", dag_dev_name);
531                return -1;
532        }
533
534        FORMAT_DATA->device = dag_device;
535
536        /* See Config_Status_API_Programming_Guide.pdf from the Endace
537           Dag Documentation */
538        /* Check kBooleanAttributeActive is true -- no point capturing
539         * on an interface that's disabled
540         *
541         * The symptom of the port being disabled is that libtrace
542         * will appear to hang. */
543        /* Check kBooleanAttributeFault is false */
544        /* Check kBooleanAttributeLocalFault is false */
545        /* Check kBooleanAttributeLock is true ? */
546        /* Check kBooleanAttributePeerLink ? */
547
548        /* Set kBooleanAttributePromisc/kBooleanPromiscuousMode based
549           on libtrace promisc attribute?*/
550        /* Set kUint32AttributeSnapLength to the snaplength */
551
552        pthread_mutex_unlock(&open_dag_mutex);
553        return 0;
554}
555
556#ifdef HAVE_DAG_CONFIG_API_H
557static int dag_csapi_set_snaplen(libtrace_t *libtrace, int slen) {
558        dag_card_ref_t card_ref = NULL;
559        dag_component_t root = NULL;
560        attr_uuid_t uuid = 0;
561
562        if (slen < 0)
563                slen = 0; 
564
565        card_ref = dag_config_init(FORMAT_DATA->device->dev_name);
566        root = dag_config_get_root_component(card_ref);
567       
568        uuid = dag_component_get_config_attribute_uuid(root, kBooleanAttributeVarlen);
569        dag_config_set_boolean_attribute(card_ref, uuid, true);
570
571        uuid = dag_component_get_config_attribute_uuid(root, kUint32AttributeSnaplength);
572        dag_config_set_uint32_attribute(card_ref, uuid, (uint32_t)slen);
573
574        return 0;
575       
576
577} 
578#endif /* HAVE_DAG_CONFIG_API_H */
579
580/* Configures a DAG input trace */
581static int dag_config_input(libtrace_t *libtrace, trace_option_t option,
582                            void *data)
583{
584        switch(option) {
585        case TRACE_OPTION_META_FREQ:
586                /* This option is used to specify the frequency of DUCK
587                 * updates */
588                DUCK.duck_freq = *(int *)data;
589                return 0;
590        case TRACE_OPTION_SNAPLEN:
591#ifdef HAVE_DAG_CONFIG_API_H
592                return dag_csapi_set_snaplen(libtrace, *(int *)data);
593#else
594                /* Tell the card our new snap length */
595        {
596                char conf_str[4096];
597                snprintf(conf_str, 4096, "varlen slen=%i", *(int *)data);
598                if (dag_configure(FORMAT_DATA->device->fd,
599                                  conf_str) != 0) {
600                        trace_set_err(libtrace, errno, "Failed to configure "
601                                      "snaplen on DAG card: %s",
602                                      libtrace->uridata);
603                        return -1;
604                }
605        }
606#endif /* HAVE_DAG_CONFIG_API_H */
607
608                return 0;
609        case TRACE_OPTION_PROMISC:
610                /* DAG already operates in a promisc fashion */
611                return -1;
612        case TRACE_OPTION_FILTER:
613                /* We don't yet support pushing filters into DAG
614                 * cards */
615                return -1;
616        case TRACE_OPTION_EVENT_REALTIME:
617        case TRACE_OPTION_REPLAY_SPEEDUP:
618                /* Live capture is always going to be realtime */
619                return -1;
620        case TRACE_OPTION_HASHER:
621                /* Lets just say we did this, it's currently still up to
622                 * the user to configure this correctly. */
623                return 0;
624        }
625        return -1;
626}
627
628/* Starts a DAG output trace */
629static int dag_start_output(libtrace_out_t *libtrace)
630{
631        struct timeval zero, nopoll;
632
633        zero.tv_sec = 0;
634        zero.tv_usec = 0;
635        nopoll = zero;
636
637        /* Attach and start the DAG stream */
638        if (dag_attach_stream64(FORMAT_DATA_OUT->device->fd,
639                        FORMAT_DATA_OUT->dagstream, 0, 4 * 1024 * 1024) < 0) {
640                trace_set_err_out(libtrace, errno, "Cannot attach DAG stream");
641                return -1;
642        }
643
644        if (dag_start_stream(FORMAT_DATA_OUT->device->fd,
645                        FORMAT_DATA_OUT->dagstream) < 0) {
646                trace_set_err_out(libtrace, errno, "Cannot start DAG stream");
647                return -1;
648        }
649        FORMAT_DATA_OUT->stream_attached = 1;
650
651        /* We don't want the dag card to do any sleeping */
652        dag_set_stream_poll64(FORMAT_DATA_OUT->device->fd,
653                        FORMAT_DATA_OUT->dagstream, 0, &zero,
654                        &nopoll);
655
656        return 0;
657}
658
659static int dag_start_input_stream(libtrace_t *libtrace,
660                                  struct dag_per_stream_t * stream) {
661        struct timeval zero, nopoll;
662        uint8_t *top, *bottom, *starttop;
663        top = bottom = NULL;
664
665        zero.tv_sec = 0;
666        zero.tv_usec = 10000;
667        nopoll = zero;
668
669        /* Attach and start the DAG stream */
670        if (dag_attach_stream64(FORMAT_DATA->device->fd,
671                              stream->dagstream, 0, 0) < 0) {
672                trace_set_err(libtrace, errno, "Cannot attach DAG stream #%u",
673                              stream->dagstream);
674                return -1;
675        }
676
677        if (dag_start_stream(FORMAT_DATA->device->fd,
678                             stream->dagstream) < 0) {
679                trace_set_err(libtrace, errno, "Cannot start DAG stream #%u",
680                              stream->dagstream);
681                return -1;
682        }
683        FORMAT_DATA->stream_attached = 1;
684
685        /* We don't want the dag card to do any sleeping */
686        if (dag_set_stream_poll64(FORMAT_DATA->device->fd,
687                            stream->dagstream, 0, &zero,
688                            &nopoll) < 0) {
689                trace_set_err(libtrace, errno,
690                              "dag_set_stream_poll failed!");
691                return -1;
692        }
693
694        starttop = dag_advance_stream(FORMAT_DATA->device->fd,
695                                      stream->dagstream,
696                                      &bottom);
697
698        /* Should probably flush the memory hole now */
699        top = starttop;
700        while (starttop - bottom > 0) {
701                bottom += (starttop - bottom);
702                top = dag_advance_stream(FORMAT_DATA->device->fd,
703                                         stream->dagstream,
704                                         &bottom);
705        }
706        stream->top = top;
707        stream->bottom = bottom;
708        stream->processed = 0;
709        stream->drops = 0;
710
711        return 0;
712
713}
714
715/* Starts a DAG input trace */
716static int dag_start_input(libtrace_t *libtrace)
717{
718        return dag_start_input_stream(libtrace, FORMAT_DATA_FIRST);
719}
720
721static int dag_pstart_input(libtrace_t *libtrace)
722{
723        char *scan, *tok;
724        uint16_t stream_count = 0, max_streams;
725        int iserror = 0;
726        struct dag_per_stream_t stream_data;
727
728        /* Check we aren't trying to create more threads than the DAG card can
729         * handle */
730        max_streams = dag_rx_get_stream_count(FORMAT_DATA->device->fd);
731        if (libtrace->perpkt_thread_count > max_streams) {
732                fprintf(stderr,
733                              "WARNING: DAG has only %u streams available, "
734                              "capping total number of threads at this value.",
735                              max_streams);
736                libtrace->perpkt_thread_count = max_streams;
737        }
738
739        /* Get the stream names from the uri */
740        if ((scan = strchr(libtrace->uridata, ',')) == NULL) {
741                trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT,
742                              "Format uri doesn't specify the DAG streams");
743                iserror = 1;
744                goto cleanup;
745        }
746
747        scan++;
748
749        tok = strtok(scan, ",");
750        while (tok != NULL) {
751                /* Ensure we haven't specified too many streams */
752                if (stream_count >= libtrace->perpkt_thread_count) {
753                        fprintf(stderr,
754                                      "WARNING: Format uri specifies too many "
755                                      "streams. Maximum is %u, so only using "
756                                      "the first %u from the uri.",
757                                      libtrace->perpkt_thread_count, 
758                                      libtrace->perpkt_thread_count);
759                        break;
760                }
761
762                /* Save the stream details */
763                if (stream_count == 0) {
764                        /* Special case where we update the existing stream
765                         * data structure */
766                        FORMAT_DATA_FIRST->dagstream = (uint16_t)atoi(tok);
767                } else {
768                        memset(&stream_data, 0, sizeof(stream_data));
769                        stream_data.dagstream = (uint16_t)atoi(tok);
770                        libtrace_list_push_back(FORMAT_DATA->per_stream,
771                                                &stream_data);
772                }
773
774                stream_count++;
775                tok = strtok(NULL, ",");
776        }
777
778        if (stream_count < libtrace->perpkt_thread_count) {
779                libtrace->perpkt_thread_count = stream_count;
780        }
781       
782        FORMAT_DATA->stream_attached = 1;
783
784 cleanup:
785        if (iserror) {
786                return -1;
787        } else {
788                return 0;
789        }
790}
791
792/* Pauses a DAG output trace */
793static int dag_pause_output(libtrace_out_t *libtrace)
794{
795        /* Stop and detach the stream */
796        if (dag_stop_stream(FORMAT_DATA_OUT->device->fd,
797                            FORMAT_DATA_OUT->dagstream) < 0) {
798                trace_set_err_out(libtrace, errno, "Could not stop DAG stream");
799                return -1;
800        }
801        if (dag_detach_stream(FORMAT_DATA_OUT->device->fd,
802                              FORMAT_DATA_OUT->dagstream) < 0) {
803                trace_set_err_out(libtrace, errno,
804                                  "Could not detach DAG stream");
805                return -1;
806        }
807        FORMAT_DATA_OUT->stream_attached = 0;
808        return 0;
809}
810
811/* Pauses a DAG input trace */
812static int dag_pause_input(libtrace_t *libtrace)
813{
814        libtrace_list_node_t *tmp = FORMAT_DATA_HEAD;
815
816        /* Stop and detach each stream */
817        while (tmp != NULL) {
818                if (dag_stop_stream(FORMAT_DATA->device->fd,
819                                    STREAM_DATA(tmp)->dagstream) < 0) {
820                        trace_set_err(libtrace, errno,
821                                      "Could not stop DAG stream");
822                        return -1;
823                }
824                if (dag_detach_stream(FORMAT_DATA->device->fd,
825                                      STREAM_DATA(tmp)->dagstream) < 0) {
826                        trace_set_err(libtrace, errno,
827                                      "Could not detach DAG stream");
828                        return -1;
829                }
830
831                tmp = tmp->next;
832        }
833
834        FORMAT_DATA->stream_attached = 0;
835        return 0;
836}
837
838
839
840/* Closes a DAG input trace */
841static int dag_fin_input(libtrace_t *libtrace)
842{
843        /* Need the lock, since we're going to be handling the device list */
844        pthread_mutex_lock(&open_dag_mutex);
845
846        /* Detach the stream if we are not paused */
847        if (FORMAT_DATA->stream_attached)
848                dag_pause_input(libtrace);
849        FORMAT_DATA->device->ref_count--;
850
851        /* Close the DAG device if there are no more references to it */
852        if (FORMAT_DATA->device->ref_count == 0)
853                dag_close_device(FORMAT_DATA->device);
854
855        if (DUCK.dummy_duck)
856                trace_destroy_dead(DUCK.dummy_duck);
857
858        /* Clear the list */
859        libtrace_list_deinit(FORMAT_DATA->per_stream);
860        free(libtrace->format_data);
861        pthread_mutex_unlock(&open_dag_mutex);
862        return 0; /* success */
863}
864
865/* Closes a DAG output trace */
866static int dag_fin_output(libtrace_out_t *libtrace)
867{
868
869        /* Commit any outstanding traffic in the txbuffer */
870        if (FORMAT_DATA_OUT->waiting) {
871                dag_tx_stream_commit_bytes(FORMAT_DATA_OUT->device->fd,
872                                           FORMAT_DATA_OUT->dagstream,
873                                           FORMAT_DATA_OUT->waiting );
874        }
875
876        /* Wait until the buffer is nearly clear before exiting the program,
877         * as we will lose packets otherwise */
878        dag_tx_get_stream_space64
879                (FORMAT_DATA_OUT->device->fd,
880                 FORMAT_DATA_OUT->dagstream,
881                 dag_get_stream_buffer_size64(FORMAT_DATA_OUT->device->fd,
882                                            FORMAT_DATA_OUT->dagstream) - 8);
883
884        /* Need the lock, since we're going to be handling the device list */
885        pthread_mutex_lock(&open_dag_mutex);
886
887        /* Detach the stream if we are not paused */
888        if (FORMAT_DATA_OUT->stream_attached)
889                dag_pause_output(libtrace);
890        FORMAT_DATA_OUT->device->ref_count --;
891
892        /* Close the DAG device if there are no more references to it */
893        if (FORMAT_DATA_OUT->device->ref_count == 0)
894                dag_close_device(FORMAT_DATA_OUT->device);
895        free(libtrace->format_data);
896        pthread_mutex_unlock(&open_dag_mutex);
897        return 0; /* success */
898}
899
900#ifdef DAGIOC_CARD_DUCK
901#define LIBTRACE_DUCK_IOCTL DAGIOC_CARD_DUCK
902#define LIBTRACE_DUCK_VERSION TRACE_RT_DUCK_5_0
903#else
904#ifdef DAGIOCDUCK
905#define LIBTRACE_DUCK_IOCTL DAGIOCDUCK
906#define LIBTRACE_DUCK_VERSION TRACE_RT_DUCK_2_5
907#else
908#warning "DAG appears to be missing DUCK support"
909#endif
910#endif
911
912/* Extracts DUCK information from the DAG card and produces a DUCK packet */
913static int dag_get_duckinfo(libtrace_t *libtrace,
914                                libtrace_packet_t *packet) {
915
916        if (DUCK.duck_freq == 0)
917                return 0;
918
919#ifndef LIBTRACE_DUCK_IOCTL
920        trace_set_err(libtrace, errno, 
921                "Requested DUCK information but unable to determine the correct ioctl for DUCK");
922        DUCK.duck_freq = 0;
923        return -1;
924#endif
925
926        if (DUCK.last_pkt - DUCK.last_duck < DUCK.duck_freq)
927                return 0;
928
929        /* Allocate memory for the DUCK data */
930        if (packet->buf_control == TRACE_CTRL_EXTERNAL ||
931            !packet->buffer) {
932                packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE);
933                packet->buf_control = TRACE_CTRL_PACKET;
934                if (!packet->buffer) {
935                        trace_set_err(libtrace, errno,
936                                      "Cannot allocate packet buffer");
937                        return -1;
938                }
939        }
940
941        /* DUCK doesn't have a format header */
942        packet->header = 0;
943        packet->payload = packet->buffer;
944
945        /* No need to check if we can get DUCK or not - we're modern
946         * enough so just grab the DUCK info */
947        if ((ioctl(FORMAT_DATA->device->fd, LIBTRACE_DUCK_IOCTL,
948                   (duckinf_t *)packet->payload) < 0)) {
949                trace_set_err(libtrace, errno, "Error using DUCK ioctl");
950                DUCK.duck_freq = 0;
951                return -1;
952        }
953
954        packet->type = LIBTRACE_DUCK_VERSION;
955
956        /* Set the packet's trace to point at a DUCK trace, so that the
957         * DUCK format functions will be called on the packet rather than the
958         * DAG ones */
959        if (!DUCK.dummy_duck)
960                DUCK.dummy_duck = trace_create_dead("duck:dummy");
961        packet->trace = DUCK.dummy_duck;
962        DUCK.last_duck = DUCK.last_pkt;
963        packet->error = sizeof(duckinf_t);
964        return sizeof(duckinf_t);
965}
966
967/* Determines the amount of data available to read from the DAG card */
968static int dag_available(libtrace_t *libtrace,
969                         struct dag_per_stream_t *stream_data)
970{
971        uint32_t diff = stream_data->top - stream_data->bottom;
972
973        /* If we've processed more than 4MB of data since we last called
974         * dag_advance_stream, then we should call it again to allow the
975         * space occupied by that 4MB to be released */
976        if (diff >= dag_record_size && stream_data->processed < 4 * 1024 * 1024)
977                return diff;
978
979        /* Update the top and bottom pointers */
980        stream_data->top = dag_advance_stream(FORMAT_DATA->device->fd,
981                                              stream_data->dagstream,
982                                              &(stream_data->bottom));
983
984        if (stream_data->top == NULL) {
985                trace_set_err(libtrace, errno, "dag_advance_stream failed!");
986                return -1;
987        }
988        stream_data->processed = 0;
989        diff = stream_data->top - stream_data->bottom;
990        return diff;
991}
992
993/* Returns a pointer to the start of the next complete ERF record */
994static dag_record_t *dag_get_record(struct dag_per_stream_t *stream_data)
995{
996        dag_record_t *erfptr = NULL;
997        uint16_t size;
998
999        erfptr = (dag_record_t *)stream_data->bottom;
1000        if (!erfptr)
1001                return NULL;
1002
1003        size = ntohs(erfptr->rlen);
1004        if (size < dag_record_size) {
1005                fprintf(stderr, "DAG2.5 rlen is invalid (rlen %u, must be at least %u\n",
1006                        size, dag_record_size);
1007                return NULL;
1008        }
1009
1010        /* Make certain we have the full packet available */
1011        if (size > (stream_data->top - stream_data->bottom))
1012                return NULL;
1013
1014        stream_data->bottom += size;
1015        stream_data->processed += size;
1016        return erfptr;
1017}
1018
1019/* Converts a buffer containing a recently read DAG packet record into a
1020 * libtrace packet */
1021static int dag_prepare_packet_stream(libtrace_t *libtrace,
1022                                     struct dag_per_stream_t *stream_data,
1023                                     libtrace_packet_t *packet,
1024                                     void *buffer, libtrace_rt_types_t rt_type,
1025                                     uint32_t flags)
1026{
1027        dag_record_t *erfptr;
1028
1029        /* If the packet previously owned a buffer that is not the buffer
1030         * that contains the new packet data, we're going to need to free the
1031         * old one to avoid memory leaks */
1032        if (packet->buffer != buffer &&
1033            packet->buf_control == TRACE_CTRL_PACKET) {
1034                free(packet->buffer);
1035        }
1036
1037        /* Set the buffer owner appropriately */
1038        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) {
1039                packet->buf_control = TRACE_CTRL_PACKET;
1040        } else
1041                packet->buf_control = TRACE_CTRL_EXTERNAL;
1042
1043        /* Update the packet pointers and type appropriately */
1044        erfptr = (dag_record_t *)buffer;
1045        packet->buffer = erfptr;
1046        packet->header = erfptr;
1047        packet->type = rt_type;
1048
1049        if (erfptr->flags.rxerror == 1) {
1050                /* rxerror means the payload is corrupt - drop the payload
1051                 * by tweaking rlen */
1052                packet->payload = NULL;
1053                erfptr->rlen = htons(erf_get_framing_length(packet));
1054        } else {
1055                packet->payload = (char*)packet->buffer
1056                        + erf_get_framing_length(packet);
1057        }
1058
1059        if (libtrace->format_data == NULL) {
1060                dag_init_format_data(libtrace);
1061        }
1062
1063        /* Update the dropped packets counter */
1064        /* No loss counter for DSM coloured records - have to use some
1065         * other API */
1066        if (erf_is_color_type(erfptr->type)) {
1067                /* TODO */
1068        } else {
1069                /* Use the ERF loss counter */
1070                if (stream_data->seeninterface[erfptr->flags.iface]
1071                    == 0) {
1072                        stream_data->seeninterface[erfptr->flags.iface]
1073                                = 1;
1074                } else {
1075                        stream_data->drops += ntohs(erfptr->lctr);
1076                }
1077        }
1078
1079        return 0;
1080}
1081
1082static int dag_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet,
1083                              void *buffer, libtrace_rt_types_t rt_type,
1084                              uint32_t flags)
1085{
1086        return dag_prepare_packet_stream(libtrace, FORMAT_DATA_FIRST, packet,
1087                                       buffer, rt_type, flags);
1088}
1089
1090/*
1091 * dag_write_packet() at this stage attempts to improve tx performance
1092 * by delaying sending a dag_tx_stream_commit_bytes() until a threshold
1093 * has been met. I observed approximately 270% performance increase
1094 * through this relatively naive tweak. No optimisation of buffer sizes
1095 * was attempted.
1096 */
1097
1098/* Pushes an ERF record onto the transmit stream */
1099static int dag_dump_packet(libtrace_out_t *libtrace,
1100                           dag_record_t *erfptr, unsigned int pad,
1101                           void *buffer)
1102{
1103        int size;
1104
1105        /*
1106         * If we've got 0 bytes waiting in the txqueue, assume that we
1107         * haven't requested any space yet, and request some, storing
1108         * the pointer at FORMAT_DATA_OUT->txbuffer.
1109         *
1110         * The amount to request is slightly magical at the moment - it's
1111         * 16Mebibytes + 128 kibibytes to ensure that we can copy a packet into
1112         * the buffer and handle overruns.
1113         */
1114        if (FORMAT_DATA_OUT->waiting == 0) {
1115                FORMAT_DATA_OUT->txbuffer =
1116                        dag_tx_get_stream_space64(FORMAT_DATA_OUT->device->fd,
1117                                                FORMAT_DATA_OUT->dagstream,
1118                                                16908288);
1119        }
1120
1121        /*
1122         * Copy the header separately to the body, as we can't guarantee they
1123         * are in contiguous memory
1124         */
1125        memcpy(FORMAT_DATA_OUT->txbuffer + FORMAT_DATA_OUT->waiting, erfptr,
1126               (dag_record_size + pad));
1127        FORMAT_DATA_OUT->waiting += (dag_record_size + pad);
1128
1129        /*
1130         * Copy our incoming packet into the outgoing buffer, and increment
1131         * our waiting count
1132         */
1133        size = ntohs(erfptr->rlen)-(dag_record_size + pad);
1134        memcpy(FORMAT_DATA_OUT->txbuffer + FORMAT_DATA_OUT->waiting, buffer,
1135               size);
1136        FORMAT_DATA_OUT->waiting += size;
1137
1138        /*
1139         * If our output buffer has more than 16 Mebibytes in it, commit those
1140         * bytes and reset the waiting count to 0.
1141         * Note: dag_fin_output will also call dag_tx_stream_commit_bytes() in
1142         * case there is still data in the buffer at program exit.
1143         */
1144        if (FORMAT_DATA_OUT->waiting >= 16*1024*1024) {
1145                FORMAT_DATA_OUT->txbuffer =
1146                        dag_tx_stream_commit_bytes(FORMAT_DATA_OUT->device->fd,
1147                                                   FORMAT_DATA_OUT->dagstream,
1148                                                   FORMAT_DATA_OUT->waiting);
1149                FORMAT_DATA_OUT->waiting = 0;
1150        }
1151
1152        return size + pad + dag_record_size;
1153}
1154
1155/* Attempts to determine a suitable ERF type for a given packet. Returns true
1156 * if one is found, false otherwise */
1157static bool find_compatible_linktype(libtrace_out_t *libtrace,
1158                                     libtrace_packet_t *packet, char *type)
1159{
1160        /* Keep trying to simplify the packet until we can find
1161         * something we can do with it */
1162
1163        do {
1164                *type = libtrace_to_erf_type(trace_get_link_type(packet));
1165
1166                /* Success */
1167                if (*type != (char)-1)
1168                        return true;
1169
1170                if (!demote_packet(packet)) {
1171                        trace_set_err_out(libtrace,
1172                                          TRACE_ERR_NO_CONVERSION,
1173                                          "No erf type for packet (%i)",
1174                                          trace_get_link_type(packet));
1175                        return false;
1176                }
1177
1178        } while(1);
1179
1180        return true;
1181}
1182
1183/* Writes a packet to the provided DAG output trace */
1184static int dag_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet)
1185{
1186        /* This is heavily borrowed from erf_write_packet(). Yes, CnP
1187         * coding sucks, sorry about that.
1188         */
1189        unsigned int pad = 0;
1190        int numbytes;
1191        void *payload = packet->payload;
1192        dag_record_t *header = (dag_record_t *)packet->header;
1193        char erf_type = 0;
1194
1195        if(!packet->header) {
1196                /* No header, probably an RT packet. Lifted from
1197                 * erf_write_packet(). */
1198                return -1;
1199        }
1200
1201        if (trace_get_link_type(packet) == TRACE_TYPE_NONDATA)
1202                return 0;
1203
1204        pad = dag_get_padding(packet);
1205
1206        /*
1207         * If the payload is null, adjust the rlen. Discussion of this is
1208         * attached to erf_write_packet()
1209         */
1210        if (payload == NULL) {
1211                header->rlen = htons(dag_record_size + pad);
1212        }
1213
1214        if (packet->type == TRACE_RT_DATA_ERF) {
1215                numbytes = dag_dump_packet(libtrace, header, pad, payload);
1216        } else {
1217                /* Build up a new packet header from the existing header */
1218
1219                /* Simplify the packet first - if we can't do this, break
1220                 * early */
1221                if (!find_compatible_linktype(libtrace,packet,&erf_type))
1222                        return -1;
1223
1224                dag_record_t erfhdr;
1225
1226                erfhdr.ts = bswap_host_to_le64(trace_get_erf_timestamp(packet));
1227                payload=packet->payload;
1228                pad = dag_get_padding(packet);
1229
1230                /* Flags. Can't do this */
1231                memset(&erfhdr.flags,1,sizeof(erfhdr.flags));
1232                if (trace_get_direction(packet)!=(int)~0U)
1233                        erfhdr.flags.iface = trace_get_direction(packet);
1234
1235                erfhdr.type = erf_type;
1236
1237                /* Packet length (rlen includes format overhead) */
1238                if (trace_get_capture_length(packet) <= 0
1239                       || trace_get_capture_length(packet) > 65536) {
1240                        trace_set_err(libtrace, TRACE_ERR_BAD_PACKET,
1241                                "Capture length is out of range in dag_write_packet()");
1242                        return -1;
1243                }
1244                if (erf_get_framing_length(packet) <= 0
1245                       || trace_get_framing_length(packet) > 65536) {
1246                        trace_set_err(libtrace, TRACE_ERR_BAD_PACKET,
1247                                "Framing length is out of range in dag_write_packet()");
1248                        return -1;
1249                }
1250                if (trace_get_capture_length(packet) +
1251                       erf_get_framing_length(packet) <= 0
1252                       || trace_get_capture_length(packet) +
1253                       erf_get_framing_length(packet) > 65536) {
1254                        trace_set_err(libtrace, TRACE_ERR_BAD_PACKET,
1255                                "Capture + framing length is out of range in dag_write_packet()");
1256                        return -1;
1257                }
1258
1259                erfhdr.rlen = htons(trace_get_capture_length(packet)
1260                                    + erf_get_framing_length(packet));
1261
1262
1263                /* Loss counter. Can't do this */
1264                erfhdr.lctr = 0;
1265                /* Wire length, does not include padding! */
1266                erfhdr.wlen = htons(trace_get_wire_length(packet));
1267
1268                /* Write it out */
1269                numbytes = dag_dump_packet(libtrace, &erfhdr, pad, payload);
1270        }
1271
1272        return numbytes;
1273}
1274
1275/* Reads the next available packet from a DAG card, in a BLOCKING fashion
1276 *
1277 * If DUCK reporting is enabled, the packet returned may be a DUCK update
1278 */
1279static int dag_read_packet_stream(libtrace_t *libtrace,
1280                                struct dag_per_stream_t *stream_data,
1281                                libtrace_thread_t *t, /* Optional */
1282                                libtrace_packet_t *packet)
1283{
1284        int size = 0;
1285        dag_record_t *erfptr = NULL;
1286        struct timeval tv;
1287        int numbytes = 0;
1288        uint32_t flags = 0;
1289        struct timeval maxwait, pollwait;
1290
1291        pollwait.tv_sec = 0;
1292        pollwait.tv_usec = 10000;
1293        maxwait.tv_sec = 0;
1294        maxwait.tv_usec = 250000;
1295
1296        /* Check if we're due for a DUCK report - only report on the first thread */
1297        if (stream_data == FORMAT_DATA_FIRST) {
1298                size = dag_get_duckinfo(libtrace, packet);
1299                if (size != 0)
1300                        return size;
1301        }
1302
1303
1304        /* Don't let anyone try to free our DAG memory hole! */
1305        flags |= TRACE_PREP_DO_NOT_OWN_BUFFER;
1306
1307        /* If the packet buffer is currently owned by libtrace, free it so
1308         * that we can set the packet to point into the DAG memory hole */
1309        if (packet->buf_control == TRACE_CTRL_PACKET) {
1310                free(packet->buffer);
1311                packet->buffer = 0;
1312        }
1313
1314        if (dag_set_stream_poll64(FORMAT_DATA->device->fd, stream_data->dagstream,
1315                                sizeof(dag_record_t), &maxwait,
1316                                &pollwait) == -1) {
1317                trace_set_err(libtrace, errno, "dag_set_stream_poll");
1318                return -1;
1319        }
1320
1321        /* Grab a full ERF record */
1322        do {
1323                numbytes = dag_available(libtrace, stream_data);
1324                if (numbytes < 0)
1325                        return numbytes;
1326                if (numbytes < dag_record_size) {
1327                        /* Check the message queue if we have one to check */
1328                        if (t != NULL &&
1329                            libtrace_message_queue_count(&t->messages) > 0)
1330                                return -2;
1331
1332                        if ((numbytes=is_halted(libtrace)) != -1)
1333                                return numbytes;
1334                        /* Block until we see a packet */
1335                        continue;
1336                }
1337                erfptr = dag_get_record(stream_data);
1338        } while (erfptr == NULL);
1339
1340        packet->trace = libtrace;
1341
1342        /* Prepare the libtrace packet */
1343        if (dag_prepare_packet_stream(libtrace, stream_data, packet, erfptr,
1344                                    TRACE_RT_DATA_ERF, flags))
1345                return -1;
1346
1347        /* Update the DUCK timer - don't re-order this check (false-sharing) */
1348        if (stream_data == FORMAT_DATA_FIRST && DUCK.duck_freq != 0) {
1349                tv = trace_get_timeval(packet);
1350                DUCK.last_pkt = tv.tv_sec;
1351        }
1352
1353        packet->order = erf_get_erf_timestamp(packet);
1354        packet->error = packet->payload ? htons(erfptr->rlen) :
1355                                          erf_get_framing_length(packet);
1356
1357        return packet->error;
1358}
1359
1360static int dag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet)
1361{
1362        return dag_read_packet_stream(libtrace, FORMAT_DATA_FIRST, NULL, packet);
1363}
1364
1365static int dag_pread_packets(libtrace_t *libtrace, libtrace_thread_t *t,
1366                             libtrace_packet_t **packets, size_t nb_packets)
1367{
1368        int ret;
1369        size_t read_packets = 0;
1370        int numbytes = 0;
1371
1372        struct dag_per_stream_t *stream_data =
1373                (struct dag_per_stream_t *)t->format_data;
1374
1375        /* Read as many packets as we can, but read atleast one packet */
1376        do {
1377                ret = dag_read_packet_stream(libtrace, stream_data, t,
1378                                           packets[read_packets]);
1379                if (ret < 0)
1380                        return ret;
1381
1382                read_packets++;
1383
1384                /* Make sure we don't read too many packets..! */
1385                if (read_packets >= nb_packets)
1386                        break;
1387
1388                numbytes = dag_available(libtrace, stream_data);
1389        } while (numbytes >= dag_record_size);
1390
1391        return read_packets;
1392}
1393
1394/* Attempts to read a packet from a DAG card in a NON-BLOCKING fashion. If a
1395 * packet is available, we will return a packet event. Otherwise we will
1396 * return a SLEEP event (as we cannot select on the DAG file descriptor).
1397 */
1398static libtrace_eventobj_t trace_event_dag(libtrace_t *libtrace,
1399                                           libtrace_packet_t *packet)
1400{
1401        libtrace_eventobj_t event = {0,0,0.0,0};
1402        dag_record_t *erfptr = NULL;
1403        int numbytes;
1404        uint32_t flags = 0;
1405        struct timeval minwait, tv;
1406       
1407        minwait.tv_sec = 0;
1408        minwait.tv_usec = 10000;
1409
1410        /* Check if we're meant to provide a DUCK update */
1411        numbytes = dag_get_duckinfo(libtrace, packet);
1412        if (numbytes < 0) {
1413                event.type = TRACE_EVENT_TERMINATE;
1414                return event;
1415        } else if (numbytes > 0) {
1416                event.type = TRACE_EVENT_PACKET;
1417                return event;
1418        }
1419       
1420        if (dag_set_stream_poll64(FORMAT_DATA->device->fd,
1421                                FORMAT_DATA_FIRST->dagstream, 0, &minwait,
1422                                &minwait) == -1) {
1423                trace_set_err(libtrace, errno, "dag_set_stream_poll");
1424                event.type = TRACE_EVENT_TERMINATE;
1425                return event;
1426        }
1427
1428        do {
1429                erfptr = NULL;
1430                numbytes = 0;
1431
1432                /* Need to call dag_available so that the top pointer will get
1433                 * updated, otherwise we'll never see any data! */
1434                numbytes = dag_available(libtrace, FORMAT_DATA_FIRST);
1435
1436                /* May as well not bother calling dag_get_record if
1437                 * dag_available suggests that there's no data */
1438                if (numbytes != 0)
1439                        erfptr = dag_get_record(FORMAT_DATA_FIRST);
1440                if (erfptr == NULL) {
1441                        /* No packet available - sleep for a very short time */
1442                        if (libtrace_halt) {
1443                                event.type = TRACE_EVENT_TERMINATE;
1444                        } else {
1445                                event.type = TRACE_EVENT_SLEEP;
1446                                event.seconds = 0.0001;
1447                        }
1448                        break;
1449                }
1450                if (dag_prepare_packet_stream(libtrace, FORMAT_DATA_FIRST, packet,
1451                                            erfptr, TRACE_RT_DATA_ERF, flags)) {
1452                        event.type = TRACE_EVENT_TERMINATE;
1453                        break;
1454                }
1455
1456
1457                event.size = trace_get_capture_length(packet) +
1458                        trace_get_framing_length(packet);
1459
1460                /* XXX trace_read_packet() normally applies the following
1461                 * config options for us, but this function is called via
1462                 * trace_event() so we have to do it ourselves */
1463
1464                if (libtrace->filter) {
1465                        int filtret = trace_apply_filter(libtrace->filter,
1466                                                         packet);
1467                        if (filtret == -1) {
1468                                trace_set_err(libtrace, TRACE_ERR_BAD_FILTER,
1469                                              "Bad BPF Filter");
1470                                event.type = TRACE_EVENT_TERMINATE;
1471                                break;
1472                        }
1473
1474                        if (filtret == 0) {
1475                                /* This packet isn't useful so we want to
1476                                 * immediately see if there is another suitable
1477                                 * one - we definitely DO NOT want to return
1478                                 * a sleep event in this case, like we used to
1479                                 * do! */
1480                                libtrace->filtered_packets ++;
1481                                trace_clear_cache(packet);
1482                                continue;
1483                        }
1484
1485                        event.type = TRACE_EVENT_PACKET;
1486                } else {
1487                        event.type = TRACE_EVENT_PACKET;
1488                }
1489
1490                /* Update the DUCK timer */
1491                tv = trace_get_timeval(packet);
1492                DUCK.last_pkt = tv.tv_sec;
1493               
1494                if (libtrace->snaplen > 0) {
1495                        trace_set_capture_length(packet, libtrace->snaplen);
1496                }
1497                libtrace->accepted_packets ++;
1498                break;
1499        } while(1);
1500
1501        return event;
1502}
1503
1504static void dag_get_statistics(libtrace_t *libtrace, libtrace_stat_t *stat)
1505{
1506        libtrace_list_node_t *tmp;
1507        if (!libtrace) {
1508                fprintf(stderr, "NULL trace passed into dag_get_statistics()\n");
1509                return;
1510        }
1511        if (!stat) {
1512                trace_set_err(libtrace, TRACE_ERR_STAT, "NULL stat passed into dag_get_statistics()");
1513                return;
1514        }
1515        tmp = FORMAT_DATA_HEAD;
1516
1517        /* Dropped packets */
1518        stat->dropped_valid = 1;
1519        stat->dropped = 0;
1520        while (tmp != NULL) {
1521                stat->dropped += STREAM_DATA(tmp)->drops;
1522                tmp = tmp->next;
1523        }
1524
1525}
1526
1527static void dag_get_thread_statistics(libtrace_t *libtrace, libtrace_thread_t *t,
1528                                       libtrace_stat_t *stat) {
1529        struct dag_per_stream_t *stream_data = t->format_data;
1530        if (!libtrace) {
1531                fprintf(stderr, "NULL trace passed into dag_get_thread_statistics()\n");
1532                return;
1533        }
1534        if (!stat) {
1535                trace_set_err(libtrace, TRACE_ERR_STAT, "NULL stat passed into dag_get_thread_statistics()");
1536                return;
1537        }
1538        stat->dropped_valid = 1;
1539        stat->dropped = stream_data->drops;
1540
1541        stat->filtered_valid = 1;
1542        stat->filtered = 0;
1543}
1544
1545/* Prints some semi-useful help text about the DAG format module */
1546static void dag_help(void) {
1547        printf("dag format module: $Revision: 1755 $\n");
1548        printf("Supported input URIs:\n");
1549        printf("\tdag:/dev/dagn\n");
1550        printf("\n");
1551        printf("\te.g.: dag:/dev/dag0\n");
1552        printf("\n");
1553        printf("Supported output URIs:\n");
1554        printf("\tnone\n");
1555        printf("\n");
1556}
1557
1558static int dag_pregister_thread(libtrace_t *libtrace, libtrace_thread_t *t,
1559                                bool reader)
1560{
1561        struct dag_per_stream_t *stream_data;
1562        libtrace_list_node_t *node;
1563
1564        if (reader && t->type == THREAD_PERPKT) {
1565                node = libtrace_list_get_index(FORMAT_DATA->per_stream,
1566                                                t->perpkt_num);
1567                if (node == NULL) {
1568                        return -1;
1569                }
1570                stream_data = node->data;
1571
1572                /* Pass the per thread data to the thread */
1573                t->format_data = stream_data;
1574
1575                /* Attach and start the DAG stream */
1576                if (dag_start_input_stream(libtrace, stream_data) < 0)
1577                        return -1;
1578        }
1579
1580        return 0;
1581}
1582
1583static struct libtrace_format_t dag = {
1584        "dag",
1585        "$Id$",
1586        TRACE_FORMAT_ERF,
1587        dag_probe_filename,             /* probe filename */
1588        NULL,                           /* probe magic */
1589        dag_init_input,                 /* init_input */
1590        dag_config_input,               /* config_input */
1591        dag_start_input,                /* start_input */
1592        dag_pause_input,                /* pause_input */
1593        dag_init_output,                /* init_output */
1594        NULL,                           /* config_output */
1595        dag_start_output,               /* start_output */
1596        dag_fin_input,                  /* fin_input */
1597        dag_fin_output,                 /* fin_output */
1598        dag_read_packet,                /* read_packet */
1599        dag_prepare_packet,             /* prepare_packet */
1600        NULL,                           /* fin_packet */
1601        dag_write_packet,               /* write_packet */
1602        NULL,                           /* flush_output */
1603        erf_get_link_type,              /* get_link_type */
1604        erf_get_direction,              /* get_direction */
1605        erf_set_direction,              /* set_direction */
1606        erf_get_erf_timestamp,          /* get_erf_timestamp */
1607        NULL,                           /* get_timeval */
1608        NULL,                           /* get_seconds */
1609        NULL,                           /* get_timespec */
1610        NULL,                           /* seek_erf */
1611        NULL,                           /* seek_timeval */
1612        NULL,                           /* seek_seconds */
1613        erf_get_capture_length,         /* get_capture_length */
1614        erf_get_wire_length,            /* get_wire_length */
1615        erf_get_framing_length,         /* get_framing_length */
1616        erf_set_capture_length,         /* set_capture_length */
1617        NULL,                           /* get_received_packets */
1618        NULL,                           /* get_filtered_packets */
1619        NULL,                           /* get_dropped_packets */
1620        dag_get_statistics,             /* get_statistics */
1621        NULL,                           /* get_fd */
1622        trace_event_dag,                /* trace_event */
1623        dag_help,                       /* help */
1624        NULL,                            /* next pointer */
1625        {true, 0}, /* live packet capture, thread limit TBD */
1626        dag_pstart_input,
1627        dag_pread_packets,
1628        dag_pause_input,
1629        NULL,
1630        dag_pregister_thread,
1631        NULL,
1632        dag_get_thread_statistics       /* get thread stats */
1633};
1634
1635void dag_constructor(void)
1636{
1637        register_format(&dag);
1638}
Note: See TracBrowser for help on using the repository browser.