source: lib/format_atmhdr.c @ 5ab626a

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

Deprecate trace_get_filtered/accepted/recevied/dropped() in favour of a single function

Adds the single trace_get_statistics function. This allows the structure to be filled
at a point in time, rather than making multiple calls to the library during which state
might have changed.

This has been designed such that the structure can be added to in the future without
breaking old code.

The old internal get_captured_packets was removed from the formats as it was never used.
Eventually we should completely remove get_filtered and received from the formats and replace
them with get_statistics.

In additon some extra fields have added, such as error and captured and the pre-existing
fields are better defined.

The linux formats have been updated to use this new API, which combined with reading
/proc/net/dev returns a full set of statistics.

  • Property mode set to 100644
File size: 7.7 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 "wandio.h"
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
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 */
64static int atmhdr_get_framing_length(const libtrace_packet_t *packet UNUSED)
65{
66        return sizeof(atmhdr_t);
67}
68
69/* Initialise an input trace to read an ATM cell header capture */
70static int atmhdr_init_input(libtrace_t *libtrace) {
71        libtrace->format_data = NULL; /* No format data needed */
72        return 0;
73}
74
75/* Start an ATM cell header input trace */
76static int atmhdr_start_input(libtrace_t *libtrace)
77{
78        if (libtrace->io) /* Already open? */
79                return 0;
80        libtrace->io = trace_open_file(libtrace);
81        if (libtrace->io)
82                return 0;
83        return -1;
84}
85
86/* Close an ATM cell header input trace */
87static int atmhdr_fin_input(libtrace_t *libtrace)
88{
89        wandio_destroy(libtrace->io);
90        return 0;
91}
92
93
94/* Converts a buffer containing a recently read ATM cell header record into
95 * a libtrace packet */
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
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 */
103        if (packet->buffer != buffer &&
104                        packet->buf_control == TRACE_CTRL_PACKET) {
105                free(packet->buffer);
106        }
107
108        /* Set the buffer owner appropriately */
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
114        /* Update the packet pointers appropriately */
115        packet->buffer = buffer;
116        packet->header = buffer;
117        packet->payload = (void*)((char*)packet->buffer + 
118                        libtrace->format->get_framing_length(packet));
119
120        /* Set the packet type */
121        packet->type = rt_type;
122
123        return 0;
124}
125
126/* Reads the next ATM cell header record from the given trace and writes it
127 * into a libtrace packet */
128static int atmhdr_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
129        int numbytes;
130        void *buffer;
131        uint32_t flags = 0;
132       
133        /* Make sure we have a buffer available to read the next record into */
134        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) {
135                packet->buffer=malloc((size_t)LIBTRACE_PACKET_BUFSIZE);
136        }
137        buffer = packet->buffer;
138        flags |= TRACE_PREP_OWN_BUFFER;
139       
140        packet->type = TRACE_RT_DATA_ATMHDR;
141
142        /* The records are a fixed size so we can read the entire record in
143         * one go */
144        if ((numbytes=wandio_read(libtrace->io, buffer, (size_t)12)) != 12)
145        {
146                if (numbytes != 0) {
147                        trace_set_err(libtrace,errno,"read(%s)",libtrace->uridata);
148                }
149                return numbytes;
150        }
151
152        /* Update all our packet pointers appropriately */
153        if (atmhdr_prepare_packet(libtrace, packet, buffer, 
154                                TRACE_RT_DATA_ATMHDR, flags)) {
155                return -1;
156        }
157                               
158       
159        return 12;
160}
161
162/* Get the link type for an ATM cell header record */
163static libtrace_linktype_t atmhdr_get_link_type(const libtrace_packet_t *packet UNUSED) {
164        /* Unsurprisingly, we're always going to be an ATM header */
165        return TRACE_TYPE_ATM;
166}
167
168/* Get the capture length for an ATM cell header record */
169static int atmhdr_get_capture_length(const libtrace_packet_t *packet UNUSED) {
170        /* There is always 4 bytes of ATM header retained by this format */
171        return 4;
172}
173
174/* Get the wire length for an ATM cell header record */
175static int atmhdr_get_wire_length(const libtrace_packet_t *packet UNUSED) {
176        /* ATM packets are 53 byte fixed length records */
177        return 53;
178}
179
180/* Returns the timestamp for an ATM cell header record in the ERF timestamp
181 * format */
182static uint64_t atmhdr_get_erf_timestamp(const libtrace_packet_t *packet) {
183        uint64_t ts;
184        atmhdr_t *atm = (atmhdr_t *)packet->header;
185       
186        /* Basically, the capture format header is an ERF timestamp except
187         * the two 32-bit segments are reversed */
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,
197        NULL,                           /* probe filename */
198        NULL,                           /* probe magic */
199        atmhdr_init_input,              /* init_input */
200        NULL,                           /* config_input */
201        atmhdr_start_input,             /* start_input */
202        NULL,                           /* pause_input */
203        NULL,                           /* init_output */
204        NULL,                           /* config_output */
205        NULL,                           /* start_output */
206        atmhdr_fin_input,               /* fin_input */
207        NULL,                           /* fin_output */
208        atmhdr_read_packet,             /* read_packet */
209        atmhdr_prepare_packet,          /* prepare_packet */
210        NULL,                           /* fin_packet */
211        NULL,                           /* write_packet */
212        atmhdr_get_link_type,           /* get_link_type */
213        NULL,                           /* get_direction */
214        NULL,                           /* set_direction */
215        atmhdr_get_erf_timestamp,       /* get_erf_timestamp */
216        NULL,                           /* get_timeval */
217        NULL,                           /* get_timespec */
218        NULL,                           /* get_seconds */
219        NULL,                           /* seek_erf */
220        NULL,                           /* seek_timeval */
221        NULL,                           /* seek_seconds */
222        atmhdr_get_capture_length,      /* get_capture_length */
223        atmhdr_get_wire_length,         /* get_wire_length */
224        atmhdr_get_framing_length,      /* get_framing_length */
225        NULL,                           /* set_capture_length */
226        NULL,                           /* get_received_packets */
227        NULL,                           /* get_filtered_packets */
228        NULL,                           /* get_dropped_packets */
229        NULL,                           /* get_statistics */
230        NULL,                           /* get_fd */
231        trace_event_trace,              /* trace_event */
232        NULL,                           /* help */
233        NULL,                            /* next pointer */
234        NON_PARALLEL(false)
235};
236       
237
238void atmhdr_constructor(void) {
239        register_format(&atmhdr);
240}
Note: See TracBrowser for help on using the repository browser.