1176998Sphk/*-
2176998Sphk * Copyright (c) 2005-2008 Poul-Henning Kamp
3176998Sphk * All rights reserved.
4176998Sphk *
5176998Sphk * Redistribution and use in source and binary forms, with or without
6176998Sphk * modification, are permitted provided that the following conditions
7176998Sphk * are met:
8176998Sphk * 1. Redistributions of source code must retain the above copyright
9176998Sphk *    notice, this list of conditions and the following disclaimer.
10176998Sphk * 2. Redistributions in binary form must reproduce the above copyright
11176998Sphk *    notice, this list of conditions and the following disclaimer in the
12176998Sphk *    documentation and/or other materials provided with the distribution.
13176998Sphk *
14176998Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15176998Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16176998Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17176998Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18176998Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19176998Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20176998Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21176998Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22176998Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23176998Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24176998Sphk * SUCH DAMAGE.
25176998Sphk *
26176998Sphk * $FreeBSD$
27176998Sphk */
28176998Sphk
29176998Sphk#ifndef __LOCAL_FIFOLOG_H_
30176998Sphk#define __LOCAL_FIFOLOG_H_
31176998Sphk
32176998Sphk/*
33176998Sphk * Definitions for fifolog "protocol": the on-media layout.
34176998Sphk *
35176998Sphk * The fifolog on-media record has three layers:
36210930Suqs *   The outer timestamping and synchronization layer.
37210930Suqs *   The zlib implemented data compression.
38176998Sphk *   The inner sequencing and identification layer.
39176998Sphk *
40210930Suqs * All three layers are synchronized at a subset of the outer layer
41176998Sphk * record boundaries, from where reading can be initiated.
42176998Sphk *
43176998Sphk *
44210930Suqs * The outer layer:
45176998Sphk * -----------------
46176998Sphk * The first record in a fifolog contains a magic string and version
47176998Sphk * information along with a 32be encoded recordsize for all records
48176998Sphk * in the fifolog, including the first.
49176998Sphk * The recordsize is explicit to avoid ambiguities when a media is
50176998Sphk * moved from one machine to another.
51176998Sphk *
52176998Sphk * Each record in the fifolog has the following contents:
53176998Sphk *	offset	type	contents
54176998Sphk *      --------------------------------------------------------------
55176998Sphk *	0	32be	sequence_number
56176998Sphk *			The sequence number is randomly chosen for the
57176998Sphk *			fifolog and increments once for each record written.
58210930Suqs *			It's presence allow quick identification of the next
59176998Sphk *			record to be written using a binary search for the
60176998Sphk *			first place where a discontinuity in the sequence
61176998Sphk *			numbers occur.
62176998Sphk *	4	 8	flags (FIFOLOG_FLG_*)
63176998Sphk *
64176998Sphk * If (flags & (FIFOLOG_FLG_SYNC)) the record is a synchronization point
65176998Sphk * at which the inner layers are aligned so that reading can be started
66176998Sphk * at this point.
67176998Sphk * To enable seeks into the file based on timestamps, a third field is
68176998Sphk * present in these records as well:
69176998Sphk *	5	32be	time_t containing POSIX's understanding of UTC.
70176998Sphk *
71219027Sphk * These fields are immediately followed by the inner layer payload as
72176998Sphk * described below, which has variable length.
73176998Sphk *
74176998Sphk * If the inner layer payload is shorter than the available space in
75176998Sphk * the record, it is padded with zero bytes, and the number of unused
76176998Sphk * bytes, including the encoded length thereof is recorded at the end
77176998Sphk * of the record as follows:
78176998Sphk *
79176998Sphk * If (flags & FIFOLOG_FLG_1BYTE)
80176998Sphk *	n-1	8	number of unused bytes
81176998Sphk * else if (flags & FIFOLOG_FLG_4BYTE)
82176998Sphk *	n-4	32be	number of unused bytes
83176998Sphk *
84176998Sphk *
85176998Sphk * The gzip layer
86176998Sphk * --------------
87176998Sphk * Is just output from zlib, nothing special here.  FIFOLOG_FLG_SYNC
88176998Sphk * corresponds to Z_FINISH flags to zlib.
89176998Sphk * In most cases, the timer will expire before zlib has filled an entire
90176998Sphk * record in which case Z_SYNC_FLUSH will be used to force as much as
91176998Sphk * possible into the buffer before it is written.  This is not marked
92210930Suqs * in outer layer (apart from a natural correlation with padding) since
93176998Sphk * zlibs data stream handles this without help.
94176998Sphk *
95176998Sphk *
96176998Sphk * The inner layer:
97176998Sphk * ----------------
98219027Sphk * The inner layer contains data identification and to the second
99219027Sphk * timestamping (the timestamp in the outer layer only marks the
100176998Sphk * first possible timestamp for content in the SYNC record).
101219027Sphk *
102176998Sphk *	offset	type	contents
103176998Sphk *      --------------------------------------------------------------
104176998Sphk *	0	32be	ident
105176998Sphk *
106176998Sphk * The bottom 30 bits of the identification word are application defined,
107176998Sphk * presently unused in the stand-alone fifolog tools, but used in the
108176998Sphk * original "measured" application that originated the fifolog format.
109176998Sphk * Should for instance syslogd(8) grow native support for fifolog format,
110176998Sphk * it could store the message priority here.
111176998Sphk *
112176998Sphk * If (ident & FIFOLOG_TIMESTAMP) the record is prefixed by:
113176998Sphk *	4	32be	time_t containing POSIX's understanding of UTC.
114176998Sphk *
115176998Sphk * Then follows the content, either as a NUL terminated string or as
116210930Suqs * a length encoded binary sequence:
117176998Sphk *
118176998Sphk * If (ident & FIFOLOG_LENGTH) the record is prefixed by:
119176998Sphk *	{0|4}	8	length of binary data
120176998Sphk *
121176998Sphk */
122176998Sphk
123176998Sphk/* Magic identification string */
124176998Sphk#define FIFOLOG_FMT_MAGIC	"Measured FIFOLOG Ver 1.01\n"
125176998Sphk
126176998Sphk/* Offset of the 32be encoded recordsize in the first sector */
127176998Sphk#define FIFOLOG_OFF_BS		0x20
128176998Sphk
129176998Sphk#define FIFOLOG_FLG_1BYTE	0x01
130176998Sphk#define FIFOLOG_FLG_4BYTE	0x02
131176998Sphk#define FIFOLOG_FLG_RESTART	0x40
132176998Sphk#define FIFOLOG_FLG_SYNC	0x80
133176998Sphk
134176998Sphk#define FIFOLOG_TIMESTAMP	0x80000000
135176998Sphk#define FIFOLOG_LENGTH		0x40000000
136176998Sphk#define FIFOLOG_IDENT		0x3fffffff
137176998Sphk
138176998Sphk#endif /* __LOCAL_FIFOLOG_H_ */
139