xref: /freebsd/sbin/hastd/hast.h (revision 7dfadd78ba3c4608c3631fb5008de77ea1e29e88)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
33 #ifndef	_HAST_H_
34 #define	_HAST_H_
35 
36 #include <sys/queue.h>
37 #include <sys/socket.h>
38 
39 #include <arpa/inet.h>
40 
41 #include <netinet/in.h>
42 
43 #include <limits.h>
44 #include <pthread.h>
45 #include <stdbool.h>
46 #include <stdint.h>
47 
48 #include <activemap.h>
49 
50 #include "proto.h"
51 
52 /*
53  * Version history:
54  * 0 - initial version
55  * 1 - HIO_KEEPALIVE added
56  * 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 #define	HASTCTL_CMD_STOP	3
70 
71 #define	HAST_ROLE_UNDEF		0
72 #define	HAST_ROLE_INIT		1
73 #define	HAST_ROLE_PRIMARY	2
74 #define	HAST_ROLE_SECONDARY	3
75 
76 #define	HAST_SYNCSRC_UNDEF	0
77 #define	HAST_SYNCSRC_PRIMARY	1
78 #define	HAST_SYNCSRC_SECONDARY	2
79 
80 #define	HIO_UNDEF		0
81 #define	HIO_READ		1
82 #define	HIO_WRITE		2
83 #define	HIO_DELETE		3
84 #define	HIO_FLUSH		4
85 #define	HIO_KEEPALIVE		5
86 
87 #define	HAST_USER		"hast"
88 #define	HAST_TIMEOUT		20
89 #define	HAST_CONFIG		"/etc/hast.conf"
90 #define	HAST_CONTROL		"/var/run/hastctl"
91 #define	HASTD_LISTEN_TCP4	"tcp4://0.0.0.0:8457"
92 #define	HASTD_LISTEN_TCP6	"tcp6://[::]:8457"
93 #define	HASTD_PIDFILE		"/var/run/hastd.pid"
94 
95 /* Default extent size. */
96 #define	HAST_EXTENTSIZE	2097152
97 /* Default maximum number of extents that are kept dirty. */
98 #define	HAST_KEEPDIRTY	64
99 
100 #define	HAST_ADDRSIZE	1024
101 #define	HAST_TOKEN_SIZE	16
102 
103 /* Number of seconds to sleep between reconnect retries or keepalive packets. */
104 #define	HAST_KEEPALIVE	10
105 
106 struct hastd_listen {
107 	/* Address to listen on. */
108 	char	 hl_addr[HAST_ADDRSIZE];
109 	/* Protocol-specific data. */
110 	struct proto_conn *hl_conn;
111 	TAILQ_ENTRY(hastd_listen) hl_next;
112 };
113 
114 struct hastd_config {
115 	/* Address to communicate with hastctl(8). */
116 	char	hc_controladdr[HAST_ADDRSIZE];
117 	/* Protocol-specific data. */
118 	struct proto_conn *hc_controlconn;
119 	/* Incoming control connection. */
120 	struct proto_conn *hc_controlin;
121 	/* PID file path. */
122 	char	hc_pidfile[PATH_MAX];
123 	/* List of addresses to listen on. */
124 	TAILQ_HEAD(, hastd_listen) hc_listen;
125 	/* List of resources. */
126 	TAILQ_HEAD(, hast_resource) hc_resources;
127 };
128 
129 #define	HAST_REPLICATION_FULLSYNC	0
130 #define	HAST_REPLICATION_MEMSYNC	1
131 #define	HAST_REPLICATION_ASYNC		2
132 
133 #define	HAST_COMPRESSION_NONE	0
134 #define	HAST_COMPRESSION_HOLE	1
135 #define	HAST_COMPRESSION_LZF	2
136 
137 #define	HAST_CHECKSUM_NONE	0
138 #define	HAST_CHECKSUM_CRC32	1
139 #define	HAST_CHECKSUM_SHA256	2
140 
141 struct nv;
142 
143 /*
144  * Structure that describes single resource.
145  */
146 struct hast_resource {
147 	/* Resource name. */
148 	char	hr_name[NAME_MAX];
149 	/* Negotiated replication mode (HAST_REPLICATION_*). */
150 	int	hr_replication;
151 	/* Configured replication mode (HAST_REPLICATION_*). */
152 	int	hr_original_replication;
153 	/* Provider name that will appear in /dev/hast/. */
154 	char	hr_provname[NAME_MAX];
155 	/* Synchronization extent size. */
156 	int	hr_extentsize;
157 	/* Maximum number of extents that are kept dirty. */
158 	int	hr_keepdirty;
159 	/* Path to a program to execute on various events. */
160 	char	hr_exec[PATH_MAX];
161 	/* Compression algorithm. */
162 	int	hr_compression;
163 	/* Checksum algorithm. */
164 	int	hr_checksum;
165 	/* Protocol version. */
166 	int	hr_version;
167 
168 	/* Path to local component. */
169 	char	hr_localpath[PATH_MAX];
170 	/* Descriptor to access local component. */
171 	int	hr_localfd;
172 	/* Offset into local component. */
173 	off_t	hr_localoff;
174 	/* Size of usable space. */
175 	off_t	hr_datasize;
176 	/* Size of entire local provider. */
177 	off_t	hr_local_mediasize;
178 	/* Sector size of local provider. */
179 	unsigned int hr_local_sectorsize;
180 	/* Is flushing write cache supported by the local provider? */
181 	bool	hr_localflush;
182 	/* Flush write cache on metadata updates? */
183 	int	hr_metaflush;
184 
185 	/* Descriptor for /dev/ggctl communication. */
186 	int	hr_ggatefd;
187 	/* Unit number for ggate communication. */
188 	int	hr_ggateunit;
189 
190 	/* Address of the remote component. */
191 	char	hr_remoteaddr[HAST_ADDRSIZE];
192 	/* Local address to bind to for outgoing connections. */
193 	char	hr_sourceaddr[HAST_ADDRSIZE];
194 	/* Connection for incoming data. */
195 	struct proto_conn *hr_remotein;
196 	/* Connection for outgoing data. */
197 	struct proto_conn *hr_remoteout;
198 	/* Token to verify both in and out connection are coming from
199 	   the same node (not necessarily from the same address). */
200 	unsigned char hr_token[HAST_TOKEN_SIZE];
201 	/* Connection timeout. */
202 	int	hr_timeout;
203 
204 	/* Resource unique identifier. */
205 	uint64_t hr_resuid;
206 	/* Primary's local modification count. */
207 	uint64_t hr_primary_localcnt;
208 	/* Primary's remote modification count. */
209 	uint64_t hr_primary_remotecnt;
210 	/* Secondary's local modification count. */
211 	uint64_t hr_secondary_localcnt;
212 	/* Secondary's remote modification count. */
213 	uint64_t hr_secondary_remotecnt;
214 	/* Synchronization source. */
215 	uint8_t hr_syncsrc;
216 
217 	/* Resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */
218 	int	hr_role;
219 	/* Previous resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */
220 	int	hr_previous_role;
221 	/* PID of child worker process. 0 - no child. */
222 	pid_t	hr_workerpid;
223 	/* Control commands from parent to child. */
224 	struct proto_conn *hr_ctrl;
225 	/* Events from child to parent. */
226 	struct proto_conn *hr_event;
227 	/* Connection requests from child to parent. */
228 	struct proto_conn *hr_conn;
229 
230 	/* Activemap structure. */
231 	struct activemap *hr_amp;
232 	/* Lock used to synchronize access to hr_amp. */
233 	pthread_mutex_t hr_amp_lock;
234 	/* Lock used to synchronize access to hr_amp diskmap. */
235 	pthread_mutex_t hr_amp_diskmap_lock;
236 
237 	/* Number of BIO_READ requests. */
238 	uint64_t	hr_stat_read;
239 	/* Number of BIO_WRITE requests. */
240 	uint64_t	hr_stat_write;
241 	/* Number of BIO_DELETE requests. */
242 	uint64_t	hr_stat_delete;
243 	/* Number of BIO_FLUSH requests. */
244 	uint64_t	hr_stat_flush;
245 	/* Number of activemap updates. */
246 	uint64_t	hr_stat_activemap_update;
247 	/* Number of local read errors. */
248 	uint64_t	hr_stat_read_error;
249 	/* Number of local write errors. */
250 	uint64_t	hr_stat_write_error;
251 	/* Number of local delete errors. */
252 	uint64_t	hr_stat_delete_error;
253 	/* Number of flush errors. */
254 	uint64_t	hr_stat_flush_error;
255 	/* Number of activemap write errors. */
256 	uint64_t	hr_stat_activemap_write_error;
257 	/* Number of activemap flush errors. */
258 	uint64_t	hr_stat_activemap_flush_error;
259 
260 	/* Function to output worker specific info on control status request. */
261 	void	(*output_status_aux)(struct nv *);
262 
263 	/* Next resource. */
264 	TAILQ_ENTRY(hast_resource) hr_next;
265 };
266 
267 struct hastd_config *yy_config_parse(const char *config, bool exitonerror);
268 void yy_config_free(struct hastd_config *config);
269 
270 #endif	/* !_HAST_H_ */
271