xref: /freebsd/sbin/hastd/control.c (revision 6f9c8e5b074419423648ffb89b83fd2f257e90b7)
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 = cnvout = NULL;
159 	error = 0;
160 
161 	/*
162 	 * Prepare and send command to worker process.
163 	 */
164 	cnvout = nv_alloc();
165 	nv_add_uint8(cnvout, CONTROL_STATUS, "cmd");
166 	error = nv_error(cnvout);
167 	if (error != 0) {
168 		pjdlog_common(LOG_ERR, 0, error,
169 		    "Unable to prepare control header");
170 		goto end;
171 	}
172 	if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) < 0) {
173 		error = errno;
174 		pjdlog_errno(LOG_ERR, "Unable to send control header");
175 		goto end;
176 	}
177 
178 	/*
179 	 * Receive response.
180 	 */
181 	if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) < 0) {
182 		error = errno;
183 		pjdlog_errno(LOG_ERR, "Unable to receive control header");
184 		goto end;
185 	}
186 
187 	error = nv_get_int16(cnvin, "error");
188 	if (error != 0)
189 		goto end;
190 
191 	if ((str = nv_get_string(cnvin, "status")) == NULL) {
192 		error = ENOENT;
193 		pjdlog_errno(LOG_ERR, "Field 'status' is missing.");
194 		goto end;
195 	}
196 	nv_add_string(nvout, str, "status%u", no);
197 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "dirty"), "dirty%u", no);
198 	nv_add_uint32(nvout, nv_get_uint32(cnvin, "extentsize"),
199 	    "extentsize%u", no);
200 	nv_add_uint32(nvout, nv_get_uint32(cnvin, "keepdirty"),
201 	    "keepdirty%u", no);
202 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_read"),
203 	    "stat_read%u", no);
204 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_write"),
205 	    "stat_write%u", no);
206 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_delete"),
207 	    "stat_delete%u", no);
208 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_flush"),
209 	    "stat_flush%u", no);
210 	nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_activemap_update"),
211 	    "stat_activemap_update%u", no);
212 end:
213 	if (cnvin != NULL)
214 		nv_free(cnvin);
215 	if (cnvout != NULL)
216 		nv_free(cnvout);
217 	if (error != 0)
218 		nv_add_int16(nvout, error, "error");
219 }
220 
221 static void
222 control_status(struct hastd_config *cfg, struct nv *nvout,
223     struct hast_resource *res, const char *name, unsigned int no)
224 {
225 
226 	assert(cfg != NULL);
227 	assert(nvout != NULL);
228 	assert(name != NULL);
229 
230 	/* Name is always needed. */
231 	nv_add_string(nvout, name, "resource%u", no);
232 
233 	if (res == NULL) {
234 		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
235 			if (strcmp(res->hr_name, name) == 0)
236 				break;
237 		}
238 		if (res == NULL) {
239 			nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
240 			return;
241 		}
242 	}
243 	assert(res != NULL);
244 	nv_add_string(nvout, res->hr_provname, "provname%u", no);
245 	nv_add_string(nvout, res->hr_localpath, "localpath%u", no);
246 	nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr%u", no);
247 	if (res->hr_sourceaddr[0] != '\0')
248 		nv_add_string(nvout, res->hr_sourceaddr, "sourceaddr%u", no);
249 	switch (res->hr_replication) {
250 	case HAST_REPLICATION_FULLSYNC:
251 		nv_add_string(nvout, "fullsync", "replication%u", no);
252 		break;
253 	case HAST_REPLICATION_MEMSYNC:
254 		nv_add_string(nvout, "memsync", "replication%u", no);
255 		break;
256 	case HAST_REPLICATION_ASYNC:
257 		nv_add_string(nvout, "async", "replication%u", no);
258 		break;
259 	default:
260 		nv_add_string(nvout, "unknown", "replication%u", no);
261 		break;
262 	}
263 	nv_add_string(nvout, checksum_name(res->hr_checksum),
264 	    "checksum%u", no);
265 	nv_add_string(nvout, compression_name(res->hr_compression),
266 	    "compression%u", no);
267 	nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
268 
269 	switch (res->hr_role) {
270 	case HAST_ROLE_PRIMARY:
271 		assert(res->hr_workerpid != 0);
272 		/* FALLTHROUGH */
273 	case HAST_ROLE_SECONDARY:
274 		if (res->hr_workerpid != 0)
275 			break;
276 		/* FALLTHROUGH */
277 	default:
278 		return;
279 	}
280 
281 	/*
282 	 * If we are here, it means that we have a worker process, which we
283 	 * want to ask some questions.
284 	 */
285 	control_status_worker(res, nvout, no);
286 }
287 
288 void
289 control_handle(struct hastd_config *cfg)
290 {
291 	struct proto_conn *conn;
292 	struct nv *nvin, *nvout;
293 	unsigned int ii;
294 	const char *str;
295 	uint8_t cmd, role;
296 	int error;
297 
298 	if (proto_accept(cfg->hc_controlconn, &conn) < 0) {
299 		pjdlog_errno(LOG_ERR, "Unable to accept control connection");
300 		return;
301 	}
302 
303 	cfg->hc_controlin = conn;
304 	nvin = nvout = NULL;
305 	role = HAST_ROLE_UNDEF;
306 
307 	if (hast_proto_recv_hdr(conn, &nvin) < 0) {
308 		pjdlog_errno(LOG_ERR, "Unable to receive control header");
309 		nvin = NULL;
310 		goto close;
311 	}
312 
313 	/* Obtain command code. 0 means that nv_get_uint8() failed. */
314 	cmd = nv_get_uint8(nvin, "cmd");
315 	if (cmd == 0) {
316 		pjdlog_error("Control header is missing 'cmd' field.");
317 		error = EHAST_INVALID;
318 		goto close;
319 	}
320 
321 	/* Allocate outgoing nv structure. */
322 	nvout = nv_alloc();
323 	if (nvout == NULL) {
324 		pjdlog_error("Unable to allocate header for control response.");
325 		error = EHAST_NOMEMORY;
326 		goto close;
327 	}
328 
329 	error = 0;
330 
331 	str = nv_get_string(nvin, "resource0");
332 	if (str == NULL) {
333 		pjdlog_error("Control header is missing 'resource0' field.");
334 		error = EHAST_INVALID;
335 		goto fail;
336 	}
337 	if (cmd == HASTCTL_CMD_SETROLE) {
338 		role = nv_get_uint8(nvin, "role");
339 		switch (role) {
340 		case HAST_ROLE_INIT:
341 		case HAST_ROLE_PRIMARY:
342 		case HAST_ROLE_SECONDARY:
343 			break;
344 		default:
345 			pjdlog_error("Invalid role received (%hhu).", role);
346 			error = EHAST_INVALID;
347 			goto fail;
348 		}
349 	}
350 	if (strcmp(str, "all") == 0) {
351 		struct hast_resource *res;
352 
353 		/* All configured resources. */
354 
355 		ii = 0;
356 		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
357 			switch (cmd) {
358 			case HASTCTL_CMD_SETROLE:
359 				control_set_role_common(cfg, nvout, role, res,
360 				    res->hr_name, ii++);
361 				break;
362 			case HASTCTL_CMD_STATUS:
363 				control_status(cfg, nvout, res, res->hr_name,
364 				    ii++);
365 				break;
366 			default:
367 				pjdlog_error("Invalid command received (%hhu).",
368 				    cmd);
369 				error = EHAST_UNIMPLEMENTED;
370 				goto fail;
371 			}
372 		}
373 	} else {
374 		/* Only selected resources. */
375 
376 		for (ii = 0; ; ii++) {
377 			str = nv_get_string(nvin, "resource%u", ii);
378 			if (str == NULL)
379 				break;
380 			switch (cmd) {
381 			case HASTCTL_CMD_SETROLE:
382 				control_set_role_common(cfg, nvout, role, NULL,
383 				    str, ii);
384 				break;
385 			case HASTCTL_CMD_STATUS:
386 				control_status(cfg, nvout, NULL, str, ii);
387 				break;
388 			default:
389 				pjdlog_error("Invalid command received (%hhu).",
390 				    cmd);
391 				error = EHAST_UNIMPLEMENTED;
392 				goto fail;
393 			}
394 		}
395 	}
396 	if (nv_error(nvout) != 0)
397 		goto close;
398 fail:
399 	if (error != 0)
400 		nv_add_int16(nvout, error, "error");
401 
402 	if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0)
403 		pjdlog_errno(LOG_ERR, "Unable to send control response");
404 close:
405 	if (nvin != NULL)
406 		nv_free(nvin);
407 	if (nvout != NULL)
408 		nv_free(nvout);
409 	proto_close(conn);
410 	cfg->hc_controlin = NULL;
411 }
412 
413 /*
414  * Thread handles control requests from the parent.
415  */
416 void *
417 ctrl_thread(void *arg)
418 {
419 	struct hast_resource *res = arg;
420 	struct nv *nvin, *nvout;
421 	uint8_t cmd;
422 
423 	for (;;) {
424 		if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) < 0) {
425 			if (sigexit_received)
426 				pthread_exit(NULL);
427 			pjdlog_errno(LOG_ERR,
428 			    "Unable to receive control message");
429 			kill(getpid(), SIGTERM);
430 			pthread_exit(NULL);
431 		}
432 		cmd = nv_get_uint8(nvin, "cmd");
433 		if (cmd == 0) {
434 			pjdlog_error("Control message is missing 'cmd' field.");
435 			nv_free(nvin);
436 			continue;
437 		}
438 		nvout = nv_alloc();
439 		switch (cmd) {
440 		case CONTROL_STATUS:
441 			if (res->hr_remotein != NULL &&
442 			    res->hr_remoteout != NULL) {
443 				nv_add_string(nvout, "complete", "status");
444 			} else {
445 				nv_add_string(nvout, "degraded", "status");
446 			}
447 			nv_add_uint32(nvout, (uint32_t)res->hr_extentsize,
448 			    "extentsize");
449 			if (res->hr_role == HAST_ROLE_PRIMARY) {
450 				nv_add_uint32(nvout,
451 				    (uint32_t)res->hr_keepdirty, "keepdirty");
452 				nv_add_uint64(nvout,
453 				    (uint64_t)(activemap_ndirty(res->hr_amp) *
454 				    res->hr_extentsize), "dirty");
455 			} else {
456 				nv_add_uint32(nvout, (uint32_t)0, "keepdirty");
457 				nv_add_uint64(nvout, (uint64_t)0, "dirty");
458 			}
459 			nv_add_uint64(nvout, res->hr_stat_read, "stat_read");
460 			nv_add_uint64(nvout, res->hr_stat_write, "stat_write");
461 			nv_add_uint64(nvout, res->hr_stat_delete,
462 			    "stat_delete");
463 			nv_add_uint64(nvout, res->hr_stat_flush, "stat_flush");
464 			nv_add_uint64(nvout, res->hr_stat_activemap_update,
465 			    "stat_activemap_update");
466 			nv_add_int16(nvout, 0, "error");
467 			break;
468 		case CONTROL_RELOAD:
469 			/*
470 			 * When parent receives SIGHUP and discovers that
471 			 * something related to us has changes, it sends reload
472 			 * message to us.
473 			 */
474 			assert(res->hr_role == HAST_ROLE_PRIMARY);
475 			primary_config_reload(res, nvin);
476 			nv_add_int16(nvout, 0, "error");
477 			break;
478 		default:
479 			nv_add_int16(nvout, EINVAL, "error");
480 			break;
481 		}
482 		nv_free(nvin);
483 		if (nv_error(nvout) != 0) {
484 			pjdlog_error("Unable to create answer on control message.");
485 			nv_free(nvout);
486 			continue;
487 		}
488 		if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) < 0) {
489 			pjdlog_errno(LOG_ERR,
490 			    "Unable to send reply to control message");
491 		}
492 		nv_free(nvout);
493 	}
494 	/* NOTREACHED */
495 	return (NULL);
496 }
497