118334Speter/*-
2117395Skan * Copyright (c) 2005-2008 Poul-Henning Kamp
318334Speter * All rights reserved.
4132718Skan *
518334Speter * Redistribution and use in source and binary forms, with or without
6132718Skan * modification, are permitted provided that the following conditions
718334Speter * are met:
818334Speter * 1. Redistributions of source code must retain the above copyright
918334Speter *    notice, this list of conditions and the following disclaimer.
1018334Speter * 2. Redistributions in binary form must reproduce the above copyright
11132718Skan *    notice, this list of conditions and the following disclaimer in the
1218334Speter *    documentation and/or other materials provided with the distribution.
1318334Speter *
1418334Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1518334Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1618334Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17132718Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2018334Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2118334Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2218334Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2318334Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2418334Speter * SUCH DAMAGE.
2518334Speter *
2618334Speter * $FreeBSD: releng/10.3/usr.sbin/fifolog/lib/fifolog.h 219027 2011-02-25 09:40:17Z phk $
2718334Speter */
2818334Speter
2950397Sobrien#ifndef __LOCAL_FIFOLOG_H_
3018334Speter#define __LOCAL_FIFOLOG_H_
3118334Speter
3218334Speter/*
3318334Speter * Definitions for fifolog "protocol": the on-media layout.
3418334Speter *
3518334Speter * The fifolog on-media record has three layers:
3618334Speter *   The outer timestamping and synchronization layer.
3718334Speter *   The zlib implemented data compression.
3818334Speter *   The inner sequencing and identification layer.
3918334Speter *
4018334Speter * All three layers are synchronized at a subset of the outer layer
4118334Speter * record boundaries, from where reading can be initiated.
4218334Speter *
4318334Speter *
4418334Speter * The outer layer:
4518334Speter * -----------------
4618334Speter * The first record in a fifolog contains a magic string and version
4718334Speter * information along with a 32be encoded recordsize for all records
4818334Speter * in the fifolog, including the first.
4990075Sobrien * The recordsize is explicit to avoid ambiguities when a media is
5018334Speter * moved from one machine to another.
5118334Speter *
5218334Speter * Each record in the fifolog has the following contents:
5390075Sobrien *	offset	type	contents
5418334Speter *      --------------------------------------------------------------
5518334Speter *	0	32be	sequence_number
5618334Speter *			The sequence number is randomly chosen for the
5790075Sobrien *			fifolog and increments once for each record written.
5818334Speter *			It's presence allow quick identification of the next
59117395Skan *			record to be written using a binary search for the
60117395Skan *			first place where a discontinuity in the sequence
6118334Speter *			numbers occur.
6218334Speter *	4	 8	flags (FIFOLOG_FLG_*)
6352284Sobrien *
6418334Speter * If (flags & (FIFOLOG_FLG_SYNC)) the record is a synchronization point
6590075Sobrien * at which the inner layers are aligned so that reading can be started
66 * at this point.
67 * To enable seeks into the file based on timestamps, a third field is
68 * present in these records as well:
69 *	5	32be	time_t containing POSIX's understanding of UTC.
70 *
71 * These fields are immediately followed by the inner layer payload as
72 * described below, which has variable length.
73 *
74 * If the inner layer payload is shorter than the available space in
75 * the record, it is padded with zero bytes, and the number of unused
76 * bytes, including the encoded length thereof is recorded at the end
77 * of the record as follows:
78 *
79 * If (flags & FIFOLOG_FLG_1BYTE)
80 *	n-1	8	number of unused bytes
81 * else if (flags & FIFOLOG_FLG_4BYTE)
82 *	n-4	32be	number of unused bytes
83 *
84 *
85 * The gzip layer
86 * --------------
87 * Is just output from zlib, nothing special here.  FIFOLOG_FLG_SYNC
88 * corresponds to Z_FINISH flags to zlib.
89 * In most cases, the timer will expire before zlib has filled an entire
90 * record in which case Z_SYNC_FLUSH will be used to force as much as
91 * possible into the buffer before it is written.  This is not marked
92 * in outer layer (apart from a natural correlation with padding) since
93 * zlibs data stream handles this without help.
94 *
95 *
96 * The inner layer:
97 * ----------------
98 * The inner layer contains data identification and to the second
99 * timestamping (the timestamp in the outer layer only marks the
100 * first possible timestamp for content in the SYNC record).
101 *
102 *	offset	type	contents
103 *      --------------------------------------------------------------
104 *	0	32be	ident
105 *
106 * The bottom 30 bits of the identification word are application defined,
107 * presently unused in the stand-alone fifolog tools, but used in the
108 * original "measured" application that originated the fifolog format.
109 * Should for instance syslogd(8) grow native support for fifolog format,
110 * it could store the message priority here.
111 *
112 * If (ident & FIFOLOG_TIMESTAMP) the record is prefixed by:
113 *	4	32be	time_t containing POSIX's understanding of UTC.
114 *
115 * Then follows the content, either as a NUL terminated string or as
116 * a length encoded binary sequence:
117 *
118 * If (ident & FIFOLOG_LENGTH) the record is prefixed by:
119 *	{0|4}	8	length of binary data
120 *
121 */
122
123/* Magic identification string */
124#define FIFOLOG_FMT_MAGIC	"Measured FIFOLOG Ver 1.01\n"
125
126/* Offset of the 32be encoded recordsize in the first sector */
127#define FIFOLOG_OFF_BS		0x20
128
129#define FIFOLOG_FLG_1BYTE	0x01
130#define FIFOLOG_FLG_4BYTE	0x02
131#define FIFOLOG_FLG_RESTART	0x40
132#define FIFOLOG_FLG_SYNC	0x80
133
134#define FIFOLOG_TIMESTAMP	0x80000000
135#define FIFOLOG_LENGTH		0x40000000
136#define FIFOLOG_IDENT		0x3fffffff
137
138#endif /* __LOCAL_FIFOLOG_H_ */
139