2 * Copyright (c) 2005-2010 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
28 | $Id: isc_soc.c 998 2009-12-20 10:32:45Z danny $
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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>
57 #include <cam/cam_ccb.h>
59 #include <dev/iscsi/initiator/iscsi.h>
60 #include <dev/iscsi/initiator/iscsivar.h>
67 static int ou_refcnt = 0;
69 | function for freeing external storage for mbuf
72 ext_free(void *a, void *b)
77 debug(3, "ou_refcnt=%d a=%p b=%p", ou_refcnt, a, pq->buf);
78 free(pq->buf, M_ISCSIBUF);
84 isc_sendPDU(isc_session_t *sp, pduq_t *pq)
86 struct mbuf *mh, **mp;
92 | mbuf for the iSCSI header
94 MGETHDR(mh, M_TRYWAIT, MT_DATA);
95 mh->m_pkthdr.rcvif = NULL;
97 mh->m_len = sizeof(union ipdu_u);
99 if(ISOK2DIG(sp->hdrDigest, pp)) {
100 pp->hdr_dig = sp->hdrDigest(&pp->ipdu, sizeof(union ipdu_u), 0);
101 mh->m_len += sizeof(pp->hdr_dig);
103 debug(2, "ahs_len=%d", pp->ahs_len);
104 pp->hdr_dig = sp->hdrDigest(&pp->ahs_addr, pp->ahs_len, pp->hdr_dig);
106 debug(3, "pp->hdr_dig=%04x", htonl(pp->hdr_dig));
110 | Add any AHS to the iSCSI hdr mbuf
112 if((mh->m_len + pp->ahs_len) < MHLEN) {
113 MH_ALIGN(mh, mh->m_len + pp->ahs_len);
114 bcopy(&pp->ipdu, mh->m_data, mh->m_len);
115 bcopy(pp->ahs_addr, mh->m_data + mh->m_len, pp->ahs_len);
116 mh->m_len += pp->ahs_len;
119 panic("len AHS=%d too big, not impleneted yet", pp->ahs_len);
122 MH_ALIGN(mh, mh->m_len);
123 bcopy(&pp->ipdu, mh->m_data, mh->m_len);
125 mh->m_pkthdr.len = mh->m_len;
127 if(pp->ds_len && pq->pdu.ds_addr) {
135 MGET(md, M_TRYWAIT, MT_DATA);
136 md->m_ext.ref_cnt = &ou_refcnt;
137 l = min(MCLBYTES, len);
138 debug(4, "setting ext_free(arg=%p len/l=%d/%d)", pq->buf, len, l);
139 MEXTADD(md, pp->ds_addr + off, l, ext_free,
140 #if __FreeBSD_version >= 800000
146 mh->m_pkthdr.len += l;
152 if(((pp->ds_len & 03) != 0) || ISOK2DIG(sp->dataDigest, pp)) {
153 MGET(md, M_TRYWAIT, MT_DATA);
155 len = 4 - (pp->ds_len & 03);
159 if(ISOK2DIG(sp->dataDigest, pp))
160 md->m_len += sizeof(pp->ds_dig);
161 M_ALIGN(md, md->m_len);
162 if(ISOK2DIG(sp->dataDigest, pp)) {
163 pp->ds_dig = sp->dataDigest(pp->ds_addr, pp->ds_len, 0);
165 bzero(md->m_data, len); // RFC says SHOULD be 0
166 pp->ds_dig = sp->dataDigest(md->m_data, len, pp->ds_dig);
168 bcopy(&pp->ds_dig, md->m_data+len, sizeof(pp->ds_dig));
171 mh->m_pkthdr.len += md->m_len;
175 if((error = sosend(sp->soc, NULL, NULL, mh, 0, 0, sp->td)) != 0) {
176 sdebug(2, "error=%d", error);
180 getbintime(&sp->stats.t_sent);
183 #else /* NO_USE_MBUF */
185 isc_sendPDU(isc_session_t *sp, pduq_t *pq)
187 struct uio *uio = &pq->uio;
189 pdu_t *pp = &pq->pdu;
194 bzero(uio, sizeof(struct uio));
195 uio->uio_rw = UIO_WRITE;
196 uio->uio_segflg = UIO_SYSSPACE;
197 uio->uio_td = sp->td;
198 uio->uio_iov = iv = pq->iov;
200 iv->iov_base = &pp->ipdu;
201 iv->iov_len = sizeof(union ipdu_u);
202 uio->uio_resid = iv->iov_len;
204 if(ISOK2DIG(sp->hdrDigest, pp))
205 pq->pdu.hdr_dig = sp->hdrDigest(&pp->ipdu, sizeof(union ipdu_u), 0);
207 iv->iov_base = pp->ahs_addr;
208 iv->iov_len = pp->ahs_len;
209 uio->uio_resid += iv->iov_len;
211 if(ISOK2DIG(sp->hdrDigest, pp))
212 pp->hdr_dig = sp->hdrDigest(&pp->ahs_addr, pp->ahs_len, pp->hdr_dig);
214 if(ISOK2DIG(sp->hdrDigest, pp)) {
215 debug(3, "hdr_dig=%04x", htonl(pp->hdr_dig));
216 iv->iov_base = &pp->hdr_dig;
217 iv->iov_len = sizeof(int);
218 uio->uio_resid += iv->iov_len ;
221 if(pq->pdu.ds_addr && pp->ds_len) {
222 iv->iov_base = pp->ds_addr;
223 iv->iov_len = pp->ds_len;
224 while(iv->iov_len & 03) // the specs say it must be int alligned
226 uio->uio_resid += iv->iov_len ;
228 if(ISOK2DIG(sp->dataDigest, pp)) {
229 pp->ds_dig = sp->dataDigest(pp->ds, pp->ds_len, 0);
230 iv->iov_base = &pp->ds_dig;
231 iv->iov_len = sizeof(pp->ds_dig);
232 uio->uio_resid += iv->iov_len ;
236 uio->uio_iovcnt = iv - pq->iov;
237 sdebug(4, "pq->len=%d uio->uio_resid=%d uio->uio_iovcnt=%d", pq->len,
241 sdebug(4, "opcode=%x iovcnt=%d uio_resid=%d itt=%x",
242 pp->ipdu.bhs.opcode, uio->uio_iovcnt, uio->uio_resid,
243 ntohl(pp->ipdu.bhs.itt));
244 sdebug(5, "sp=%p sp->soc=%p uio=%p sp->td=%p",
245 sp, sp->soc, uio, sp->td);
247 len = uio->uio_resid;
248 error = sosend(sp->soc, NULL, uio, 0, 0, 0, sp->td);
249 if(uio->uio_resid == 0 || error || len == uio->uio_resid) {
251 sdebug(2, "uio->uio_resid=%d uio->uio_iovcnt=%d error=%d len=%d",
252 uio->uio_resid, uio->uio_iovcnt, error, len);
254 error = EAGAIN; // 35
261 sdebug(1, "uio->uio_resid=%d uio->uio_iovcnt=%d",
262 uio->uio_resid, uio->uio_iovcnt);
264 len -= uio->uio_resid;
265 while(uio->uio_iovcnt > 0) {
266 if(iv->iov_len > len) {
267 caddr_t bp = (caddr_t)iv->iov_base;
270 iv->iov_base = (void *)&bp[len];
278 } while(uio->uio_resid);
282 getbintime(&sp->stats.t_sent);
287 #endif /* USE_MBUF */
290 | wait till a PDU header is received
294 The format of the BHS is:
296 Byte/ 0 | 1 | 2 | 3 |
298 |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|
299 +---------------+---------------+---------------+---------------+
300 0|.|I| Opcode |F| Opcode-specific fields |
301 +---------------+---------------+---------------+---------------+
302 4|TotalAHSLength | DataSegmentLength |
303 +---------------+---------------+---------------+---------------+
304 8| LUN or Opcode-specific fields |
307 +---------------+---------------+---------------+---------------+
308 16| Initiator Task Tag |
309 +---------------+---------------+---------------+---------------+
310 20/ Opcode-specific fields /
312 +---------------+---------------+---------------+---------------+
316 so_getbhs(isc_session_t *sp)
318 bhs_t *bhs = &sp->bhs;
319 struct uio *uio = &sp->uio;
320 struct iovec *iov = &sp->iov;
326 iov->iov_len = sizeof(bhs_t);
330 uio->uio_rw = UIO_READ;
331 uio->uio_segflg = UIO_SYSSPACE;
332 uio->uio_td = curthread; // why ...
333 uio->uio_resid = sizeof(bhs_t);
336 error = soreceive(sp->soc, NULL, uio, 0, 0, &flags);
340 #if __FreeBSD_version > 800000
341 "error=%d so_error=%d uio->uio_resid=%zd iov.iov_len=%zd",
343 "error=%d so_error=%d uio->uio_resid=%d iov.iov_len=%zd",
346 sp->soc->so_error, uio->uio_resid, iov->iov_len);
347 if(!error && (uio->uio_resid > 0)) {
348 error = EPIPE; // was EAGAIN
350 #if __FreeBSD_version > 800000
351 "error=%d so_error=%d uio->uio_resid=%zd iov.iov_len=%zd so_state=%x",
353 "error=%d so_error=%d uio->uio_resid=%d iov.iov_len=%zd so_state=%x",
356 sp->soc->so_error, uio->uio_resid, iov->iov_len, sp->soc->so_state);
362 | so_recv gets called when
363 | an iSCSI header has been received.
364 | Note: the designers had no intentions
365 | in making programmer's life easy.
368 so_recv(isc_session_t *sp, pduq_t *pq)
371 struct uio *uio = &pq->uio;
372 pdu_t *pp = &pq->pdu;
373 bhs_t *bhs = &pp->ipdu.bhs;
374 struct iovec *iov = pq->iov;
378 int flags = MSG_WAITALL;
382 | now calculate how much data should be in the buffer
388 debug(2, "bhs->AHSLength=%d", bhs->AHSLength);
389 pp->ahs_len = bhs->AHSLength * 4;
391 pp->ahs_addr = malloc(pp->ahs_len, M_TEMP, M_WAITOK); // XXX: could get stuck here
392 iov->iov_base = pp->ahs_addr;
393 iov->iov_len = pp->ahs_len;
397 if(ISOK2DIG(sp->hdrDigest, pp)) {
398 len += sizeof(pp->hdr_dig);
399 iov->iov_base = &pp->hdr_dig;
400 iov->iov_len = sizeof(pp->hdr_dig);
404 uio->uio_rw = UIO_READ;
405 uio->uio_segflg = UIO_SYSSPACE;
406 uio->uio_resid = len;
407 uio->uio_td = sp->td; // why ...
408 error = soreceive(sp->soc, NULL, uio, NULL, NULL, &flags);
409 //if(error == EAGAIN)
410 // XXX: this needs work! it hangs iscontrol
411 if(error || uio->uio_resid) {
413 #if __FreeBSD_version > 800000
414 "len=%d error=%d uio->uio_resid=%zd",
416 "len=%d error=%d uio->uio_resid=%d",
418 len, error, uio->uio_resid);
421 if(ISOK2DIG(sp->hdrDigest, pp)) {
425 bhs = (bhs_t *)&pp->ipdu;
426 digest = sp->hdrDigest(bhs, sizeof(bhs_t), 0);
428 digest = sp->hdrDigest(pp->ahs_addr, pp->ahs_len, digest);
429 if(pp->hdr_dig != digest) {
430 debug(2, "bad header digest: received=%x calculated=%x", pp->hdr_dig, digest);
437 debug(2, "ahs len=%x type=%x spec=%x",
438 pp->ahs_addr->len, pp->ahs_addr->type, pp->ahs_addr->spec);
439 // XXX: till I figure out what to do with this
440 free(pp->ahs_addr, M_TEMP);
442 pq->len += len; // XXX: who needs this?
443 bzero(uio, sizeof(struct uio));
449 #if BYTE_ORDER == LITTLE_ENDIAN
450 len = ((len & 0x00ff0000) >> 16)
452 | ((len & 0x000000ff) << 16);
455 if((sp->opt.maxRecvDataSegmentLength > 0) && (len > sp->opt.maxRecvDataSegmentLength)) {
456 xdebug("impossible PDU length(%d) opt.maxRecvDataSegmentLength=%d",
457 len, sp->opt.maxRecvDataSegmentLength);
459 "so_recv: impossible PDU length(%d) from iSCSI %s/%s\n",
460 len, sp->opt.targetAddress, sp->opt.targetName);
462 | XXX: this will really screwup the stream.
463 | should clear up the buffer till a valid header
464 | is found, or just close connection ...
465 | should read the RFC.
472 if(ISOK2DIG(sp->dataDigest, pp))
474 uio->uio_resid = len;
475 uio->uio_td = sp->td; // why ...
476 pq->len += len; // XXX: do we need this?
477 error = soreceive(sp->soc, NULL, uio, &pq->mp, NULL, &flags);
478 //if(error == EAGAIN)
479 // XXX: this needs work! it hangs iscontrol
480 if(error || uio->uio_resid)
482 if(ISOK2DIG(sp->dataDigest, pp)) {
484 u_int digest, ds_len, cnt;
486 // get the received digest
488 len - sizeof(pp->ds_dig),
490 (caddr_t)&pp->ds_dig);
491 // calculate all mbufs
493 ds_len = len - sizeof(pp->ds_dig);
494 for(m = pq->mp; m != NULL; m = m->m_next) {
495 cnt = MIN(ds_len, m->m_len);
496 digest = sp->dataDigest(mtod(m, char *), cnt, digest);
501 if(digest != pp->ds_dig) {
502 sdebug(1, "bad data digest: received=%x calculated=%x", pp->ds_dig, digest);
503 error = EIO; // XXX: find a better error
506 KASSERT(ds_len == 0, ("ds_len not zero"));
509 sdebug(6, "len=%d] opcode=0x%x ahs_len=0x%x ds_len=0x%x",
510 pq->len, bhs->opcode, pp->ahs_len, pp->ds_len);
512 max = ntohl(bhs->MaxCmdSN);
513 exp = ntohl(bhs->ExpStSN);
515 max > exp - _MAXINCR) {
516 sdebug(2, "bad cmd window size");
517 error = EIO; // XXX: for now;
520 if(SNA_GT(max, sn->maxCmd))
522 if(SNA_GT(exp, sn->expCmd))
525 | remove from the holding queue packets
526 | that have been acked and don't need
527 | further processing.
529 i_acked_hld(sp, NULL);
531 sp->cws = sn->maxCmd - sn->expCmd + 1;
536 // XXX: need some work here
538 // XXX: till I figure out what to do with this
539 free(pp->ahs_addr, M_TEMP);
541 xdebug("have a problem, error=%d", error);
542 pdu_free(sp->isc, pq);
543 if(!error && uio->uio_resid > 0)
549 | wait for something to arrive.
550 | and if the pdu is without errors, process it.
553 so_input(isc_session_t *sp)
560 | first read in the iSCSI header
562 error = so_getbhs(sp);
567 pq = pdu_alloc(sp->isc, M_NOWAIT);
568 if(pq == NULL) { // XXX: might cause a deadlock ...
569 debug(2, "out of pdus, wait");
570 pq = pdu_alloc(sp->isc, M_WAITOK); // OK to WAIT
572 pq->pdu.ipdu.bhs = sp->bhs;
573 pq->len = sizeof(bhs_t); // so far only the header was read
574 error = so_recv(sp, pq);
576 error += 0x800; // XXX: just to see the error.
578 // XXX: close connection and exit
582 getbintime(&sp->stats.t_recv);
590 | one per active (connected) session.
591 | this thread is responsible for reading
592 | in packets from the target.
597 isc_session_t *sp = (isc_session_t *)vp;
598 struct socket *so = sp->soc;
603 sp->flags |= ISC_CON_RUNNING;
605 while((sp->flags & (ISC_CON_RUN | ISC_LINK_UP)) == (ISC_CON_RUN | ISC_LINK_UP)) {
607 if(sp->soc == NULL || !(so->so_state & SS_ISCONNECTED)) {
608 debug(2, "sp->soc=%p", sp->soc);
611 error = so_input(sp);
613 mtx_lock(&sp->io_mtx);
614 if(sp->flags & ISC_OWAITING) {
617 mtx_unlock(&sp->io_mtx);
618 } else if(error == EPIPE) {
621 else if(error == EAGAIN) {
622 if(so->so_state & SS_ISCONNECTED)
623 // there seems to be a problem in 6.0 ...
624 tsleep(sp, PRIBIO, "isc_soc", 2*hz);
627 sdebug(2, "terminated, flags=%x so_count=%d so_state=%x error=%d proc=%p",
628 sp->flags, so->so_count, so->so_state, error, sp->proc);
629 if((sp->proc != NULL) && sp->signal) {
631 psignal(sp->proc, sp->signal);
632 PROC_UNLOCK(sp->proc);
633 sp->flags |= ISC_SIGNALED;
634 sdebug(2, "pid=%d signaled(%d)", sp->proc->p_pid, sp->signal);
637 // we have to do something ourselves
638 // like closing this session ...
641 | we've been terminated
643 // do we need this mutex ...?
644 mtx_lock(&sp->io_mtx);
645 sp->flags &= ~(ISC_CON_RUNNING | ISC_LINK_UP);
647 mtx_unlock(&sp->io_mtx);
649 sdebug(2, "dropped ISC_CON_RUNNING");
650 #if __FreeBSD_version >= 800000
658 isc_stop_receiver(isc_session_t *sp)
663 sdebug(3, "sp=%p sp->soc=%p", sp, sp? sp->soc: 0);
664 mtx_lock(&sp->io_mtx);
665 sp->flags &= ~ISC_LINK_UP;
666 msleep(&sp->soc, &sp->io_mtx, PRIBIO|PDROP, "isc_stpc", 5*hz);
668 soshutdown(sp->soc, SHUT_RD);
670 mtx_lock(&sp->io_mtx);
671 sdebug(3, "soshutdown");
672 sp->flags &= ~ISC_CON_RUN;
674 while(n-- && (sp->flags & ISC_CON_RUNNING)) {
675 sdebug(3, "waiting n=%d... flags=%x", n, sp->flags);
676 msleep(&sp->soc, &sp->io_mtx, PRIBIO, "isc_stpc", 5*hz);
678 mtx_unlock(&sp->io_mtx);
681 fdrop(sp->fp, sp->td);
690 isc_start_receiver(isc_session_t *sp)
694 sp->flags |= ISC_CON_RUN | ISC_LINK_UP;
695 #if __FreeBSD_version >= 800000
700 (isc_in, sp, &sp->soc_proc, 0, 0, "isc_in %d", sp->sid);