Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*	$NetBSD: iscsic_main.c,v 1.6 2015/05/30 15:57:32 joerg Exp $	*/

/*-
 * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "iscsic_globals.h"

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>

#define DEVICE    "/dev/iscsi0"

#define ISCSICTL_VERSION	"20110805"

/*
 *-------- Table of commands and the associated handler function -------------
*/

static command_t cmds[] = {
	{"version", get_version},
	{"add_target", add_target},
	{"add_portal", add_portal},
	{"remove_target", remove_target},
	{"slp_find_targets", slp_find_targets},
	{"refresh_targets", refresh_targets},
	{"list_targets", list_targets},
	{"add_send_target", add_send_target},
	{"remove_send_target", remove_send_target},
	{"list_send_targets", list_send_targets},
	{"add_isns_server", add_isns_server},
	{"remove_isns_server", remove_isns_server},
	{"find_isns_servers", find_isns_servers},
	{"list_isns_servers", list_isns_servers},
	{"refresh_isns", refresh_isns},
	{"login", login},
	{"logout", logout},
	{"add_connection", add_connection},
	{"remove_connection", remove_connection},
	{"inquiry", inquiry},
	{"read_capacity", read_capacity},
	{"report_luns", report_luns},
	{"test_unit_ready", test_unit_ready},
	{"add_initiator", add_initiator},
	{"remove_initiator", remove_initiator},
	{"list_initiators", list_initiators},
	{"list_sessions", list_sessions},
	{"set_node_name", set_node_name},
	{NULL, NULL}
};


/*
 *-------- Table of error codes and the associated message text -------------
*/

typedef struct {
	unsigned	 code;
	const char	*str;
} status_msg_t;

