The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/iscsi_initiator/isc_soc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  *
   28  */
   29 /*
   30  | $Id: isc_soc.c 998 2009-12-20 10:32:45Z danny $
   31  */
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 #include "opt_iscsi_initiator.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/kernel.h>
   39 #include <sys/conf.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>
   45 #include <sys/file.h>
   46 #include <sys/uio.h>
   47 #include <sys/socketvar.h>
   48 #include <sys/socket.h>
   49 #include <sys/protosw.h>
   50 #include <sys/proc.h>
   51 #include <sys/ioccom.h>
   52 #include <sys/queue.h>
   53 #include <sys/kthread.h>
   54 #include <sys/syslog.h>
   55 #include <sys/mbuf.h>
   56 #include <sys/user.h>
   57 #include <vm/uma.h>
   58 
   59 #include <cam/cam.h>
   60 #include <cam/cam_ccb.h>
   61 
   62 #include <dev/iscsi_initiator/iscsi.h>
   63 #include <dev/iscsi_initiator/iscsivar.h>
   64 
   65 #ifndef NO_USE_MBUF
   66 #define USE_MBUF
   67 #endif
   68 
   69 #ifdef USE_MBUF
   70 static int ou_refcnt = 0;
   71 /*
   72  | function for freeing external storage for mbuf
   73  */
   74 static void
   75 ext_free(struct mbuf *m)
   76 {
   77      pduq_t *pq = m->m_ext.ext_arg1;
   78 
   79      if(pq->buf != NULL) {
   80           debug(3, "ou_refcnt=%d a=%p b=%p",
   81                ou_refcnt, m->m_ext.ext_buf, pq->buf);
   82           free(pq->buf, M_ISCSIBUF);
   83           pq->buf = NULL;
   84      }
   85 }
   86 
   87 int
   88 isc_sendPDU(isc_session_t *sp, pduq_t *pq)
   89 {
   90      struct mbuf *mh, **mp;
   91      pdu_t      *pp = &pq->pdu;
   92      int        len, error;
   93 
   94      debug_called(8);
   95      /* 
   96       | mbuf for the iSCSI header
   97       */
   98      MGETHDR(mh, M_WAITOK, MT_DATA);
   99      mh->m_pkthdr.rcvif = NULL;
  100      mh->m_next = NULL;
  101      mh->m_len = sizeof(union ipdu_u);
  102 
  103      if(ISOK2DIG(sp->hdrDigest, pp)) {
  104           pp->hdr_dig = sp->hdrDigest(&pp->ipdu, sizeof(union ipdu_u), 0);
  105           mh->m_len += sizeof(pp->hdr_dig);
  106           if(pp->ahs_len) {
  107                debug(2, "ahs_len=%d", pp->ahs_len);
  108                pp->hdr_dig = sp->hdrDigest(&pp->ahs_addr, pp->ahs_len, pp->hdr_dig);
  109           }
  110           debug(3, "pp->hdr_dig=%04x", htonl(pp->hdr_dig));
  111      }
  112      if(pp->ahs_len) {
  113           /* 
  114            | Add any AHS to the iSCSI hdr mbuf
  115            */
  116           if((mh->m_len + pp->ahs_len) < MHLEN) {
  117                M_ALIGN(mh, mh->m_len + pp->ahs_len);
  118                bcopy(&pp->ipdu, mh->m_data, mh->m_len);
  119                bcopy(pp->ahs_addr, mh->m_data + mh->m_len, pp->ahs_len);
  120                mh->m_len += pp->ahs_len;
  121           }
  122           else
  123                panic("len AHS=%d too big, not impleneted yet", pp->ahs_len);
  124      }
  125      else {
  126           M_ALIGN(mh, mh->m_len);
  127           bcopy(&pp->ipdu, mh->m_data, mh->m_len);
  128      }
  129      mh->m_pkthdr.len = mh->m_len;
  130      mp = &mh->m_next;
  131      if(pp->ds_len && pq->pdu.ds_addr) {
  132           struct mbuf *md;
  133           int   off = 0;
  134 
  135           len = pp->ds_len;
  136           while(len > 0) {
  137                int l;
  138 
  139                MGET(md, M_WAITOK, MT_DATA);
  140                md->m_ext.ext_cnt = &ou_refcnt;
  141                l = min(MCLBYTES, len);
  142                debug(4, "setting ext_free(arg=%p len/l=%d/%d)", pq->buf, len, l);
  143                m_extadd(md, pp->ds_addr + off, l, ext_free, pq, NULL, 0,
  144                     EXT_EXTREF);
  145                md->m_len = l;
  146                md->m_next = NULL;
  147                mh->m_pkthdr.len += l;
  148                *mp = md;
  149                mp = &md->m_next;
  150                len -= l;
  151                off += l;
  152           }
  153           if(((pp->ds_len & 03) != 0) || ISOK2DIG(sp->dataDigest, pp)) {
  154                MGET(md, M_WAITOK, MT_DATA);
  155                if(pp->ds_len & 03)
  156                     len = 4 - (pp->ds_len & 03);
  157                else
  158                     len = 0;
  159                md->m_len = len;
  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);
  165                     if(len) {
  166                          bzero(md->m_data, len); // RFC says SHOULD be 0
  167                          pp->ds_dig = sp->dataDigest(md->m_data, len, pp->ds_dig);
  168                     }
  169                     bcopy(&pp->ds_dig, md->m_data+len, sizeof(pp->ds_dig));
  170                }
  171                md->m_next = NULL;
  172                mh->m_pkthdr.len += md->m_len;
  173                *mp = md;
  174           }
  175      }
  176      if((error = sosend(sp->soc, NULL, NULL, mh, 0, 0, sp->td)) != 0) {
  177           sdebug(2, "error=%d", error);
  178           return error;
  179      }
  180      sp->stats.nsent++;
  181      getbintime(&sp->stats.t_sent);
  182      return 0;
  183 }
  184 #else /* NO_USE_MBUF */
  185 int
  186 isc_sendPDU(isc_session_t *sp, pduq_t *pq)
  187 {
  188      struct uio *uio = &pq->uio;
  189      struct iovec *iv;
  190      pdu_t      *pp = &pq->pdu;
  191      int        len, error;
  192 
  193      debug_called(8);
  194 
  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;
  200 
  201      iv->iov_base = &pp->ipdu;
  202      iv->iov_len = sizeof(union ipdu_u);
  203      uio->uio_resid = iv->iov_len;
  204      iv++;
  205      if(ISOK2DIG(sp->hdrDigest, pp))
  206           pq->pdu.hdr_dig = sp->hdrDigest(&pp->ipdu, sizeof(union ipdu_u), 0);
  207      if(pp->ahs_len) {
  208           iv->iov_base = pp->ahs_addr;
  209           iv->iov_len = pp->ahs_len;
  210           uio->uio_resid += iv->iov_len;
  211           iv++;
  212           if(ISOK2DIG(sp->hdrDigest, pp))
  213                pp->hdr_dig = sp->hdrDigest(&pp->ahs_addr, pp->ahs_len, pp->hdr_dig);
  214      }
  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 ;
  220           iv++;
  221      }
  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
  226                iv->iov_len++;
  227           uio->uio_resid += iv->iov_len ;
  228           iv++;
  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 ;
  234                iv++;
  235           }
  236      }
  237      uio->uio_iovcnt = iv - pq->iov;
  238      sdebug(4, "pq->len=%d uio->uio_resid=%d  uio->uio_iovcnt=%d", pq->len,
  239             uio->uio_resid,
  240             uio->uio_iovcnt);
  241 
  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);
  247      do {
  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) {
  251                if(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);
  254                     if(error == 0)
  255                          error = EAGAIN; // 35
  256                }
  257                break;
  258           }
  259           /*
  260            | XXX: untested code
  261            */
  262           sdebug(1, "uio->uio_resid=%d uio->uio_iovcnt=%d",
  263                  uio->uio_resid, uio->uio_iovcnt);
  264           iv = uio->uio_iov;
  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;
  269 
  270                     iv->iov_len -= len;
  271                     iv->iov_base = (void *)&bp[len];
  272                     break;
  273                }
  274                len -= iv->iov_len;
  275                uio->uio_iovcnt--;
  276                uio->uio_iov++;
  277                iv++;
  278           }
  279      } while(uio->uio_resid);
  280 
  281      if(error == 0) {
  282           sp->stats.nsent++;
  283           getbintime(&sp->stats.t_sent);
  284      }
  285 
  286      return error;
  287 }
  288 #endif /* USE_MBUF */
  289 
  290 /*
  291  | wait till a PDU header is received
  292  | from the socket.
  293  */
  294 /*
  295    The format of the BHS is:
  296 
  297    Byte/     0       |       1       |       2       |       3       |
  298       /              |               |               |               |
  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                                 |
  306      +                                                               +
  307    12|                                                               |
  308      +---------------+---------------+---------------+---------------+
  309    16| Initiator Task Tag                                            |
  310      +---------------+---------------+---------------+---------------+
  311    20/ Opcode-specific fields                                        /
  312     +/                                                               /
  313      +---------------+---------------+---------------+---------------+
  314    48
  315  */
  316 static __inline int
  317 so_getbhs(isc_session_t *sp)
  318 {
  319      bhs_t *bhs         = &sp->bhs;
  320      struct uio         *uio = &sp->uio;
  321      struct iovec       *iov = &sp->iov;
  322      int                error, flags;
  323 
  324      debug_called(8);
  325 
  326      iov->iov_base      = bhs;
  327      iov->iov_len       = sizeof(bhs_t);
  328 
  329      uio->uio_iov       = iov;
  330      uio->uio_iovcnt    = 1;
  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);
  335 
  336      flags = MSG_WAITALL;
  337      error = soreceive(sp->soc, NULL, uio, 0, 0, &flags);
  338 
  339      if(error)
  340           debug(2, 
  341                 "error=%d so_error=%d uio->uio_resid=%zd iov.iov_len=%zd",
  342                 error,
  343                 sp->soc->so_error, uio->uio_resid, iov->iov_len);
  344      if(!error && (uio->uio_resid > 0)) {
  345           error = EPIPE; // was EAGAIN
  346           debug(2,
  347                 "error=%d so_error=%d uio->uio_resid=%zd iov.iov_len=%zd so_state=%x",
  348                 error,
  349                 sp->soc->so_error, uio->uio_resid, iov->iov_len, sp->soc->so_state);
  350      }
  351      return error;
  352 }
  353 
  354 /*
  355  | so_recv gets called when 
  356  | an iSCSI header has been received.
  357  | Note: the designers had no intentions 
  358  |       in making programmer's life easy.
  359  */
  360 static int
  361 so_recv(isc_session_t *sp, pduq_t *pq)
  362 {
  363      sn_t               *sn = &sp->sn;
  364      struct uio         *uio = &pq->uio;
  365      pdu_t              *pp = &pq->pdu;
  366      bhs_t              *bhs = &pp->ipdu.bhs;
  367      struct iovec       *iov = pq->iov;
  368      int                error;
  369      u_int              len;
  370      u_int              max, exp;
  371      int                flags = MSG_WAITALL;
  372 
  373      debug_called(8);
  374      /*
  375       | now calculate how much data should be in the buffer
  376       */
  377      uio->uio_iov       = iov;
  378      uio->uio_iovcnt    = 0;
  379      len = 0;
  380      if(bhs->AHSLength) {
  381           debug(2, "bhs->AHSLength=%d", bhs->AHSLength);
  382           pp->ahs_len = bhs->AHSLength * 4;
  383           len += pp->ahs_len;
  384           pp->ahs_addr = malloc(pp->ahs_len, M_TEMP, M_WAITOK); // XXX: could get stuck here
  385           iov->iov_base = pp->ahs_addr;
  386           iov->iov_len = pp->ahs_len;
  387           uio->uio_iovcnt++;
  388           iov++;
  389      }
  390      if(ISOK2DIG(sp->hdrDigest, pp)) {
  391           len += sizeof(pp->hdr_dig);
  392           iov->iov_base = &pp->hdr_dig;
  393           iov->iov_len = sizeof(pp->hdr_dig);
  394           uio->uio_iovcnt++;
  395      }
  396      if(len) {
  397           uio->uio_rw           = UIO_READ;
  398           uio->uio_segflg       = UIO_SYSSPACE;
  399           uio->uio_resid        = len;
  400           uio->uio_td           = sp->td; // why ...
  401           error = soreceive(sp->soc, NULL, uio, NULL, NULL, &flags);
  402           //if(error == EAGAIN)
  403           // XXX: this needs work! it hangs iscontrol
  404           if(error || uio->uio_resid) {
  405                debug(2, 
  406                      "len=%d error=%d uio->uio_resid=%zd",
  407                      len, error, uio->uio_resid);
  408                goto out;
  409           }
  410           if(ISOK2DIG(sp->hdrDigest, pp)) {
  411                bhs_t    *bhs;
  412                u_int    digest;
  413                
  414                bhs = (bhs_t *)&pp->ipdu;
  415                digest = sp->hdrDigest(bhs, sizeof(bhs_t), 0);
  416                if(pp->ahs_len)
  417                     digest = sp->hdrDigest(pp->ahs_addr, pp->ahs_len, digest);
  418                if(pp->hdr_dig != digest) {
  419                     debug(2, "bad header digest: received=%x calculated=%x", pp->hdr_dig, digest);
  420                     // XXX: now what?
  421                     error = EIO;
  422                     goto out;
  423                }
  424           }
  425           if(pp->ahs_len) {
  426                debug(2, "ahs len=%x type=%x spec=%x",
  427                      pp->ahs_addr->len, pp->ahs_addr->type, pp->ahs_addr->spec);
  428                // XXX: till I figure out what to do with this
  429                free(pp->ahs_addr, M_TEMP);
  430           }
  431           pq->len += len; // XXX: who needs this?
  432           bzero(uio, sizeof(struct uio));
  433           len = 0;
  434      }
  435 
  436      if(bhs->DSLength) {
  437           len = bhs->DSLength;
  438 #if BYTE_ORDER == LITTLE_ENDIAN
  439           len = ((len & 0x00ff0000) >> 16)
  440                | (len & 0x0000ff00)
  441                | ((len & 0x000000ff) << 16);
  442 #endif
  443           pp->ds_len = len;
  444           if((sp->opt.maxRecvDataSegmentLength > 0) && (len > sp->opt.maxRecvDataSegmentLength)) {
  445                xdebug("impossible PDU length(%d) opt.maxRecvDataSegmentLength=%d",
  446                       len, sp->opt.maxRecvDataSegmentLength);
  447                log(LOG_ERR,
  448                    "so_recv: impossible PDU length(%d) from iSCSI %s/%s\n",
  449                    len, sp->opt.targetAddress, sp->opt.targetName);
  450                /*
  451                 | XXX: this will really screwup the stream.
  452                 | should clear up the buffer till a valid header
  453                 | is found, or just close connection ...
  454                 | should read the RFC.
  455                 */
  456                error = E2BIG;
  457                goto out;
  458           }
  459           while(len & 03)
  460                len++;
  461           if(ISOK2DIG(sp->dataDigest, pp))
  462                len += 4;
  463           uio->uio_resid = len;
  464           uio->uio_td = sp->td; // why ...
  465           pq->len += len; // XXX: do we need this?
  466           error = soreceive(sp->soc, NULL, uio, &pq->mp, NULL, &flags);
  467           //if(error == EAGAIN)
  468           // XXX: this needs work! it hangs iscontrol
  469           if(error || uio->uio_resid)
  470                goto out;
  471           if(ISOK2DIG(sp->dataDigest, pp)) {
  472                struct mbuf *m;
  473                u_int    digest, ds_len, cnt;
  474 
  475                // get the received digest
  476                m_copydata(pq->mp,
  477                           len - sizeof(pp->ds_dig),
  478                           sizeof(pp->ds_dig),
  479                           (caddr_t)&pp->ds_dig);
  480                // calculate all mbufs 
  481                digest = 0;
  482                ds_len = len - sizeof(pp->ds_dig);
  483                for(m = pq->mp; m != NULL; m = m->m_next) {
  484                     cnt = MIN(ds_len, m->m_len);
  485                     digest = sp->dataDigest(mtod(m, char *), cnt, digest);
  486                     ds_len -= cnt;
  487                     if(ds_len == 0)
  488                          break;
  489                }
  490                if(digest != pp->ds_dig) {
  491                     sdebug(1, "bad data digest: received=%x calculated=%x", pp->ds_dig, digest);
  492                     error = EIO; // XXX: find a better error
  493                     goto out;
  494                }
  495                KASSERT(ds_len == 0, ("ds_len not zero"));
  496           }
  497      }
  498      sdebug(6, "len=%d] opcode=0x%x ahs_len=0x%x ds_len=0x%x",
  499             pq->len, bhs->opcode, pp->ahs_len, pp->ds_len);
  500 
  501      max = ntohl(bhs->MaxCmdSN);
  502      exp = ntohl(bhs->ExpStSN);
  503      if(max < exp - 1 &&
  504         max > exp - _MAXINCR) {
  505           sdebug(2,  "bad cmd window size");
  506           error = EIO; // XXX: for now;
  507           goto out; // error
  508      }
  509      if(SNA_GT(max, sn->maxCmd))
  510           sn->maxCmd = max;
  511      if(SNA_GT(exp, sn->expCmd))
  512           sn->expCmd = exp;
  513      /*
  514       | remove from the holding queue packets
  515       | that have been acked and don't need
  516       | further processing.
  517       */
  518      i_acked_hld(sp, NULL);
  519 
  520      sp->cws = sn->maxCmd - sn->expCmd + 1;
  521 
  522      return 0;
  523 
  524  out:
  525      // XXX: need some work here
  526      if(pp->ahs_len) {
  527           // XXX: till I figure out what to do with this
  528           free(pp->ahs_addr, M_TEMP);
  529      }
  530      xdebug("have a problem, error=%d", error);
  531      pdu_free(sp->isc, pq);
  532      if(!error && uio->uio_resid > 0)
  533           error = EPIPE;
  534      return error;
  535 }
  536 
  537 /*
  538  | wait for something to arrive.
  539  | and if the pdu is without errors, process it.
  540  */
  541 static int
  542 so_input(isc_session_t *sp)
  543 {
  544      pduq_t             *pq;
  545      int                error;
  546 
  547      debug_called(8);
  548      /*
  549       | first read in the iSCSI header
  550       */
  551      error = so_getbhs(sp);
  552      if(error == 0) {
  553           /*
  554            | now read the rest.
  555            */
  556           pq = pdu_alloc(sp->isc, M_NOWAIT); 
  557           if(pq == NULL) { // XXX: might cause a deadlock ...
  558                debug(2, "out of pdus, wait");
  559                pq = pdu_alloc(sp->isc, M_WAITOK);  // OK to WAIT
  560           }
  561           pq->pdu.ipdu.bhs = sp->bhs;
  562           pq->len = sizeof(bhs_t);      // so far only the header was read
  563           error = so_recv(sp, pq);
  564           if(error != 0) {
  565                error += 0x800; // XXX: just to see the error.
  566                // terminal error
  567                // XXX: close connection and exit
  568           }
  569           else {
  570                sp->stats.nrecv++;
  571                getbintime(&sp->stats.t_recv);
  572                ism_recv(sp, pq);
  573           }
  574      }
  575      return error;
  576 }
  577 
  578 /*
  579  | one per active (connected) session.
  580  | this thread is responsible for reading
  581  | in packets from the target.
  582  */
  583 static void
  584 isc_in(void *vp)
  585 {
  586      isc_session_t      *sp = (isc_session_t *)vp;
  587      struct socket      *so = sp->soc;
  588      int                error;
  589 
  590      debug_called(8);
  591 
  592      sp->flags |= ISC_CON_RUNNING;
  593      error = 0;
  594      while((sp->flags & (ISC_CON_RUN | ISC_LINK_UP)) == (ISC_CON_RUN | ISC_LINK_UP)) {
  595           // XXX: hunting ...
  596           if(sp->soc == NULL || !(so->so_state & SS_ISCONNECTED)) {
  597                debug(2, "sp->soc=%p", sp->soc);
  598                break;
  599           }
  600           error = so_input(sp);
  601           if(error == 0) {
  602                mtx_lock(&sp->io_mtx);
  603                if(sp->flags & ISC_OWAITING) {
  604                     wakeup(&sp->flags);
  605                }
  606                mtx_unlock(&sp->io_mtx);
  607           } else if(error == EPIPE) {
  608                break;
  609           }
  610           else if(error == EAGAIN) {
  611                if(so->so_state & SS_ISCONNECTED) 
  612                     // there seems to be a problem in 6.0 ...
  613                     tsleep(sp, PRIBIO, "isc_soc", 2*hz);
  614           }
  615      }
  616      sdebug(2, "terminated, flags=%x so_count=%d so_state=%x error=%d proc=%p",
  617             sp->flags, so->so_count, so->so_state, error, sp->proc);
  618      if((sp->proc != NULL) && sp->signal) {
  619           PROC_LOCK(sp->proc);
  620           kern_psignal(sp->proc, sp->signal);
  621           PROC_UNLOCK(sp->proc);
  622           sp->flags |= ISC_SIGNALED;
  623           sdebug(2, "pid=%d signaled(%d)", sp->proc->p_pid, sp->signal);
  624      }
  625      else {
  626           // we have to do something ourselves
  627           // like closing this session ...
  628      }
  629      /*
  630       | we've been terminated
  631       */
  632      // do we need this mutex ...?
  633      mtx_lock(&sp->io_mtx);
  634      sp->flags &= ~(ISC_CON_RUNNING | ISC_LINK_UP);
  635      wakeup(&sp->soc);
  636      mtx_unlock(&sp->io_mtx);
  637 
  638      sdebug(2, "dropped ISC_CON_RUNNING");
  639      kproc_exit(0);
  640 }
  641 
  642 void
  643 isc_stop_receiver(isc_session_t *sp)
  644 {
  645      int        n;
  646 
  647      debug_called(8);
  648      sdebug(3, "sp=%p sp->soc=%p", sp, sp? sp->soc: 0);
  649      mtx_lock(&sp->io_mtx);
  650      sp->flags &= ~ISC_LINK_UP;
  651      msleep(&sp->soc, &sp->io_mtx, PRIBIO|PDROP, "isc_stpc", 5*hz);
  652 
  653      soshutdown(sp->soc, SHUT_RD);
  654 
  655      mtx_lock(&sp->io_mtx);
  656      sdebug(3, "soshutdown");
  657      sp->flags &= ~ISC_CON_RUN;
  658      n = 2;
  659      while(n-- && (sp->flags & ISC_CON_RUNNING)) {
  660           sdebug(3, "waiting n=%d... flags=%x", n, sp->flags);
  661           msleep(&sp->soc, &sp->io_mtx, PRIBIO, "isc_stpc", 5*hz);
  662      }
  663      mtx_unlock(&sp->io_mtx);
  664 
  665      if(sp->fp != NULL)
  666           fdrop(sp->fp, sp->td);
  667      sp->soc = NULL;
  668      sp->fp = NULL;
  669 
  670      sdebug(3, "done");
  671 }
  672 
  673 void
  674 isc_start_receiver(isc_session_t *sp)
  675 {
  676      debug_called(8);
  677 
  678      sp->flags |= ISC_CON_RUN | ISC_LINK_UP;
  679      kproc_create(isc_in, sp, &sp->soc_proc, 0, 0, "isc_in %d", sp->sid);
  680 }

Cache object: 58849699cbbba165c6a5e59b89adcc23


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.