xref: /freebsd/sbin/ggate/ggatec/ggatec.c (revision 587392a5a04955b85c3beac4cff26ca568359f54)
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 = G_GATE_UNIT_AUTO;
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 		fflush(stdout);
463 	}
464 	unit = ggioc.gctl_unit;
465 
466 	mydaemon();
467 	g_gatec_loop();
468 }
469 
470 static void
471 g_gatec_rescue(void)
472 {
473 	struct g_gate_ctl_cancel ggioc;
474 
475 	if (!g_gatec_connect())
476 		g_gate_xlog("Cannot connect: %s.", strerror(errno));
477 
478 	ggioc.gctl_version = G_GATE_VERSION;
479 	ggioc.gctl_unit = unit;
480 	ggioc.gctl_seq = 0;
481 	g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
482 
483 	mydaemon();
484 	g_gatec_loop();
485 }
486 
487 int
488 main(int argc, char *argv[])
489 {
490 
491 	if (argc < 2)
492 		usage();
493 	if (strcasecmp(argv[1], "create") == 0)
494 		action = CREATE;
495 	else if (strcasecmp(argv[1], "destroy") == 0)
496 		action = DESTROY;
497 	else if (strcasecmp(argv[1], "list") == 0)
498 		action = LIST;
499 	else if (strcasecmp(argv[1], "rescue") == 0)
500 		action = RESCUE;
501 	else
502 		usage();
503 	argc -= 1;
504 	argv += 1;
505 	for (;;) {
506 		int ch;
507 
508 		ch = getopt(argc, argv, "fno:p:q:R:S:s:t:u:v");
509 		if (ch == -1)
510 			break;
511 		switch (ch) {
512 		case 'f':
513 			if (action != DESTROY)
514 				usage();
515 			force = 1;
516 			break;
517 		case 'n':
518 			if (action != CREATE && action != RESCUE)
519 				usage();
520 			nagle = 0;
521 			break;
522 		case 'o':
523 			if (action != CREATE && action != RESCUE)
524 				usage();
525 			if (strcasecmp("ro", optarg) == 0)
526 				flags = G_GATE_FLAG_READONLY;
527 			else if (strcasecmp("wo", optarg) == 0)
528 				flags = G_GATE_FLAG_WRITEONLY;
529 			else if (strcasecmp("rw", optarg) == 0)
530 				flags = 0;
531 			else {
532 				errx(EXIT_FAILURE,
533 				    "Invalid argument for '-o' option.");
534 			}
535 			break;
536 		case 'p':
537 			if (action != CREATE && action != RESCUE)
538 				usage();
539 			errno = 0;
540 			port = strtoul(optarg, NULL, 10);
541 			if (port == 0 && errno != 0)
542 				errx(EXIT_FAILURE, "Invalid port.");
543 			break;
544 		case 'q':
545 			if (action != CREATE)
546 				usage();
547 			errno = 0;
548 			queue_size = strtoul(optarg, NULL, 10);
549 			if (queue_size == 0 && errno != 0)
550 				errx(EXIT_FAILURE, "Invalid queue_size.");
551 			break;
552 		case 'R':
553 			if (action != CREATE && action != RESCUE)
554 				usage();
555 			errno = 0;
556 			rcvbuf = strtoul(optarg, NULL, 10);
557 			if (rcvbuf == 0 && errno != 0)
558 				errx(EXIT_FAILURE, "Invalid rcvbuf.");
559 			break;
560 		case 'S':
561 			if (action != CREATE && action != RESCUE)
562 				usage();
563 			errno = 0;
564 			sndbuf = strtoul(optarg, NULL, 10);
565 			if (sndbuf == 0 && errno != 0)
566 				errx(EXIT_FAILURE, "Invalid sndbuf.");
567 			break;
568 		case 's':
569 			if (action != CREATE)
570 				usage();
571 			errno = 0;
572 			sectorsize = strtoul(optarg, NULL, 10);
573 			if (sectorsize == 0 && errno != 0)
574 				errx(EXIT_FAILURE, "Invalid sectorsize.");
575 			break;
576 		case 't':
577 			if (action != CREATE)
578 				usage();
579 			errno = 0;
580 			timeout = strtoul(optarg, NULL, 10);
581 			if (timeout == 0 && errno != 0)
582 				errx(EXIT_FAILURE, "Invalid timeout.");
583 			break;
584 		case 'u':
585 			errno = 0;
586 			unit = strtol(optarg, NULL, 10);
587 			if (unit == 0 && errno != 0)
588 				errx(EXIT_FAILURE, "Invalid unit number.");
589 			break;
590 		case 'v':
591 			if (action == DESTROY)
592 				usage();
593 			g_gate_verbose++;
594 			break;
595 		default:
596 			usage();
597 		}
598 	}
599 	argc -= optind;
600 	argv += optind;
601 
602 	switch (action) {
603 	case CREATE:
604 		if (argc != 2)
605 			usage();
606 		g_gate_load_module();
607 		g_gate_open_device();
608 		host = argv[0];
609 		path = argv[1];
610 		g_gatec_create();
611 		break;
612 	case DESTROY:
613 		if (unit == -1) {
614 			fprintf(stderr, "Required unit number.\n");
615 			usage();
616 		}
617 		g_gate_verbose = 1;
618 		g_gate_open_device();
619 		g_gate_destroy(unit, force);
620 		break;
621 	case LIST:
622 		g_gate_list(unit, g_gate_verbose);
623 		break;
624 	case RESCUE:
625 		if (argc != 2)
626 			usage();
627 		if (unit == -1) {
628 			fprintf(stderr, "Required unit number.\n");
629 			usage();
630 		}
631 		g_gate_open_device();
632 		host = argv[0];
633 		path = argv[1];
634 		g_gatec_rescue();
635 		break;
636 	case UNSET:
637 	default:
638 		usage();
639 	}
640 	g_gate_close_device();
641 	exit(EXIT_SUCCESS);
642 }
643