static status_msg_t status_msg[] = {
	{ISCSI_STATUS_LIST_EMPTY, "The list is empty"},
	{ISCSI_STATUS_DUPLICATE_NAME, "The specified name is not unique"},
	{ISCSI_STATUS_GENERAL_ERROR, "A non-specific error occurred"},
	{ISCSI_STATUS_LOGIN_FAILED, "The login failed"},
	{ISCSI_STATUS_CONNECTION_FAILED, "The attempt to establish a connection failed"},
	{ISCSI_STATUS_AUTHENTICATION_FAILED, "Authentication negotiation failed"},
	{ISCSI_STATUS_NO_RESOURCES, "Could not allocate resources (e.g. memory)"},
	{ISCSI_STATUS_MAXED_CONNECTIONS, "Maximum number of connections exceeded"},
	{ISCSI_STATUS_INVALID_SESSION_ID, "Session ID not found"},
	{ISCSI_STATUS_INVALID_CONNECTION_ID, "Connection ID not found"},
	{ISCSI_STATUS_INVALID_SOCKET, "Specified socket is invalid"},
	{ISCSI_STATUS_NOTIMPL, "Feature not implemented"},
	{ISCSI_STATUS_CHECK_CONDITION, "Target reported CHECK CONDITION"},
	{ISCSI_STATUS_TARGET_BUSY, "Target reported BUSY"},
	{ISCSI_STATUS_TARGET_ERROR, "Target reported other error"},
	{ISCSI_STATUS_TARGET_FAILURE, "Command Response was Target Failure"},
	{ISCSI_STATUS_TARGET_DROP, "Target dropped connection"},
	{ISCSI_STATUS_SOCKET_ERROR, "Communication failure"},
	{ISCSI_STATUS_PARAMETER_MISSING, "A required ioctl parameter is missing"},
	{ISCSI_STATUS_PARAMETER_INVALID, "A parameter is malformed (string too long etc.)"},
	{ISCSI_STATUS_MAP_FAILED, "Mapping the LUNs failed"},
	{ISCSI_STATUS_NO_INITIATOR_NAME, "Initiator name not set"},
	{ISCSI_STATUS_NEGOTIATION_ERROR, "Negotiation failure (invalid key or value)"},
	{ISCSI_STATUS_TIMEOUT, "Command timed out (at iSCSI level)"},
	{ISCSI_STATUS_PROTOCOL_ERROR, "Internal Error (Protocol error reject)"},
	{ISCSI_STATUS_PDU_ERROR, "Internal Error (Invalid PDU field reject)"},
	{ISCSI_STATUS_CMD_NOT_SUPPORTED, "Target does not support iSCSI command"},
	{ISCSI_STATUS_DRIVER_UNLOAD, "Driver is unloading"},
	{ISCSI_STATUS_LOGOUT, "Session was logged out"},
	{ISCSI_STATUS_PDUS_LOST, "Excessive PDU loss"},
	{ISCSI_STATUS_INVALID_EVENT_ID, "Invalid Event ID"},
	{ISCSI_STATUS_EVENT_DEREGISTERED, "Wait for event cancelled by deregistration"},
	{ISCSI_STATUS_EVENT_WAITING, "Already waiting for event"},
	{ISCSI_STATUS_TASK_NOT_FOUND, "Task Management: task not found"},
	{ISCSI_STATUS_LUN_NOT_FOUND, "Task Management: LUN not found"},
	{ISCSI_STATUS_TASK_ALLEGIANT, "Task Management: Task still allegiant"},
	{ISCSI_STATUS_CANT_REASSIGN, "Task Management: Task reassignment not supported"},
	{ISCSI_STATUS_FUNCTION_UNSUPPORTED, "Task Management: Function unsupported"},
	{ISCSI_STATUS_FUNCTION_NOT_AUTHORIZED, "Task Management: Function not authorized"},
	{ISCSI_STATUS_FUNCTION_REJECTED, "Task Management: Function rejected"},
	{ISCSI_STATUS_UNKNOWN_REASON, "Task Management: Unknown reason code"},
	{ISCSI_STATUS_DUPLICATE_ID, "Duplicate ID"},
	{ISCSI_STATUS_INVALID_ID, "ID not found"},
	{ISCSI_STATUS_TARGET_LOGOUT, "Target requested logout"},
	{ISCSI_STATUS_LOGOUT_CID_NOT_FOUND, "Logout error: Connection ID not found"},
	{ISCSI_STATUS_LOGOUT_RECOVERY_NS, "Logout error: Recovery not supported"},
	{ISCSI_STATUS_LOGOUT_ERROR, "Logout error: Unknown reason"},

	{ISCSID_STATUS_LIST_EMPTY, "The list is empty"},
	{ISCSID_STATUS_DUPLICATE_NAME, "The specified name is not unique"},
	{ISCSID_STATUS_GENERAL_ERROR, "A non-specific error occurred"},
	{ISCSID_STATUS_CONNECT_ERROR, "Failed to connect to target"},
	{ISCSID_STATUS_NO_RESOURCES, "Could not allocate resources (e.g. memory)"},
	{ISCSID_STATUS_INVALID_SESSION_ID, "Session ID not found"},
	{ISCSID_STATUS_INVALID_CONNECTION_ID, "Connection ID not found"},
	{ISCSID_STATUS_NOTIMPL, "Feature not implemented"},
	{ISCSID_STATUS_SOCKET_ERROR, "Failed to create socket"},
	{ISCSID_STATUS_PARAMETER_MISSING, "A required parameter is missing"},
	{ISCSID_STATUS_PARAMETER_INVALID, "Request parameter is invalid"},
	{ISCSID_STATUS_NO_INITIATOR_NAME, "Initiator name was not set"},
	{ISCSID_STATUS_TIMEOUT, "Request timed out"},
	{ISCSID_STATUS_DRIVER_NOT_LOADED, "iSCSI Driver not loaded"},
	{ISCSID_STATUS_INVALID_REQUEST, "Unknown request code"},
	{ISCSID_STATUS_INVALID_PORTAL_ID, "Portal ID not found"},
	{ISCSID_STATUS_INVALID_TARGET_ID, "Target ID not found"},
	{ISCSID_STATUS_NOT_FOUND, "Requested item not found"},
	{ISCSID_STATUS_HOST_NOT_FOUND, "Target address not found"},
	{ISCSID_STATUS_HOST_TRY_AGAIN, "Target address retrieval failed, try again later"},
	{ISCSID_STATUS_HOST_ERROR, "Target address invalid"},
	{ISCSID_STATUS_NO_TARGETS_FOUND, "No targets found"},
	{ISCSID_STATUS_INVALID_ISNS_ID, "iSNS ID not found"},
	{ISCSID_STATUS_ISNS_ERROR, "Problem connecting to iSNS"},
	{ISCSID_STATUS_ISNS_SERVER_ERROR, "iSNS server returned garbage"},
	{ISCSID_STATUS_DUPLICATE_ENTRY, "The entry already exists"},
	{ISCSID_STATUS_INVALID_INITIATOR_ID, "Initiator ID not found"},
	{ISCSID_STATUS_INITIATOR_BIND_ERROR, "Bind to initiator portal failed"},

	{0, NULL}
};

/* -------------------------------------------------------------------------- */
/* local variables */

static struct sockaddr_un	daemon_name;	/* daemon socket name */

static char		 	sockdir[MAXPATHLEN]; /* where myname lives */
static struct sockaddr_un	myname;		/* my socket name */
static int			sock;		/* the socket */

static char *cmdname;			/* pointer to command name for error msgs */

/* global variables */

uint8_t buf[BUF_SIZE];			/* buffer for daemon comm and driver I/O */

int driver;						/* driver handle */

