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>
58 #include <cam/cam_ccb.h>
60 #include <dev/iscsi_initiator/iscsi.h>
61 #include <dev/iscsi_initiator/iscsivar.h>
68 static int ou_refcnt = 0;
70 | function for freeing external storage for mbuf
73 ext_free(struct mbuf *m, void *a, void *b)
78 debug(3, "ou_refcnt=%d a=%p b=%p", ou_refcnt, a, pq->buf);
79 free(pq->buf, M_ISCSIBUF);
85 isc_sendPDU(isc_session_t *sp, pduq_t *pq)
87 struct mbuf *mh, **mp;
93 | mbuf for the iSCSI header
95 MGETHDR(mh, M_WAITOK, MT_DATA);
96 mh->m_pkthdr.rcvif = NULL;
98 mh->m_len = sizeof(union ipdu_u);
100 if(ISOK2DIG(sp->hdrDigest, pp)) {
101 pp->hdr_dig = sp->hdrDigest(&pp->ipdu, sizeof(union ipdu_u), 0);
102 mh->m_len += sizeof(pp->hdr_dig);
104 debug(2, "ahs_len=%d", pp->ahs_len);
105 pp->hdr_dig = sp->hdrDigest(&pp->ahs_addr, pp->ahs_len, pp->hdr_dig);
107 debug(3, "pp->hdr_dig=%04x", htonl(pp->hdr_dig));
111 | Add any AHS to the iSCSI hdr mbuf
113 if((mh->m_len + pp->ahs_len) < MHLEN) {
114 M_ALIGN(mh, mh->m_len + pp->ahs_len);
115 bcopy(&pp->ipdu, mh->m_data, mh->m_len);
116 bcopy(pp->ahs_addr, mh->m_data + mh->m_len, pp->ahs_len);
117 mh->m_len += pp->ahs_len;
120 panic("len AHS=%d too big, not impleneted yet", pp->ahs_len);
123 M_ALIGN(mh, mh->m_len);
124 bcopy(&pp->ipdu, mh->m_data, mh->m_len);
126 mh->m_pkthdr.len = mh->m_len;
128 if(pp->ds_len && pq->pdu.ds_addr) {
136 MGET(md, M_WAITOK, MT_DATA);
137 md->m_ext.ext_cnt = &ou_refcnt;
138 l = min(MCLBYTES, len);
139 debug(4, "setting ext_free(arg=%p len/l=%d/%d)", pq->buf, len, l);
140 MEXTADD(md, pp->ds_addr + off, l, ext_free,
141 #if __FreeBSD_version >= 800000
147 mh->m_pkthdr.len += l;
153 if(((pp->ds_len & 03) != 0) || ISOK2DIG(sp->dataDigest, pp)) {
154 MGET(md, M_WAITOK, MT_DATA);
156 len = 4 - (pp->ds_len & 03);
160 if(ISOK2DIG(sp->dataDigest, pp))
161 md->m_len += sizeof(pp->ds_dig);
162 M_ALIGN(md, md->m_len);
163 if(ISOK2DIG(sp->dataDigest, pp)) {
164 pp->ds_dig = sp->dataDigest(pp->ds_addr, pp->ds_len, 0);
166 bzero(md->m_data, len); // RFC says SHOULD be 0
167 pp->ds_dig = sp->dataDigest(md->m_data, len, pp->ds_dig);
169 bcopy(&pp->ds_dig, md->m_data+len, sizeof(pp->ds_dig));
172 mh->m_pkthdr.len += md->m_len;
176 if((error = sosend(sp->soc, NULL, NULL, mh, 0, 0, sp->td)) != 0) {
177 sdebug(2, "error=%d", error);
181 getbintime(&sp->stats.t_sent);
184 #else /* NO_USE_MBUF */
186 isc_sendPDU(isc_session_t *sp, pduq_t *pq)
188 struct uio *uio = &pq->uio;
190 pdu_t *pp = &pq->pdu;
195 bzero(uio, sizeof(struct uio));
196 uio->uio_rw = UIO_WRITE;
197 uio->uio_segflg = UIO_SYSSPACE;
198 uio->uio_td = sp->td;
199 uio->uio_iov = iv = pq->iov;
201 iv->iov_base = &pp->ipdu;
202 iv->iov_len = sizeof(union ipdu_u);
203 uio->uio_resid = iv->iov_len;
205 if(ISOK2DIG(sp->hdrDigest, pp))
206 pq->pdu.hdr_dig = sp->hdrDigest(&pp->ipdu, sizeof(union ipdu_u), 0);
208 iv->iov_base = pp->ahs_addr;
209 iv->iov_len = pp->ahs_len;
210 uio->uio_resid += iv->iov_len;
212 if(ISOK2DIG(sp->hdrDigest, pp))
213 pp->hdr_dig = sp->hdrDigest(&pp->ahs_addr, pp->ahs_len, pp->hdr_dig);
215 if(ISOK2DIG(sp->hdrDigest, pp)) {
216 debug(3, "hdr_dig=%04x", htonl(pp->hdr_dig));
217 iv->iov_base = &pp->hdr_dig;
218 iv->iov_len = sizeof(int);
219 uio->uio_resid += iv->iov_len ;
222 if(pq->pdu.ds_addr && pp->ds_len) {
223 iv->iov_base = pp->ds_addr;
224 iv->iov_len = pp->ds_len;
225 while(iv->iov_len & 03) // the specs say it must be int aligned
227 uio->uio_resid += iv->iov_len ;
229 if(ISOK2DIG(sp->dataDigest, pp)) {
230 pp->ds_dig = sp->dataDigest(pp->ds, pp->ds_len, 0);
231 iv->iov_base = &pp->ds_dig;
232 iv->iov_len = sizeof(pp->ds_dig);
233 uio->uio_resid += iv->iov_len ;
237 uio->uio_iovcnt = iv - pq->iov;
238 sdebug(4, "pq->len=%d uio->uio_resid=%d uio->uio_iovcnt=%d", pq->len,
242 sdebug(4, "opcode=%x iovcnt=%d uio_resid=%d itt=%x",
243 pp->ipdu.bhs.opcode, uio->uio_iovcnt, uio->uio_resid,
244 ntohl(pp->ipdu.bhs.itt));
245 sdebug(5, "sp=%p sp->soc=%p uio=%p sp->td=%p",
246 sp, sp->soc, uio, sp->td);
248 len = uio->uio_resid;
249 error = sosend(sp->soc, NULL, uio, 0, 0, 0, sp->td);
250 if(uio->uio_resid == 0 || error || len == uio->uio_resid) {
252 sdebug(2, "uio->uio_resid=%d uio->uio_iovcnt=%d error=%d len=%d",
253 uio->uio_resid, uio->uio_iovcnt, error, len);
255 error = EAGAIN; // 35
262 sdebug(1, "uio->uio_resid=%d uio->uio_iovcnt=%d",
263 uio->uio_resid, uio->uio_iovcnt);
265 len -= uio->uio_resid;
266 while(uio->uio_iovcnt > 0) {
267 if(iv->iov_len > len) {
268 caddr_t bp = (caddr_t)iv->iov_base;
271 iv->iov_base = (void *)&bp[len];
279 } while(uio->uio_resid);
283 getbintime(&sp->stats.t_sent);
288 #endif /* USE_MBUF */
291 | wait till a PDU header is received
295 The format of the BHS is:
297 Byte/ 0 | 1 | 2 | 3 |
299 |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|
300 +---------------+---------------+---------------+---------------+
301 0|.|I| Opcode |F| Opcode-specific fields |
302 +---------------+---------------+---------------+---------------+
303 4|TotalAHSLength | DataSegmentLength |
304 +---------------+---------------+---------------+---------------+
305 8| LUN or Opcode-specific fields |
308 +---------------+---------------+---------------+---------------+
309 16| Initiator Task Tag |
310 +---------------+---------------+---------------+---------------+
311 20/ Opcode-specific fields /
313 +---------------+---------------+---------------+---------------+
317 so_getbhs(isc_session_t *sp)
319 bhs_t *bhs = &sp->bhs;
320 struct uio *uio = &sp->uio;
321 struct iovec *iov = &sp->iov;
327 iov->iov_len = sizeof(bhs_t);
331 uio->uio_rw = UIO_READ;
332 uio->uio_segflg = UIO_SYSSPACE;
333 uio->uio_td = curthread; // why ...
334 uio->uio_resid = sizeof(bhs_t);
337 error = soreceive(sp->soc, NULL, uio, 0, 0, &flags);
341 #if __FreeBSD_version > 800000
342 "error=%d so_error=%d uio->uio_resid=%zd iov.iov_len=%zd",
344 "error=%d so_error=%d uio->uio_resid=%d iov.iov_len=%zd",
347 sp->soc->so_error, uio->uio_resid, iov->iov_len);
348 if(!error && (uio->uio_resid > 0)) {
349 error = EPIPE; // was EAGAIN
351 #if __FreeBSD_version > 800000
352 "error=%d so_error=%d uio->uio_resid=%zd iov.iov_len=%zd so_state=%x",
354 "error=%d so_error=%d uio->uio_resid=%d iov.iov_len=%zd so_state=%x",
357 sp->soc->so_error, uio->uio_resid, iov->iov_len, sp->soc->so_state);
363 | so_recv gets called when
364 | an iSCSI header has been received.
365 | Note: the designers had no intentions
366 | in making programmer's life easy.
369 so_recv(isc_session_t *sp, pduq_t *pq)
372 struct uio *uio = &pq->uio;
373 pdu_t *pp = &pq->pdu;
374 bhs_t *bhs = &pp->ipdu.bhs;
375 struct iovec *iov = pq->iov;
379 int flags = MSG_WAITALL;
383 | now calculate how much data should be in the buffer
389 debug(2, "bhs->AHSLength=%d", bhs->AHSLength);
390 pp->ahs_len = bhs->AHSLength * 4;
392 pp->ahs_addr = malloc(pp->ahs_len, M_TEMP, M_WAITOK); // XXX: could get stuck here
393 iov->iov_base = pp->ahs_addr;
394 iov->iov_len = pp->ahs_len;
398 if(ISOK2DIG(sp->hdrDigest, pp)) {
399 len += sizeof(pp->hdr_dig);
400 iov->iov_base = &pp->hdr_dig;
401 iov->iov_len = sizeof(pp->hdr_dig);
405 uio->uio_rw = UIO_READ;
406 uio->uio_segflg = UIO_SYSSPACE;
407 uio->uio_resid = len;
408 uio->uio_td = sp->td; // why ...
409 error = soreceive(sp->soc, NULL, uio, NULL, NULL, &flags);
410 //if(error == EAGAIN)
411 // XXX: this needs work! it hangs iscontrol
412 if(error || uio->uio_resid) {
414 #if __FreeBSD_version > 800000
415 "len=%d error=%d uio->uio_resid=%zd",
417 "len=%d error=%d uio->uio_resid=%d",
419 len, error, uio->uio_resid);
422 if(ISOK2DIG(sp->hdrDigest, pp)) {
426 bhs = (bhs_t *)&pp->ipdu;
427 digest = sp->hdrDigest(bhs, sizeof(bhs_t), 0);
429 digest = sp->hdrDigest(pp->ahs_addr, pp->ahs_len, digest);
430 if(pp->hdr_dig != digest) {
431 debug(2, "bad header digest: received=%x calculated=%x", pp->hdr_dig, digest);
438 debug(2, "ahs len=%x type=%x spec=%x",
439 pp->ahs_addr->len, pp->ahs_addr->type, pp->ahs_addr->spec);
440 // XXX: till I figure out what to do with this
441 free(pp->ahs_addr, M_TEMP);
443 pq->len += len; // XXX: who needs this?
444 bzero(uio, sizeof(struct uio));
450 #if BYTE_ORDER == LITTLE_ENDIAN
451 len = ((len & 0x00ff0000) >> 16)
453 | ((len & 0x000000ff) << 16);
456 if((sp->opt.maxRecvDataSegmentLength > 0) && (len > sp->opt.maxRecvDataSegmentLength)) {
457 xdebug("impossible PDU length(%d) opt.maxRecvDataSegmentLength=%d",
458 len, sp->opt.maxRecvDataSegmentLength);
460 "so_recv: impossible PDU length(%d) from iSCSI %s/%s\n",
461 len, sp->opt.targetAddress, sp->opt.targetName);
463 | XXX: this will really screwup the stream.
464 | should clear up the buffer till a valid header
465 | is found, or just close connection ...
466 | should read the RFC.
473 if(ISOK2DIG(sp->dataDigest, pp))
475 uio->uio_resid = len;
476 uio->uio_td = sp->td; // why ...
477 pq->len += len; // XXX: do we need this?
478 error = soreceive(sp->soc, NULL, uio, &pq->mp, NULL, &flags);
479 //if(error == EAGAIN)
480 // XXX: this needs work! it hangs iscontrol
481 if(error || uio->uio_resid)
483 if(ISOK2DIG(sp->dataDigest, pp)) {
485 u_int digest, ds_len, cnt;
487 // get the received digest
489 len - sizeof(pp->ds_dig),
491 (caddr_t)&pp->ds_dig);
492 // calculate all mbufs
494 ds_len = len - sizeof(pp->ds_dig);
495 for(m = pq->mp; m != NULL; m = m->m_next) {
496 cnt = MIN(ds_len, m->m_len);
497 digest = sp->dataDigest(mtod(m, char *), cnt, digest);
502 if(digest != pp->ds_dig) {
503 sdebug(1, "bad data digest: received=%x calculated=%x", pp->ds_dig, digest);
504 error = EIO; // XXX: find a better error
507 KASSERT(ds_len == 0, ("ds_len not zero"));
510 sdebug(6, "len=%d] opcode=0x%x ahs_len=0x%x ds_len=0x%x",
511 pq->len, bhs->opcode, pp->ahs_len, pp->ds_len);
513 max = ntohl(bhs->MaxCmdSN);
514 exp = ntohl(bhs->ExpStSN);
516 max > exp - _MAXINCR) {
517 sdebug(2, "bad cmd window size");
518 error = EIO; // XXX: for now;
521 if(SNA_GT(max, sn->maxCmd))
523 if(SNA_GT(exp, sn->expCmd))
526 | remove from the holding queue packets
527 | that have been acked and don't need
528 | further processing.
530 i_acked_hld(sp, NULL);
532 sp->cws = sn->maxCmd - sn->expCmd + 1;
537 // XXX: need some work here
539 // XXX: till I figure out what to do with this
540 free(pp->ahs_addr, M_TEMP);
542 xdebug("have a problem, error=%d", error);
543 pdu_free(sp->isc, pq);
544 if(!error && uio->uio_resid > 0)
550 | wait for something to arrive.
551 | and if the pdu is without errors, process it.
554 so_input(isc_session_t *sp)
561 | first read in the iSCSI header
563 error = so_getbhs(sp);
568 pq = pdu_alloc(sp->isc, M_NOWAIT);
569 if(pq == NULL) { // XXX: might cause a deadlock ...
570 debug(2, "out of pdus, wait");
571 pq = pdu_alloc(sp->isc, M_WAITOK); // OK to WAIT
573 pq->pdu.ipdu.bhs = sp->bhs;
574 pq->len = sizeof(bhs_t); // so far only the header was read
575 error = so_recv(sp, pq);
577 error += 0x800; // XXX: just to see the error.
579 // XXX: close connection and exit
583 getbintime(&sp->stats.t_recv);
591 | one per active (connected) session.
592 | this thread is responsible for reading
593 | in packets from the target.
598 isc_session_t *sp = (isc_session_t *)vp;
599 struct socket *so = sp->soc;
604 sp->flags |= ISC_CON_RUNNING;
606 while((sp->flags & (ISC_CON_RUN | ISC_LINK_UP)) == (ISC_CON_RUN | ISC_LINK_UP)) {
608 if(sp->soc == NULL || !(so->so_state & SS_ISCONNECTED)) {
609 debug(2, "sp->soc=%p", sp->soc);
612 error = so_input(sp);
614 mtx_lock(&sp->io_mtx);
615 if(sp->flags & ISC_OWAITING) {
618 mtx_unlock(&sp->io_mtx);
619 } else if(error == EPIPE) {
622 else if(error == EAGAIN) {
623 if(so->so_state & SS_ISCONNECTED)
624 // there seems to be a problem in 6.0 ...
625 tsleep(sp, PRIBIO, "isc_soc", 2*hz);
628 sdebug(2, "terminated, flags=%x so_count=%d so_state=%x error=%d proc=%p",
629 sp->flags, so->so_count, so->so_state, error, sp->proc);
630 if((sp->proc != NULL) && sp->signal) {
632 kern_psignal(sp->proc, sp->signal);
633 PROC_UNLOCK(sp->proc);
634 sp->flags |= ISC_SIGNALED;
635 sdebug(2, "pid=%d signaled(%d)", sp->proc->p_pid, sp->signal);
638 // we have to do something ourselves
639 // like closing this session ...
642 | we've been terminated
644 // do we need this mutex ...?
645 mtx_lock(&sp->io_mtx);
646 sp->flags &= ~(ISC_CON_RUNNING | ISC_LINK_UP);
648 mtx_unlock(&sp->io_mtx);
650 sdebug(2, "dropped ISC_CON_RUNNING");
651 #if __FreeBSD_version >= 800000
659 isc_stop_receiver(isc_session_t *sp)
664 sdebug(3, "sp=%p sp->soc=%p", sp, sp? sp->soc: 0);
665 mtx_lock(&sp->io_mtx);
666 sp->flags &= ~ISC_LINK_UP;
667 msleep(&sp->soc, &sp->io_mtx, PRIBIO|PDROP, "isc_stpc", 5*hz);
669 soshutdown(sp->soc, SHUT_RD);
671 mtx_lock(&sp->io_mtx);
672 sdebug(3, "soshutdown");
673 sp->flags &= ~ISC_CON_RUN;
675 while(n-- && (sp->flags & ISC_CON_RUNNING)) {
676 sdebug(3, "waiting n=%d... flags=%x", n, sp->flags);
677 msleep(&sp->soc, &sp->io_mtx, PRIBIO, "isc_stpc", 5*hz);
679 mtx_unlock(&sp->io_mtx);
682 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);