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
29 | $Id: isc_subr.c,v 1.20 2006/12/01 09:10:17 danny Exp danny $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_iscsi_initiator.h"
37 #include <sys/param.h>
38 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/ctype.h>
43 #include <sys/errno.h>
44 #include <sys/sysctl.h>
47 #include <sys/socketvar.h>
48 #include <sys/socket.h>
49 #include <sys/protosw.h>
51 #include <sys/ioccom.h>
52 #include <sys/queue.h>
53 #include <sys/kthread.h>
54 #include <sys/syslog.h>
56 #include <sys/libkern.h>
58 #include <dev/iscsi/initiator/iscsi.h>
59 #include <dev/iscsi/initiator/iscsivar.h>
62 i_strdupin(char *s, size_t maxlen)
67 p = malloc(maxlen, M_ISCSI, M_WAITOK);
68 if(copyinstr(s, p, maxlen, &len)) {
72 q = malloc(len, M_ISCSI, M_WAITOK);
80 i_crc32c(const void *buf, size_t size, uint32_t crc)
82 crc = crc ^ 0xffffffff;
83 crc = calculate_crc32c(crc, buf, size);
84 crc = crc ^ 0xffffffff;
89 | XXX: not finished coding
92 i_setopt(isc_session_t *sp, isc_opt_t *opt)
94 if(opt->maxRecvDataSegmentLength > 0) {
95 sp->opt.maxRecvDataSegmentLength = opt->maxRecvDataSegmentLength;
96 sdebug(2, "maxRecvDataSegmentLength=%d", sp->opt.maxRecvDataSegmentLength);
98 if(opt->maxXmitDataSegmentLength > 0) {
100 sp->opt.maxXmitDataSegmentLength = opt->maxXmitDataSegmentLength;
101 sdebug(2, "maXmitDataSegmentLength=%d", sp->opt.maxXmitDataSegmentLength);
103 if(opt->maxBurstLength != 0) {
104 sp->opt.maxBurstLength = opt->maxBurstLength;
105 sdebug(2, "maxBurstLength=%d", sp->opt.maxBurstLength);
108 if(opt->targetAddress != NULL) {
109 if(sp->opt.targetAddress != NULL)
110 free(sp->opt.targetAddress, M_ISCSI);
111 sp->opt.targetAddress = i_strdupin(opt->targetAddress, 128);
112 sdebug(4, "opt.targetAddress='%s'", sp->opt.targetAddress);
114 if(opt->targetName != NULL) {
115 if(sp->opt.targetName != NULL)
116 free(sp->opt.targetName, M_ISCSI);
117 sp->opt.targetName = i_strdupin(opt->targetName, 128);
118 sdebug(4, "opt.targetName='%s'", sp->opt.targetName);
120 if(opt->initiatorName != NULL) {
121 if(sp->opt.initiatorName != NULL)
122 free(sp->opt.initiatorName, M_ISCSI);
123 sp->opt.initiatorName = i_strdupin(opt->initiatorName, 128);
124 sdebug(4, "opt.initiatorName='%s'", sp->opt.initiatorName);
127 if(opt->maxluns > 0) {
128 if(opt->maxluns > ISCSI_MAX_LUNS)
129 sp->opt.maxluns = ISCSI_MAX_LUNS; // silently chop it down ...
130 sp->opt.maxluns = opt->maxluns;
131 sdebug(4, "opt.maxluns=%d", sp->opt.maxluns);
134 if(opt->headerDigest != NULL) {
135 sdebug(2, "opt.headerDigest='%s'", opt->headerDigest);
136 if(strcmp(opt->headerDigest, "CRC32C") == 0) {
137 sp->hdrDigest = (digest_t *)i_crc32c;
138 sdebug(2, "headerDigest set");
141 if(opt->dataDigest != NULL) {
142 if(strcmp(opt->dataDigest, "CRC32C") == 0) {
143 sp->dataDigest = (digest_t *)i_crc32c;
144 sdebug(2, "dataDigest set");
152 i_freeopt(isc_opt_t *opt)
154 if(opt->targetAddress != NULL) {
155 free(opt->targetAddress, M_ISCSI);
156 opt->targetAddress = NULL;
158 if(opt->targetName != NULL) {
159 free(opt->targetName, M_ISCSI);
160 opt->targetName = NULL;
162 if(opt->initiatorName != NULL) {
163 free(opt->initiatorName, M_ISCSI);
164 opt->initiatorName = NULL;