hast.h revision 222119
1181344Sdfr/*-
2181344Sdfr * Copyright (c) 2009-2010 The FreeBSD Foundation
3181344Sdfr * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
4181344Sdfr * All rights reserved.
5181344Sdfr *
6181344Sdfr * This software was developed by Pawel Jakub Dawidek under sponsorship from
7181344Sdfr * the FreeBSD Foundation.
8181344Sdfr *
9181344Sdfr * Redistribution and use in source and binary forms, with or without
10181344Sdfr * modification, are permitted provided that the following conditions
11181344Sdfr * are met:
12181344Sdfr * 1. Redistributions of source code must retain the above copyright
13181344Sdfr *    notice, this list of conditions and the following disclaimer.
14181344Sdfr * 2. Redistributions in binary form must reproduce the above copyright
15181344Sdfr *    notice, this list of conditions and the following disclaimer in the
16181344Sdfr *    documentation and/or other materials provided with the distribution.
17181344Sdfr *
18181344Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19181344Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20181344Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21181344Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22181344Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23181344Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24181344Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25181344Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26181344Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27181344Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28181344Sdfr * SUCH DAMAGE.
29181344Sdfr *
30181344Sdfr * $FreeBSD: head/sbin/hastd/hast.h 222119 2011-05-20 11:15:27Z pjd $
31181344Sdfr */
32181344Sdfr
33181344Sdfr#ifndef	_HAST_H_
34181344Sdfr#define	_HAST_H_
35181344Sdfr
36181344Sdfr#include <sys/queue.h>
37181344Sdfr#include <sys/socket.h>
38181344Sdfr
39181344Sdfr#include <arpa/inet.h>
40181344Sdfr
41181344Sdfr#include <netinet/in.h>
42181344Sdfr
43181344Sdfr#include <limits.h>
44181344Sdfr#include <pthread.h>
45181344Sdfr#include <stdbool.h>
46181344Sdfr#include <stdint.h>
47181344Sdfr
48181344Sdfr#include <activemap.h>
49181344Sdfr
50181344Sdfr#include "proto.h"
51181344Sdfr
52181344Sdfr/*
53181344Sdfr * Version history:
54181344Sdfr * 0 - initial version
55181344Sdfr * 1 - HIO_KEEPALIVE added
56181344Sdfr */
57181344Sdfr#define	HAST_PROTO_VERSION	1
58181344Sdfr
59181344Sdfr#define	EHAST_OK		0
60181344Sdfr#define	EHAST_NOENTRY		1
61181344Sdfr#define	EHAST_INVALID		2
62181344Sdfr#define	EHAST_NOMEMORY		3
63181344Sdfr#define	EHAST_UNIMPLEMENTED	4
64181344Sdfr
65181344Sdfr#define	HASTCTL_CMD_UNKNOWN	0
66181344Sdfr#define	HASTCTL_CMD_SETROLE	1
67181344Sdfr#define	HASTCTL_CMD_STATUS	2
68181344Sdfr
69181344Sdfr#define	HAST_ROLE_UNDEF		0
70181344Sdfr#define	HAST_ROLE_INIT		1
71181344Sdfr#define	HAST_ROLE_PRIMARY	2
72181344Sdfr#define	HAST_ROLE_SECONDARY	3
73181344Sdfr
74181344Sdfr#define	HAST_SYNCSRC_UNDEF	0
75181344Sdfr#define	HAST_SYNCSRC_PRIMARY	1
76181344Sdfr#define	HAST_SYNCSRC_SECONDARY	2
77181344Sdfr
78181344Sdfr#define	HIO_UNDEF		0
79181344Sdfr#define	HIO_READ		1
80181344Sdfr#define	HIO_WRITE		2
81181344Sdfr#define	HIO_DELETE		3
82181344Sdfr#define	HIO_FLUSH		4
83181344Sdfr#define	HIO_KEEPALIVE		5
84181344Sdfr
85181344Sdfr#define	HAST_USER		"hast"
86181344Sdfr#define	HAST_TIMEOUT		20
87181344Sdfr#define	HAST_CONFIG		"/etc/hast.conf"
88181344Sdfr#define	HAST_CONTROL		"/var/run/hastctl"
89181344Sdfr#define	HASTD_LISTEN_TCP4	"tcp4://0.0.0.0:8457"
90181344Sdfr#define	HASTD_LISTEN_TCP6	"tcp6://[::]:8457"
91181344Sdfr#define	HASTD_PIDFILE		"/var/run/hastd.pid"
92181344Sdfr
93181344Sdfr/* Default extent size. */
94181344Sdfr#define	HAST_EXTENTSIZE	2097152
95181344Sdfr/* 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/* Number of seconds to sleep between reconnect retries or keepalive packets. */
102#define	HAST_KEEPALIVE	10
103
104struct hastd_listen {
105	/* Address to listen on. */
106	char	 hl_addr[HAST_ADDRSIZE];
107	/* Protocol-specific data. */
108	struct proto_conn *hl_conn;
109	TAILQ_ENTRY(hastd_listen) hl_next;
110};
111
112struct hastd_config {
113	/* Address to communicate with hastctl(8). */
114	char	 hc_controladdr[HAST_ADDRSIZE];
115	/* Protocol-specific data. */
116	struct proto_conn *hc_controlconn;
117	/* Incoming control connection. */
118	struct proto_conn *hc_controlin;
119	/* List of addresses to listen on. */
120	TAILQ_HEAD(, hastd_listen) hc_listen;
121	/* List of resources. */
122	TAILQ_HEAD(, hast_resource) hc_resources;
123};
124
125#define	HAST_REPLICATION_FULLSYNC	0
126#define	HAST_REPLICATION_MEMSYNC	1
127#define	HAST_REPLICATION_ASYNC		2
128
129#define	HAST_COMPRESSION_NONE	0
130#define	HAST_COMPRESSION_HOLE	1
131#define	HAST_COMPRESSION_LZF	2
132
133#define	HAST_CHECKSUM_NONE	0
134#define	HAST_CHECKSUM_CRC32	1
135#define	HAST_CHECKSUM_SHA256	2
136
137/*
138 * Structure that describes single resource.
139 */
140struct hast_resource {
141	/* Resource name. */
142	char	hr_name[NAME_MAX];
143	/* Replication mode (HAST_REPLICATION_*). */
144	int	hr_replication;
145	/* Provider name that will appear in /dev/hast/. */
146	char	hr_provname[NAME_MAX];
147	/* Synchronization extent size. */
148	int	hr_extentsize;
149	/* Maximum number of extents that are kept dirty. */
150	int	hr_keepdirty;
151	/* Path to a program to execute on various events. */
152	char	hr_exec[PATH_MAX];
153	/* Compression algorithm. */
154	int	hr_compression;
155	/* Checksum algorithm. */
156	int	hr_checksum;
157
158	/* Path to local component. */
159	char	hr_localpath[PATH_MAX];
160	/* Descriptor to access local component. */
161	int	hr_localfd;
162	/* Offset into local component. */
163	off_t	hr_localoff;
164	/* Size of usable space. */
165	off_t	hr_datasize;
166	/* Size of entire local provider. */
167	off_t	hr_local_mediasize;
168	/* Sector size of local provider. */
169	unsigned int hr_local_sectorsize;
170
171	/* Descriptor for /dev/ggctl communication. */
172	int	hr_ggatefd;
173	/* Unit number for ggate communication. */
174	int	hr_ggateunit;
175
176	/* Address of the remote component. */
177	char	hr_remoteaddr[HAST_ADDRSIZE];
178	/* Local address to bind to for outgoing connections. */
179	char	hr_sourceaddr[HAST_ADDRSIZE];
180	/* Connection for incoming data. */
181	struct proto_conn *hr_remotein;
182	/* Connection for outgoing data. */
183	struct proto_conn *hr_remoteout;
184	/* Token to verify both in and out connection are coming from
185	   the same node (not necessarily from the same address). */
186	unsigned char hr_token[HAST_TOKEN_SIZE];
187	/* Connection timeout. */
188	int	hr_timeout;
189
190	/* Resource unique identifier. */
191	uint64_t hr_resuid;
192	/* Primary's local modification count. */
193	uint64_t hr_primary_localcnt;
194	/* Primary's remote modification count. */
195	uint64_t hr_primary_remotecnt;
196	/* Secondary's local modification count. */
197	uint64_t hr_secondary_localcnt;
198	/* Secondary's remote modification count. */
199	uint64_t hr_secondary_remotecnt;
200	/* Synchronization source. */
201	uint8_t hr_syncsrc;
202
203	/* Resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */
204	int	hr_role;
205	/* Previous resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */
206	int	hr_previous_role;
207	/* PID of child worker process. 0 - no child. */
208	pid_t	hr_workerpid;
209	/* Control commands from parent to child. */
210	struct proto_conn *hr_ctrl;
211	/* Events from child to parent. */
212	struct proto_conn *hr_event;
213	/* Connection requests from child to parent. */
214	struct proto_conn *hr_conn;
215
216	/* Activemap structure. */
217	struct activemap *hr_amp;
218	/* Locked used to synchronize access to hr_amp. */
219	pthread_mutex_t hr_amp_lock;
220
221	/* Next resource. */
222	TAILQ_ENTRY(hast_resource) hr_next;
223};
224
225struct hastd_config *yy_config_parse(const char *config, bool exitonerror);
226void yy_config_free(struct hastd_config *config);
227
228void yyerror(const char *);
229int yylex(void);
230int yyparse(void);
231
232#endif	/* !_HAST_H_ */
233