2013-09-14 15:29:06 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2012 The FreeBSD Foundation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Edward Tomasz Napierala under sponsorship
|
|
|
|
* from the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* iSCSI Common Layer. It's used by both the initiator and target to send
|
|
|
|
* and receive iSCSI PDUs.
|
|
|
|
*/
|
|
|
|
|
2014-08-21 15:32:38 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2013-09-14 15:29:06 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/condvar.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/lock.h>
|
2015-01-31 07:49:50 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
2013-09-14 15:29:06 +00:00
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/module.h>
|
2015-01-31 07:49:50 +00:00
|
|
|
#include <sys/queue.h>
|
2016-02-10 19:01:26 +00:00
|
|
|
#include <sys/sbuf.h>
|
2016-05-17 11:10:44 +00:00
|
|
|
#include <sys/socket.h>
|
2013-09-14 15:29:06 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sx.h>
|
|
|
|
|
2014-08-21 16:08:17 +00:00
|
|
|
#include <dev/iscsi/icl.h>
|
2015-01-31 07:49:50 +00:00
|
|
|
#include <icl_conn_if.h>
|
|
|
|
|
|
|
|
struct icl_module {
|
|
|
|
TAILQ_ENTRY(icl_module) im_next;
|
|
|
|
char *im_name;
|
2016-05-24 08:44:45 +00:00
|
|
|
bool im_iser;
|
2015-01-31 07:49:50 +00:00
|
|
|
int im_priority;
|
2016-08-25 05:22:53 +00:00
|
|
|
int (*im_limits)(struct icl_drv_limits *idl);
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_conn *(*im_new_conn)(const char *name,
|
|
|
|
struct mtx *lock);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct icl_softc {
|
|
|
|
struct sx sc_lock;
|
|
|
|
TAILQ_HEAD(, icl_module) sc_modules;
|
|
|
|
};
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2016-05-24 08:54:41 +00:00
|
|
|
static int sysctl_kern_icl_offloads(SYSCTL_HANDLER_ARGS);
|
2016-02-10 19:01:26 +00:00
|
|
|
static MALLOC_DEFINE(M_ICL, "icl", "iSCSI Common Layer");
|
|
|
|
static struct icl_softc *sc;
|
|
|
|
|
2013-09-14 15:29:06 +00:00
|
|
|
SYSCTL_NODE(_kern, OID_AUTO, icl, CTLFLAG_RD, 0, "iSCSI Common Layer");
|
2015-01-31 07:49:50 +00:00
|
|
|
int icl_debug = 1;
|
2014-04-04 08:48:55 +00:00
|
|
|
SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN,
|
2015-01-31 07:49:50 +00:00
|
|
|
&icl_debug, 0, "Enable debug messages");
|
2016-05-24 08:54:41 +00:00
|
|
|
SYSCTL_PROC(_kern_icl, OID_AUTO, offloads,
|
2016-02-10 19:01:26 +00:00
|
|
|
CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
|
2016-05-24 14:34:36 +00:00
|
|
|
NULL, false, sysctl_kern_icl_offloads, "A",
|
2016-05-24 08:54:41 +00:00
|
|
|
"List of ICL modules");
|
2016-05-24 14:34:36 +00:00
|
|
|
SYSCTL_PROC(_kern_icl, OID_AUTO, iser_offloads,
|
|
|
|
CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
|
|
|
|
NULL, true, sysctl_kern_icl_offloads, "A",
|
|
|
|
"List of iSER ICL modules");
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2016-02-10 19:01:26 +00:00
|
|
|
static int
|
2016-05-24 08:54:41 +00:00
|
|
|
sysctl_kern_icl_offloads(SYSCTL_HANDLER_ARGS)
|
2016-02-10 19:01:26 +00:00
|
|
|
{
|
|
|
|
const struct icl_module *im;
|
|
|
|
struct sbuf sb;
|
2016-05-24 14:34:36 +00:00
|
|
|
bool iser = arg2;
|
2016-02-10 19:01:26 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
|
|
|
|
|
|
|
|
sx_slock(&sc->sc_lock);
|
|
|
|
TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
|
2016-05-24 14:34:36 +00:00
|
|
|
if (im->im_iser != iser)
|
|
|
|
continue;
|
2016-02-10 19:01:26 +00:00
|
|
|
if (im != TAILQ_FIRST(&sc->sc_modules))
|
|
|
|
sbuf_putc(&sb, ' ');
|
|
|
|
sbuf_printf(&sb, "%s", im->im_name);
|
|
|
|
}
|
|
|
|
sx_sunlock(&sc->sc_lock);
|
|
|
|
|
|
|
|
error = sbuf_finish(&sb);
|
|
|
|
if (error == 0)
|
|
|
|
error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
|
|
|
|
sbuf_delete(&sb);
|
|
|
|
return (error);
|
|
|
|
}
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
static struct icl_module *
|
2016-05-24 08:44:45 +00:00
|
|
|
icl_find(const char *name, bool iser, bool quiet)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_module *im, *im_max;
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_assert(&sc->sc_lock, SA_LOCKED);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
|
|
|
/*
|
2015-01-31 07:49:50 +00:00
|
|
|
* If the name was not specified, pick a module with highest
|
|
|
|
* priority.
|
2013-09-14 15:29:06 +00:00
|
|
|
*/
|
2015-01-31 07:49:50 +00:00
|
|
|
if (name == NULL || name[0] == '\0') {
|
2016-05-24 08:44:45 +00:00
|
|
|
im_max = NULL;
|
2015-01-31 07:49:50 +00:00
|
|
|
TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
|
2016-05-24 08:44:45 +00:00
|
|
|
if (im->im_iser != iser)
|
|
|
|
continue;
|
|
|
|
if (im_max == NULL ||
|
|
|
|
im->im_priority > im_max->im_priority)
|
2015-01-31 07:49:50 +00:00
|
|
|
im_max = im;
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 08:44:45 +00:00
|
|
|
if (iser && im_max == NULL && !quiet)
|
|
|
|
ICL_WARN("no iSER-capable offload found");
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
return (im_max);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
|
2016-05-24 08:44:45 +00:00
|
|
|
if (strcasecmp(im->im_name, name) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!im->im_iser && iser && !quiet) {
|
|
|
|
ICL_WARN("offload \"%s\" is not iSER-capable", name);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (im->im_iser && !iser && !quiet) {
|
|
|
|
ICL_WARN("offload \"%s\" is iSER-only", name);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (im);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 08:44:45 +00:00
|
|
|
if (!quiet)
|
|
|
|
ICL_WARN("offload \"%s\" not found", name);
|
|
|
|
|
2013-09-14 15:29:06 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_conn *
|
2016-05-24 08:44:45 +00:00
|
|
|
icl_new_conn(const char *offload, bool iser, const char *name, struct mtx *lock)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_module *im;
|
2013-09-14 15:29:06 +00:00
|
|
|
struct icl_conn *ic;
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_slock(&sc->sc_lock);
|
2016-05-24 08:44:45 +00:00
|
|
|
im = icl_find(offload, iser, false);
|
2015-01-31 07:49:50 +00:00
|
|
|
if (im == NULL) {
|
|
|
|
sx_sunlock(&sc->sc_lock);
|
|
|
|
return (NULL);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
ic = im->im_new_conn(name, lock);
|
|
|
|
sx_sunlock(&sc->sc_lock);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
return (ic);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
int
|
2016-08-25 05:22:53 +00:00
|
|
|
icl_limits(const char *offload, bool iser, struct icl_drv_limits *idl)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_module *im;
|
|
|
|
int error;
|
2014-04-04 15:49:37 +00:00
|
|
|
|
2016-08-25 05:22:53 +00:00
|
|
|
bzero(idl, sizeof(*idl));
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_slock(&sc->sc_lock);
|
2016-05-24 08:44:45 +00:00
|
|
|
im = icl_find(offload, iser, false);
|
2015-01-31 07:49:50 +00:00
|
|
|
if (im == NULL) {
|
|
|
|
sx_sunlock(&sc->sc_lock);
|
|
|
|
return (ENXIO);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
2013-10-24 15:54:06 +00:00
|
|
|
|
2016-08-25 05:22:53 +00:00
|
|
|
error = im->im_limits(idl);
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_sunlock(&sc->sc_lock);
|
2014-04-04 15:49:37 +00:00
|
|
|
|
2016-08-25 05:22:53 +00:00
|
|
|
/*
|
|
|
|
* Validate the limits provided by the driver against values allowed by
|
|
|
|
* the iSCSI RFC. 0 means iscsid/ctld should pick a reasonable value.
|
|
|
|
*
|
|
|
|
* Note that max_send_dsl is an internal implementation detail and not
|
|
|
|
* part of the RFC.
|
|
|
|
*/
|
|
|
|
#define OUT_OF_RANGE(x, lo, hi) ((x) != 0 && ((x) < (lo) || (x) > (hi)))
|
|
|
|
if (error == 0 &&
|
|
|
|
(OUT_OF_RANGE(idl->idl_max_recv_data_segment_length, 512, 16777215) ||
|
|
|
|
OUT_OF_RANGE(idl->idl_max_send_data_segment_length, 512, 16777215) ||
|
|
|
|
OUT_OF_RANGE(idl->idl_max_burst_length, 512, 16777215) ||
|
|
|
|
OUT_OF_RANGE(idl->idl_first_burst_length, 512, 16777215))) {
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
#undef OUT_OF_RANGE
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If both first_burst and max_burst are provided then first_burst must
|
|
|
|
* not exceed max_burst.
|
|
|
|
*/
|
|
|
|
if (error == 0 && idl->idl_first_burst_length > 0 &&
|
|
|
|
idl->idl_max_burst_length > 0 &&
|
|
|
|
idl->idl_first_burst_length > idl->idl_max_burst_length) {
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
return (error);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-08-25 05:22:53 +00:00
|
|
|
icl_register(const char *offload, bool iser, int priority,
|
|
|
|
int (*limits)(struct icl_drv_limits *),
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_conn *(*new_conn)(const char *, struct mtx *))
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_module *im;
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_xlock(&sc->sc_lock);
|
2016-05-24 08:44:45 +00:00
|
|
|
im = icl_find(offload, iser, true);
|
2015-01-31 07:49:50 +00:00
|
|
|
if (im != NULL) {
|
|
|
|
ICL_WARN("offload \"%s\" already registered", offload);
|
|
|
|
sx_xunlock(&sc->sc_lock);
|
|
|
|
return (EBUSY);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
im = malloc(sizeof(*im), M_ICL, M_ZERO | M_WAITOK);
|
|
|
|
im->im_name = strdup(offload, M_ICL);
|
2016-05-24 08:44:45 +00:00
|
|
|
im->im_iser = iser;
|
2015-01-31 07:49:50 +00:00
|
|
|
im->im_priority = priority;
|
|
|
|
im->im_limits = limits;
|
|
|
|
im->im_new_conn = new_conn;
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
TAILQ_INSERT_HEAD(&sc->sc_modules, im, im_next);
|
|
|
|
sx_xunlock(&sc->sc_lock);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
ICL_DEBUG("offload \"%s\" registered", offload);
|
2013-09-14 15:29:06 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
int
|
2016-05-24 08:44:45 +00:00
|
|
|
icl_unregister(const char *offload, bool rdma)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_module *im;
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_xlock(&sc->sc_lock);
|
2016-05-24 08:44:45 +00:00
|
|
|
im = icl_find(offload, rdma, true);
|
2015-01-31 07:49:50 +00:00
|
|
|
if (im == NULL) {
|
|
|
|
ICL_WARN("offload \"%s\" not registered", offload);
|
|
|
|
sx_xunlock(&sc->sc_lock);
|
|
|
|
return (ENXIO);
|
2014-04-04 15:49:37 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
TAILQ_REMOVE(&sc->sc_modules, im, im_next);
|
|
|
|
sx_xunlock(&sc->sc_lock);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
free(im->im_name, M_ICL);
|
|
|
|
free(im, M_ICL);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
ICL_DEBUG("offload \"%s\" unregistered", offload);
|
|
|
|
return (0);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-01-31 07:49:50 +00:00
|
|
|
icl_load(void)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sc = malloc(sizeof(*sc), M_ICL, M_ZERO | M_WAITOK);
|
|
|
|
sx_init(&sc->sc_lock, "icl");
|
|
|
|
TAILQ_INIT(&sc->sc_modules);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
icl_unload(void)
|
|
|
|
{
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_slock(&sc->sc_lock);
|
|
|
|
KASSERT(TAILQ_EMPTY(&sc->sc_modules), ("still have modules"));
|
|
|
|
sx_sunlock(&sc->sc_lock);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_destroy(&sc->sc_lock);
|
|
|
|
free(sc, M_ICL);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
icl_modevent(module_t mod, int what, void *arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (what) {
|
|
|
|
case MOD_LOAD:
|
2015-01-31 07:49:50 +00:00
|
|
|
return (icl_load());
|
2013-09-14 15:29:06 +00:00
|
|
|
case MOD_UNLOAD:
|
|
|
|
return (icl_unload());
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moduledata_t icl_data = {
|
|
|
|
"icl",
|
|
|
|
icl_modevent,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST);
|
|
|
|
MODULE_VERSION(icl, 1);
|