2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2011 Daniel Braniss <danny@cs.huji.ac.il>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 | $Id: iscsi.c 752 2009-08-20 11:23:28Z danny $
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_iscsi_initiator.h"
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #ifdef DO_EVENTHANDLER
41 #include <sys/eventhandler.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/ctype.h>
50 #include <sys/errno.h>
51 #include <sys/sysctl.h>
54 #include <sys/socketvar.h>
55 #include <sys/socket.h>
56 #include <sys/protosw.h>
58 #include <sys/ioccom.h>
59 #include <sys/queue.h>
60 #include <sys/kthread.h>
62 #include <sys/syslog.h>
66 #include <dev/iscsi_initiator/iscsi.h>
67 #include <dev/iscsi_initiator/iscsivar.h>
68 static char *iscsi_driver_version = "2.3.1";
70 static struct isc_softc *isc;
72 MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI driver");
73 MALLOC_DEFINE(M_ISCSIBUF, "iSCbuf", "iSCSI buffers");
74 static MALLOC_DEFINE(M_TMP, "iSCtmp", "iSCSI tmp");
76 #ifdef ISCSI_INITIATOR_DEBUG
77 int iscsi_debug = ISCSI_INITIATOR_DEBUG;
78 SYSCTL_INT(_debug, OID_AUTO, iscsi_initiator, CTLFLAG_RW, &iscsi_debug, 0,
79 "iSCSI driver debug flag");
81 struct mtx iscsi_dbg_mtx;
84 static int max_sessions = MAX_SESSIONS;
85 SYSCTL_INT(_net, OID_AUTO, iscsi_initiator_max_sessions, CTLFLAG_RDTUN,
86 &max_sessions, 0, "Max sessions allowed");
87 static int max_pdus = MAX_PDUS;
88 SYSCTL_INT(_net, OID_AUTO, iscsi_initiator_max_pdus, CTLFLAG_RDTUN,
89 &max_pdus, 0, "Max PDU pool");
91 static char isid[6+1] = {
101 static int i_create_session(struct cdev *dev, int *ndev);
103 static int i_ping(struct cdev *dev);
104 static int i_send(struct cdev *dev, caddr_t arg, struct thread *td);
105 static int i_recv(struct cdev *dev, caddr_t arg, struct thread *td);
106 static int i_setsoc(isc_session_t *sp, int fd, struct thread *td);
107 static int i_fullfeature(struct cdev *dev, int flag);
109 static d_open_t iscsi_open;
110 static d_close_t iscsi_close;
111 static d_ioctl_t iscsi_ioctl;
112 #ifdef ISCSI_INITIATOR_DEBUG
113 static d_read_t iscsi_read;
116 static struct cdevsw iscsi_cdevsw = {
117 .d_version = D_VERSION,
118 .d_open = iscsi_open,
119 .d_close = iscsi_close,
120 .d_ioctl = iscsi_ioctl,
121 #ifdef ISCSI_INITIATOR_DEBUG
122 .d_read = iscsi_read,
128 iscsi_open(struct cdev *dev, int flags, int otype, struct thread *td)
132 debug(7, "dev=%d", dev2unit(dev));
134 if(dev2unit(dev) > max_sessions) {
142 iscsi_close(struct cdev *dev, int flag, int otyp, struct thread *td)
148 debug(3, "session=%d flag=%x", dev2unit(dev), flag);
150 if(dev2unit(dev) == max_sessions) {
155 sdebug(3, "sp->flags=%x", sp->flags );
157 | if still in full phase, this probably means
158 | that something went really bad.
159 | it could be a result from 'shutdown', in which case
160 | we will ignore it (so buffers can be flushed).
161 | the problem is that there is no way of differentiating
162 | between a shutdown procedure and 'iscontrol' dying.
164 if(sp->flags & ISC_FFPHASE)
165 // delay in case this is a shutdown.
166 tsleep(sp, PRIBIO, "isc-cls", 60*hz);
174 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
176 struct isc_softc *sc;
184 if(dev2unit(dev) == max_sessions) {
186 | non Session commands
194 error = i_create_session(dev, (int *)arg);
210 sdebug(6, "dev=%d cmd=%d", dev2unit(dev), (int)(cmd & 0xff));
214 error = i_setsoc(sp, *(u_int *)arg, td);
218 opt = (isc_opt_t *)arg;
219 error = i_setopt(sp, opt);
223 error = i_send(dev, arg, td);
227 error = i_recv(dev, arg, td);
235 error = sp->soc == NULL? ENOTCONN: i_fullfeature(dev, 1);
237 sp->proc = td->td_proc;
238 SYSCTL_ADD_INT(&sp->clist, SYSCTL_CHILDREN(sp->oid),
239 OID_AUTO, "pid", CTLFLAG_RD,
240 &sp->proc->p_pid, sizeof(pid_t), "control process id");
245 error = sp->soc == NULL? ENOTCONN: i_fullfeature(dev, 2);
249 error = i_fullfeature(dev, 0);
253 int sig = *(int *)arg;
255 if(sig < 0 || sig > _SIG_MAXSIG)
263 iscsi_cam_t *cp = (iscsi_cam_t *)arg;
265 error = ic_getCamVals(sp, cp);
277 iscsi_read(struct cdev *dev, struct uio *uio, int ioflag)
279 #ifdef ISCSI_INITIATOR_DEBUG
280 struct isc_softc *sc;
287 if(dev2unit(dev) == max_sessions) {
288 sprintf(buf, "/----- Session ------/\n");
289 uiomove(buf, strlen(buf), uio);
292 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
293 if(uio->uio_resid == 0)
295 sprintf(buf, "%03d] '%s' '%s'\n", i++, sp->opt.targetAddress, sp->opt.targetName);
296 uiomove(buf, strlen(buf), uio);
298 sprintf(buf, "free npdu_alloc=%d, npdu_max=%d\n", sc->npdu_alloc, sc->npdu_max);
299 uiomove(buf, strlen(buf), uio);
303 struct socket *so = sp->soc;
304 #define pukeit(i, pq) do {\
305 sprintf(buf, "%03d] %06x %02x %06x %06x %jd\n",\
306 i, ntohl(pq->pdu.ipdu.bhs.CmdSN),\
307 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
308 ntohl(pq->pdu.ipdu.bhs.ExpStSN),\
309 (intmax_t)pq->ts.sec);\
312 sprintf(buf, "%d/%d /---- hld -----/\n", sp->stats.nhld, sp->stats.max_hld);
313 uiomove(buf, strlen(buf), uio);
314 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
315 if(uio->uio_resid == 0)
318 uiomove(buf, strlen(buf), uio);
320 sprintf(buf, "%d/%d /---- rsp -----/\n", sp->stats.nrsp, sp->stats.max_rsp);
321 uiomove(buf, strlen(buf), uio);
323 TAILQ_FOREACH(pq, &sp->rsp, pq_link) {
324 if(uio->uio_resid == 0)
327 uiomove(buf, strlen(buf), uio);
329 sprintf(buf, "%d/%d /---- csnd -----/\n", sp->stats.ncsnd, sp->stats.max_csnd);
331 uiomove(buf, strlen(buf), uio);
332 TAILQ_FOREACH(pq, &sp->csnd, pq_link) {
333 if(uio->uio_resid == 0)
336 uiomove(buf, strlen(buf), uio);
338 sprintf(buf, "%d/%d /---- wsnd -----/\n", sp->stats.nwsnd, sp->stats.max_wsnd);
340 uiomove(buf, strlen(buf), uio);
341 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) {
342 if(uio->uio_resid == 0)
345 uiomove(buf, strlen(buf), uio);
347 sprintf(buf, "%d/%d /---- isnd -----/\n", sp->stats.nisnd, sp->stats.max_isnd);
349 uiomove(buf, strlen(buf), uio);
350 TAILQ_FOREACH(pq, &sp->isnd, pq_link) {
351 if(uio->uio_resid == 0)
354 uiomove(buf, strlen(buf), uio);
357 sprintf(buf, "/---- Stats ---/\n");
358 uiomove(buf, strlen(buf), uio);
360 sprintf(buf, "recv=%d sent=%d\n", sp->stats.nrecv, sp->stats.nsent);
361 uiomove(buf, strlen(buf), uio);
363 sprintf(buf, "flags=%x pdus: alloc=%d max=%d\n",
364 sp->flags, sc->npdu_alloc, sc->npdu_max);
365 uiomove(buf, strlen(buf), uio);
367 sprintf(buf, "cws=%d last cmd=%x exp=%x max=%x stat=%x itt=%x\n",
368 sp->cws, sp->sn.cmd, sp->sn.expCmd, sp->sn.maxCmd, sp->sn.stat, sp->sn.itt);
369 uiomove(buf, strlen(buf), uio);
371 sprintf(buf, "/---- socket -----/\nso_count=%d so_state=%x\n", so->so_count, so->so_state);
372 uiomove(buf, strlen(buf), uio);
380 i_ping(struct cdev *dev)
388 i_setsoc(isc_session_t *sp, int fd, struct thread *td)
394 isc_stop_receiver(sp);
396 error = getsock_cap(td, fd, cap_rights_init(&rights, CAP_SOCK_CLIENT),
397 &sp->fp, NULL, NULL);
401 sp->soc = sp->fp->f_data;
403 isc_start_receiver(sp);
409 i_send(struct cdev *dev, caddr_t arg, struct thread *td)
411 isc_session_t *sp = dev->si_drv2;
422 if((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL)
425 pq->pdu = *(pdu_t *)arg;
426 if((error = i_prepPDU(sp, pq)) != 0)
430 if((pq->len - sizeof(union ipdu_u)) > 0) {
431 pq->buf = bp = malloc(pq->len - sizeof(union ipdu_u), M_ISCSIBUF, M_NOWAIT);
432 if(pq->buf == NULL) {
438 pq->buf = NULL; // just in case?
440 sdebug(2, "len=%d ahs_len=%d ds_len=%d buf=%zu@%p",
441 pq->len, pp->ahs_len, pp->ds_len, pq->len - sizeof(union ipdu_u), bp);
444 // XXX: never tested, looks suspicious
446 error = copyin(pp->ahs_addr, bp, n);
448 sdebug(3, "copyin ahs: error=%d", error);
451 pp->ahs_addr = (ahs_t *)bp;
456 error = copyin(pp->ds_addr, bp, n);
458 sdebug(3, "copyin ds: error=%d", error);
469 error = isc_qout(sp, pq);
471 wakeup(&sp->flags); // XXX: to 'push' proc_out ...
474 pdu_free(sp->isc, pq);
480 i_recv(struct cdev *dev, caddr_t arg, struct thread *td)
482 isc_session_t *sp = dev->si_drv2;
486 int error, mustfree, cnt;
487 size_t need, have, n;
496 cnt = 6; // XXX: maybe the user can request a time out?
497 mtx_lock(&sp->rsp_mtx);
498 while((pq = TAILQ_FIRST(&sp->rsp)) == NULL) {
499 msleep(&sp->rsp, &sp->rsp_mtx, PRIBIO, "isc_rsp", hz*10);
500 if(cnt-- == 0) break; // XXX: for now, needs work
504 TAILQ_REMOVE(&sp->rsp, pq, pq_link);
506 mtx_unlock(&sp->rsp_mtx);
508 sdebug(6, "cnt=%d", cnt);
512 sdebug(3, "error=%d sp->flags=%x ", error, sp->flags);
536 if(len > pq->mp->m_len) {
538 bp = malloc(len, M_TMP, M_WAITOK);
539 sdebug(4, "need mbufcopy: %d", len);
540 i_mbufcopy(pq->mp, bp, len);
543 bp = mtod(pq->mp, caddr_t);
547 n = MIN(up->ahs_size, need);
548 error = copyout(bp, (caddr_t)up->ahs_addr, n);
552 if(!error && pp->ds_len) {
554 if((have = up->ds_size) == 0) {
555 have = up->ahs_size - n;
556 up->ds_addr = (caddr_t)up->ahs_addr + n;
559 error = copyout(bp, (caddr_t)up->ds_addr, n);
567 sdebug(6, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
569 pdu_free(sp->isc, pq);
575 i_fullfeature(struct cdev *dev, int flag)
577 isc_session_t *sp = dev->si_drv2;
580 sdebug(2, "flag=%d", flag);
585 sp->flags &= ~ISC_FFPHASE;
588 sp->flags |= ISC_FFPHASE;
592 sp->flags |= ISC_FFPHASE;
600 i_create_session(struct cdev *dev, int *ndev)
602 struct isc_softc *sc = dev->si_drv1;
608 sp = malloc(sizeof(isc_session_t), M_ISCSI, M_WAITOK | M_ZERO);
612 sx_xlock(&sc->unit_sx);
613 if((n = alloc_unr(sc->unit)) < 0) {
614 sx_unlock(&sc->unit_sx);
616 xdebug("too many sessions!");
619 sx_unlock(&sc->unit_sx);
621 mtx_lock(&sc->isc_mtx);
622 TAILQ_INSERT_TAIL(&sc->isc_sess, sp, sp_link);
624 mtx_unlock(&sc->isc_mtx);
626 sp->dev = make_dev(&iscsi_cdevsw, n, UID_ROOT, GID_WHEEL, 0600, "iscsi%d", n);
629 sp->dev->si_drv1 = sc;
630 sp->dev->si_drv2 = sp;
632 sp->opt.maxRecvDataSegmentLength = 8192;
633 sp->opt.maxXmitDataSegmentLength = 8192;
634 sp->opt.maxBurstLength = 65536; // 64k
635 sp->opt.maxluns = ISCSI_MAX_LUNS;
637 error = ism_start(sp);
644 iscsi_counters(isc_session_t *sp)
649 #define _puke(i, pq) do {\
650 debug(2, "%03d] %06x %02x %x %ld %jd %x\n",\
651 i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
652 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
653 (long)pq->ts.sec, pq->ts.frac, pq->flags);\
657 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
661 TAILQ_FOREACH(pq, &sp->rsp, pq_link) r++;
662 TAILQ_FOREACH(pq, &sp->csnd, pq_link) s++;
663 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) s++;
664 TAILQ_FOREACH(pq, &sp->isnd, pq_link) s++;
665 debug(2, "hld=%d rsp=%d snd=%d", h, r, s);
670 iscsi_shutdown(void *v)
672 struct isc_softc *sc = v;
678 xdebug("sc is NULL!");
681 #ifdef DO_EVENTHANDLER
683 debug(2, "sc->eh is NULL");
685 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->eh);
686 debug(2, "done n=%d", sc->nsess);
690 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
691 debug(2, "%2d] sp->flags=0x%08x", n, sp->flags);
698 free_pdus(struct isc_softc *sc)
702 if(sc->pdu_zone != NULL) {
703 uma_zdestroy(sc->pdu_zone);
713 isc = malloc(sizeof(struct isc_softc), M_ISCSI, M_ZERO|M_WAITOK);
714 mtx_init(&isc->isc_mtx, "iscsi-isc", NULL, MTX_DEF);
716 TAILQ_INIT(&isc->isc_sess);
718 | now init the free pdu list
720 isc->pdu_zone = uma_zcreate("pdu", sizeof(pduq_t),
721 NULL, NULL, NULL, NULL,
723 uma_zone_set_max(isc->pdu_zone, max_pdus);
724 isc->unit = new_unrhdr(0, max_sessions-1, NULL);
725 sx_init(&isc->unit_sx, "iscsi sx");
727 #ifdef DO_EVENTHANDLER
728 if((isc->eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, iscsi_shutdown,
729 sc, SHUTDOWN_PRI_DEFAULT-1)) == NULL)
730 xdebug("shutdown event registration failed\n");
735 sysctl_ctx_init(&isc->clist);
736 isc->oid = SYSCTL_ADD_NODE(&isc->clist,
737 SYSCTL_STATIC_CHILDREN(_net),
744 SYSCTL_ADD_STRING(&isc->clist,
745 SYSCTL_CHILDREN(isc->oid),
749 iscsi_driver_version,
751 "iscsi driver version");
753 SYSCTL_ADD_STRING(&isc->clist,
754 SYSCTL_CHILDREN(isc->oid),
760 "initiator part of the Session Identifier");
762 SYSCTL_ADD_INT(&isc->clist,
763 SYSCTL_CHILDREN(isc->oid),
769 "number of active session");
771 #ifdef ISCSI_INITIATOR_DEBUG
772 mtx_init(&iscsi_dbg_mtx, "iscsi_dbg", NULL, MTX_DEF);
775 isc->dev = make_dev_credf(MAKEDEV_CHECKNAME, &iscsi_cdevsw, max_sessions,
776 NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi");
777 if (isc->dev == NULL) {
778 xdebug("iscsi_initiator: make_dev_credf failed");
781 isc->dev->si_drv1 = isc;
783 printf("iscsi: version %s\n", iscsi_driver_version);
789 | unload SHOULD fail if there is activity
790 | activity: there is/are active session/s
795 isc_session_t *sp, *sp_tmp;
800 | go through all the sessions
801 | Note: close should have done this ...
803 TAILQ_FOREACH_SAFE(sp, &isc->isc_sess, sp_link, sp_tmp) {
804 //XXX: check for activity ...
807 mtx_destroy(&isc->isc_mtx);
808 sx_destroy(&isc->unit_sx);
813 destroy_dev(isc->dev);
815 if(sysctl_ctx_free(&isc->clist))
816 xdebug("sysctl_ctx_free failed");
818 iscsi_shutdown(isc); // XXX: check EVENTHANDLER_ ...
820 #ifdef ISCSI_INITIATOR_DEBUG
821 mtx_destroy(&iscsi_dbg_mtx);
828 iscsi_modevent(module_t mod, int what, void *arg)
836 error = iscsi_start();
841 xdebug("iscsi module busy(nsess=%d), cannot unload", isc->nsess);
842 log(LOG_ERR, "iscsi module busy, cannot unload");
859 moduledata_t iscsi_mod = {
861 (modeventhand_t) iscsi_modevent,
870 nfs_setup_diskless();
871 if (nfs_diskless_valid)
872 rootdevnames[0] = "nfs:";
874 printf("** iscsi_rootconf **\n");
877 SYSINIT(cpu_rootconf1, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, iscsi_rootconf, NULL)
880 DECLARE_MODULE(iscsi_initiator, iscsi_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
881 MODULE_DEPEND(iscsi_initiator, cam, 1, 1, 1);