2 * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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
28 | $Id: iscontrol.c,v 2.2 2006/12/01 09:11:56 danny Exp danny $
31 | the user level initiator (client)
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
45 #include <sys/ioctl.h>
58 #include <dev/iscsi_initiator/iscsi.h>
59 #include "iscontrol.h"
61 static char version[] = "2.3.1"; // keep in sync with iscsi_initiator
63 #define USAGE "[-v] [-d] [-c config] [-n name] [-t target] [-p pidfile]"
64 #define OPTIONS "vdc:t:n:p:"
66 token_t AuthMethods[] = {
76 token_t DigestMethods[] = {
90 .iqn = "iqn.2005-01.il.ac.huji.cs:",
92 .sessionType = "Normal",
97 .headerDigest = "None,CRC32C",
98 .dataDigest = "None,CRC32C",
100 .maxRecvDataSegmentLength = 64 * 1024,
101 .maxXmitDataSegmentLength = 8 * 1024, // 64 * 1024,
102 .maxBurstLength = 128 * 1024,
103 .firstBurstLength = 64 * 1024, // must be less than maxBurstLength
104 .defaultTime2Wait = 0,
105 .defaultTime2Retain = 0,
106 .maxOutstandingR2T = 1,
107 .errorRecoveryLevel = 0,
109 .dataPDUInOrder = TRUE,
110 .dataSequenceInOrder = TRUE,
113 .immediateData = TRUE,
117 usage(const char *pname)
119 fprintf(stderr, "usage: %s " USAGE "\n", pname);
124 lookup(token_t *tbl, char *m)
128 for(tp = tbl; tp->name != NULL; tp++)
129 if(strcasecmp(tp->name, m) == 0)
135 main(int cc, char **vv)
138 char *pname, *pidfile, *p, *q, *ta, *kw, *v;
144 iscsidev = "/dev/"ISCSIDEV;
147 if ((pname = basename(pname)) == NULL)
154 | check for driver & controller version match
157 #define VERSION_OID_S "net.iscsi_initiator.driver_version"
158 if (sysctlbyname(VERSION_OID_S, 0, &n, 0, 0) != 0) {
160 errx(1, "sysctlbyname(\"" VERSION_OID_S "\") "
161 "failed; is the iscsi driver loaded?");
162 err(1, "sysctlbyname(\"" VERSION_OID_S "\")");
167 if (sysctlbyname(VERSION_OID_S, v, &n, 0, 0) != 0)
168 err(1, "sysctlbyname");
170 if (strncmp(version, v, 3) != 0)
171 errx(1, "versions mismatch");
173 while((ch = getopt(cc, vv, OPTIONS)) != -1) {
179 fd = fopen(optarg, "r");
181 err(1, "fopen(\"%s\")", optarg);
200 fd = fopen("/etc/iscsi.conf", "r");
203 parseConfig(fd, kw, op);
210 printf("adding '%s'\n", *vv);
211 parseArgs(cc, vv, op);
214 op->targetAddress = ta;
216 if(op->targetAddress == NULL) {
217 warnx("no target specified!");
220 q = op->targetAddress;
221 if(*q == '[' && (q = strchr(q, ']')) != NULL) {
225 q = op->targetAddress;
226 if((p = strchr(q, ':')) != NULL) {
231 if(p || ((p = strchr(q, ',')) != NULL)) {
233 op->targetPortalGroupTag = atoi(p);
235 if(op->initiatorName == 0) {
236 char hostname[MAXHOSTNAMELEN];
239 if(gethostname(hostname, sizeof(hostname)) == 0)
240 asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname);
242 asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX:
245 if(gethostname(hostname, sizeof(hostname)) == 0)
246 asprintf(&op->initiatorName, "%s", hostname);
248 asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX:
252 op->sessionType = "Discovery";
255 op->pidfile = pidfile;