xref: /freebsd/sbin/hastd/control.c (revision a02aba5f3c73d7ed377f88327fedd11f70f23353)
1 /*-
2  * Copyright (c) 2009-2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 
36 #include <assert.h>
37 #include <errno.h>
38 #include <pthread.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "hast.h"
45 #include "hastd.h"
46 #include "hast_checksum.h"
47 #include "hast_compression.h"
48 #include "hast_proto.h"
49 #include "hooks.h"
50 #include "nv.h"
51 #include "pjdlog.h"
52 #include "proto.h"
53 #include "subr.h"
54 
55 #include "control.h"
56 
57 void
58 child_cleanup(struct hast_resource *res)
59 {
60 
61 	proto_close(res->hr_ctrl);
62 	res->hr_ctrl = NULL;
63 	if (res->hr_event != NULL) {
64 		proto_close(res->hr_event);
65 		res->hr_event = NULL;
66 	}
67 	if (res->hr_conn != NULL) {
68 		proto_close(res->hr_conn);
69 		res->hr_conn = NULL;
70 	}
71 	res->hr_workerpid = 0;
72 }
73 
74 static void
75 control_set_role_common(struct hastd_config *cfg, struct nv *nvout,
76     uint8_t role, struct hast_resource *res, const char *name, unsigned int no)
77 {
78 	int oldrole;
79 
80 	/* Name is always needed. */
81 	if (name != NULL)
82 		nv_add_string(nvout, name, "resource%u", no);
83 
84 	if (res == NULL) {
85 		assert(cfg != NULL);
86 		assert(name != NULL);
87 
88 		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
89 			if (strcmp(res->hr_name, name) == 0)
90 				break;
91 		}
92 		if (res == NULL) {
93 			nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
94 			return;
95 		}
96 	}
97 	assert(res != NULL);
98 
99 	/* Send previous role back. */
100 	nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
101 
102 	/* Nothing changed, return here. */
103 	if (role == res->hr_role)
104 		return;
105 
106 	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
107 	pjdlog_info("Role changed to %s.", role2str(role));
108 
109 	/* Change role to the new one. */
110 	oldrole = res->hr_role;
111 	res->hr_role = role;
112 	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
113 
114 	/*
115 	 * If previous role was primary or secondary we have to kill process
116 	 * doing that work.
117 	 */
118 	if (res->hr_workerpid != 0) {
119 		if (kill(res->hr_workerpid, SIGTERM) < 0) {
120 			pjdlog_errno(LOG_WARNING,
121 			    "Unable to kill worker process %u",
122 			    (unsigned int)res->hr_workerpid);
123 		} else if (waitpid(res->hr_workerpid, NULL, 0) !=
124 		    res->hr_workerpid) {
125 			pjdlog_errno(LOG_WARNING,
126 			    "Error while waiting for worker process %u",
127 			    (unsigned int)res->hr_workerpid);
128 		} else {
129 			pjdlog_debug(1, "Worker process %u stopped.",
130 			    (unsigned int)res->hr_workerpid);
131 		}
132 		child_cleanup(res);
133 	}
134 
135 	/* Start worker process if we are changing to primary. */
136 	if (role == HAST_ROLE_PRIMARY)
137 		hastd_primary(res);
138 	pjdlog_prefix_set("%s", "");
139 	hook_exec(res->hr_exec, "role", res->hr_name, role2str(oldrole),
140 	    role2str(res->hr_role), NULL);
141 }
142 
143 void
144 control_set_role(struct hast_resource *res, uint8_t role)
145 {
146 
147 	control_set_role_common(NULL, NULL, role, res, NULL, 0);
148 }
149 
150 static void
151 control_status_worker(struct hast_resource *res, struct nv *nvout,
152     unsigned int no)
153 {
154 	struct nv *cnvin, *cnvout;
155 	const char *str;
156 	int error;
157 
158 	cnvin = NULL;
159 
160 	/*
161 	 * Prepare and send command to worker process.
162 	 */
163 	cnvout = nv_alloc();
164 	nv_add_uint8(cnvout, CONTROL_STATUS, "cmd");
165 	error = nv_error(cnvout);
166 	if (error != 0) {
167 		pjdlog_common(LOG_ERR, 0, error,
168 		    "Unable to prepare control header");
169 		goto end;
170 	}
171 	if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) < 0) {
172 		error = errno;
173 		pjdlog_errno(LOG_ERR, "Unable to send control header");
174 		goto end;
175 	}
176 
177 	/*
178 	 * Receive response.
179 	 */
180 	if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) < 0) {
181 		error = errno;
182 		pjdlog_errno(LOG_ERR, "Unable to receive control header");
183 		goto end;
184 	}
185 
186 	error = nv_get_int16(cnvin, "error");
187 	if (error != 0)
188 		goto end;
189 
190 	if ((str = nv_get_string(cnvin, "status")) == NULL) {
191 		error = ENOENT;
192 		pjdlog_errno(LOG_ERR, "Field 'status' is missing.");
193 		goto end;
194 	}
195 	nv_add_string(nvout, str, "status%u", no);
196 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "dirty"), "dirty%u", no);
197 	nv_add_uint32(nvout, nv_get_uint32(cnvin, "extentsize"),
198 	    "extentsize%u", no);
199 	nv_add_uint32(nvout, nv_get_uint32(cnvin, "keepdirty"),
200 	    "keepdirty%u", no);
201 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_read"),
202 	    "stat_read%u", no);
203 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_write"),
204 	    "stat_write%u", no);
205 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_delete"),
206 	    "stat_delete%u", no);
207 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_flush"),
208 	    "stat_flush%u", no);
209 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_activemap_update"),
210 	    "stat_activemap_update%u", no);
211 end:
212 	if (cnvin != NULL)
213 		nv_free(cnvin);
214 	if (cnvout != NULL)
215 		nv_free(cnvout);
216 	if (error != 0)
217 		nv_add_int16(nvout, error, "error");
218 }
219 
220 static void
221 control_status(struct hastd_config *cfg, struct nv *nvout,
222     struct hast_resource *res, const char *name, unsigned int no)
223 {
224 
225 	assert(cfg != NULL);
226 	assert(nvout != NULL);
227 	assert(name != NULL);
228 
229 	/* Name is always needed. */
230 	nv_add_string(nvout, name, "resource%u", no);
231 
232 	if (res == NULL) {
233 		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
234 			if (strcmp(res->hr_name, name) == 0)
235 				break;
236 		}
237 		if (res == NULL) {
238 			nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
239 			return;
240 		}
241 	}
242 	assert(res != NULL);
243 	nv_add_string(nvout, res->hr_provname, "provname%u", no);
244 	nv_add_string(nvout, res->hr_localpath, "localpath%u", no);
245 	nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr%u", no);
246 	if (res->hr_sourceaddr[0] != '\0')
247 		nv_add_string(nvout, res->hr_sourceaddr, "sourceaddr%u", no);
248 	switch (res->hr_replication) {
249 	case HAST_REPLICATION_FULLSYNC:
250 		nv_add_string(nvout, "fullsync", "replication%u", no);
251 		break;
252 	case HAST_REPLICATION_MEMSYNC:
253 		nv_add_string(nvout, "memsync", "replication%u", no);
254 		break;
255 	case HAST_REPLICATION_ASYNC:
256 		nv_add_string(nvout, "async", "replication%u", no);
257 		break;
258 	default:
259 		nv_add_string(nvout, "unknown", "replication%u", no);
260 		break;
261 	}
262 	nv_add_string(nvout, checksum_name(res->hr_checksum),
263 	    "checksum%u", no);
264 	nv_add_string(nvout, compression_name(res->hr_compression),
265 	    "compression%u", no);
266 	nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
267 
268 	switch (res->hr_role) {
269 	case HAST_ROLE_PRIMARY:
270 		assert(res->hr_workerpid != 0);
271 		/* FALLTHROUGH */
272 	case HAST_ROLE_SECONDARY:
273 		if (res->hr_workerpid != 0)
274 			break;
275 		/* FALLTHROUGH */
276 	default:
277 		return;
278 	}
279 
280 	/*
281 	 * If we are here, it means that we have a worker process, which we
282 	 * want to ask some questions.
283 	 */
284 	control_status_worker(res, nvout, no);
285 }
286 
287 void
288 control_handle(struct hastd_config *cfg)
289 {
290 	struct proto_conn *conn;
291 	struct nv *nvin, *nvout;
292 	unsigned int ii;
293 	const char *str;
294 	uint8_t cmd, role;
295 	int error;
296 
297 	if (proto_accept(cfg->hc_controlconn, &conn) < 0) {
298 		pjdlog_errno(LOG_ERR, "Unable to accept control connection");
299 		return;
300 	}
301 
302 	cfg->hc_controlin = conn;
303 	nvin = nvout = NULL;
304 	role = HAST_ROLE_UNDEF;
305 
306 	if (hast_proto_recv_hdr(conn, &nvin) < 0) {
307 		pjdlog_errno(LOG_ERR, "Unable to receive control header");
308 		nvin = NULL;
309 		goto close;
310 	}
311 
312 	/* Obtain command code. 0 means that nv_get_uint8() failed. */
313 	cmd = nv_get_uint8(nvin, "cmd");
314 	if (cmd == 0) {
315 		pjdlog_error("Control header is missing 'cmd' field.");
316 		error = EHAST_INVALID;
317 		goto close;
318 	}
319 
320 	/* Allocate outgoing nv structure. */
321 	nvout = nv_alloc();
322 	if (nvout == NULL) {
323 		pjdlog_error("Unable to allocate header for control response.");
324 		error = EHAST_NOMEMORY;
325 		goto close;
326 	}
327 
328 	error = 0;
329 
330 	str = nv_get_string(nvin, "resource0");
331 	if (str == NULL) {
332 		pjdlog_error("Control header is missing 'resource0' field.");
333 		error = EHAST_INVALID;
334 		goto fail;
335 	}
336 	if (cmd == HASTCTL_CMD_SETROLE) {
337 		role = nv_get_uint8(nvin, "role");
338 		switch (role) {
339 		case HAST_ROLE_INIT:
340 		case HAST_ROLE_PRIMARY:
341 		case HAST_ROLE_SECONDARY:
342 			break;
343 		default:
344 			pjdlog_error("Invalid role received (%hhu).", role);
345 			error = EHAST_INVALID;
346 			goto fail;
347 		}
348 	}
349 	if (strcmp(str, "all") == 0) {
350 		struct hast_resource *res;
351 
352 		/* All configured resources. */
353 
354 		ii = 0;
355 		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
356 			switch (cmd) {
357 			case HASTCTL_CMD_SETROLE:
358 				control_set_role_common(cfg, nvout, role, res,
359 				    res->hr_name, ii++);
360 				break;
361 			case HASTCTL_CMD_STATUS:
362 				control_status(cfg, nvout, res, res->hr_name,
363 				    ii++);
364 				break;
365 			default:
366 				pjdlog_error("Invalid command received (%hhu).",
367 				    cmd);
368 				error = EHAST_UNIMPLEMENTED;
369 				goto fail;
370 			}
371 		}
372 	} else {
373 		/* Only selected resources. */
374 
375 		for (ii = 0; ; ii++) {
376 			str = nv_get_string(nvin, "resource%u", ii);
377 			if (str == NULL)
378 				break;
379 			switch (cmd) {
380 			case HASTCTL_CMD_SETROLE:
381 				control_set_role_common(cfg, nvout, role, NULL,
382 				    str, ii);
383 				break;
384 			case HASTCTL_CMD_STATUS:
385 				control_status(cfg, nvout, NULL, str, ii);
386 				break;
387 			default:
388 				pjdlog_error("Invalid command received (%hhu).",
389 				    cmd);
390 				error = EHAST_UNIMPLEMENTED;
391 				goto fail;
392 			}
393 		}
394 	}
395 	if (nv_error(nvout) != 0)
396 		goto close;
397 fail:
398 	if (error != 0)
399 		nv_add_int16(nvout, error, "error");
400 
401 	if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0)
402 		pjdlog_errno(LOG_ERR, "Unable to send control response");
403 close:
404 	if (nvin != NULL)
405 		nv_free(nvin);
406 	if (nvout != NULL)
407 		nv_free(nvout);
408 	proto_close(conn);
409 	cfg->hc_controlin = NULL;
410 }
411 
412 /*
413  * Thread handles control requests from the parent.
414  */
415 void *
416 ctrl_thread(void *arg)
417 {
418 	struct hast_resource *res = arg;
419 	struct nv *nvin, *nvout;
420 	uint8_t cmd;
421 
422 	for (;;) {
423 		if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) < 0) {
424 			if (sigexit_received)
425 				pthread_exit(NULL);
426 			pjdlog_errno(LOG_ERR,
427 			    "Unable to receive control message");
428 			kill(getpid(), SIGTERM);
429 			pthread_exit(NULL);
430 		}
431 		cmd = nv_get_uint8(nvin, "cmd");
432 		if (cmd == 0) {
433 			pjdlog_error("Control message is missing 'cmd' field.");
434 			nv_free(nvin);
435 			continue;
436 		}
437 		nvout = nv_alloc();
438 		switch (cmd) {
439 		case CONTROL_STATUS:
440 			if (res->hr_remotein != NULL &&
441 			    res->hr_remoteout != NULL) {
442 				nv_add_string(nvout, "complete", "status");
443 			} else {
444 				nv_add_string(nvout, "degraded", "status");
445 			}
446 			nv_add_uint32(nvout, (uint32_t)res->hr_extentsize,
447 			    "extentsize");
448 			if (res->hr_role == HAST_ROLE_PRIMARY) {
449 				nv_add_uint32(nvout,
450 				    (uint32_t)res->hr_keepdirty, "keepdirty");
451 				nv_add_uint64(nvout,
452 				    (uint64_t)(activemap_ndirty(res->hr_amp) *
453 				    res->hr_extentsize), "dirty");
454 			} else {
455 				nv_add_uint32(nvout, (uint32_t)0, "keepdirty");
456 				nv_add_uint64(nvout, (uint64_t)0, "dirty");
457 			}
458 			nv_add_uint64(nvout, res->hr_stat_read, "stat_read");
459 			nv_add_uint64(nvout, res->hr_stat_write, "stat_write");
460 			nv_add_uint64(nvout, res->hr_stat_delete,
461 			    "stat_delete");
462 			nv_add_uint64(nvout, res->hr_stat_flush, "stat_flush");
463 			nv_add_uint64(nvout, res->hr_stat_activemap_update,
464 			    "stat_activemap_update");
465 			nv_add_int16(nvout, 0, "error");
466 			break;
467 		case CONTROL_RELOAD:
468 			/*
469 			 * When parent receives SIGHUP and discovers that
470 			 * something related to us has changes, it sends reload
471 			 * message to us.
472 			 */
473 			assert(res->hr_role == HAST_ROLE_PRIMARY);
474 			primary_config_reload(res, nvin);
475 			nv_add_int16(nvout, 0, "error");
476 			break;
477 		default:
478 			nv_add_int16(nvout, EINVAL, "error");
479 			break;
480 		}
481 		nv_free(nvin);
482 		if (nv_error(nvout) != 0) {
483 			pjdlog_error("Unable to create answer on control message.");
484 			nv_free(nvout);
485 			continue;
486 		}
487 		if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) < 0) {
488 			pjdlog_errno(LOG_ERR,
489 			    "Unable to send reply to control message");
490 		}
491 		nv_free(nvout);
492 	}
493 	/* NOTREACHED */
494 	return (NULL);
495 }
496