source: lib/format_atmhdr.c @ 0a1d2d0

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

Adds a configuration option for the tick messages.
Adds the trace_information structure which contains information about traces.
Updates trace_rt_stats to use both of these.

Replaced libtrace_t->joined internally with a state

  • Property mode set to 100644
File size: 7.7 KB
RevLine 
[5952ff0]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
[e1fdc05]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"
[c66068d]41#include "wandio.h"
[e1fdc05]42
43#include <sys/stat.h>
44#include <assert.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <stdio.h>
48#include <string.h>
49#include <stdlib.h>
50
[5952ff0]51/* This particular format covers the ATM cell header capture format used to
52 * take the Auckland VII trace set.
53 *
54 * Each capture record contains only a timestamp and the first four bytes of
55 * the ATM header - nothing else.
56 *
57 * As a result, there isn't a lot you can actually do with these traces!
58 *
59 * Libtrace does not support writing using this format, because it is so
60 * useless :)
61 */
62
63/* Returns the size of the ATM cell framing header */
[e1fdc05]64static int atmhdr_get_framing_length(const libtrace_packet_t *packet UNUSED)
65{
66        return sizeof(atmhdr_t);
67}
68
[5952ff0]69/* Initialise an input trace to read an ATM cell header capture */
[e1fdc05]70static int atmhdr_init_input(libtrace_t *libtrace) {
[91b72d3]71        libtrace->format_data = NULL; /* No format data needed */
[e1fdc05]72        return 0;
73}
74
[5952ff0]75/* Start an ATM cell header input trace */
[e1fdc05]76static int atmhdr_start_input(libtrace_t *libtrace)
77{
[91b72d3]78        if (libtrace->io) /* Already open? */
[e1fdc05]79                return 0;
[91b72d3]80        libtrace->io = trace_open_file(libtrace);
81        if (libtrace->io)
[e1fdc05]82                return 0;
83        return -1;
84}
85
[5952ff0]86/* Close an ATM cell header input trace */
[e1fdc05]87static int atmhdr_fin_input(libtrace_t *libtrace)
88{
[91b72d3]89        wandio_destroy(libtrace->io);
[e1fdc05]90        return 0;
91}
92
[5952ff0]93
94/* Converts a buffer containing a recently read ATM cell header record into
95 * a libtrace packet */
[f0fb38f]96static int atmhdr_prepare_packet(libtrace_t *libtrace, 
97                libtrace_packet_t *packet, void *buffer, 
98                libtrace_rt_types_t rt_type, uint32_t flags) {
99
[5952ff0]100        /* If the packet previously owned a buffer that was not the buffer
101         * containing the new packet data, we need to free the old one to
102         * avoid leaking memory */
[f0fb38f]103        if (packet->buffer != buffer &&
104                        packet->buf_control == TRACE_CTRL_PACKET) {
105                free(packet->buffer);
106        }
107
[5952ff0]108        /* Set the buffer owner appropriately */
[f0fb38f]109        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) {
110                packet->buf_control = TRACE_CTRL_PACKET;
111        } else
112                packet->buf_control = TRACE_CTRL_EXTERNAL;
113
[5952ff0]114        /* Update the packet pointers appropriately */
[f0fb38f]115        packet->buffer = buffer;
116        packet->header = buffer;
117        packet->payload = (void*)((char*)packet->buffer + 
118                        libtrace->format->get_framing_length(packet));
[5952ff0]119
120        /* Set the packet type */
[f0fb38f]121        packet->type = rt_type;
122
123        return 0;
124}
125
[5952ff0]126/* Reads the next ATM cell header record from the given trace and writes it
127 * into a libtrace packet */
[e1fdc05]128static int atmhdr_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
129        int numbytes;
130        void *buffer;
[f0fb38f]131        uint32_t flags = 0;
132       
[5952ff0]133        /* Make sure we have a buffer available to read the next record into */
[e1fdc05]134        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) {
135                packet->buffer=malloc((size_t)LIBTRACE_PACKET_BUFSIZE);
136        }
137        buffer = packet->buffer;
[f0fb38f]138        flags |= TRACE_PREP_OWN_BUFFER;
139       
[e1fdc05]140        packet->type = TRACE_RT_DATA_ATMHDR;
141
[5952ff0]142        /* The records are a fixed size so we can read the entire record in
143         * one go */
[91b72d3]144        if ((numbytes=wandio_read(libtrace->io, buffer, (size_t)12)) != 12)
[e1fdc05]145        {
146                if (numbytes != 0) {
147                        trace_set_err(libtrace,errno,"read(%s)",libtrace->uridata);
148                }
149                return numbytes;
150        }
151
[5952ff0]152        /* Update all our packet pointers appropriately */
[f0fb38f]153        if (atmhdr_prepare_packet(libtrace, packet, buffer, 
154                                TRACE_RT_DATA_ATMHDR, flags)) {
155                return -1;
156        }
157                               
158       
[e1fdc05]159        return 12;
160}
161
[5952ff0]162/* Get the link type for an ATM cell header record */
[e1fdc05]163static libtrace_linktype_t atmhdr_get_link_type(const libtrace_packet_t *packet UNUSED) {
[5952ff0]164        /* Unsurprisingly, we're always going to be an ATM header */
[e1fdc05]165        return TRACE_TYPE_ATM;
166}
167
[5952ff0]168/* Get the capture length for an ATM cell header record */
[e1fdc05]169static int atmhdr_get_capture_length(const libtrace_packet_t *packet UNUSED) {
[5952ff0]170        /* There is always 4 bytes of ATM header retained by this format */
[e1fdc05]171        return 4;
172}
173
[5952ff0]174/* Get the wire length for an ATM cell header record */
[e1fdc05]175static int atmhdr_get_wire_length(const libtrace_packet_t *packet UNUSED) {
[5952ff0]176        /* ATM packets are 53 byte fixed length records */
[e1fdc05]177        return 53;
178}
179
[5952ff0]180/* Returns the timestamp for an ATM cell header record in the ERF timestamp
181 * format */
[e1fdc05]182static uint64_t atmhdr_get_erf_timestamp(const libtrace_packet_t *packet) {
183        uint64_t ts;
184        atmhdr_t *atm = (atmhdr_t *)packet->header;
[5952ff0]185       
186        /* Basically, the capture format header is an ERF timestamp except
187         * the two 32-bit segments are reversed */
[e1fdc05]188        ts = (uint64_t)atm->ts_fraction + ((uint64_t)atm->ts_sec << 32);
189
190        return ts;
191}
192
193static struct libtrace_format_t atmhdr = {
194        "atmhdr",
195        "$Id$",
196        TRACE_FORMAT_ATMHDR,
[91b72d3]197        NULL,                           /* probe filename */
198        NULL,                           /* probe magic */
[f2fae49]199        atmhdr_init_input,              /* init_input */
[e1fdc05]200        NULL,                           /* config_input */
[f2fae49]201        atmhdr_start_input,             /* start_input */
[e1fdc05]202        NULL,                           /* pause_input */
203        NULL,                           /* init_output */
204        NULL,                           /* config_output */
205        NULL,                           /* start_output */
[f2fae49]206        atmhdr_fin_input,               /* fin_input */
[e1fdc05]207        NULL,                           /* fin_output */
208        atmhdr_read_packet,             /* read_packet */
[f0fb38f]209        atmhdr_prepare_packet,          /* prepare_packet */
210        NULL,                           /* fin_packet */
[e1fdc05]211        NULL,                           /* write_packet */
[f2fae49]212        atmhdr_get_link_type,           /* get_link_type */
[e1fdc05]213        NULL,                           /* get_direction */
214        NULL,                           /* set_direction */
215        atmhdr_get_erf_timestamp,       /* get_erf_timestamp */
216        NULL,                           /* get_timeval */
[1aa4bf7]217        NULL,                           /* get_timespec */
[e1fdc05]218        NULL,                           /* get_seconds */
219        NULL,                           /* seek_erf */
220        NULL,                           /* seek_timeval */
221        NULL,                           /* seek_seconds */
222        atmhdr_get_capture_length,      /* get_capture_length */
[f2fae49]223        atmhdr_get_wire_length,         /* get_wire_length */
224        atmhdr_get_framing_length,      /* get_framing_length */
[e1fdc05]225        NULL,                           /* set_capture_length */
[f2fae49]226        NULL,                           /* get_received_packets */
227        NULL,                           /* get_filtered_packets */
228        NULL,                           /* get_dropped_packets */
229        NULL,                           /* get_captured_packets */
[e1fdc05]230        NULL,                           /* get_fd */
231        trace_event_trace,              /* trace_event */
[f2fae49]232        NULL,                           /* help */
[b13b939]233        NULL,                            /* next pointer */
234        NON_PARALLEL(false)
[e1fdc05]235};
236       
237
238void atmhdr_constructor(void) {
239        register_format(&atmhdr);
240}
Note: See TracBrowser for help on using the repository browser.