xref: /freebsd/sbin/ggate/ggatec/ggatec.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
1 /*-
2  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <libgen.h>
37 #include <pthread.h>
38 #include <signal.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <assert.h>
42 
43 #include <sys/param.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
48 #include <sys/time.h>
49 #include <sys/bio.h>
50 #include <netinet/in.h>
51 #include <netinet/tcp.h>
52 #include <arpa/inet.h>
53 
54 #include <geom/gate/g_gate.h>
55 #include "ggate.h"
56 
57 
58 enum { UNSET, CREATE, DESTROY, LIST, RESCUE } action = UNSET;
59 
60 static const char *path = NULL;
61 static const char *host = NULL;
62 static int unit = -1;
63 static unsigned flags = 0;
64 static int force = 0;
65 static unsigned queue_size = G_GATE_QUEUE_SIZE;
66 static unsigned port = G_GATE_PORT;
67 static off_t mediasize;
68 static unsigned sectorsize = 0;
69 static unsigned timeout = G_GATE_TIMEOUT;
70 static int sendfd, recvfd;
71 static uint32_t token;
72 static pthread_t sendtd, recvtd;
73 static int reconnect;
74 
75 static void
76 usage(void)
77 {
78 
79 	fprintf(stderr, "usage: %s create [-nv] [-o <ro|wo|rw>] [-p port] "
80 	    "[-q queue_size] [-R rcvbuf] [-S sndbuf] [-s sectorsize] "
81 	    "[-t timeout] [-u unit] <host> <path>\n", getprogname());
82 	fprintf(stderr, "       %s rescue [-nv] [-o <ro|wo|rw>] [-p port] "
83 	    "[-R rcvbuf] [-S sndbuf] <-u unit> <host> <path>\n", getprogname());
84 	fprintf(stderr, "       %s destroy [-f] <-u unit>\n", getprogname());
85 	fprintf(stderr, "       %s list [-v] [-u unit]\n", getprogname());
86 	exit(EXIT_FAILURE);
87 }
88 
89 static void *
90 send_thread(void *arg __unused)
91 {
92 	struct g_gate_ctl_io ggio;
93 	struct g_gate_hdr hdr;
94 	char buf[MAXPHYS];
95 	ssize_t data;
96 	int error;
97 
98 	g_gate_log(LOG_NOTICE, "%s: started!", __func__);
99 
100 	ggio.gctl_version = G_GATE_VERSION;
101 	ggio.gctl_unit = unit;
102 	ggio.gctl_data = buf;
103 
104 	for (;;) {
105 		ggio.gctl_length = sizeof(buf);
106 		ggio.gctl_error = 0;
107 		g_gate_ioctl(G_GATE_CMD_START, &ggio);
108 		error = ggio.gctl_error;
109 		switch (error) {
110 		case 0:
111 			break;
112 		case ECANCELED:
113 			if (reconnect)
114 				break;
115 			/* Exit gracefully. */
116 			g_gate_close_device();
117 			exit(EXIT_SUCCESS);
118 #if 0
119 		case ENOMEM:
120 			/* Buffer too small. */
121 			ggio.gctl_data = realloc(ggio.gctl_data,
122 			    ggio.gctl_length);
123 			if (ggio.gctl_data != NULL) {
124 				bsize = ggio.gctl_length;
125 				goto once_again;
126 			}
127 			/* FALLTHROUGH */
128 #endif
129 		case ENXIO:
130 		default:
131 			g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME,
132 			    strerror(error));
133 		}
134 
135 		if (reconnect)
136 			break;
137 
138 		switch (ggio.gctl_cmd) {
139 		case BIO_READ:
140 			hdr.gh_cmd = GGATE_CMD_READ;
141 			break;
142 		case BIO_WRITE:
143 			hdr.gh_cmd = GGATE_CMD_WRITE;
144 			break;
145 		}
146 		hdr.gh_seq = ggio.gctl_seq;
147 		hdr.gh_offset = ggio.gctl_offset;
148 		hdr.gh_length = ggio.gctl_length;
149 		hdr.gh_error = 0;
150 		g_gate_swap2n_hdr(&hdr);
151 
152 		data = g_gate_send(sendfd, &hdr, sizeof(hdr), MSG_NOSIGNAL);
153 		g_gate_log(LOG_DEBUG, "Sent hdr packet.");
154 		g_gate_swap2h_hdr(&hdr);
155 		if (reconnect)
156 			break;
157 		if (data != sizeof(hdr)) {
158 			g_gate_log(LOG_ERR, "Lost connection 1.");
159 			reconnect = 1;
160 			pthread_kill(recvtd, SIGUSR1);
161 			break;
162 		}
163 
164 		if (hdr.gh_cmd == GGATE_CMD_WRITE) {
165 			data = g_gate_send(sendfd, ggio.gctl_data,
166 			    ggio.gctl_length, MSG_NOSIGNAL);
167 			if (reconnect)
168 				break;
169 			if (data != ggio.gctl_length) {
170 				g_gate_log(LOG_ERR, "Lost connection 2 (%zd != %zd).", data, (ssize_t)ggio.gctl_length);
171 				reconnect = 1;
172 				pthread_kill(recvtd, SIGUSR1);
173 				break;
174 			}
175 			g_gate_log(LOG_DEBUG, "Sent %zd bytes (offset=%llu, "
176 			    "size=%u).", data, hdr.gh_offset, hdr.gh_length);
177 		}
178 	}
179 	g_gate_log(LOG_DEBUG, "%s: Died.", __func__);
180 	return (NULL);
181 }
182 
183 static void *
184 recv_thread(void *arg __unused)
185 {
186 	struct g_gate_ctl_io ggio;
187 	struct g_gate_hdr hdr;
188 	char buf[MAXPHYS];
189 	ssize_t data;
190 
191 	g_gate_log(LOG_NOTICE, "%s: started!", __func__);
192 
193 	ggio.gctl_version = G_GATE_VERSION;
194 	ggio.gctl_unit = unit;
195 	ggio.gctl_data = buf;
196 
197 	for (;;) {
198 		data = g_gate_recv(recvfd, &hdr, sizeof(hdr), MSG_WAITALL);
199 		if (reconnect)
200 			break;
201 		g_gate_swap2h_hdr(&hdr);
202 		if (data != sizeof(hdr)) {
203 			if (data == -1 && errno == EAGAIN)
204 				continue;
205 			g_gate_log(LOG_ERR, "Lost connection 3.");
206 			reconnect = 1;
207 			pthread_kill(sendtd, SIGUSR1);
208 			break;
209 		}
210 		g_gate_log(LOG_DEBUG, "Received hdr packet.");
211 
212 		ggio.gctl_seq = hdr.gh_seq;
213 		ggio.gctl_cmd = hdr.gh_cmd;
214 		ggio.gctl_offset = hdr.gh_offset;
215 		ggio.gctl_length = hdr.gh_length;
216 		ggio.gctl_error = hdr.gh_error;
217 
218 		if (ggio.gctl_error == 0 && ggio.gctl_cmd == GGATE_CMD_READ) {
219 			data = g_gate_recv(recvfd, ggio.gctl_data,
220 			    ggio.gctl_length, MSG_WAITALL);
221 			if (reconnect)
222 				break;
223 			g_gate_log(LOG_DEBUG, "Received data packet.");
224 			if (data != ggio.gctl_length) {
225 				g_gate_log(LOG_ERR, "Lost connection 4.");
226 				reconnect = 1;
227 				pthread_kill(sendtd, SIGUSR1);
228 				break;
229 			}
230 			g_gate_log(LOG_DEBUG, "Received %d bytes (offset=%ju, "
231 			    "size=%zu).", data, (uintmax_t)hdr.gh_offset,
232 			    (size_t)hdr.gh_length);
233 		}
234 
235 		g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
236 	}
237 	g_gate_log(LOG_DEBUG, "%s: Died.", __func__);
238 	pthread_exit(NULL);
239 }
240 
241 static int
242 handshake(int dir)
243 {
244 	struct g_gate_version ver;
245 	struct g_gate_cinit cinit;
246 	struct g_gate_sinit sinit;
247 	struct sockaddr_in serv;
248 	int sfd;
249 
250 	/*
251 	 * Do the network stuff.
252 	 */
253 	bzero(&serv, sizeof(serv));
254 	serv.sin_family = AF_INET;
255 	serv.sin_addr.s_addr = g_gate_str2ip(host);
256 	if (serv.sin_addr.s_addr == INADDR_NONE) {
257 		g_gate_log(LOG_DEBUG, "Invalid IP/host name: %s.", host);
258 		return (-1);
259 	}
260 	serv.sin_port = htons(port);
261 	sfd = socket(AF_INET, SOCK_STREAM, 0);
262 	if (sfd == -1) {
263 		g_gate_log(LOG_DEBUG, "Cannot open socket: %s.",
264 		    strerror(errno));
265 		return (-1);
266 	}
267 
268 	g_gate_socket_settings(sfd);
269 
270 	if (connect(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1) {
271 		g_gate_log(LOG_DEBUG, "Cannot connect to server: %s.",
272 		    strerror(errno));
273 		close(sfd);
274 		return (-1);
275 	}
276 
277 	g_gate_log(LOG_INFO, "Connected to the server: %s:%d.", host, port);
278 
279 	/*
280 	 * Create and send version packet.
281 	 */
282 	g_gate_log(LOG_DEBUG, "Sending version packet.");
283 	assert(strlen(GGATE_MAGIC) == sizeof(ver.gv_magic));
284 	bcopy(GGATE_MAGIC, ver.gv_magic, sizeof(ver.gv_magic));
285 	ver.gv_version = GGATE_VERSION;
286 	ver.gv_error = 0;
287 	g_gate_swap2n_version(&ver);
288 	if (g_gate_send(sfd, &ver, sizeof(ver), MSG_NOSIGNAL) == -1) {
289 		g_gate_log(LOG_DEBUG, "Error while sending version packet: %s.",
290 		    strerror(errno));
291 		close(sfd);
292 		return (-1);
293 	}
294 	bzero(&ver, sizeof(ver));
295 	if (g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL) == -1) {
296 		g_gate_log(LOG_DEBUG, "Error while receiving data: %s.",
297 		    strerror(errno));
298 		close(sfd);
299 		return (-1);
300 	}
301 	if (ver.gv_error != 0) {
302 		g_gate_log(LOG_DEBUG, "Version verification problem: %s.",
303 		    strerror(errno));
304 		close(sfd);
305 		return (-1);
306 	}
307 
308 	/*
309 	 * Create and send initial packet.
310 	 */
311 	g_gate_log(LOG_DEBUG, "Sending initial packet.");
312 	if (strlcpy(cinit.gc_path, path, sizeof(cinit.gc_path)) >=
313 	    sizeof(cinit.gc_path)) {
314 		g_gate_log(LOG_DEBUG, "Path name too long.");
315 		close(sfd);
316 		return (-1);
317 	}
318 	cinit.gc_flags = flags | dir;
319 	cinit.gc_token = token;
320 	cinit.gc_nconn = 2;
321 	g_gate_swap2n_cinit(&cinit);
322 	if (g_gate_send(sfd, &cinit, sizeof(cinit), MSG_NOSIGNAL) == -1) {
323 	        g_gate_log(LOG_DEBUG, "Error while sending initial packet: %s.",
324 		    strerror(errno));
325 		close(sfd);
326 		return (-1);
327 	}
328 	g_gate_swap2h_cinit(&cinit);
329 
330 	/*
331 	 * Receiving initial packet from server.
332 	 */
333 	g_gate_log(LOG_DEBUG, "Receiving initial packet.");
334 	if (g_gate_recv(sfd, &sinit, sizeof(sinit), MSG_WAITALL) == -1) {
335 		g_gate_log(LOG_DEBUG, "Error while receiving data: %s.",
336 		    strerror(errno));
337 		close(sfd);
338 		return (-1);
339 	}
340 	g_gate_swap2h_sinit(&sinit);
341 	if (sinit.gs_error != 0) {
342 	        g_gate_log(LOG_DEBUG, "Error from server: %s.",
343 		    strerror(sinit.gs_error));
344 		close(sfd);
345 		return (-1);
346 	}
347 	g_gate_log(LOG_DEBUG, "Received initial packet.");
348 
349 	mediasize = sinit.gs_mediasize;
350 	if (sectorsize == 0)
351 		sectorsize = sinit.gs_sectorsize;
352 
353 	return (sfd);
354 }
355 
356 static void
357 mydaemon(void)
358 {
359 
360 	if (g_gate_verbose > 0)
361 		return;
362 	if (daemon(0, 0) == 0)
363 		return;
364 	if (action == CREATE)
365 		g_gate_destroy(unit, 1);
366 	err(EXIT_FAILURE, "Cannot daemonize");
367 }
368 
369 static int
370 g_gatec_connect(void)
371 {
372 
373 	token = arc4random();
374 	/*
375 	 * Our receive descriptor is connected to the send descriptor on the
376 	 * server side.
377 	 */
378 	recvfd = handshake(GGATE_FLAG_SEND);
379 	if (recvfd == -1)
380 		return (0);
381 	/*
382 	 * Our send descriptor is connected to the receive descriptor on the
383 	 * server side.
384 	 */
385 	sendfd = handshake(GGATE_FLAG_RECV);
386 	if (sendfd == -1)
387 		return (0);
388 	return (1);
389 }
390 
391 static void
392 g_gatec_start(void)
393 {
394 	int error;
395 
396 	reconnect = 0;
397 	error = pthread_create(&recvtd, NULL, recv_thread, NULL);
398 	if (error != 0) {
399 		g_gate_destroy(unit, 1);
400 		g_gate_xlog("pthread_create(recv_thread): %s.",
401 		    strerror(error));
402 	}
403 	sendtd = pthread_self();
404 	send_thread(NULL);
405 	/* Disconnected. */
406 	close(sendfd);
407 	close(recvfd);
408 }
409 
410 static void
411 signop(int sig __unused)
412 {
413 
414 	/* Do nothing. */
415 }
416 
417 static void
418 g_gatec_loop(void)
419 {
420 	struct g_gate_ctl_cancel ggioc;
421 
422 	signal(SIGUSR1, signop);
423 	for (;;) {
424 		g_gatec_start();
425 		g_gate_log(LOG_NOTICE, "Disconnected [%s %s]. Connecting...",
426 		    host, path);
427 		while (!g_gatec_connect()) {
428 			sleep(2);
429 			g_gate_log(LOG_NOTICE, "Connecting [%s %s]...", host,
430 			    path);
431 		}
432 		ggioc.gctl_version = G_GATE_VERSION;
433 		ggioc.gctl_unit = unit;
434 		ggioc.gctl_seq = 0;
435 		g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
436 	}
437 }
438 
439 static void
440 g_gatec_create(void)
441 {
442 	struct g_gate_ctl_create ggioc;
443 
444 	if (!g_gatec_connect())
445 		g_gate_xlog("Cannot connect: %s.", strerror(errno));
446 
447 	/*
448 	 * Ok, got both sockets, time to create provider.
449 	 */
450 	ggioc.gctl_version = G_GATE_VERSION;
451 	ggioc.gctl_mediasize = mediasize;
452 	ggioc.gctl_sectorsize = sectorsize;
453 	ggioc.gctl_flags = flags;
454 	ggioc.gctl_maxcount = queue_size;
455 	ggioc.gctl_timeout = timeout;
456 	ggioc.gctl_unit = unit;
457 	snprintf(ggioc.gctl_info, sizeof(ggioc.gctl_info), "%s:%u %s", host,
458 	    port, path);
459 	g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc);
460 	if (unit == -1)
461 		printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
462 	unit = ggioc.gctl_unit;
463 
464 	mydaemon();
465 	g_gatec_loop();
466 }
467 
468 static void
469 g_gatec_rescue(void)
470 {
471 	struct g_gate_ctl_cancel ggioc;
472 
473 	if (!g_gatec_connect())
474 		g_gate_xlog("Cannot connect: %s.", strerror(errno));
475 
476 	ggioc.gctl_version = G_GATE_VERSION;
477 	ggioc.gctl_unit = unit;
478 	ggioc.gctl_seq = 0;
479 	g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
480 
481 	mydaemon();
482 	g_gatec_loop();
483 }
484 
485 int
486 main(int argc, char *argv[])
487 {
488 
489 	if (argc < 2)
490 		usage();
491 	if (strcasecmp(argv[1], "create") == 0)
492 		action = CREATE;
493 	else if (strcasecmp(argv[1], "destroy") == 0)
494 		action = DESTROY;
495 	else if (strcasecmp(argv[1], "list") == 0)
496 		action = LIST;
497 	else if (strcasecmp(argv[1], "rescue") == 0)
498 		action = RESCUE;
499 	else
500 		usage();
501 	argc -= 1;
502 	argv += 1;
503 	for (;;) {
504 		int ch;
505 
506 		ch = getopt(argc, argv, "fno:p:q:R:S:s:t:u:v");
507 		if (ch == -1)
508 			break;
509 		switch (ch) {
510 		case 'f':
511 			if (action != DESTROY)
512 				usage();
513 			force = 1;
514 			break;
515 		case 'n':
516 			if (action != CREATE && action != RESCUE)
517 				usage();
518 			nagle = 0;
519 			break;
520 		case 'o':
521 			if (action != CREATE && action != RESCUE)
522 				usage();
523 			if (strcasecmp("ro", optarg) == 0)
524 				flags = G_GATE_FLAG_READONLY;
525 			else if (strcasecmp("wo", optarg) == 0)
526 				flags = G_GATE_FLAG_WRITEONLY;
527 			else if (strcasecmp("rw", optarg) == 0)
528 				flags = 0;
529 			else {
530 				errx(EXIT_FAILURE,
531 				    "Invalid argument for '-o' option.");
532 			}
533 			break;
534 		case 'p':
535 			if (action != CREATE && action != RESCUE)
536 				usage();
537 			errno = 0;
538 			port = strtoul(optarg, NULL, 10);
539 			if (port == 0 && errno != 0)
540 				errx(EXIT_FAILURE, "Invalid port.");
541 			break;
542 		case 'q':
543 			if (action != CREATE)
544 				usage();
545 			errno = 0;
546 			queue_size = strtoul(optarg, NULL, 10);
547 			if (queue_size == 0 && errno != 0)
548 				errx(EXIT_FAILURE, "Invalid queue_size.");
549 			break;
550 		case 'R':
551 			if (action != CREATE && action != RESCUE)
552 				usage();
553 			errno = 0;
554 			rcvbuf = strtoul(optarg, NULL, 10);
555 			if (rcvbuf == 0 && errno != 0)
556 				errx(EXIT_FAILURE, "Invalid rcvbuf.");
557 			break;
558 		case 'S':
559 			if (action != CREATE && action != RESCUE)
560 				usage();
561 			errno = 0;
562 			sndbuf = strtoul(optarg, NULL, 10);
563 			if (sndbuf == 0 && errno != 0)
564 				errx(EXIT_FAILURE, "Invalid sndbuf.");
565 			break;
566 		case 's':
567 			if (action != CREATE)
568 				usage();
569 			errno = 0;
570 			sectorsize = strtoul(optarg, NULL, 10);
571 			if (sectorsize == 0 && errno != 0)
572 				errx(EXIT_FAILURE, "Invalid sectorsize.");
573 			break;
574 		case 't':
575 			if (action != CREATE)
576 				usage();
577 			errno = 0;
578 			timeout = strtoul(optarg, NULL, 10);
579 			if (timeout == 0 && errno != 0)
580 				errx(EXIT_FAILURE, "Invalid timeout.");
581 			break;
582 		case 'u':
583 			errno = 0;
584 			unit = strtol(optarg, NULL, 10);
585 			if (unit == 0 && errno != 0)
586 				errx(EXIT_FAILURE, "Invalid unit number.");
587 			break;
588 		case 'v':
589 			if (action == DESTROY)
590 				usage();
591 			g_gate_verbose++;
592 			break;
593 		default:
594 			usage();
595 		}
596 	}
597 	argc -= optind;
598 	argv += optind;
599 
600 	switch (action) {
601 	case CREATE:
602 		if (argc != 2)
603 			usage();
604 		g_gate_load_module();
605 		g_gate_open_device();
606 		host = argv[0];
607 		path = argv[1];
608 		g_gatec_create();
609 		break;
610 	case DESTROY:
611 		if (unit == -1) {
612 			fprintf(stderr, "Required unit number.\n");
613 			usage();
614 		}
615 		g_gate_verbose = 1;
616 		g_gate_open_device();
617 		g_gate_destroy(unit, force);
618 		break;
619 	case LIST:
620 		g_gate_list(unit, g_gate_verbose);
621 		break;
622 	case RESCUE:
623 		if (argc != 2)
624 			usage();
625 		if (unit == -1) {
626 			fprintf(stderr, "Required unit number.\n");
627 			usage();
628 		}
629 		g_gate_open_device();
630 		host = argv[0];
631 		path = argv[1];
632 		g_gatec_rescue();
633 		break;
634 	case UNSET:
635 	default:
636 		usage();
637 	}
638 	g_gate_close_device();
639 	exit(EXIT_SUCCESS);
640 }
641