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