hast.h revision 247281
199461Sobrien/*-
2218822Sdim * Copyright (c) 2009-2010 The FreeBSD Foundation
399461Sobrien * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
499461Sobrien * All rights reserved.
599461Sobrien *
699461Sobrien * This software was developed by Pawel Jakub Dawidek under sponsorship from
799461Sobrien * the FreeBSD Foundation.
899461Sobrien *
999461Sobrien * Redistribution and use in source and binary forms, with or without
1099461Sobrien * modification, are permitted provided that the following conditions
1199461Sobrien * are met:
1299461Sobrien * 1. Redistributions of source code must retain the above copyright
1399461Sobrien *    notice, this list of conditions and the following disclaimer.
1499461Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1599461Sobrien *    notice, this list of conditions and the following disclaimer in the
1699461Sobrien *    documentation and/or other materials provided with the distribution.
1799461Sobrien *
1899461Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19218822Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20218822Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2199461Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22218822Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2399461Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2499461Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2599461Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2699461Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2799461Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2899461Sobrien * SUCH DAMAGE.
2999461Sobrien *
3099461Sobrien * $FreeBSD: head/sbin/hastd/hast.h 247281 2013-02-25 20:09:07Z trociny $
3199461Sobrien */
3299461Sobrien
3399461Sobrien#ifndef	_HAST_H_
3499461Sobrien#define	_HAST_H_
3599461Sobrien
36130561Sobrien#include <sys/queue.h>
3799461Sobrien#include <sys/socket.h>
3899461Sobrien
3999461Sobrien#include <arpa/inet.h>
4099461Sobrien
4199461Sobrien#include <netinet/in.h>
4299461Sobrien
4399461Sobrien#include <limits.h>
4499461Sobrien#include <pthread.h>
4599461Sobrien#include <stdbool.h>
4699461Sobrien#include <stdint.h>
4799461Sobrien
4899461Sobrien#include <activemap.h>
4999461Sobrien
5099461Sobrien#include "proto.h"
5199461Sobrien
52130561Sobrien/*
5399461Sobrien * Version history:
5499461Sobrien * 0 - initial version
5599461Sobrien * 1 - HIO_KEEPALIVE added
5699461Sobrien * 2 - "memsync" and "received" attributes added for memsync mode
57 */
58#define	HAST_PROTO_VERSION	2
59
60#define	EHAST_OK		0
61#define	EHAST_NOENTRY		1
62#define	EHAST_INVALID		2
63#define	EHAST_NOMEMORY		3
64#define	EHAST_UNIMPLEMENTED	4
65
66#define	HASTCTL_CMD_UNKNOWN	0
67#define	HASTCTL_CMD_SETROLE	1
68#define	HASTCTL_CMD_STATUS	2
69
70#define	HAST_ROLE_UNDEF		0
71#define	HAST_ROLE_INIT		1
72#define	HAST_ROLE_PRIMARY	2
73#define	HAST_ROLE_SECONDARY	3
74
75#define	HAST_SYNCSRC_UNDEF	0
76#define	HAST_SYNCSRC_PRIMARY	1
77#define	HAST_SYNCSRC_SECONDARY	2
78
79#define	HIO_UNDEF		0
80#define	HIO_READ		1
81#define	HIO_WRITE		2
82#define	HIO_DELETE		3
83#define	HIO_FLUSH		4
84#define	HIO_KEEPALIVE		5
85
86#define	HAST_USER		"hast"
87#define	HAST_TIMEOUT		20
88#define	HAST_CONFIG		"/etc/hast.conf"
89#define	HAST_CONTROL		"/var/run/hastctl"
90#define	HASTD_LISTEN_TCP4	"tcp4://0.0.0.0:8457"
91#define	HASTD_LISTEN_TCP6	"tcp6://[::]:8457"
92#define	HASTD_PIDFILE		"/var/run/hastd.pid"
93
94/* Default extent size. */
95#define	HAST_EXTENTSIZE	2097152
96/* Default maximum number of extents that are kept dirty. */
97#define	HAST_KEEPDIRTY	64
98
99#define	HAST_ADDRSIZE	1024
100#define	HAST_TOKEN_SIZE	16
101
102/* Number of seconds to sleep between reconnect retries or keepalive packets. */
103#define	HAST_KEEPALIVE	10
104
105struct hastd_listen {
106	/* Address to listen on. */
107	char	 hl_addr[HAST_ADDRSIZE];
108	/* Protocol-specific data. */
109	struct proto_conn *hl_conn;
110	TAILQ_ENTRY(hastd_listen) hl_next;
111};
112
113struct hastd_config {
114	/* Address to communicate with hastctl(8). */
115	char	hc_controladdr[HAST_ADDRSIZE];
116	/* Protocol-specific data. */
117	struct proto_conn *hc_controlconn;
118	/* Incoming control connection. */
119	struct proto_conn *hc_controlin;
120	/* PID file path. */
121	char	hc_pidfile[PATH_MAX];
122	/* List of addresses to listen on. */
123	TAILQ_HEAD(, hastd_listen) hc_listen;
124	/* List of resources. */
125	TAILQ_HEAD(, hast_resource) hc_resources;
126};
127
128#define	HAST_REPLICATION_FULLSYNC	0
129#define	HAST_REPLICATION_MEMSYNC	1
130#define	HAST_REPLICATION_ASYNC		2
131
132#define	HAST_COMPRESSION_NONE	0
133#define	HAST_COMPRESSION_HOLE	1
134#define	HAST_COMPRESSION_LZF	2
135
136#define	HAST_CHECKSUM_NONE	0
137#define	HAST_CHECKSUM_CRC32	1
138#define	HAST_CHECKSUM_SHA256	2
139
140/*
141 * Structure that describes single resource.
142 */
143struct hast_resource {
144	/* Resource name. */
145	char	hr_name[NAME_MAX];
146	/* Negotiated replication mode (HAST_REPLICATION_*). */
147	int	hr_replication;
148	/* Configured replication mode (HAST_REPLICATION_*). */
149	int	hr_original_replication;
150	/* Provider name that will appear in /dev/hast/. */
151	char	hr_provname[NAME_MAX];
152	/* Synchronization extent size. */
153	int	hr_extentsize;
154	/* Maximum number of extents that are kept dirty. */
155	int	hr_keepdirty;
156	/* Path to a program to execute on various events. */
157	char	hr_exec[PATH_MAX];
158	/* Compression algorithm. */
159	int	hr_compression;
160	/* Checksum algorithm. */
161	int	hr_checksum;
162	/* Protocol version. */
163	int	hr_version;
164
165	/* Path to local component. */
166	char	hr_localpath[PATH_MAX];
167	/* Descriptor to access local component. */
168	int	hr_localfd;
169	/* Offset into local component. */
170	off_t	hr_localoff;
171	/* Size of usable space. */
172	off_t	hr_datasize;
173	/* Size of entire local provider. */
174	off_t	hr_local_mediasize;
175	/* Sector size of local provider. */
176	unsigned int hr_local_sectorsize;
177	/* Is flushing write cache supported by the local provider? */
178	bool	hr_localflush;
179	/* Flush write cache on metadata updates? */
180	int	hr_metaflush;
181
182	/* Descriptor for /dev/ggctl communication. */
183	int	hr_ggatefd;
184	/* Unit number for ggate communication. */
185	int	hr_ggateunit;
186
187	/* Address of the remote component. */
188	char	hr_remoteaddr[HAST_ADDRSIZE];
189	/* Local address to bind to for outgoing connections. */
190	char	hr_sourceaddr[HAST_ADDRSIZE];
191	/* Connection for incoming data. */
192	struct proto_conn *hr_remotein;
193	/* Connection for outgoing data. */
194	struct proto_conn *hr_remoteout;
195	/* Token to verify both in and out connection are coming from
196	   the same node (not necessarily from the same address). */
197	unsigned char hr_token[HAST_TOKEN_SIZE];
198	/* Connection timeout. */
199	int	hr_timeout;
200
201	/* Resource unique identifier. */
202	uint64_t hr_resuid;
203	/* Primary's local modification count. */
204	uint64_t hr_primary_localcnt;
205	/* Primary's remote modification count. */
206	uint64_t hr_primary_remotecnt;
207	/* Secondary's local modification count. */
208	uint64_t hr_secondary_localcnt;
209	/* Secondary's remote modification count. */
210	uint64_t hr_secondary_remotecnt;
211	/* Synchronization source. */
212	uint8_t hr_syncsrc;
213
214	/* Resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */
215	int	hr_role;
216	/* Previous resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */
217	int	hr_previous_role;
218	/* PID of child worker process. 0 - no child. */
219	pid_t	hr_workerpid;
220	/* Control commands from parent to child. */
221	struct proto_conn *hr_ctrl;
222	/* Events from child to parent. */
223	struct proto_conn *hr_event;
224	/* Connection requests from child to parent. */
225	struct proto_conn *hr_conn;
226
227	/* Activemap structure. */
228	struct activemap *hr_amp;
229	/* Locked used to synchronize access to hr_amp. */
230	pthread_mutex_t hr_amp_lock;
231
232	/* Number of BIO_READ requests. */
233	uint64_t	hr_stat_read;
234	/* Number of BIO_WRITE requests. */
235	uint64_t	hr_stat_write;
236	/* Number of BIO_DELETE requests. */
237	uint64_t	hr_stat_delete;
238	/* Number of BIO_FLUSH requests. */
239	uint64_t	hr_stat_flush;
240	/* Number of activemap updates. */
241	uint64_t	hr_stat_activemap_update;
242	/* Number of local read errors. */
243	uint64_t	hr_stat_read_error;
244	/* Number of local write errors. */
245	uint64_t	hr_stat_write_error;
246	/* Number of local delete errors. */
247	uint64_t	hr_stat_delete_error;
248	/* Number of flush errors. */
249	uint64_t	hr_stat_flush_error;
250	/* Number of activemap write errors. */
251	uint64_t	hr_stat_activemap_write_error;
252	/* Number of activemap flush errors. */
253	uint64_t	hr_stat_activemap_flush_error;
254
255	/* Next resource. */
256	TAILQ_ENTRY(hast_resource) hr_next;
257};
258
259struct hastd_config *yy_config_parse(const char *config, bool exitonerror);
260void yy_config_free(struct hastd_config *config);
261
262void yyerror(const char *);
263int yylex(void);
264
265#endif	/* !_HAST_H_ */
266