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
26 * $FreeBSD: src/sys/dev/iscsi/initiator/isc_soc.c,v 1.6 2009/06/25 18:46:30 kib Exp $
30 | $Id: isc_soc.c,v 1.26 2007/05/19 06:09:01 danny Exp danny $
33 #include "opt_iscsi_initiator.h"
35 #include <sys/param.h>
36 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/ctype.h>
41 #include <sys/errno.h>
42 #include <sys/sysctl.h>
45 #include <sys/socketvar.h>
46 #include <sys/socket.h>
47 #include <sys/protosw.h>
49 #include <sys/ioccom.h>
50 #include <sys/queue.h>
51 #include <sys/kthread.h>
52 #include <sys/syslog.h>
56 #include <sys/eventhandler.h>
57 #include <sys/mutex.h>
58 #include <sys/mutex2.h>
59 #include <sys/socketops.h>
61 #include <bus/cam/cam.h>
62 #include <bus/cam/cam_ccb.h>
64 #include <dev/disk/iscsi/initiator/iscsi.h>
65 #include <dev/disk/iscsi/initiator/iscsivar.h>
73 static int ou_refcnt = 0;
76 | function for counting refs on external storage for mbuf
83 debug(3, "ou_refcnt=%d arg=%p b=%p", ou_refcnt, a, a->buf);
84 atomic_add_int(&a->refcnt, 1);
88 | function for freeing external storage for mbuf
95 if (atomic_fetchadd_int(&a->refcnt, -1) == 1)
97 debug(3, "ou_refcnt=%d a=%p b=%p", ou_refcnt, a, a->buf);
98 kfree(a->buf, M_ISCSI);
104 isc_sendPDU(isc_session_t *sp, pduq_t *pq)
106 struct mbuf *mh, **mp;
107 pdu_t *pp = &pq->pdu;
112 | mbuf for the iSCSI header
114 MGETHDR(mh, MB_TRYWAIT, MT_DATA);
115 mh->m_len = mh->m_pkthdr.len = sizeof(union ipdu_u);
116 mh->m_pkthdr.rcvif = NULL;
117 MH_ALIGN(mh, sizeof(union ipdu_u));
118 bcopy(&pp->ipdu, mh->m_data, sizeof(union ipdu_u));
122 pq->pdu.hdr_dig = sp->hdrDigest(&pp->ipdu, sizeof(union ipdu_u), 0);
125 | Add any AHS to the iSCSI hdr mbuf
126 | XXX Assert: (mh->m_pkthdr.len + pp->ahs_len) < MHLEN
128 bcopy(pp->ahs, (mh->m_data + mh->m_len), pp->ahs_len);
129 mh->m_len += pp->ahs_len;
130 mh->m_pkthdr.len += pp->ahs_len;
133 pq->pdu.hdr_dig = sp->hdrDigest(&pp->ahs, pp->ahs_len, pq->pdu.hdr_dig);
136 debug(2, "hdr_dig=%x", pq->pdu.hdr_dig);
138 | Add header digest to the iSCSI hdr mbuf
139 | XXX Assert: (mh->m_pkthdr.len + 4) < MHLEN
141 bcopy(&pp->hdr_dig, (mh->m_data + mh->m_len), sizeof(int));
142 mh->m_len += sizeof(int);
143 mh->m_pkthdr.len += sizeof(int);
151 while(len & 03) // the specs say it must be int alligned
156 MGET(md, MB_TRYWAIT, MT_DATA);
159 l = min(MCLBYTES, len);
160 debug(5, "setting ext_free(arg=%p len/l=%d/%d)", pq->buf, len, l);
161 md->m_ext.ext_buf = pq->buf;
162 md->m_ext.ext_free = ext_free;
163 md->m_ext.ext_ref = ext_ref;
164 md->m_ext.ext_arg = pq;
165 md->m_ext.ext_size = l;
166 md->m_flags |= M_EXT;
167 md->m_data = pp->ds + off;
170 mh->m_pkthdr.len += l;
180 pp->ds_dig = sp->dataDigest(pp->ds, pp->ds_len, 0);
182 MGET(me, MB_TRYWAIT, MT_DATA);
183 me->m_len = sizeof(int);
184 MH_ALIGN(mh, sizeof(int));
185 bcopy(&pp->ds_dig, me->m_data, sizeof(int));
187 mh->m_pkthdr.len += sizeof(int);
190 if((error = sosend(sp->soc, NULL, NULL, mh, 0, 0, sp->td)) != 0) {
191 sdebug(3, "error=%d", error);
195 getmicrouptime(&sp->stats.t_sent);
198 #else /* NO_USE_MBUF */
200 isc_sendPDU(isc_session_t *sp, pduq_t *pq)
202 struct uio *uio = &pq->uio;
204 pdu_t *pp = &pq->pdu;
209 bzero(uio, sizeof(struct uio));
210 uio->uio_rw = UIO_WRITE;
211 uio->uio_segflg = UIO_SYSSPACE;
212 uio->uio_td = sp->td;
213 uio->uio_iov = iv = pq->iov;
215 iv->iov_base = &pp->ipdu;
216 iv->iov_len = sizeof(union ipdu_u);
217 uio->uio_resid = pq->len;
220 pq->pdu.hdr_dig = sp->hdrDigest(&pp->ipdu, sizeof(union ipdu_u), 0);
222 iv->iov_base = pp->ahs;
223 iv->iov_len = pp->ahs_len;
227 pq->pdu.hdr_dig = sp->hdrDigest(&pp->ahs, pp->ahs_len, pq->pdu.hdr_dig);
230 debug(2, "hdr_dig=%x", pq->pdu.hdr_dig);
231 iv->iov_base = &pp->hdr_dig;
232 iv->iov_len = sizeof(int);
236 iv->iov_base = pp->ds;
237 iv->iov_len = pp->ds_len;
238 while(iv->iov_len & 03) // the specs say it must be int alligned
243 pp->ds_dig = sp->dataDigest(pp->ds, pp->ds_len, 0);
244 iv->iov_base = &pp->ds_dig;
245 iv->iov_len = sizeof(int);
248 uio->uio_iovcnt = iv - pq->iov;
249 sdebug(5, "opcode=%x iovcnt=%d uio_resid=%d itt=%x",
250 pp->ipdu.bhs.opcode, uio->uio_iovcnt, uio->uio_resid,
251 ntohl(pp->ipdu.bhs.itt));
252 sdebug(5, "sp=%p sp->soc=%p uio=%p sp->td=%p",
253 sp, sp->soc, uio, sp->td);
255 len = uio->uio_resid;
256 error = sosend(sp->soc, NULL, uio, 0, 0, 0, sp->td);
257 if(uio->uio_resid == 0 || error || len == uio->uio_resid) {
259 sdebug(2, "uio->uio_resid=%d uio->uio_iovcnt=%d error=%d len=%d",
260 uio->uio_resid, uio->uio_iovcnt, error, len);
262 error = EAGAIN; // 35
269 sdebug(1, "uio->uio_resid=%d uio->uio_iovcnt=%d",
270 uio->uio_resid, uio->uio_iovcnt);
272 len -= uio->uio_resid;
273 while(uio->uio_iovcnt > 0) {
274 if(iv->iov_len > len) {
275 caddr_t bp = (caddr_t)iv->iov_base;
278 iv->iov_base = (void *)&bp[len];
286 } while(uio->uio_resid);
290 getmicrouptime(&sp->stats.t_sent);
296 #endif /* USE_MBUF */
299 | wait till a PDU header is received
303 The format of the BHS is:
305 Byte/ 0 | 1 | 2 | 3 |
307 |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|
308 +---------------+---------------+---------------+---------------+
309 0|.|I| Opcode |F| Opcode-specific fields |
310 +---------------+---------------+---------------+---------------+
311 4|TotalAHSLength | DataSegmentLength |
312 +---------------+---------------+---------------+---------------+
313 8| LUN or Opcode-specific fields |
316 +---------------+---------------+---------------+---------------+
317 16| Initiator Task Tag |
318 +---------------+---------------+---------------+---------------+
319 20/ Opcode-specific fields /
321 +---------------+---------------+---------------+---------------+
325 so_getbhs(isc_session_t *sp)
327 bhs_t *bhs = &sp->bhs;
328 struct uio *uio = &sp->uio;
329 struct iovec *iov = &sp->iov;
335 iov->iov_len = sizeof(bhs_t);
339 uio->uio_rw = UIO_READ;
340 uio->uio_segflg = UIO_SYSSPACE;
341 uio->uio_td = curthread; // why ...
342 uio->uio_resid = sizeof(bhs_t);
345 error = so_pru_soreceive(sp->soc, NULL, uio, NULL, NULL, &flags);
348 debug(2, "error=%d so_error=%d uio->uio_resid=%zd iov.iov_len=%zd",
350 sp->soc->so_error, uio->uio_resid, iov->iov_len);
351 if(!error && (uio->uio_resid > 0)) {
352 error = EPIPE; // was EAGAIN
353 debug(2, "error=%d so_error=%d uio->uio_resid=%zd iov.iov_len=%zd "
356 sp->soc->so_error, uio->uio_resid, iov->iov_len,
364 | so_recv gets called when there is at least
365 | an iSCSI header in the queue
368 so_recv(isc_session_t *sp, pduq_t *pq)
370 struct socket *so = sp->soc;
372 struct uio *uio = &pq->uio;
382 | now calculate how much data should be in the buffer
383 | NOTE: digest is not verified/calculated - yet
391 pp->ahs_len = bhs->AHSLength * 4;
398 #if BYTE_ORDER == LITTLE_ENDIAN
399 pp->ds_len = ((n & 0x00ff0000) >> 16)
401 | ((n & 0x000000ff) << 16);
412 if((sp->opt.maxRecvDataSegmentLength > 0) && (len > sp->opt.maxRecvDataSegmentLength)) {
414 xdebug("impossible PDU length(%d) opt.maxRecvDataSegmentLength=%d",
415 len, sp->opt.maxRecvDataSegmentLength);
416 // deep trouble here, probably all we can do is
417 // force a disconnect, XXX: check RFC ...
419 "so_recv: impossible PDU length(%ld) from iSCSI %s/%s\n",
420 len, sp->opt.targetAddress, sp->opt.targetName);
423 | XXX: this will really screwup the stream.
424 | should clear up the buffer till a valid header
425 | is found, or just close connection ...
426 | should read the RFC.
438 uio->uio_td = curthread; // why ...
440 // it's more efficient to use mbufs -- why?
441 if(bhs->opcode == ISCSI_READ_DATA) {
444 opq = i_search_hld(sp, pq->pdu.ipdu.bhs.itt, 1);
446 union ccb *ccb = opq->ccb;
447 struct ccb_scsiio *csio = &ccb->csio;
448 pdu_t *opp = &opq->pdu;
449 scsi_req_t *cmd = &opp->ipdu.scsi_req;
450 data_in_t *rcmd = &pq->pdu.ipdu.data_in;
451 bhs_t *bhp = &opp->ipdu.bhs;
454 if(bhp->opcode == ISCSI_SCSI_CMD
456 && (ntohl(cmd->edtlen) >= pq->pdu.ds_len)) {
457 struct iovec *iov = pq->iov;
458 iov->iov_base = csio->data_ptr + ntohl(rcmd->bo);
459 iov->iov_len = pq->pdu.ds_len;
461 uio->uio_rw = UIO_READ;
462 uio->uio_segflg = UIO_SYSSPACE;
465 if(len > pq->pdu.ds_len) {
466 pq->iov[1].iov_base = &r;
467 pq->iov[1].iov_len = len - pq->pdu.ds_len;
472 sdebug(4, "uio_resid=0x%zx itt=0x%x bp=%p bo=%x len=%x/%x",
474 ntohl(pq->pdu.ipdu.bhs.itt),
475 csio->data_ptr, ntohl(rcmd->bo), ntohl(cmd->edtlen), pq->pdu.ds_len);
481 * Here we call so_pru_receive with a sockbuf so we can obtain
482 * the mbuf chain that can be assigned later to the pq->mp,
483 * which is the mbuf wanted.
484 * For the moment, resid will be saved in the uio.
487 error = so_pru_soreceive(so, NULL, NULL, &sbp, NULL, &flags);
489 uio->uio_resid = sbp.sb_climit - sbp.sb_cc;
490 //if(error == EAGAIN)
491 // XXX: this needs work! it hangs iscontrol
492 if(error || uio->uio_resid)
496 sdebug(6, "len=%d] opcode=0x%x ahs_len=0x%x ds_len=0x%x",
497 pq->len, bhs->opcode, pp->ahs_len, pp->ds_len);
499 max = ntohl(bhs->MaxCmdSN);
500 exp = ntohl(bhs->ExpStSN);
503 max > exp - _MAXINCR) {
504 sdebug(2, "bad cmd window size");
505 error = EIO; // XXX: for now;
509 if(SNA_GT(max, sn->maxCmd))
512 if(SNA_GT(exp, sn->expCmd))
515 sp->cws = sn->maxCmd - sn->expCmd + 1;
520 // XXX: need some work here
521 xdebug("have a problem, error=%d", error);
522 pdu_free(sp->isc, pq);
523 if(!error && uio->uio_resid > 0)
529 | wait for something to arrive.
530 | and if the pdu is without errors, process it.
533 so_input(isc_session_t *sp)
540 | first read in the iSCSI header
542 error = so_getbhs(sp);
547 pq = pdu_alloc(sp->isc, M_NOWAIT);
548 if(pq == NULL) { // XXX: might cause a deadlock ...
549 debug(3, "out of pdus, wait");
550 pq = pdu_alloc(sp->isc, M_NOWAIT); // OK to WAIT
552 pq->pdu.ipdu.bhs = sp->bhs;
553 pq->len = sizeof(bhs_t); // so far only the header was read
554 error = so_recv(sp, pq);
556 error += 0x800; // XXX: just to see the error.
558 // XXX: close connection and exit
562 getmicrouptime(&sp->stats.t_recv);
570 | one per active (connected) session.
571 | this thread is responsible for reading
572 | in packets from the target.
577 isc_session_t *sp = (isc_session_t *)vp;
578 struct socket *so = sp->soc;
587 while((sp->flags & (ISC_CON_RUN | ISC_LINK_UP)) == (ISC_CON_RUN | ISC_LINK_UP)) {
589 if(sp->soc == NULL || !(so->so_state & SS_ISCONNECTED)) {
590 debug(2, "sp->soc=%p", sp->soc);
593 error = so_input(sp);
595 iscsi_lock_ex(&sp->io_mtx);
596 if(sp->flags & ISC_OWAITING) {
599 iscsi_unlock_ex(&sp->io_mtx);
600 } else if(error == EPIPE) {
603 else if(error == EAGAIN) {
604 if(so->so_state & SS_ISCONNECTED)
605 // there seems to be a problem in 6.0 ...
606 tsleep(sp, 0, "iscsoc", 2*hz);
609 sdebug(2, "terminated, flags=%x so_state=%x error=%d proc=%p",
610 sp->flags, so->so_state, error, sp->proc);
611 if((sp->proc != NULL) && sp->signal) {
613 ksignal(sp->proc, sp->signal);
614 PROC_UNLOCK(sp->proc);
615 sp->flags |= ISC_SIGNALED;
616 sdebug(2, "pid=%d signaled(%d)", sp->proc->p_pid, sp->signal);
619 // we have to do something ourselves
620 // like closing this session ...
623 | we've been terminated
625 // do we need this mutex ...?
626 //iscsi_lock_ex(&sp->io_mtx);
627 sp->flags &= ~(ISC_CON_RUNNING | ISC_LINK_UP);
629 //iscsi_unlock_ex(&sp->io_mtx);
631 sdebug(2, "dropped ISC_CON_RUNNING");
637 isc_stop_receiver(isc_session_t *sp)
640 sdebug(3, "sp=%p sp->soc=%p", sp, sp? sp->soc: 0);
641 iscsi_lock_ex(&sp->io_mtx);
642 sp->flags &= ~ISC_LINK_UP;
643 if (sp->flags & ISC_CON_RUNNING) {
644 issleep(&sp->soc, &sp->io_mtx, 0, "iscstpc", 5*hz);
646 iscsi_unlock_ex(&sp->io_mtx);
649 soshutdown(sp->soc, SHUT_RD);
651 iscsi_lock_ex(&sp->io_mtx);
652 sdebug(3, "soshutdown");
653 sp->flags &= ~ISC_CON_RUN;
654 while(sp->flags & ISC_CON_RUNNING) {
655 sdebug(3, "waiting flags=%x", sp->flags);
656 issleep(&sp->soc, &sp->io_mtx, 0, "iscstpc", hz);
658 iscsi_unlock_ex(&sp->io_mtx);
660 if (sp->fp != NULL) {
664 /* sofree(sp->soc); fp deals with socket termination */
671 isc_start_receiver(isc_session_t *sp)
675 sp->flags |= ISC_CON_RUN | ISC_LINK_UP;
676 sp->flags |= ISC_CON_RUNNING;
678 kthread_create(isc_soc, sp, &sp->soc_thr, "iscsi%d", sp->sid);