2 * Copyright (c) 2005-2011 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: iscsi.c 752 2009-08-20 11:23:28Z danny $
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_iscsi_initiator.h"
36 #include <sys/param.h>
37 #include <sys/capsicum.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/ctype.h>
45 #include <sys/errno.h>
46 #include <sys/sysctl.h>
49 #include <sys/socketvar.h>
50 #include <sys/socket.h>
51 #include <sys/protosw.h>
53 #include <sys/ioccom.h>
54 #include <sys/queue.h>
55 #include <sys/kthread.h>
57 #include <sys/syslog.h>
61 #include <dev/iscsi_initiator/iscsi.h>
62 #include <dev/iscsi_initiator/iscsivar.h>
63 static char *iscsi_driver_version = "2.3.1";
65 static struct isc_softc *isc;
67 MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI driver");
68 MALLOC_DEFINE(M_ISCSIBUF, "iSCbuf", "iSCSI buffers");
69 static MALLOC_DEFINE(M_TMP, "iSCtmp", "iSCSI tmp");
71 #ifdef ISCSI_INITIATOR_DEBUG
72 int iscsi_debug = ISCSI_INITIATOR_DEBUG;
73 SYSCTL_INT(_debug, OID_AUTO, iscsi_initiator, CTLFLAG_RW, &iscsi_debug, 0,
74 "iSCSI driver debug flag");
76 struct mtx iscsi_dbg_mtx;
79 static int max_sessions = MAX_SESSIONS;
80 SYSCTL_INT(_net, OID_AUTO, iscsi_initiator_max_sessions, CTLFLAG_RDTUN, &max_sessions, MAX_SESSIONS,
81 "Max sessions allowed");
82 static int max_pdus = MAX_PDUS;
83 SYSCTL_INT(_net, OID_AUTO, iscsi_initiator_max_pdus, CTLFLAG_RDTUN, &max_pdus, MAX_PDUS,
86 static char isid[6+1] = {
96 static int i_create_session(struct cdev *dev, int *ndev);
98 static int i_ping(struct cdev *dev);
99 static int i_send(struct cdev *dev, caddr_t arg, struct thread *td);
100 static int i_recv(struct cdev *dev, caddr_t arg, struct thread *td);
101 static int i_setsoc(isc_session_t *sp, int fd, struct thread *td);
102 static int i_fullfeature(struct cdev *dev, int flag);
104 static d_open_t iscsi_open;
105 static d_close_t iscsi_close;
106 static d_ioctl_t iscsi_ioctl;
107 #ifdef ISCSI_INITIATOR_DEBUG
108 static d_read_t iscsi_read;
111 static struct cdevsw iscsi_cdevsw = {
112 .d_version = D_VERSION,
113 .d_open = iscsi_open,
114 .d_close = iscsi_close,
115 .d_ioctl = iscsi_ioctl,
116 #ifdef ISCSI_INITIATOR_DEBUG
117 .d_read = iscsi_read,
123 iscsi_open(struct cdev *dev, int flags, int otype, struct thread *td)
127 debug(7, "dev=%d", dev2unit(dev));
129 if(dev2unit(dev) > max_sessions) {
137 iscsi_close(struct cdev *dev, int flag, int otyp, struct thread *td)
143 debug(3, "session=%d flag=%x", dev2unit(dev), flag);
145 if(dev2unit(dev) == max_sessions) {
150 sdebug(3, "sp->flags=%x", sp->flags );
152 | if still in full phase, this probably means
153 | that something went realy bad.
154 | it could be a result from 'shutdown', in which case
155 | we will ignore it (so buffers can be flushed).
156 | the problem is that there is no way of differentiating
157 | between a shutdown procedure and 'iscontrol' dying.
159 if(sp->flags & ISC_FFPHASE)
160 // delay in case this is a shutdown.
161 tsleep(sp, PRIBIO, "isc-cls", 60*hz);
169 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
171 struct isc_softc *sc;
179 if(dev2unit(dev) == max_sessions) {
181 | non Session commands
189 error = i_create_session(dev, (int *)arg);
205 sdebug(6, "dev=%d cmd=%d", dev2unit(dev), (int)(cmd & 0xff));
209 error = i_setsoc(sp, *(u_int *)arg, td);
213 opt = (isc_opt_t *)arg;
214 error = i_setopt(sp, opt);
218 error = i_send(dev, arg, td);
222 error = i_recv(dev, arg, td);
230 error = sp->soc == NULL? ENOTCONN: i_fullfeature(dev, 1);
232 sp->proc = td->td_proc;
233 SYSCTL_ADD_INT(&sp->clist, SYSCTL_CHILDREN(sp->oid),
234 OID_AUTO, "pid", CTLFLAG_RD,
235 &sp->proc->p_pid, sizeof(pid_t), "control process id");
240 error = sp->soc == NULL? ENOTCONN: i_fullfeature(dev, 2);
244 error = i_fullfeature(dev, 0);
248 int sig = *(int *)arg;
250 if(sig < 0 || sig > _SIG_MAXSIG)
258 iscsi_cam_t *cp = (iscsi_cam_t *)arg;
260 error = ic_getCamVals(sp, cp);
272 iscsi_read(struct cdev *dev, struct uio *uio, int ioflag)
274 #ifdef ISCSI_INITIATOR_DEBUG
275 struct isc_softc *sc;
282 if(dev2unit(dev) == max_sessions) {
283 sprintf(buf, "/----- Session ------/\n");
284 uiomove(buf, strlen(buf), uio);
287 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
288 if(uio->uio_resid == 0)
290 sprintf(buf, "%03d] '%s' '%s'\n", i++, sp->opt.targetAddress, sp->opt.targetName);
291 uiomove(buf, strlen(buf), uio);
293 sprintf(buf, "free npdu_alloc=%d, npdu_max=%d\n", sc->npdu_alloc, sc->npdu_max);
294 uiomove(buf, strlen(buf), uio);
298 struct socket *so = sp->soc;
299 #define pukeit(i, pq) do {\
300 sprintf(buf, "%03d] %06x %02x %06x %06x %jd\n",\
301 i, ntohl(pq->pdu.ipdu.bhs.CmdSN),\
302 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
303 ntohl(pq->pdu.ipdu.bhs.ExpStSN),\
304 (intmax_t)pq->ts.sec);\
307 sprintf(buf, "%d/%d /---- hld -----/\n", sp->stats.nhld, sp->stats.max_hld);
308 uiomove(buf, strlen(buf), uio);
309 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
310 if(uio->uio_resid == 0)
313 uiomove(buf, strlen(buf), uio);
315 sprintf(buf, "%d/%d /---- rsp -----/\n", sp->stats.nrsp, sp->stats.max_rsp);
316 uiomove(buf, strlen(buf), uio);
318 TAILQ_FOREACH(pq, &sp->rsp, pq_link) {
319 if(uio->uio_resid == 0)
322 uiomove(buf, strlen(buf), uio);
324 sprintf(buf, "%d/%d /---- csnd -----/\n", sp->stats.ncsnd, sp->stats.max_csnd);
326 uiomove(buf, strlen(buf), uio);
327 TAILQ_FOREACH(pq, &sp->csnd, pq_link) {
328 if(uio->uio_resid == 0)
331 uiomove(buf, strlen(buf), uio);
333 sprintf(buf, "%d/%d /---- wsnd -----/\n", sp->stats.nwsnd, sp->stats.max_wsnd);
335 uiomove(buf, strlen(buf), uio);
336 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) {
337 if(uio->uio_resid == 0)
340 uiomove(buf, strlen(buf), uio);
342 sprintf(buf, "%d/%d /---- isnd -----/\n", sp->stats.nisnd, sp->stats.max_isnd);
344 uiomove(buf, strlen(buf), uio);
345 TAILQ_FOREACH(pq, &sp->isnd, pq_link) {
346 if(uio->uio_resid == 0)
349 uiomove(buf, strlen(buf), uio);
352 sprintf(buf, "/---- Stats ---/\n");
353 uiomove(buf, strlen(buf), uio);
355 sprintf(buf, "recv=%d sent=%d\n", sp->stats.nrecv, sp->stats.nsent);
356 uiomove(buf, strlen(buf), uio);
358 sprintf(buf, "flags=%x pdus: alloc=%d max=%d\n",
359 sp->flags, sc->npdu_alloc, sc->npdu_max);
360 uiomove(buf, strlen(buf), uio);
362 sprintf(buf, "cws=%d last cmd=%x exp=%x max=%x stat=%x itt=%x\n",
363 sp->cws, sp->sn.cmd, sp->sn.expCmd, sp->sn.maxCmd, sp->sn.stat, sp->sn.itt);
364 uiomove(buf, strlen(buf), uio);
366 sprintf(buf, "/---- socket -----/\nso_count=%d so_state=%x\n", so->so_count, so->so_state);
367 uiomove(buf, strlen(buf), uio);
375 i_ping(struct cdev *dev)
383 i_setsoc(isc_session_t *sp, int fd, struct thread *td)
389 isc_stop_receiver(sp);
391 error = fget(td, fd, cap_rights_init(&rights, CAP_SOCK_CLIENT), &sp->fp);
395 error = fgetsock(td, fd, cap_rights_init(&rights, CAP_SOCK_CLIENT),
399 isc_start_receiver(sp);
410 i_send(struct cdev *dev, caddr_t arg, struct thread *td)
412 isc_session_t *sp = dev->si_drv2;
423 if((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL)
426 pq->pdu = *(pdu_t *)arg;
427 if((error = i_prepPDU(sp, pq)) != 0)
431 if((pq->len - sizeof(union ipdu_u)) > 0) {
432 pq->buf = bp = malloc(pq->len - sizeof(union ipdu_u), M_ISCSIBUF, M_NOWAIT);
433 if(pq->buf == NULL) {
439 pq->buf = NULL; // just in case?
441 sdebug(2, "len=%d ahs_len=%d ds_len=%d buf=%zu@%p",
442 pq->len, pp->ahs_len, pp->ds_len, pq->len - sizeof(union ipdu_u), bp);
445 // XXX: never tested, looks suspicious
447 error = copyin(pp->ahs_addr, bp, n);
449 sdebug(3, "copyin ahs: error=%d", error);
452 pp->ahs_addr = (ahs_t *)bp;
457 error = copyin(pp->ds_addr, bp, n);
459 sdebug(3, "copyin ds: error=%d", error);
470 error = isc_qout(sp, pq);
472 wakeup(&sp->flags); // XXX: to 'push' proc_out ...
475 pdu_free(sp->isc, pq);
481 i_recv(struct cdev *dev, caddr_t arg, struct thread *td)
483 isc_session_t *sp = dev->si_drv2;
487 int error, mustfree, cnt;
488 size_t need, have, n;
497 cnt = 6; // XXX: maybe the user can request a time out?
498 mtx_lock(&sp->rsp_mtx);
499 while((pq = TAILQ_FIRST(&sp->rsp)) == NULL) {
500 msleep(&sp->rsp, &sp->rsp_mtx, PRIBIO, "isc_rsp", hz*10);
501 if(cnt-- == 0) break; // XXX: for now, needs work
505 TAILQ_REMOVE(&sp->rsp, pq, pq_link);
507 mtx_unlock(&sp->rsp_mtx);
509 sdebug(6, "cnt=%d", cnt);
513 sdebug(3, "error=%d sp->flags=%x ", error, sp->flags);
537 if(len > pq->mp->m_len) {
539 bp = malloc(len, M_TMP, M_WAITOK);
540 sdebug(4, "need mbufcopy: %d", len);
541 i_mbufcopy(pq->mp, bp, len);
544 bp = mtod(pq->mp, caddr_t);
548 n = MIN(up->ahs_size, need);
549 error = copyout(bp, (caddr_t)up->ahs_addr, n);
553 if(!error && pp->ds_len) {
555 if((have = up->ds_size) == 0) {
556 have = up->ahs_size - n;
557 up->ds_addr = (caddr_t)up->ahs_addr + n;
560 error = copyout(bp, (caddr_t)up->ds_addr, n);
568 sdebug(6, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
570 pdu_free(sp->isc, pq);
576 i_fullfeature(struct cdev *dev, int flag)
578 isc_session_t *sp = dev->si_drv2;
581 sdebug(2, "flag=%d", flag);
586 sp->flags &= ~ISC_FFPHASE;
589 sp->flags |= ISC_FFPHASE;
593 sp->flags |= ISC_FFPHASE;
601 i_create_session(struct cdev *dev, int *ndev)
603 struct isc_softc *sc = dev->si_drv1;
609 sp = malloc(sizeof(isc_session_t), M_ISCSI, M_WAITOK | M_ZERO);
613 sx_xlock(&sc->unit_sx);
614 if((n = alloc_unr(sc->unit)) < 0) {
615 sx_unlock(&sc->unit_sx);
617 xdebug("too many sessions!");
620 sx_unlock(&sc->unit_sx);
622 mtx_lock(&sc->isc_mtx);
623 TAILQ_INSERT_TAIL(&sc->isc_sess, sp, sp_link);
625 mtx_unlock(&sc->isc_mtx);
627 sp->dev = make_dev(&iscsi_cdevsw, n, UID_ROOT, GID_WHEEL, 0600, "iscsi%d", n);
630 sp->dev->si_drv1 = sc;
631 sp->dev->si_drv2 = sp;
633 sp->opt.maxRecvDataSegmentLength = 8192;
634 sp->opt.maxXmitDataSegmentLength = 8192;
635 sp->opt.maxBurstLength = 65536; // 64k
636 sp->opt.maxluns = ISCSI_MAX_LUNS;
638 error = ism_start(sp);
645 iscsi_counters(isc_session_t *sp)
650 #define _puke(i, pq) do {\
651 debug(2, "%03d] %06x %02x %x %ld %jd %x\n",\
652 i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
653 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
654 (long)pq->ts.sec, pq->ts.frac, pq->flags);\
658 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
662 TAILQ_FOREACH(pq, &sp->rsp, pq_link) r++;
663 TAILQ_FOREACH(pq, &sp->csnd, pq_link) s++;
664 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) s++;
665 TAILQ_FOREACH(pq, &sp->isnd, pq_link) s++;
666 debug(2, "hld=%d rsp=%d snd=%d", h, r, s);
671 iscsi_shutdown(void *v)
673 struct isc_softc *sc = v;
679 xdebug("sc is NULL!");
682 #ifdef DO_EVENTHANDLER
684 debug(2, "sc->eh is NULL");
686 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->eh);
687 debug(2, "done n=%d", sc->nsess);
691 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
692 debug(2, "%2d] sp->flags=0x%08x", n, sp->flags);
699 free_pdus(struct isc_softc *sc)
703 if(sc->pdu_zone != NULL) {
704 uma_zdestroy(sc->pdu_zone);
714 TUNABLE_INT_FETCH("net.iscsi_initiator.max_sessions", &max_sessions);
715 TUNABLE_INT_FETCH("net.iscsi_initiator.max_pdus", &max_pdus);
717 isc = malloc(sizeof(struct isc_softc), M_ISCSI, M_ZERO|M_WAITOK);
718 mtx_init(&isc->isc_mtx, "iscsi-isc", NULL, MTX_DEF);
720 TAILQ_INIT(&isc->isc_sess);
722 | now init the free pdu list
724 isc->pdu_zone = uma_zcreate("pdu", sizeof(pduq_t),
725 NULL, NULL, NULL, NULL,
727 uma_zone_set_max(isc->pdu_zone, max_pdus);
728 isc->unit = new_unrhdr(0, max_sessions-1, NULL);
729 sx_init(&isc->unit_sx, "iscsi sx");
731 #ifdef DO_EVENTHANDLER
732 if((isc->eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, iscsi_shutdown,
733 sc, SHUTDOWN_PRI_DEFAULT-1)) == NULL)
734 xdebug("shutdown event registration failed\n");
739 sysctl_ctx_init(&isc->clist);
740 isc->oid = SYSCTL_ADD_NODE(&isc->clist,
741 SYSCTL_STATIC_CHILDREN(_net),
748 SYSCTL_ADD_STRING(&isc->clist,
749 SYSCTL_CHILDREN(isc->oid),
753 iscsi_driver_version,
755 "iscsi driver version");
757 SYSCTL_ADD_STRING(&isc->clist,
758 SYSCTL_CHILDREN(isc->oid),
764 "initiator part of the Session Identifier");
766 SYSCTL_ADD_INT(&isc->clist,
767 SYSCTL_CHILDREN(isc->oid),
773 "number of active session");
775 #ifdef ISCSI_INITIATOR_DEBUG
776 mtx_init(&iscsi_dbg_mtx, "iscsi_dbg", NULL, MTX_DEF);
779 isc->dev = make_dev_credf(MAKEDEV_CHECKNAME, &iscsi_cdevsw, max_sessions,
780 NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi");
781 if (isc->dev == NULL) {
782 xdebug("iscsi_initiator: make_dev_credf failed");
785 isc->dev->si_drv1 = isc;
787 printf("iscsi: version %s\n", iscsi_driver_version);
793 | unload SHOULD fail if there is activity
794 | activity: there is/are active session/s
799 isc_session_t *sp, *sp_tmp;
804 | go through all the sessions
805 | Note: close should have done this ...
807 TAILQ_FOREACH_SAFE(sp, &isc->isc_sess, sp_link, sp_tmp) {
808 //XXX: check for activity ...
810 if(sp->cam_sim != NULL)
813 mtx_destroy(&isc->isc_mtx);
814 sx_destroy(&isc->unit_sx);
819 destroy_dev(isc->dev);
821 if(sysctl_ctx_free(&isc->clist))
822 xdebug("sysctl_ctx_free failed");
824 iscsi_shutdown(isc); // XXX: check EVENTHANDLER_ ...
826 #ifdef ISCSI_INITIATOR_DEBUG
827 mtx_destroy(&iscsi_dbg_mtx);
834 iscsi_modevent(module_t mod, int what, void *arg)
842 error = iscsi_start();
847 xdebug("iscsi module busy(nsess=%d), cannot unload", isc->nsess);
848 log(LOG_ERR, "iscsi module busy, cannot unload");
865 moduledata_t iscsi_mod = {
867 (modeventhand_t) iscsi_modevent,
876 nfs_setup_diskless();
877 if (nfs_diskless_valid)
878 rootdevnames[0] = "nfs:";
880 printf("** iscsi_rootconf **\n");
883 SYSINIT(cpu_rootconf1, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, iscsi_rootconf, NULL)
886 DECLARE_MODULE(iscsi_initiator, iscsi_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
887 MODULE_DEPEND(iscsi_initiator, cam, 1, 1, 1);