isc_subr.c revision 227293
1171568Sscottl/*-
2211095Sdes * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
3171568Sscottl * All rights reserved.
4171568Sscottl *
5171568Sscottl * Redistribution and use in source and binary forms, with or without
6171568Sscottl * modification, are permitted provided that the following conditions
7171568Sscottl * are met:
8171568Sscottl * 1. Redistributions of source code must retain the above copyright
9171568Sscottl *    notice, this list of conditions and the following disclaimer.
10171568Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11171568Sscottl *    notice, this list of conditions and the following disclaimer in the
12171568Sscottl *    documentation and/or other materials provided with the distribution.
13171568Sscottl *
14171568Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15171568Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16171568Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17171568Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18171568Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19171568Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20171568Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21171568Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22171568Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23171568Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24171568Sscottl * SUCH DAMAGE.
25171568Sscottl *
26171568Sscottl */
27171568Sscottl/*
28171568Sscottl | iSCSI
29211095Sdes | $Id: isc_subr.c 560 2009-05-07 07:37:49Z danny $
30171568Sscottl */
31171568Sscottl
32171568Sscottl#include <sys/cdefs.h>
33171568Sscottl__FBSDID("$FreeBSD: head/sys/dev/iscsi/initiator/isc_subr.c 227293 2011-11-07 06:44:47Z ed $");
34171568Sscottl
35171568Sscottl#include "opt_iscsi_initiator.h"
36171568Sscottl
37171568Sscottl#include <sys/param.h>
38171568Sscottl#include <sys/kernel.h>
39171568Sscottl#include <sys/conf.h>
40171568Sscottl#include <sys/systm.h>
41171568Sscottl#include <sys/malloc.h>
42171568Sscottl#include <sys/ctype.h>
43171568Sscottl#include <sys/errno.h>
44171568Sscottl#include <sys/sysctl.h>
45171568Sscottl#include <sys/file.h>
46171568Sscottl#include <sys/uio.h>
47171568Sscottl#include <sys/socketvar.h>
48171568Sscottl#include <sys/socket.h>
49171568Sscottl#include <sys/protosw.h>
50171568Sscottl#include <sys/proc.h>
51171568Sscottl#include <sys/ioccom.h>
52171568Sscottl#include <sys/queue.h>
53171568Sscottl#include <sys/kthread.h>
54171568Sscottl#include <sys/syslog.h>
55171568Sscottl#include <sys/mbuf.h>
56171568Sscottl#include <sys/libkern.h>
57171568Sscottl
58171568Sscottl#include <dev/iscsi/initiator/iscsi.h>
59171568Sscottl#include <dev/iscsi/initiator/iscsivar.h>
60171568Sscottl
61227293Sedstatic MALLOC_DEFINE(M_ISC, "iSC", "iSCSI driver options");
62211095Sdes
63171568Sscottlstatic char *
64171568Sscottli_strdupin(char *s, size_t maxlen)
65171568Sscottl{
66171568Sscottl     size_t	len;
67171568Sscottl     char	*p, *q;
68171568Sscottl
69211095Sdes     p = malloc(maxlen, M_ISC, M_WAITOK);
70171568Sscottl     if(copyinstr(s, p, maxlen, &len)) {
71211095Sdes	  free(p, M_ISC);
72171568Sscottl	  return NULL;
73171568Sscottl     }
74211095Sdes     q = malloc(len, M_ISC, M_WAITOK);
75171568Sscottl     bcopy(p, q, len);
76211095Sdes     free(p, M_ISC);
77171568Sscottl
78171568Sscottl     return q;
79171568Sscottl}
80171568Sscottl
81171568Sscottlstatic uint32_t
82171568Sscottli_crc32c(const void *buf, size_t size, uint32_t crc)
83171568Sscottl{
84171568Sscottl     crc = crc ^ 0xffffffff;
85188605Srrs     crc = calculate_crc32c(crc, buf, size);
86171568Sscottl     crc = crc ^ 0xffffffff;
87171568Sscottl     return crc;
88171568Sscottl}
89171568Sscottl
90171568Sscottl/*
91171568Sscottl | XXX: not finished coding
92171568Sscottl */
93171568Sscottlint
94171568Sscottli_setopt(isc_session_t *sp, isc_opt_t *opt)
95171568Sscottl{
96171568Sscottl     if(opt->maxRecvDataSegmentLength > 0) {
97171568Sscottl	  sp->opt.maxRecvDataSegmentLength = opt->maxRecvDataSegmentLength;
98171568Sscottl	  sdebug(2, "maxRecvDataSegmentLength=%d", sp->opt.maxRecvDataSegmentLength);
99171568Sscottl     }
100171568Sscottl     if(opt->maxXmitDataSegmentLength > 0) {
101171568Sscottl	  // danny's RFC
102171568Sscottl	  sp->opt.maxXmitDataSegmentLength = opt->maxXmitDataSegmentLength;
103211095Sdes	  sdebug(2, "opt.maXmitDataSegmentLength=%d", sp->opt.maxXmitDataSegmentLength);
104171568Sscottl     }
105171568Sscottl     if(opt->maxBurstLength != 0) {
106171568Sscottl	  sp->opt.maxBurstLength = opt->maxBurstLength;
107211095Sdes	  sdebug(2, "opt.maxBurstLength=%d", sp->opt.maxBurstLength);
108171568Sscottl     }
109171568Sscottl
110171568Sscottl     if(opt->targetAddress != NULL) {
111171568Sscottl	  if(sp->opt.targetAddress != NULL)
112211095Sdes	       free(sp->opt.targetAddress, M_ISC);
113171568Sscottl	  sp->opt.targetAddress = i_strdupin(opt->targetAddress, 128);
114211095Sdes	  sdebug(2, "opt.targetAddress='%s'", sp->opt.targetAddress);
115171568Sscottl     }
116171568Sscottl     if(opt->targetName != NULL) {
117171568Sscottl	  if(sp->opt.targetName != NULL)
118211095Sdes	       free(sp->opt.targetName, M_ISC);
119171568Sscottl	  sp->opt.targetName = i_strdupin(opt->targetName, 128);
120211095Sdes	  sdebug(2, "opt.targetName='%s'", sp->opt.targetName);
121171568Sscottl     }
122171568Sscottl     if(opt->initiatorName != NULL) {
123171568Sscottl	  if(sp->opt.initiatorName != NULL)
124211095Sdes	       free(sp->opt.initiatorName, M_ISC);
125171568Sscottl	  sp->opt.initiatorName = i_strdupin(opt->initiatorName, 128);
126211095Sdes	  sdebug(2, "opt.initiatorName='%s'", sp->opt.initiatorName);
127171568Sscottl     }
128171568Sscottl
129171568Sscottl     if(opt->maxluns > 0) {
130171568Sscottl	  if(opt->maxluns > ISCSI_MAX_LUNS)
131171568Sscottl	       sp->opt.maxluns = ISCSI_MAX_LUNS; // silently chop it down ...
132171568Sscottl	  sp->opt.maxluns = opt->maxluns;
133211095Sdes	  sdebug(2, "opt.maxluns=%d", sp->opt.maxluns);
134171568Sscottl     }
135171568Sscottl
136171568Sscottl     if(opt->headerDigest != NULL) {
137171568Sscottl	  sdebug(2, "opt.headerDigest='%s'", opt->headerDigest);
138171568Sscottl	  if(strcmp(opt->headerDigest, "CRC32C") == 0) {
139171568Sscottl	       sp->hdrDigest = (digest_t *)i_crc32c;
140211095Sdes	       sdebug(2, "opt.headerDigest set");
141171568Sscottl	  }
142171568Sscottl     }
143171568Sscottl     if(opt->dataDigest != NULL) {
144211095Sdes	  sdebug(2, "opt.dataDigest='%s'", opt->headerDigest);
145171568Sscottl	  if(strcmp(opt->dataDigest, "CRC32C") == 0) {
146171568Sscottl	       sp->dataDigest = (digest_t *)i_crc32c;
147211095Sdes	       sdebug(2, "opt.dataDigest set");
148171568Sscottl	  }
149171568Sscottl     }
150171568Sscottl
151171568Sscottl     return 0;
152171568Sscottl}
153171568Sscottl
154171568Sscottlvoid
155171568Sscottli_freeopt(isc_opt_t *opt)
156171568Sscottl{
157211095Sdes     debug_called(8);
158211095Sdes
159171568Sscottl     if(opt->targetAddress != NULL) {
160211095Sdes	  free(opt->targetAddress, M_ISC);
161171568Sscottl	  opt->targetAddress = NULL;
162171568Sscottl     }
163171568Sscottl     if(opt->targetName != NULL) {
164211095Sdes	  free(opt->targetName, M_ISC);
165171568Sscottl	  opt->targetName = NULL;
166171568Sscottl     }
167171568Sscottl     if(opt->initiatorName != NULL) {
168211095Sdes	  free(opt->initiatorName, M_ISC);
169171568Sscottl	  opt->initiatorName = NULL;
170171568Sscottl     }
171171568Sscottl}
172