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_soc.c,v 1.26 2007/05/19 06:09:01 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>
59 #include <cam/cam_ccb.h>
61 #include <dev/iscsi/initiator/iscsi.h>
62 #include <dev/iscsi/initiator/iscsivar.h>
70 static int ou_refcnt = 0;
73 | function for freeing external storage for mbuf
76 ext_free(void *a, void *b)
81 debug(3, "ou_refcnt=%d a=%p b=%p", ou_refcnt, a, pq->buf);
82 free(pq->buf, M_ISCSI);
88 isc_sendPDU(isc_session_t *sp, pduq_t *pq)
90 struct mbuf *mh, **mp;
96 | mbuf for the iSCSI header
98 MGETHDR(mh, M_TRYWAIT, MT_DATA);
99 mh->m_len = mh->m_pkthdr.len = sizeof(union ipdu_u);
100 mh->m_pkthdr.rcvif = NULL;
101 MH_ALIGN(mh, sizeof(union ipdu_u));
102 bcopy(&pp->ipdu, mh->m_data, sizeof(union ipdu_u));
106 pq->pdu.hdr_dig = sp->hdrDigest(&pp->ipdu, sizeof(union ipdu_u), 0);
109 | Add any AHS to the iSCSI hdr mbuf
110 | XXX Assert: (mh->m_pkthdr.len + pp->ahs_len) < MHLEN
112 bcopy(pp->ahs, (mh->m_data + mh->m_len), pp->ahs_len);
113 mh->m_len += pp->ahs_len;
114 mh->m_pkthdr.len += pp->ahs_len;
117 pq->pdu.hdr_dig = sp->hdrDigest(&pp->ahs, pp->ahs_len, pq->pdu.hdr_dig);
120 debug(2, "hdr_dig=%x", pq->pdu.hdr_dig);
122 | Add header digest to the iSCSI hdr mbuf
123 | XXX Assert: (mh->m_pkthdr.len + 4) < MHLEN
125 bcopy(&pp->hdr_dig, (mh->m_data + mh->m_len), sizeof(int));
126 mh->m_len += sizeof(int);
127 mh->m_pkthdr.len += sizeof(int);
135 while(len & 03) // the specs say it must be int alligned
140 MGET(md, M_TRYWAIT, MT_DATA);
141 md->m_ext.ref_cnt = &ou_refcnt;
142 l = min(MCLBYTES, len);
143 debug(5, "setting ext_free(arg=%p len/l=%d/%d)", pq->buf, len, l);
144 MEXTADD(md, pp->ds + off, l, ext_free, pp->ds + off, pq, 0, EXT_EXTREF);
147 mh->m_pkthdr.len += l;
157 pp->ds_dig = sp->dataDigest(pp->ds, pp->ds_len, 0);
159 MGET(me, M_TRYWAIT, MT_DATA);
160 me->m_len = sizeof(int);
161 MH_ALIGN(mh, sizeof(int));
162 bcopy(&pp->ds_dig, me->m_data, sizeof(int));
164 mh->m_pkthdr.len += sizeof(int);
167 if((error = sosend(sp->soc, NULL, NULL, mh, 0, 0, sp->td)) != 0) {
168 sdebug(3, "error=%d", error);
172 getbintime(&sp->stats.t_sent);
175 #else /* NO_USE_MBUF */
177 isc_sendPDU(isc_session_t *sp, pduq_t *pq)
179 struct uio *uio = &pq->uio;
181 pdu_t *pp = &pq->pdu;
186 bzero(uio, sizeof(struct uio));
187 uio->uio_rw = UIO_WRITE;
188 uio->uio_segflg = UIO_SYSSPACE;
189 uio->uio_td = sp->td;
190 uio->uio_iov = iv = pq->iov;
192 iv->iov_base = &pp->ipdu;
193 iv->iov_len = sizeof(union ipdu_u);
194 uio->uio_resid = pq->len;
197 pq->pdu.hdr_dig = sp->hdrDigest(&pp->ipdu, sizeof(union ipdu_u), 0);
199 iv->iov_base = pp->ahs;
200 iv->iov_len = pp->ahs_len;
204 pq->pdu.hdr_dig = sp->hdrDigest(&pp->ahs, pp->ahs_len, pq->pdu.hdr_dig);
207 debug(2, "hdr_dig=%x", pq->pdu.hdr_dig);
208 iv->iov_base = &pp->hdr_dig;
209 iv->iov_len = sizeof(int);
213 iv->iov_base = pp->ds;
214 iv->iov_len = pp->ds_len;
215 while(iv->iov_len & 03) // the specs say it must be int alligned
220 pp->ds_dig = sp->dataDigest(pp->ds, pp->ds_len, 0);
221 iv->iov_base = &pp->ds_dig;
222 iv->iov_len = sizeof(int);
225 uio->uio_iovcnt = iv - pq->iov;
226 sdebug(5, "opcode=%x iovcnt=%d uio_resid=%d itt=%x",
227 pp->ipdu.bhs.opcode, uio->uio_iovcnt, uio->uio_resid,
228 ntohl(pp->ipdu.bhs.itt));
229 sdebug(5, "sp=%p sp->soc=%p uio=%p sp->td=%p",
230 sp, sp->soc, uio, sp->td);
232 len = uio->uio_resid;
233 error = sosend(sp->soc, NULL, uio, 0, 0, 0, sp->td);
234 if(uio->uio_resid == 0 || error || len == uio->uio_resid) {
236 sdebug(2, "uio->uio_resid=%d uio->uio_iovcnt=%d error=%d len=%d",
237 uio->uio_resid, uio->uio_iovcnt, error, len);
239 error = EAGAIN; // 35
246 sdebug(1, "uio->uio_resid=%d uio->uio_iovcnt=%d",
247 uio->uio_resid, uio->uio_iovcnt);
249 len -= uio->uio_resid;
250 while(uio->uio_iovcnt > 0) {
251 if(iv->iov_len > len) {
252 caddr_t bp = (caddr_t)iv->iov_base;
255 iv->iov_base = (void *)&bp[len];
263 } while(uio->uio_resid);
267 getbintime(&sp->stats.t_sent);
273 #endif /* USE_MBUF */
276 | wait till a PDU header is received
280 The format of the BHS is:
282 Byte/ 0 | 1 | 2 | 3 |
284 |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
285 +---------------+---------------+---------------+---------------+
286 0|.|I| Opcode |F| Opcode-specific fields |
287 +---------------+---------------+---------------+---------------+
288 4|TotalAHSLength | DataSegmentLength |
289 +---------------+---------------+---------------+---------------+
290 8| LUN or Opcode-specific fields |
293 +---------------+---------------+---------------+---------------+
294 16| Initiator Task Tag |
295 +---------------+---------------+---------------+---------------+
296 20/ Opcode-specific fields /
298 +---------------+---------------+---------------+---------------+
302 so_getbhs(isc_session_t *sp)
304 bhs_t *bhs = &sp->bhs;
305 struct uio *uio = &sp->uio;
306 struct iovec *iov = &sp->iov;
312 iov->iov_len = sizeof(bhs_t);
316 uio->uio_rw = UIO_READ;
317 uio->uio_segflg = UIO_SYSSPACE;
318 uio->uio_td = curthread; // why ...
319 uio->uio_resid = sizeof(bhs_t);
322 error = soreceive(sp->soc, NULL, uio, 0, 0, &flags);
325 debug(2, "error=%d so_error=%d uio->uio_resid=%zd iov.iov_len=%zd",
327 sp->soc->so_error, uio->uio_resid, iov->iov_len);
328 if(!error && (uio->uio_resid > 0)) {
329 error = EPIPE; // was EAGAIN
330 debug(2, "error=%d so_error=%d uio->uio_resid=%zd iov.iov_len=%zd so_state=%x",
332 sp->soc->so_error, uio->uio_resid, iov->iov_len, sp->soc->so_state);
339 | so_recv gets called when there is at least
340 | an iSCSI header in the queue
343 so_recv(isc_session_t *sp, pduq_t *pq)
345 struct socket *so = sp->soc;
347 struct uio *uio = &pq->uio;
356 | now calculate how much data should be in the buffer
357 | NOTE: digest is not verified/calculated - yet
364 pp->ahs_len = bhs->AHSLength * 4;
371 #if BYTE_ORDER == LITTLE_ENDIAN
372 pp->ds_len = ((n & 0x00ff0000) >> 16)
374 | ((n & 0x000000ff) << 16);
385 if((sp->opt.maxRecvDataSegmentLength > 0) && (len > sp->opt.maxRecvDataSegmentLength)) {
387 xdebug("impossible PDU length(%d) opt.maxRecvDataSegmentLength=%d",
388 len, sp->opt.maxRecvDataSegmentLength);
389 // deep trouble here, probably all we can do is
390 // force a disconnect, XXX: check RFC ...
392 "so_recv: impossible PDU length(%ld) from iSCSI %s/%s\n",
393 len, sp->opt.targetAddress, sp->opt.targetName);
396 | XXX: this will really screwup the stream.
397 | should clear up the buffer till a valid header
398 | is found, or just close connection ...
399 | should read the RFC.
405 int flags = MSG_WAITALL;
410 uio->uio_resid = len;
411 uio->uio_td = curthread; // why ...
413 // it's more efficient to use mbufs -- why?
414 if(bhs->opcode == ISCSI_READ_DATA) {
417 opq = i_search_hld(sp, pq->pdu.ipdu.bhs.itt, 1);
419 union ccb *ccb = opq->ccb;
420 struct ccb_scsiio *csio = &ccb->csio;
421 pdu_t *opp = &opq->pdu;
422 scsi_req_t *cmd = &opp->ipdu.scsi_req;
423 data_in_t *rcmd = &pq->pdu.ipdu.data_in;
424 bhs_t *bhp = &opp->ipdu.bhs;
427 if(bhp->opcode == ISCSI_SCSI_CMD
429 && (ntohl(cmd->edtlen) >= pq->pdu.ds_len)) {
430 struct iovec *iov = pq->iov;
431 iov->iov_base = csio->data_ptr + ntohl(rcmd->bo);
432 iov->iov_len = pq->pdu.ds_len;
434 uio->uio_rw = UIO_READ;
435 uio->uio_segflg = UIO_SYSSPACE;
438 if(len > pq->pdu.ds_len) {
439 pq->iov[1].iov_base = &r;
440 pq->iov[1].iov_len = len - pq->pdu.ds_len;
445 sdebug(4, "uio_resid=0x%zx itt=0x%x bp=%p bo=%x len=%x/%x",
447 ntohl(pq->pdu.ipdu.bhs.itt),
448 csio->data_ptr, ntohl(rcmd->bo), ntohl(cmd->edtlen), pq->pdu.ds_len);
453 error = soreceive(so, NULL, uio, mp, NULL, &flags);
454 //if(error == EAGAIN)
455 // XXX: this needs work! it hangs iscontrol
456 if(error || uio->uio_resid)
460 sdebug(6, "len=%d] opcode=0x%x ahs_len=0x%x ds_len=0x%x",
461 pq->len, bhs->opcode, pp->ahs_len, pp->ds_len);
463 max = ntohl(bhs->MaxCmdSN);
464 exp = ntohl(bhs->ExpStSN);
467 max > exp - _MAXINCR) {
468 sdebug(2, "bad cmd window size");
469 error = EIO; // XXX: for now;
473 if(SNA_GT(max, sn->maxCmd))
476 if(SNA_GT(exp, sn->expCmd))
479 sp->cws = sn->maxCmd - sn->expCmd + 1;
484 // XXX: need some work here
485 xdebug("have a problem, error=%d", error);
486 pdu_free(sp->isc, pq);
487 if(!error && uio->uio_resid > 0)
493 | wait for something to arrive.
494 | and if the pdu is without errors, process it.
497 so_input(isc_session_t *sp)
504 | first read in the iSCSI header
506 error = so_getbhs(sp);
511 pq = pdu_alloc(sp->isc, M_NOWAIT);
512 if(pq == NULL) { // XXX: might cause a deadlock ...
513 debug(3, "out of pdus, wait");
514 pq = pdu_alloc(sp->isc, M_NOWAIT); // OK to WAIT
516 pq->pdu.ipdu.bhs = sp->bhs;
517 pq->len = sizeof(bhs_t); // so far only the header was read
518 error = so_recv(sp, pq);
520 error += 0x800; // XXX: just to see the error.
522 // XXX: close connection and exit
526 getbintime(&sp->stats.t_recv);
534 | one per active (connected) session.
535 | this thread is responsible for reading
536 | in packets from the target.
541 isc_session_t *sp = (isc_session_t *)vp;
542 struct socket *so = sp->soc;
547 sp->flags |= ISC_CON_RUNNING;
552 while((sp->flags & (ISC_CON_RUN | ISC_LINK_UP)) == (ISC_CON_RUN | ISC_LINK_UP)) {
554 if(sp->soc == NULL || !(so->so_state & SS_ISCONNECTED)) {
555 debug(2, "sp->soc=%p", sp->soc);
558 error = so_input(sp);
560 mtx_lock(&sp->io_mtx);
561 if(sp->flags & ISC_OWAITING) {
564 mtx_unlock(&sp->io_mtx);
565 } else if(error == EPIPE) {
568 else if(error == EAGAIN) {
569 if(so->so_state & SS_ISCONNECTED)
570 // there seems to be a problem in 6.0 ...
571 tsleep(sp, PRIBIO, "isc_soc", 2*hz);
574 sdebug(2, "terminated, flags=%x so_count=%d so_state=%x error=%d proc=%p",
575 sp->flags, so->so_count, so->so_state, error, sp->proc);
576 if((sp->proc != NULL) && sp->signal) {
578 psignal(sp->proc, sp->signal);
579 PROC_UNLOCK(sp->proc);
580 sp->flags |= ISC_SIGNALED;
581 sdebug(2, "pid=%d signaled(%d)", sp->proc->p_pid, sp->signal);
584 // we have to do something ourselves
585 // like closing this session ...
588 | we've been terminated
590 // do we need this mutex ...?
591 mtx_lock(&sp->io_mtx);
592 sp->flags &= ~(ISC_CON_RUNNING | ISC_LINK_UP);
594 mtx_unlock(&sp->io_mtx);
596 sdebug(2, "dropped ISC_CON_RUNNING");
602 isc_stop_receiver(isc_session_t *sp)
607 sdebug(3, "sp=%p sp->soc=%p", sp, sp? sp->soc: 0);
608 mtx_lock(&sp->io_mtx);
609 sp->flags &= ~ISC_LINK_UP;
610 msleep(&sp->soc, &sp->io_mtx, PRIBIO|PDROP, "isc_stpc", 5*hz);
612 soshutdown(sp->soc, SHUT_RD);
614 mtx_lock(&sp->io_mtx);
615 sdebug(3, "soshutdown");
616 sp->flags &= ~ISC_CON_RUN;
618 while(n-- && (sp->flags & ISC_CON_RUNNING)) {
619 sdebug(3, "waiting n=%d... flags=%x", n, sp->flags);
620 msleep(&sp->soc, &sp->io_mtx, PRIBIO, "isc_stpc", 5*hz);
622 mtx_unlock(&sp->io_mtx);
626 fdrop(sp->fp, sp->td);
635 isc_start_receiver(isc_session_t *sp)
639 sp->flags |= ISC_CON_RUN | ISC_LINK_UP;
641 kproc_create(isc_soc, sp, &sp->soc_proc, 0, 0, "iscsi%d", sp->sid);