]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/iscsi/initiator/isc_subr.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / iscsi / initiator / isc_subr.c
1 /*-
2  * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27 /*
28  | iSCSI
29  | $Id: isc_subr.c 560 2009-05-07 07:37:49Z danny $
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_iscsi_initiator.h"
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/conf.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>
45 #include <sys/file.h>
46 #include <sys/uio.h>
47 #include <sys/socketvar.h>
48 #include <sys/socket.h>
49 #include <sys/protosw.h>
50 #include <sys/proc.h>
51 #include <sys/ioccom.h>
52 #include <sys/queue.h>
53 #include <sys/kthread.h>
54 #include <sys/syslog.h>
55 #include <sys/mbuf.h>
56 #include <sys/libkern.h>
57
58 #include <dev/iscsi/initiator/iscsi.h>
59 #include <dev/iscsi/initiator/iscsivar.h>
60
61 MALLOC_DEFINE(M_ISC, "iSC", "iSCSI driver options");
62
63 static char *
64 i_strdupin(char *s, size_t maxlen)
65 {
66      size_t     len;
67      char       *p, *q;
68
69      p = malloc(maxlen, M_ISC, M_WAITOK);
70      if(copyinstr(s, p, maxlen, &len)) {
71           free(p, M_ISC);
72           return NULL;
73      }
74      q = malloc(len, M_ISC, M_WAITOK);
75      bcopy(p, q, len);
76      free(p, M_ISC);
77
78      return q;
79 }
80
81 static uint32_t
82 i_crc32c(const void *buf, size_t size, uint32_t crc)
83 {
84      crc = crc ^ 0xffffffff;
85      crc = calculate_crc32c(crc, buf, size);
86      crc = crc ^ 0xffffffff;
87      return crc;
88 }
89
90 /*
91  | XXX: not finished coding
92  */
93 int
94 i_setopt(isc_session_t *sp, isc_opt_t *opt)
95 {
96      if(opt->maxRecvDataSegmentLength > 0) {
97           sp->opt.maxRecvDataSegmentLength = opt->maxRecvDataSegmentLength;
98           sdebug(2, "maxRecvDataSegmentLength=%d", sp->opt.maxRecvDataSegmentLength);
99      }
100      if(opt->maxXmitDataSegmentLength > 0) {
101           // danny's RFC
102           sp->opt.maxXmitDataSegmentLength = opt->maxXmitDataSegmentLength;
103           sdebug(2, "opt.maXmitDataSegmentLength=%d", sp->opt.maxXmitDataSegmentLength);
104      }
105      if(opt->maxBurstLength != 0) {
106           sp->opt.maxBurstLength = opt->maxBurstLength;
107           sdebug(2, "opt.maxBurstLength=%d", sp->opt.maxBurstLength);
108      }
109
110      if(opt->targetAddress != NULL) {
111           if(sp->opt.targetAddress != NULL)
112                free(sp->opt.targetAddress, M_ISC);
113           sp->opt.targetAddress = i_strdupin(opt->targetAddress, 128);
114           sdebug(2, "opt.targetAddress='%s'", sp->opt.targetAddress);
115      }
116      if(opt->targetName != NULL) {
117           if(sp->opt.targetName != NULL)
118                free(sp->opt.targetName, M_ISC);
119           sp->opt.targetName = i_strdupin(opt->targetName, 128);
120           sdebug(2, "opt.targetName='%s'", sp->opt.targetName);
121      }
122      if(opt->initiatorName != NULL) {
123           if(sp->opt.initiatorName != NULL)
124                free(sp->opt.initiatorName, M_ISC);
125           sp->opt.initiatorName = i_strdupin(opt->initiatorName, 128);
126           sdebug(2, "opt.initiatorName='%s'", sp->opt.initiatorName);
127      }
128
129      if(opt->maxluns > 0) {
130           if(opt->maxluns > ISCSI_MAX_LUNS)
131                sp->opt.maxluns = ISCSI_MAX_LUNS; // silently chop it down ...
132           sp->opt.maxluns = opt->maxluns;
133           sdebug(2, "opt.maxluns=%d", sp->opt.maxluns);
134      }
135
136      if(opt->headerDigest != NULL) {
137           sdebug(2, "opt.headerDigest='%s'", opt->headerDigest);
138           if(strcmp(opt->headerDigest, "CRC32C") == 0) {
139                sp->hdrDigest = (digest_t *)i_crc32c;
140                sdebug(2, "opt.headerDigest set");
141           }
142      }
143      if(opt->dataDigest != NULL) {
144           sdebug(2, "opt.dataDigest='%s'", opt->headerDigest);
145           if(strcmp(opt->dataDigest, "CRC32C") == 0) {
146                sp->dataDigest = (digest_t *)i_crc32c;
147                sdebug(2, "opt.dataDigest set");
148           }
149      }
150
151      return 0;
152 }
153
154 void
155 i_freeopt(isc_opt_t *opt)
156 {
157      debug_called(8);
158
159      if(opt->targetAddress != NULL) {
160           free(opt->targetAddress, M_ISC);
161           opt->targetAddress = NULL;
162      }
163      if(opt->targetName != NULL) {
164           free(opt->targetName, M_ISC);
165           opt->targetName = NULL;
166      }
167      if(opt->initiatorName != NULL) {
168           free(opt->initiatorName, M_ISC);
169           opt->initiatorName = NULL;
170      }
171 }