summaryrefslogtreecommitdiff
path: root/usr.sbin/iscsictl/iscsictl.c
blob: c629b4bbfbd7ce4c0ee7f6486a19630bedcf1ca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*	$OpenBSD: iscsictl.c,v 1.12 2021/04/16 14:39:33 claudio Exp $ */

/*
 * Copyright (c) 2010 Claudio Jeker <claudio@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/un.h>

#include <event.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>

#include "iscsid.h"
#include "iscsictl.h"

__dead void	 usage(void);
void		 run(void);
void		 run_command(struct pdu *);
struct pdu	*ctl_getpdu(char *, size_t);
int		 ctl_sendpdu(int, struct pdu *);
void		 show_config(struct ctrlmsghdr *, struct pdu *);
void		 show_vscsi_stats(struct ctrlmsghdr *, struct pdu *);
void             poll_and_wait(void);
void             poll_session_status(void);
void             register_poll(struct ctrlmsghdr *, struct pdu *);
void             poll_print(struct session_poll *);

char		cbuf[CONTROL_READ_SIZE];

struct control {
	struct pduq	channel;
	int		fd;
} control;


struct session_poll poll_result;
#define POLL_DELAY_SEC	1
#define POLL_ATTEMPTS	10

__dead void
usage(void)
{
	extern char *__progname;

	fprintf(stderr,"usage: %s [-s socket] command [argument ...]\n",
	    __progname);
	exit(1);
}

int
main (int argc, char* argv[])
{
	struct sockaddr_un sun;
	struct session_config sc;
	struct parse_result *res;
	char *confname = ISCSID_CONFIG;
	char *sockname = ISCSID_CONTROL;
	struct session_ctlcfg *s;
	struct iscsi_config *cf;
	int ch, poll = 0, val = 0;

	/* check flags */
	while ((ch = getopt(argc, argv, "f:s:")) != -1) {
		switch (ch) {
		case 'f':
			confname = optarg;
			break;
		case 's':
			sockname = optarg;
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	/* parse options */
	if ((res = parse(argc, argv)) == NULL)
		exit(1);

	/* connect to iscsid control socket */
	TAILQ_INIT(&control.channel);
	if ((control.fd = socket(AF_UNIX, SOCK_SEQPACKET, 0)) == -1)
		err(1, "socket");

	bzero(&sun, sizeof(sun));
	sun.sun_family = AF_UNIX;
	strlcpy(sun.sun_path, sockname, sizeof(sun.sun_path));

	if (connect(control.fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
		err(1, "connect: %s", sockname);

	if (pledge("stdio rpath dns", NULL) == -1)
		err(1, "pledge");

	switch (res->action) {
	case NONE:
	case LOG_VERBOSE:
		val = 1;
		/* FALLTHROUGH */
	case LOG_BRIEF:
		if (control_compose(NULL, CTRL_LOG_VERBOSE,
		    &val, sizeof(int)) == -1)
			err(1, "control_compose");
		break;
	case SHOW_SUM:
		if (control_compose(NULL, CTRL_SHOW_SUM, NULL, 0) == -1)
			err(1, "control_compose");
		break;
	case SHOW_SESS:
		usage();
		/* NOTREACHED */
	case SHOW_VSCSI_STATS:
		if (control_compose(NULL, CTRL_VSCSI_STATS, NULL, 0) == -1)
			err(1, "control_compose");
		break;
	case RELOAD:
		if ((cf = parse_config(confname)) == NULL)
			errx(1, "errors while loading configuration file.");
		if (cf->initiator.isid_base != 0) {
			if (control_compose(NULL, CTRL_INITIATOR_CONFIG,
			    &cf->initiator, sizeof(cf->initiator)) == -1)
				err(1, "control_compose");
		}

		SIMPLEQ_FOREACH(s, &cf->sessions, entry) {
			struct ctrldata cdv[3];
			bzero(cdv, sizeof(cdv));

			cdv[0].buf = &s->session;
			cdv[0].len = sizeof(s->session);

			if (s->session.TargetName) {
				cdv[1].buf = s->session.TargetName;
				cdv[1].len =
				    strlen(s->session.TargetName) + 1;
			}
			if (s->session.InitiatorName) {
				cdv[2].buf = s->session.InitiatorName;
				cdv[2].len =
				    strlen(s->session.InitiatorName) + 1;
			}

			if (control_build(NULL, CTRL_SESSION_CONFIG,
			    nitems(cdv), cdv) == -1)
				err(1, "control_build");
		}

		/* Reloading, so poll for connection establishment. */
		poll = 1;
		break;
	case DISCOVERY:
		printf("discover %s\n", log_sockaddr(&res->addr));

		bzero(&sc, sizeof(sc));
		snprintf(sc.SessionName, sizeof(sc.SessionName),
		    "discovery.%d", (int)getpid());
		bcopy(&res->addr, &sc.connection.TargetAddr, res->addr.ss_len);
		sc.SessionType = SESSION_TYPE_DISCOVERY;

		if (control_compose(NULL, CTRL_SESSION_CONFIG,
		    &sc, sizeof(sc)) == -1)
			err(1, "control_compose");
	}

	run();

	if (poll)
		poll_and_wait();

	close(control.fd);
	return (0);
}

void
control_queue(void *ch, struct pdu *pdu)
{
	TAILQ_INSERT_TAIL(&control.channel, pdu, entry);
}

void
run(void)
{
	struct pdu *pdu;

	while ((pdu = TAILQ_FIRST(&control.channel)) != NULL) {
		TAILQ_REMOVE(&control.channel, pdu, entry);
		run_command(pdu);
	}
}

void
run_command(struct pdu *pdu)
{
	struct ctrlmsghdr *cmh;
	int done = 0;
	ssize_t n;

	if (ctl_sendpdu(control.fd, pdu) == -1)
		err(1, "send");
	while (!done) {
		if ((n = recv(control.fd, cbuf, sizeof(cbuf), 0)) == -1 &&
		    !(errno == EAGAIN || errno == EINTR))
			err(1, "recv");

		if (n == 0)
			errx(1, "connection to iscsid closed");

		pdu = ctl_getpdu(cbuf, n);
		cmh = pdu_getbuf(pdu, NULL, 0);
		if (cmh == NULL)
			break;
		switch (cmh->type) {
		case CTRL_SUCCESS:
			printf("command successful\n");
			done = 1;
			break;
		case CTRL_FAILURE:
			printf("command failed\n");
			done = 1;
			break;
		case CTRL_INPROGRESS:
			printf("command in progress...\n");
			break;
		case CTRL_INITIATOR_CONFIG:
		case CTRL_SESSION_CONFIG:
			show_config(cmh, pdu);
			break;
		case CTRL_VSCSI_STATS:
			show_vscsi_stats(cmh, pdu);
			done = 1;
			break;
		case CTRL_SESS_POLL:
			register_poll(cmh, pdu);
			done = 1;
			break;

		}
	}
}

struct pdu *
ctl_getpdu(char *buf, size_t len)
{
	struct pdu *p;
	struct ctrlmsghdr *cmh;
	void *data;
	size_t n;
	int i;

	if (len < sizeof(*cmh))
		return NULL;

	if (!(p = pdu_new()))
		return NULL;

	n = sizeof(*cmh);
	cmh = pdu_alloc(n);
	bcopy(buf, cmh, n);
	buf += n;
	len -= n;

	if (pdu_addbuf(p, cmh, n, 0)) {
		free(cmh);
fail:
		pdu_free(p);
		return NULL;
	}

	for (i = 0; i < 3; i++) {
		n = cmh->len[i];
		if (n == 0)
			continue;
		if (PDU_LEN(n) > len)
			goto fail;
		if (!(data = pdu_alloc(n)))
			goto fail;
		bcopy(buf, data, n);
		if (pdu_addbuf(p, data, n, i + 1)) {
			free(data);
			goto fail;
		}
		buf += PDU_LEN(n);
		len -= PDU_LEN(n);
	}

	return p;
}

int
ctl_sendpdu(int fd, struct pdu *pdu)
{
	struct iovec iov[PDU_MAXIOV];
	struct msghdr msg;
	unsigned int niov = 0;

	for (niov = 0; niov < PDU_MAXIOV; niov++) {
		iov[niov].iov_base = pdu->iov[niov].iov_base;
		iov[niov].iov_len = pdu->iov[niov].iov_len;
	}
	bzero(&msg, sizeof(msg));
	msg.msg_iov = iov;
	msg.msg_iovlen = niov;
	if (sendmsg(fd, &msg, 0) == -1)
		return -1;
	return 0;
}

void
show_config(struct ctrlmsghdr *cmh, struct pdu *pdu)
{
	struct initiator_config	*ic;
	struct session_config	*sc;
	char *name;

	switch (cmh->type) {
	case CTRL_INITIATOR_CONFIG:
		if (cmh->len[0] != sizeof(*ic))
			errx(1, "bad size of response");
		ic = pdu_getbuf(pdu, NULL, 1);
		if (ic == NULL)
			return;

		printf("Initiator: ISID base %x qalifier %hx\n",
		    ic->isid_base, ic->isid_qual);
		break;
	case CTRL_SESSION_CONFIG:
		if (cmh->len[0] != sizeof(*sc))
			errx(1, "bad size of response");
		sc = pdu_getbuf(pdu, NULL, 1);
		if (sc == NULL)
			return;

		printf("\nSession '%s':%s\n", sc->SessionName,
		    sc->disabled ? " disabled" : "");
		printf("    SessionType: %s\tMaxConnections: %hd\n",
		    sc->SessionType == SESSION_TYPE_DISCOVERY ? "discovery" :
		    "normal", sc->MaxConnections);
		if ((name = pdu_getbuf(pdu, NULL, 2)))
			printf("    TargetName: %s\n", name);
		printf("    TargetAddr: %s\n",
		    log_sockaddr(&sc->connection.TargetAddr));
		if ((name = pdu_getbuf(pdu, NULL, 3)))
			printf("    InitiatorName: %s\n", name);
		printf("    InitiatorAddr: %s\n",
		    log_sockaddr(&sc->connection.LocalAddr));
		break;
	}
}

void
show_vscsi_stats(struct ctrlmsghdr *cmh, struct pdu *pdu)
{
	struct vscsi_stats *vs;
	char buf[FMT_SCALED_STRSIZE];

	if (cmh->len[0] != sizeof(struct vscsi_stats))
		errx(1, "bad size of response");
	vs = pdu_getbuf(pdu, NULL, 1);
	if (vs == NULL)
		return;

	printf("VSCSI ioctl statistics:\n");
	printf("%u probe calls and %u detach calls\n",
	    vs->cnt_probe, vs->cnt_detach);
	printf("%llu I2T calls (%llu read, %llu writes)\n",
	    vs->cnt_i2t,
	    vs->cnt_i2t_dir[1], 
	    vs->cnt_i2t_dir[2]);

	if (fmt_scaled(vs->bytes_rd, buf) != 0)
		(void)strlcpy(buf, "NaN", sizeof(buf));
	printf("%llu data reads (%s bytes read)\n", vs->cnt_read, buf);
	if (fmt_scaled(vs->bytes_wr, buf) != 0)
		(void)strlcpy(buf, "NaN", sizeof(buf));
	printf("%llu data writes (%s bytes written)\n", vs->cnt_write, buf);

	printf("%llu T2I calls (%llu done, %llu sense errors, %llu errors)\n",
	    vs->cnt_t2i,
	    vs->cnt_t2i_status[0], 
	    vs->cnt_t2i_status[1], 
	    vs->cnt_t2i_status[2]);
}

void
poll_session_status(void)
{
	struct pdu *pdu;

	if (control_compose(NULL, CTRL_SESS_POLL, NULL, 0) == -1)
		err(1, "control_compose");

	while ((pdu = TAILQ_FIRST(&control.channel)) != NULL) {
		TAILQ_REMOVE(&control.channel, pdu, entry);
		run_command(pdu);
	}
}

void
poll_and_wait(void)
{
	int attempts;

	printf("waiting for config to settle..");
	fflush(stdout);

	for (attempts = 0; attempts < POLL_ATTEMPTS; attempts++) {

		poll_session_status();

		/* Poll says we are good to go. */
		if (poll_result.sess_conn_status != 0) {
			printf("ok\n");
			/* wait a bit longer so all is settled. */
			sleep(POLL_DELAY_SEC);
			return;
		}

		/* Poll says we should wait... */
		printf(".");
		fflush(stdout);
		sleep(POLL_DELAY_SEC);
	}

	printf("giving up.\n");

	poll_print(&poll_result);
}

void
register_poll(struct ctrlmsghdr *cmh, struct pdu *pdu)
{
	if (cmh->len[0] != sizeof(poll_result))
		errx(1, "poll: bad size of response");

	poll_result = *((struct session_poll *)pdu_getbuf(pdu, NULL, 1));
}

void
poll_print(struct session_poll *p)
{
	printf("Configured sessions: %d\n", p->session_count);
	printf("Sessions initializing: %d\n", p->session_init_count);
	printf("Sessions started/failed: %d\n", p->session_running_count);
	printf("Sessions logged in: %d\n", p->conn_logged_in_count);
	printf("Sessions with failed connections: %d\n", p->conn_failed_count);
	printf("Sessions still attempting to connect: %d\n",
	    p->conn_waiting_count);
}