2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | $Id: iscontrol.c,v 2.2 2006/12/01 09:11:56 danny Exp danny $
33 | the user level initiator (client)
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
44 #include <netinet/in.h>
45 #include <netinet/tcp.h>
46 #include <arpa/inet.h>
47 #include <sys/ioctl.h>
60 #include <dev/iscsi_initiator/iscsi.h>
61 #include "iscontrol.h"
63 static char version[] = "2.3.1"; // keep in sync with iscsi_initiator
65 #define USAGE "[-v] [-d] [-c config] [-n name] [-t target] [-p pidfile]"
66 #define OPTIONS "vdc:t:n:p:"
68 token_t AuthMethods[] = {
78 token_t DigestMethods[] = {
92 .iqn = "iqn.2005-01.il.ac.huji.cs:",
94 .sessionType = "Normal",
99 .headerDigest = "None,CRC32C",
100 .dataDigest = "None,CRC32C",
102 .maxRecvDataSegmentLength = 64 * 1024,
103 .maxXmitDataSegmentLength = 8 * 1024, // 64 * 1024,
104 .maxBurstLength = 128 * 1024,
105 .firstBurstLength = 64 * 1024, // must be less than maxBurstLength
106 .defaultTime2Wait = 0,
107 .defaultTime2Retain = 0,
108 .maxOutstandingR2T = 1,
109 .errorRecoveryLevel = 0,
111 .dataPDUInOrder = TRUE,
112 .dataSequenceInOrder = TRUE,
115 .immediateData = TRUE,
119 usage(const char *pname)
121 fprintf(stderr, "usage: %s " USAGE "\n", pname);
126 lookup(token_t *tbl, char *m)
130 for(tp = tbl; tp->name != NULL; tp++)
131 if(strcasecmp(tp->name, m) == 0)
137 main(int cc, char **vv)
140 char *pname, *pidfile, *p, *q, *ta, *kw, *v;
146 iscsidev = "/dev/"ISCSIDEV;
149 if ((pname = basename(pname)) == NULL)
156 | check for driver & controller version match
159 #define VERSION_OID_S "net.iscsi_initiator.driver_version"
160 if (sysctlbyname(VERSION_OID_S, 0, &n, 0, 0) != 0) {
162 errx(1, "sysctlbyname(\"" VERSION_OID_S "\") "
163 "failed; is the iscsi driver loaded?");
164 err(1, "sysctlbyname(\"" VERSION_OID_S "\")");
169 if (sysctlbyname(VERSION_OID_S, v, &n, 0, 0) != 0)
170 err(1, "sysctlbyname");
172 if (strncmp(version, v, 3) != 0)
173 errx(1, "versions mismatch");
175 while((ch = getopt(cc, vv, OPTIONS)) != -1) {
181 fd = fopen(optarg, "r");
183 err(1, "fopen(\"%s\")", optarg);
202 fd = fopen("/etc/iscsi.conf", "r");
205 parseConfig(fd, kw, op);
212 printf("adding '%s'\n", *vv);
213 parseArgs(cc, vv, op);
216 op->targetAddress = ta;
218 if(op->targetAddress == NULL) {
219 warnx("no target specified!");
222 q = op->targetAddress;
223 if(*q == '[' && (q = strchr(q, ']')) != NULL) {
227 q = op->targetAddress;
228 if((p = strchr(q, ':')) != NULL) {
233 if(p || ((p = strchr(q, ',')) != NULL)) {
235 op->targetPortalGroupTag = atoi(p);
237 if(op->initiatorName == 0) {
238 char hostname[MAXHOSTNAMELEN];
241 if(gethostname(hostname, sizeof(hostname)) == 0)
242 asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname);
244 asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX:
247 if(gethostname(hostname, sizeof(hostname)) == 0)
248 asprintf(&op->initiatorName, "%s", hostname);
250 asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX:
254 op->sessionType = "Discovery";
257 op->pidfile = pidfile;