/* -------------------------------------------------------------------------- */

#define progname  getprogname()


/*
 * bye:
 *    Cleanup and exit. Does not return.
*/

__dead static void
bye(void)
{
	close(sock);
	(void) unlink(myname.sun_path);
	(void) rmdir(sockdir);
	exit(EXIT_FAILURE);
}


/*
 * arg_error:
 *    Display error message about an invalid argument, exit.
 *
 *    Parameters:
 *       argp        Argument value
 *       fmt         Error message
 *       ...         additional output arguments
 *
 *    Does not return.
*/

void
arg_error(char *argp, const char *fmt, ...)
{
	va_list args;
	char	lbuf[BUF_SIZE];

	va_start(args, fmt);
	vsnprintf(lbuf, sizeof(lbuf), fmt, args);
	fprintf(stderr, "%s: %s: Invalid option at or near '%s': %s\n",
		progname, cmdname, argp, lbuf);
	bye();
}


/*
 * arg_missing:
 *    Display error message about a missing argument, exit.
 *
 *    Parameters:
 *       arg      Argument name
 *
 *    Does not return.
 */

void
arg_missing(const char *arg)
{
	warnx("%s: Missing argument: %s", cmdname, arg);
	bye();
}


/*
 * io_error:
 *    Display error message about an I/O error (includes system error code)
 *
 *    Parameters:
 *       fmt         format string
 *       ...         additional output arguments
 *
 *    Does not return.
 */

void
io_error(const char *fmt, ...)
{
	va_list args;
	char	lbuf[BUF_SIZE];

	va_start(args, fmt);
	vsnprintf(lbuf, sizeof(lbuf), fmt, args);
	fprintf(stderr, "%s: %s: %s: %s\n",
		progname, cmdname, lbuf, strerror(errno));
	bye();
}


/*
 * gen_error:
 *    Display general error message.
 *
 *    Parameters:
 *       fmt         format string
 *       ...         additional output arguments
 *
 *    Does not return.
 */

void
gen_error(const char *fmt, ...)
{
	va_list args;
	char	lbuf[BUF_SIZE];

	va_start(args, fmt);
	vsnprintf(lbuf, sizeof(lbuf), fmt, args);
	fprintf(stderr, "%s: %s: %s\n", progname, cmdname, lbuf);
	bye();
}


/*
 * check_extra_args:
 *    Display error message & exit if there is an unrecognized argument.
 *
 *    Parameters:
 *       argc     Argument count
 *       argv     Argument value array.
 *
 *    Does not return if an extra arg is found.
 */

void
check_extra_args(int argc, char **argv)
{
	int i;

	for (i = 0; i < argc; i++)
		if (argv[i] != NULL) {
			warnx("%s: Unrecognized argument '%s'", cmdname, argv[i]);
			bye();
		}
}


/*
 * status_error:
 *    Display error message for status returned by daemon or driver, exit.
 *
 *    Parameters:
 *          n     Status code.
 *
 *    Does not return.
 */

void
status_error(unsigned n)
{
	int i;

	for (i = 0; status_msg[i].code; i++)
		if (status_msg[i].code == n)
			break;

	if (status_msg[i].code)
		warnx("%s: %s", cmdname, status_msg[i].str);
	else
		warnx("%s: Undefined error code %d", cmdname, n);

	bye();
}


/*
 * status_error_slist:
 *    Display error message for status returned by daemon or driver, but
 *	  replace a "list is empty" code by an "ID not found" code, exit.
 *
 *    Parameters:
 *          n     Status code.
 *
 *    Does not return.
 */

void
status_error_slist(unsigned n)
{
	if (n == ISCSI_STATUS_LIST_EMPTY || n == ISCSID_STATUS_LIST_EMPTY)
		n = ISCSI_STATUS_INVALID_ID;
	status_error (n);
}


/*
 * get_response:
 *    Read the response from the daemon.
 *
 *    Parameters:
 *          temp  If TRUE, the response is dynamically allocated (so it is not
 *                overwritten by further requests or responses).
 *
 *    Returns:
 *          Pointer to the response.
 *
 *    Notes:
 *       This routine allocates an extra integer to mark whether the returned
 *       buffer is dynamic or static. This marker is one int before the
 *       returned pointer.
 */

