summaryrefslogtreecommitdiff
path: root/usr/iscsistart.c
blob: 546840ff8c6a07d97f7b8f30686f5cb5e22d5179 (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
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
580
581
/*
 * iSCSI Root Boot Program based on daemon code
 *
 * Copyright (C) 2004 Dmitry Yusupov, Alex Aizman
 * Copyright (C) 2006 Mike Christie
 * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
 * maintained by open-iscsi@googlegroups.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * See the file COPYING included with this distribution for more details.
 */
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "initiator.h"
#include "iscsi_ipc.h"
#include "event_poll.h"
#include "transport.h"
#include "log.h"
#include "iscsi_util.h"
#include "idbm.h"
#include "idbm_fields.h"
#include "version.h"
#include "iscsi_sysfs.h"
#include "iscsi_settings.h"
#include "fw_context.h"
#include "iface.h"
#include "sysdeps.h"
#include "iscsid_req.h"
#include "iscsi_err.h"
#include "iface.h"

/* global config info */
/* initiator needs initiator name/alias */
struct iscsi_daemon_config daemon_config;
struct iscsi_daemon_config *dconfig = &daemon_config;

static node_rec_t config_rec;
static LIST_HEAD(targets);
static LIST_HEAD(user_params);

static char program_name[] = "iscsistart";
static char config_file[TARGET_NAME_MAXLEN];

/* used by initiator */
extern struct iscsi_ipc *ipc;

static struct option const long_options[] = {
	{"config", required_argument, NULL, 'c'},
	{"initiatorname", required_argument, NULL, 'i'},
	{"targetname", required_argument, NULL, 't'},
	{"tgpt", required_argument, NULL, 'g'},
	{"address", required_argument, NULL, 'a'},
	{"port", required_argument, NULL, 'p'},
	{"username", required_argument, NULL, 'u'},
	{"password", required_argument, NULL, 'w'},
	{"username_in", required_argument, NULL, 'U'},
	{"password_in", required_argument, NULL, 'W'},
	{"debug", required_argument, NULL, 'd'},
	{"fwparam_connect", no_argument, NULL, 'b'},
	{"fwparam_network", no_argument, NULL, 'N'},
	{"fwparam_print", no_argument, NULL, 'f'},
	{"param", required_argument, NULL, 'P'},
	{"help", no_argument, NULL, 'h'},
	{"version", no_argument, NULL, 'v'},
	{NULL, 0, NULL, 0},
};

static void usage(int status)
{
	if (status != 0)
		fprintf(stderr, "Try `%s --help' for more information.\n",
			program_name);
	else {
		printf("Usage: %s [OPTION]\n", program_name);
		printf("\
Open-iSCSI initiator.\n\
  -c, --config=[path]      set config file (default " CONFIG_FILE ").\n\
  -i, --initiatorname=name set InitiatorName to name (Required)\n\
  -t, --targetname=name    set TargetName to name (Required)\n\
  -g, --tgpt=N             set target portal group tag to N (Required)\n\
  -a, --address=A.B.C.D    set IP address to A.B.C.D (Required)\n\
  -p, --port=N             set port to N (Default 3260)\n\
  -u, --username=N         set username to N (optional)\n\
  -w, --password=N         set password to N (optional\n\
  -U, --username_in=N      set incoming username to N (optional)\n\
  -W, --password_in=N      set incoming password to N (optional)\n\
  -d, --debug=debuglevel   print debugging information \n\
  -b, --fwparam_connect    create a session to the target using iBFT or OF\n\
  -N, --fwparam_network    bring up the network as specified by iBFT or OF\n\
  -f, --fwparam_print      print the iBFT or OF info to STDOUT \n\
  -P, --param=NAME=VALUE   set parameter with the name NAME to VALUE\n\
  -h, --help               display this help and exit\n\
  -v, --version            display version and exit\n\
");
	}
	exit(status);
}

static int stop_event_loop(void)
{
	iscsiadm_req_t req;
	iscsiadm_rsp_t rsp;
	int rc;

	memset(&req, 0, sizeof(req));
	req.command = MGMT_IPC_IMMEDIATE_STOP;
	rc = iscsid_exec_req(&req, &rsp, 0, -1);
	if (rc) {
		iscsi_err_print_msg(rc);
		log_error("Could not stop event_loop");
	}
	return rc;
}

static int apply_params(struct node_rec *rec)
{
	struct user_param *param;
	int rc;

	/* Must init this so we can check if user overrode them */
	rec->session.initial_login_retry_max = -1;
	rec->conn[0].timeo.noop_out_interval = -1;
	rec->conn[0].timeo.noop_out_timeout = -1;
	rec->session.scan = -1;

	list_for_each_entry(param, &user_params, list) {
		/*
		 * user may not have passed in all params that were set by
		 * ibft/iscsi_boot, so clear out values that might conflict
		 * with user overrides
		 */
		if (!strcmp(param->name, IFACE_NETNAME)) {
			/* overriding netname so MAC will be for old netdev */
			memset(rec->iface.hwaddress, 0,
				sizeof(rec->iface.hwaddress));
		} else if (!strcmp(param->name, IFACE_HWADDR)) {
			/* overriding MAC so netdev will be for old MAC */
			memset(rec->iface.netdev, 0, sizeof(rec->iface.netdev));
		} else if (!strcmp(param->name, IFACE_TRANSPORTNAME)) {
			/*
			 * switching drivers so all old binding info is no
			 * longer valid. Old values were either for offload
			 * and we are switching to software or the reverse,
			 * or switching types of cards (bnx2i to cxgb3i).
			 */
			memset(&rec->iface, 0, sizeof(rec->iface));
			iface_setup_defaults(&rec->iface);
		}
	}

	rc = idbm_node_set_rec_from_param(&user_params, rec, 0);
	if (rc)
		return rc;

	/*
	 * For root boot we could not change this in older versions so
	 * if user did not override then use the defaults.
	 *
	 * Increase to account for boot using static setup.
	 */
	if (rec->session.initial_login_retry_max == -1)
		rec->session.initial_login_retry_max = 30;
	/* we used to not be able to answer so turn off */
	if (rec->conn[0].timeo.noop_out_interval == -1)
		rec->conn[0].timeo.noop_out_interval = 0;
	if (rec->conn[0].timeo.noop_out_timeout == -1)
		rec->conn[0].timeo.noop_out_timeout = 0;
	if (rec->session.scan == -1)
		rec->session.scan = DEF_INITIAL_SCAN;

	return 0;
}

static int parse_param(char *param_str)
{
	struct user_param *param;
	char *name, *value;

	name = param_str;

	value = strchr(param_str, '=');
	if (!value) {
		log_error("Invalid --param %s. Missing value.", param_str);
		return ISCSI_ERR_INVAL;
	}
	*value = '\0';

	value++;
	if (!strlen(value)) {
		log_error("Invalid --param %s. Missing value.", param_str);
		return ISCSI_ERR_INVAL;
	}

	param = idbm_alloc_user_param(name, value);
	if (!param) {
		log_error("Could not allocate memory for param.");
		return ISCSI_ERR_NOMEM;
	}

	list_add(&param->list, &user_params);
	return 0;
}

static int login_session(struct node_rec *rec)
{
	iscsiadm_req_t req;
	iscsiadm_rsp_t rsp;
	int rc, msec, err;
	struct timespec ts;

	rc = apply_params(rec);
	if (rc)
		return rc;

	printf("%s: Logging into %s %s:%d,%d\n", program_name, rec->name,
		rec->conn[0].address, rec->conn[0].port,
		rec->tpgt);
	memset(&req, 0, sizeof(req));
	req.command = MGMT_IPC_SESSION_LOGIN;
	memcpy(&req.u.session.rec, rec, sizeof(*rec));

	/*
	 * Need to handle race where iscsid proc is starting up while we are
	 * trying to connect. Retry with exponential backoff, start from 50 ms.
	 */
	for (msec = 50; msec <= 15000; msec <<= 1) {
		/*
		 * Once our event loop is up then we want to wait for the login
		 * response. Either it logs in, we hit the login retries count,
		 * or this program crashes, so there no need for the response
		 * timeout.
		 */
		rc = iscsid_exec_req(&req, &rsp, 0, -1);
		if (rc == 0) {
			return rc;
		} else if (rc == ISCSI_ERR_ISCSID_NOTCONN) {
			ts.tv_sec = msec / 1000;
			ts.tv_nsec = (msec % 1000) * 1000000L;

			/* On EINTR, retry nanosleep with remaining time. */
			while ((err = nanosleep(&ts, &ts)) < 0 &&
			       errno == EINTR);
			if (err < 0)
				break;
		} else {
			break;
		}
	}

	iscsi_err_print_msg(rc);
	return rc;
}

static char *get_config_file(void)
{
	return config_file;
}

static int setup_session(void)
{
	struct boot_context *context;
	int rc = 0, rc2 = 0;

	if (list_empty(&targets))
		return login_session(&config_rec);

	increase_max_files();
	if (idbm_init(get_config_file)) {
		log_warning("exiting due to idbm configuration error");
		rc = ISCSI_ERR_IDBM;
		goto out;
	}

	list_for_each_entry(context, &targets, list) {
		struct node_rec *rec;

		rec = idbm_create_rec_from_boot_context(context);
		if (!rec) {
			log_error("Could not allocate memory. Could "
				  "not start boot session to "
				  "%s,%s,%d", context->targetname,
				  context->target_ipaddr,
				  context->target_port);
			continue;
		}

		rc2 = login_session(rec);
		if (rc2)
			rc = rc2;
		free(rec);
	}
	fw_free_targets(&targets);
out:
	return rc;
}

int session_in_use(__attribute__((unused))int sid)
{
	return 0;
}

static void catch_signal(int signo)
{
	log_warning("pid %d caught signal -%d", getpid(), signo);
}

static int check_params(char *initiatorname)
{
	if (!initiatorname) {
		log_error("InitiatorName not set. Exiting %s", program_name);
		return EINVAL;
	}

	if (config_rec.tpgt == PORTAL_GROUP_TAG_UNKNOWN) {
		log_error("Portal Group not set. Exiting %s", program_name);
		return EINVAL;
	}

	if (!strlen(config_rec.name)) {
		log_error("TargetName not set. Exiting %s", program_name);
		return EINVAL;
	}

	if (!strlen(config_rec.conn[0].address)) {
		log_error("IP Address not set. Exiting %s", program_name);
		return EINVAL;
	}

	return 0;
}

#define check_str_param_len(str, max_len, param)			\
do {									\
	if (strlen(str) > max_len) {					\
		printf("%s: invalid %s %s. Max %s length is %d.\n",	\
			program_name, param, str, param, max_len);	\
		exit(ISCSI_ERR_INVAL);					\
	}								\
} while (0);

int main(int argc, char *argv[])
{
	struct utsname host_info; /* will use to compound initiator alias */
	struct iscsi_auth_config *auth;
	char *initiatorname = NULL;
	int ch, longindex, ret;
	struct boot_context *context, boot_context;
	struct sigaction sa_old;
	struct sigaction sa_new;
	struct user_param *param, *tmp_param;
	int control_fd, mgmt_ipc_fd, err;
	pid_t pid;

	strcpy(config_file, CONFIG_FILE);
	idbm_node_setup_defaults(&config_rec);
	config_rec.name[0] = '\0';
	config_rec.conn[0].address[0] = '\0';
	auth = &config_rec.session.auth;

	/* do not allow ctrl-c for now... */
	sa_new.sa_handler = catch_signal;
	sigemptyset(&sa_new.sa_mask);
	sa_new.sa_flags = 0;
	sigaction(SIGINT, &sa_new, &sa_old );

	/* initialize logger */
	log_init(program_name, DEFAULT_AREA_SIZE, log_do_log_std, NULL);

	sysfs_init();

	while ((ch = getopt_long(argc, argv, "c:P:i:t:g:a:p:d:u:w:U:W:bNfvh",
				 long_options, &longindex)) >= 0) {
		switch (ch) {
		case 'c':
			strncpy(config_file, optarg, TARGET_NAME_MAXLEN);
			config_file[TARGET_NAME_MAXLEN-1] = 0;
			break;
		case 'i':
			initiatorname = optarg;
			break;
		case 't':
			check_str_param_len(optarg, TARGET_NAME_MAXLEN,
					    "targetname");
			strlcpy(config_rec.name, optarg, TARGET_NAME_MAXLEN);
			break;
		case 'g':
			config_rec.tpgt = atoi(optarg);
			break;
		case 'a':
			check_str_param_len(optarg, NI_MAXHOST, "address");
			strlcpy(config_rec.conn[0].address, optarg, NI_MAXHOST);
			break;
		case 'p':
			config_rec.conn[0].port = atoi(optarg);
			break;
		case 'w':
			check_str_param_len(optarg, AUTH_STR_MAX_LEN,
					   "password");
			strlcpy((char *)auth->password, optarg,
				AUTH_STR_MAX_LEN);
			auth->password_length = strlen((char *)auth->password);
			break;
		case 'W':
			check_str_param_len(optarg, AUTH_STR_MAX_LEN,
					   "password_in");
			strlcpy((char *)auth->password_in, optarg,
				AUTH_STR_MAX_LEN);
			auth->password_in_length =
				strlen((char *)auth->password_in);
			break;
		case 'u':
			check_str_param_len(optarg, AUTH_STR_MAX_LEN,
					    "username");
			strlcpy(auth->username, optarg, AUTH_STR_MAX_LEN);
			break;
		case 'U':
			check_str_param_len(optarg, AUTH_STR_MAX_LEN,
					    "username_in");
			strlcpy(auth->username_in, optarg, AUTH_STR_MAX_LEN);
			break;
		case 'd':
			log_level = atoi(optarg);
			break;
		case 'b':
			memset(&boot_context, 0, sizeof(boot_context));
			ret = fw_get_entry(&boot_context);
			if (ret) {
				printf("Could not get boot entry.\n");
				exit(ret);
			}

			initiatorname = boot_context.initiatorname;
			ret = fw_get_targets(&targets);
			if (ret || list_empty(&targets)) {
				printf("Could not setup fw entries.\n");
				exit(ret);
			}
			break;
		case 'N':
			exit(fw_setup_nics());
		case 'f':
			ret = fw_get_targets(&targets);
			if (ret || list_empty(&targets)) {
				printf("Could not get list of targets from "
				       "firmware.\n");
				exit(ret);
			}

			list_for_each_entry(context, &targets, list)
				fw_print_entry(context);

			fw_free_targets(&targets);
			exit(0);
		case 'P':
			err = parse_param(optarg);
			if (err)
				exit(err);
			break;
		case 'v':
			printf("%s version %s\n", program_name,
				ISCSI_VERSION_STR);
			exit(0);
		case 'h':
			usage(0);
			break;
		default:
			usage(ISCSI_ERR_INVAL);
			break;
		}
	}

	if (list_empty(&targets) && check_params(initiatorname))
		exit(ISCSI_ERR_INVAL);

	pid = fork();
	if (pid < 0) {
		log_error("iscsiboot fork failed");
		exit(ISCSI_ERR_NOMEM);
	} else if (pid) {
		int status, rc, rc2;

		/* make a special socket path for only this iscsistart instance */
		iscsid_set_namespace(pid);
		sleep(1);

		rc = setup_session();
		rc2 = stop_event_loop();
		/*
		 * something horrible happened. kill child and get
		 * out of here
		 */
		if (rc2)
			kill(pid, SIGTERM);

		waitpid(pid, &status, WUNTRACED);
		if (rc || rc2)
			exit(ISCSI_ERR);

		log_debug(1, "iscsi parent done");
		exit(0);
	}

	pid = getpid();
	iscsid_set_namespace(pid);

	mgmt_ipc_fd = mgmt_ipc_listen();
	if (mgmt_ipc_fd  < 0) {
		log_error("Could not setup mgmt ipc");
		exit(ISCSI_ERR_NOMEM);
	}

	control_fd = ipc->ctldev_open();
	if (control_fd < 0)
		exit(ISCSI_ERR_NOMEM);

	memset(&daemon_config, 0, sizeof (daemon_config));
	daemon_config.initiator_name = initiatorname;
	/* optional InitiatorAlias */
	memset(&host_info, 0, sizeof (host_info));
	if (uname(&host_info) >= 0)
		daemon_config.initiator_alias = host_info.nodename;

	log_debug(1, "InitiatorName=%s", daemon_config.initiator_name);
	log_debug(1, "InitiatorAlias=%s", daemon_config.initiator_alias);
	log_debug(1, "TargetName=%s", config_rec.name);
	log_debug(1, "TPGT=%d", config_rec.tpgt);
	log_debug(1, "IP Address=%s", config_rec.conn[0].address);

	ipc->auth_type = ISCSI_IPC_AUTH_UID;

	/* log the version, so that we can tell if the daemon and kernel module
	 * match based on what shows up in the syslog.  Tarballs releases
	 * always install both, but Linux distributors may put the kernel module
	 * in a different RPM from the daemon and utils, and users may try to
	 * mix and match in ways that don't work.
	 */
	log_error("version %s", ISCSI_VERSION_STR);

	/* oom-killer will not kill us at the night... */
	if (oom_adjust())
		log_debug(1, "can not adjust oom-killer's pardon");

	/*
	 * Start Main Event Loop
	 */
	iscsi_initiator_init();
	event_loop(ipc, control_fd, mgmt_ipc_fd);
	ipc->ctldev_close();
	mgmt_ipc_close(mgmt_ipc_fd);
	free_initiator();
	sysfs_cleanup();
	list_for_each_entry_safe(param, tmp_param, &user_params, list) {
		list_del(&param->list);
		idbm_free_user_param(param);
	}

	log_debug(1, "iscsi child done");
	return 0;
}