source: lib/format_dag25.c @ 341c38f

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 341c38f was 341c38f, checked in by Richard Sanger <rsangerarj@…>, 6 years ago

Merge remote-tracking branch 'upstream/develop' into develop

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