iscsid_response_t *
get_response(int temp)
{
	ssize_t ret;
	size_t len;
	iscsid_response_t *rsp;
	int *pbuf;

	pbuf = (int *)(void *)buf;
	rsp = (iscsid_response_t *)(void *)&pbuf[1];
	*pbuf = 0;

	/* get size of response */
	len = sizeof(iscsid_response_t);
	ret = recv(sock, rsp, len, MSG_PEEK | MSG_WAITALL);
	if ((size_t)ret != len)
		io_error("Receiving daemon data");

	len += rsp->parameter_length;

	/*
	   if a temp buffer has been requested, or if the response is too large
	   to fit into the static buffer, alloc a temp buffer.
	 */

	temp = temp || (len > (int)(sizeof(buf) - sizeof(int)));

	if (temp) {
		if (NULL == (pbuf = (int *) malloc(len + sizeof(int))))
			gen_error("Can't allocate response buffer (%zu bytes)",
				len + sizeof(int));

		rsp = (iscsid_response_t *)(void *)&pbuf[1];
		*pbuf = 1;
	}
	/* get the complete response */

	ret = recv(sock, rsp, len, MSG_WAITALL);
	if ((size_t)ret != len)
		io_error("Receiving daemon data");

	return rsp;
}


/*
 * free_response:
 *    If the response buffer was dynamically allocated, free it.
 *
 *    Parameters:
 *          rsp   The response buffer.
 *                The dynamic allocation marker is the int preceding
 *                this address.
 */

void
free_response(iscsid_response_t * rsp)
{
	int *pbuf;

	pbuf = ((int *)(void *)rsp) - 1;
	if (*pbuf)
		free(pbuf);
}


/*
 * send_request:
 *    Send a request to the daemon.
 *
 *    Parameters:
 *          request  The request code.
 *          par_len  The parameter length.
 *          par      The parameter.
 */

void
send_request(unsigned request, size_t par_len, void *par)
{
	iscsid_request_t *req;
	size_t len;
	ssize_t ret;
	int req_temp;

	len = sizeof(iscsid_request_t) + par_len;

	/* alloc buffer if static one is too small to hold request */
	req_temp = len > sizeof(buf);

	if (req_temp) {
		req = malloc(len);
		if (req == NULL)
			gen_error("Out of memory allocating %zu bytes\n", len);
	} else
		req = (iscsid_request_t *)(void *)buf;

	/* setup request */
	req->request = request;
	req->parameter_length = (uint32_t)par_len;
	if (par_len)
		memcpy(req->parameter, par, par_len);

	/* and send it out */
	ret = sendto(sock, req, len, 0, (struct sockaddr *)(void *)&daemon_name,
				 (socklen_t)sizeof(struct sockaddr_un));
	if ((size_t)ret != len) {
		io_error("Sending daemon message");
	}
	if (req_temp)
		free(req);
}


/*
 * main:
 *    check command, init driver handle and socket, dispatch command.
 *
 *    Parameter:  argc, argv - passed on to commands with offset of 2, so
 *                argv [0] is first argument after command verb.
 *
 *    Returns:
 *          Whatever the command handler returns, which is currently always 0.
 */

int
main(int argc, char **argv)
{
	command_t	*c;
	int		 res;
	int		 i;

	(void) snprintf(sockdir, sizeof(sockdir), "/tmp/iscsictl.XXXXXX");
	while ((i = getopt(argc, argv, "d:")) != -1) {
		switch(i) {
		case 'd':
			(void) snprintf(sockdir, sizeof(sockdir), "%s", optarg);
			break;
		default:
			break;
		}
	}
	if (argc - optind  < 1) {
		errx(EXIT_FAILURE, "Usage: %s <command> <options>, see manual for details.",
			progname);
	}

	cmdname = argv[optind];

	for (c = cmds; c->cmd != NULL; c++) {
		if (strcmp(c->cmd, cmdname) == 0) {
			break;
		}
	}
	if (c->cmd == NULL) {
		errx(EXIT_FAILURE, "Unknown command: '%s'", cmdname);
	}
	if ((driver = open(DEVICE, O_RDONLY)) < 0)
		err(EXIT_FAILURE, "Opening " DEVICE);

	sock = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (sock < 0)
		err(EXIT_FAILURE, "opening datagram socket");

	/* bind socket to unique name */
	if (mkdtemp(sockdir) == NULL) {
		errx(EXIT_FAILURE, "can't create iscsictl dir '%s'", sockdir);
	}
	myname.sun_family = AF_UNIX;
	(void) snprintf(myname.sun_path, sizeof(myname.sun_path), "%s/socket", sockdir);
	if (bind(sock, (struct sockaddr *)(void *)&myname,
	    (socklen_t)sizeof(struct sockaddr_un)) < 0) {
		io_error("Binding name to datagram socket");
	}
	daemon_name.sun_family = AF_UNIX;
	strlcpy(daemon_name.sun_path, ISCSID_SOCK_NAME,
		sizeof(daemon_name.sun_path));

	/* dispatch command */
	res = (*c->proc)(argc - optind - 1, &argv[optind + 1]);

	/* cleanup */
	close(sock);
	unlink(myname.sun_path);
	rmdir(sockdir);

	return res;
}