]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/iscsi/initiator/isc_subr.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / iscsi / initiator / isc_subr.c
1 /*-
2  * Copyright (c) 2005-2008 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,v 1.20 2006/12/01 09:10:17 danny Exp 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 static char *
62 i_strdupin(char *s, size_t maxlen)
63 {
64      size_t     len;
65      char       *p, *q;
66
67      p = malloc(maxlen, M_ISCSI, M_WAITOK);
68      if(copyinstr(s, p, maxlen, &len)) {
69           free(p, M_ISCSI);
70           return NULL;
71      }
72      q = malloc(len, M_ISCSI, M_WAITOK);
73      bcopy(p, q, len);
74      free(p, M_ISCSI);
75
76      return q;
77 }
78
79 static uint32_t
80 i_crc32c(const void *buf, size_t size, uint32_t crc)
81 {
82      crc = crc ^ 0xffffffff;
83      crc = calculate_crc32c(crc, buf, size);
84      crc = crc ^ 0xffffffff;
85      return crc;
86 }
87
88 /*
89  | XXX: not finished coding
90  */
91 int
92 i_setopt(isc_session_t *sp, isc_opt_t *opt)
93 {
94      if(opt->maxRecvDataSegmentLength > 0) {
95           sp->opt.maxRecvDataSegmentLength = opt->maxRecvDataSegmentLength;
96           sdebug(2, "maxRecvDataSegmentLength=%d", sp->opt.maxRecvDataSegmentLength);
97      }
98      if(opt->maxXmitDataSegmentLength > 0) {
99           // danny's RFC
100           sp->opt.maxXmitDataSegmentLength = opt->maxXmitDataSegmentLength;
101           sdebug(2, "maXmitDataSegmentLength=%d", sp->opt.maxXmitDataSegmentLength);
102      }
103      if(opt->maxBurstLength != 0) {
104           sp->opt.maxBurstLength = opt->maxBurstLength;
105           sdebug(2, "maxBurstLength=%d", sp->opt.maxBurstLength);
106      }
107
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);
113      }
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);
119      }
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);
125      }
126
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);
132      }
133
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");
139           }
140      }
141      if(opt->dataDigest != NULL) {
142           if(strcmp(opt->dataDigest, "CRC32C") == 0) {
143                sp->dataDigest = (digest_t *)i_crc32c;
144                sdebug(2, "dataDigest set");
145           }
146      }
147
148      return 0;
149 }
150
151 void
152 i_freeopt(isc_opt_t *opt)
153 {
154      if(opt->targetAddress != NULL) {
155           free(opt->targetAddress, M_ISCSI);
156           opt->targetAddress = NULL;
157      }
158      if(opt->targetName != NULL) {
159           free(opt->targetName, M_ISCSI);
160           opt->targetName = NULL;
161      }
162      if(opt->initiatorName != NULL) {
163           free(opt->initiatorName, M_ISCSI);
164           opt->initiatorName = NULL;
165      }
166 }