2 * Copyright (c) 2005-2008 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/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 #include <sys/module.h>
39 #include <sys/linker.h>
41 #include <netinet/in.h>
42 #include <netinet/tcp.h>
43 #include <arpa/inet.h>
44 #include <sys/ioctl.h>
56 #include "iscontrol.h"
58 #define USAGE "[-dv] [-c file] [-n nickname] [-t target] [variable=value ...]"
59 #define OPTIONS "vdc:t:n:"
65 token_t AuthMethods[] = {
75 token_t DigestMethods[] = {
89 .iqn = "iqn.2005-01.il.ac.huji.cs:",
91 .sessionType = "Normal",
96 .headerDigest = "None,CRC32C",
97 .dataDigest = "None,CRC32C",
99 .maxRecvDataSegmentLength = 64 * 1024,
100 .maxXmitDataSegmentLength = 8 * 1024, // 64 * 1024,
101 .maxBurstLength = 128 * 1024,
102 .firstBurstLength = 64 * 1024, // must be less than maxBurstLength
103 .defaultTime2Wait = 0,
104 .defaultTime2Retain = 0,
105 .maxOutstandingR2T = 1,
106 .errorRecoveryLevel = 0,
108 .dataPDUInOrder = TRUE,
109 .dataSequenceInOrder = TRUE,
112 .immediateData = TRUE,
116 lookup(token_t *tbl, char *m)
120 for(tp = tbl; tp->name != NULL; tp++)
121 if(strcasecmp(tp->name, m) == 0)
127 main(int cc, char **vv)
130 char *pname, *p, *q, *ta, *kw;
134 /* Try to load iscsi_initiator module before starting its operation */
135 if (modfind(INITIATORMOD) < 0) {
136 if (kldload(INITIATORMOD) < 0 || modfind(INITIATORMOD) < 0) {
137 perror(INITIATORMOD ": Error while handling kernel module");
143 iscsidev = "/dev/"ISCSIDEV;
146 if((p = strrchr(pname, '/')) != NULL)
152 while((ch = getopt(cc, vv, OPTIONS)) != -1) {
158 fd = fopen(optarg, "r");
175 fprintf(stderr, "Usage: %s %s\n", pname, USAGE);
180 fd = fopen("/etc/iscsi.conf", "r");
183 parseConfig(fd, kw, op);
190 printf("adding '%s'\n", *vv);
191 parseArgs(cc, vv, op);
194 op->targetAddress = ta;
196 if(op->targetAddress == NULL) {
197 fprintf(stderr, "No target!\n");
200 q = op->targetAddress;
201 if(*q == '[' && (q = strchr(q, ']')) != NULL) {
205 q = op->targetAddress;
206 if((p = strchr(q, ':')) != NULL) {
211 if(p || ((p = strchr(q, ',')) != NULL)) {
213 op->targetPortalGroupTag = atoi(p);
215 if(op->initiatorName == 0) {
219 if(gethostname(hostname, sizeof(hostname)) == 0)
220 asprintf(&op->initiatorName, "%s:%s", op->iqn, hostname);
222 asprintf(&op->initiatorName, "%s:%d", op->iqn, (int)time(0) & 0xff); // XXX:
225 if(gethostname(hostname, sizeof(hostname)) == 0)
226 asprintf(&op->initiatorName, "%s", hostname);
228 asprintf(&op->initiatorName, "%d", (int)time(0) & 0xff); // XXX:
232 op->sessionType = "Discovery";