xref: /freebsd/sbin/hastd/hast.h (revision 01ded8b942effbbb4d9225c4227f264e499e9698)
1 /*-
2  * Copyright (c) 2009-2010 The FreeBSD Foundation
3  * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
4  * All rights reserved.
5  *
6  * This software was developed by Pawel Jakub Dawidek under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
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  */
57 #define	HAST_PROTO_VERSION	1
58 
59 #define	EHAST_OK		0
60 #define	EHAST_NOENTRY		1
61 #define	EHAST_INVALID		2
62 #define	EHAST_NOMEMORY		3
63 #define	EHAST_UNIMPLEMENTED	4
64 
65 #define	HASTCTL_CMD_UNKNOWN	0
66 #define	HASTCTL_CMD_SETROLE	1
67 #define	HASTCTL_CMD_STATUS	2
68 
69 #define	HAST_ROLE_UNDEF		0
70 #define	HAST_ROLE_INIT		1
71 #define	HAST_ROLE_PRIMARY	2
72 #define	HAST_ROLE_SECONDARY	3
73 
74 #define	HAST_SYNCSRC_UNDEF	0
75 #define	HAST_SYNCSRC_PRIMARY	1
76 #define	HAST_SYNCSRC_SECONDARY	2
77 
78 #define	HIO_UNDEF		0
79 #define	HIO_READ		1
80 #define	HIO_WRITE		2
81 #define	HIO_DELETE		3
82 #define	HIO_FLUSH		4
83 #define	HIO_KEEPALIVE		5
84 
85 #define	HAST_USER	"hast"
86 #define	HAST_TIMEOUT	5
87 #define	HAST_CONFIG	"/etc/hast.conf"
88 #define	HAST_CONTROL	"/var/run/hastctl"
89 #define	HASTD_PORT	8457
90 #define	HASTD_LISTEN	"tcp4://0.0.0.0:8457"
91 #define	HASTD_PIDFILE	"/var/run/hastd.pid"
92 
93 /* Default extent size. */
94 #define	HAST_EXTENTSIZE	2097152
95 /* Default maximum number of extents that are kept dirty. */
96 #define	HAST_KEEPDIRTY	64
97 
98 #define	HAST_ADDRSIZE	1024
99 #define	HAST_TOKEN_SIZE	16
100 
101 struct hastd_config {
102 	/* Address to communicate with hastctl(8). */
103 	char	 hc_controladdr[HAST_ADDRSIZE];
104 	/* Protocol-specific data. */
105 	struct proto_conn *hc_controlconn;
106 	/* Incoming control connection. */
107 	struct proto_conn *hc_controlin;
108 	/* Address to listen on. */
109 	char	 hc_listenaddr[HAST_ADDRSIZE];
110 	/* Protocol-specific data. */
111 	struct proto_conn *hc_listenconn;
112 	/* List of resources. */
113 	TAILQ_HEAD(, hast_resource) hc_resources;
114 };
115 
116 #define	HAST_REPLICATION_FULLSYNC	0
117 #define	HAST_REPLICATION_MEMSYNC	1
118 #define	HAST_REPLICATION_ASYNC		2
119 
120 #define	HAST_COMPRESSION_NONE	0
121 #define	HAST_COMPRESSION_HOLE	1
122 #define	HAST_COMPRESSION_LZF	2
123 
124 #define	HAST_CHECKSUM_NONE	0
125 #define	HAST_CHECKSUM_CRC32	1
126 #define	HAST_CHECKSUM_SHA256	2
127 
128 /*
129  * Structure that describes single resource.
130  */
131 struct hast_resource {
132 	/* Resource name. */
133 	char	hr_name[NAME_MAX];
134 	/* Replication mode (HAST_REPLICATION_*). */
135 	int	hr_replication;
136 	/* Provider name that will appear in /dev/hast/. */
137 	char	hr_provname[NAME_MAX];
138 	/* Synchronization extent size. */
139 	int	hr_extentsize;
140 	/* Maximum number of extents that are kept dirty. */
141 	int	hr_keepdirty;
142 	/* Path to a program to execute on various events. */
143 	char	hr_exec[PATH_MAX];
144 	/* Compression algorithm. */
145 	int	hr_compression;
146 	/* Checksum algorithm. */
147 	int	hr_checksum;
148 
149 	/* Path to local component. */
150 	char	hr_localpath[PATH_MAX];
151 	/* Descriptor to access local component. */
152 	int	hr_localfd;
153 	/* Offset into local component. */
154 	off_t	hr_localoff;
155 	/* Size of usable space. */
156 	off_t	hr_datasize;
157 	/* Size of entire local provider. */
158 	off_t	hr_local_mediasize;
159 	/* Sector size of local provider. */
160 	unsigned int hr_local_sectorsize;
161 
162 	/* Descriptor for /dev/ggctl communication. */
163 	int	hr_ggatefd;
164 	/* Unit number for ggate communication. */
165 	int	hr_ggateunit;
166 
167 	/* Address of the remote component. */
168 	char	hr_remoteaddr[HAST_ADDRSIZE];
169 	/* Connection for incoming data. */
170 	struct proto_conn *hr_remotein;
171 	/* Connection for outgoing data. */
172 	struct proto_conn *hr_remoteout;
173 	/* Token to verify both in and out connection are coming from
174 	   the same node (not necessarily from the same address). */
175 	unsigned char hr_token[HAST_TOKEN_SIZE];
176 	/* Connection timeout. */
177 	int	hr_timeout;
178 
179 	/* Resource unique identifier. */
180 	uint64_t hr_resuid;
181 	/* Primary's local modification count. */
182 	uint64_t hr_primary_localcnt;
183 	/* Primary's remote modification count. */
184 	uint64_t hr_primary_remotecnt;
185 	/* Secondary's local modification count. */
186 	uint64_t hr_secondary_localcnt;
187 	/* Secondary's remote modification count. */
188 	uint64_t hr_secondary_remotecnt;
189 	/* Synchronization source. */
190 	uint8_t hr_syncsrc;
191 
192 	/* Resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */
193 	int	hr_role;
194 	/* Previous resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */
195 	int	hr_previous_role;
196 	/* PID of child worker process. 0 - no child. */
197 	pid_t	hr_workerpid;
198 	/* Control commands from parent to child. */
199 	struct proto_conn *hr_ctrl;
200 	/* Events from child to parent. */
201 	struct proto_conn *hr_event;
202 	/* Connection requests from child to parent. */
203 	struct proto_conn *hr_conn;
204 
205 	/* Activemap structure. */
206 	struct activemap *hr_amp;
207 	/* Locked used to synchronize access to hr_amp. */
208 	pthread_mutex_t hr_amp_lock;
209 
210 	/* Next resource. */
211 	TAILQ_ENTRY(hast_resource) hr_next;
212 };
213 
214 struct hastd_config *yy_config_parse(const char *config, bool exitonerror);
215 void yy_config_free(struct hastd_config *config);
216 
217 void yyerror(const char *);
218 int yylex(void);
219 int yyparse(void);
220 
221 #endif	/* !_HAST_H_ */
222