bdev_ftl: Added FTL bdev module
This patch adds FTL bdev. RPC scripts have been updated to allow for creation and removal of FTL bdevs. Change-Id: I82a5c5033b65bbeb67c238cae969a68cff767dcc Signed-off-by: Jakub Radtke <jakub.radtke@intel.com> Signed-off-by: Wojciech Malikowski <wojciech.malikowski@intel.com> Signed-off-by: Konrad Sztyber <konrad.sztyber@intel.com> Signed-off-by: Mateusz Kozlowski <mateusz.kozlowski@intel.com> Reviewed-on: https://review.gerrithub.io/c/431329 Reviewed-by: Jim Harris <james.r.harris@intel.com> Reviewed-by: Ben Walker <benjamin.walker@intel.com> Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
This commit is contained in:
parent
363e48d970
commit
7c233adeff
@ -294,6 +294,8 @@ Example response:
|
||||
"construct_null_bdev",
|
||||
"delete_malloc_bdev",
|
||||
"construct_malloc_bdev",
|
||||
"delete_ftl_bdev",
|
||||
"construct_ftl_bdev",
|
||||
"get_lvol_stores",
|
||||
"destroy_lvol_bdev",
|
||||
"resize_lvol_bdev",
|
||||
@ -1501,6 +1503,95 @@ Example response:
|
||||
}
|
||||
~~~
|
||||
|
||||
## construct_ftl_bdev {#rpc_construct_ftl_bdev}
|
||||
|
||||
Create FTL bdev.
|
||||
|
||||
This RPC is subject to change.
|
||||
|
||||
### Parameters
|
||||
|
||||
Name | Optional | Type | Description
|
||||
----------------------- | -------- | ----------- | -----------
|
||||
name | Required | string | Bdev name
|
||||
trtype | Required | string | Transport type
|
||||
traddr | Required | string | NVMe target address
|
||||
punits | Required | string | Parallel unit range in the form of start-end e.g 4-8
|
||||
uuid | Optional | string | UUID of restored bdev (not applicable when creating new instance)
|
||||
|
||||
### Result
|
||||
|
||||
Name of newly created bdev.
|
||||
|
||||
### Example
|
||||
|
||||
Example request:
|
||||
|
||||
~~~
|
||||
{
|
||||
"params": {
|
||||
"name": "nvme0"
|
||||
"trtype" "pcie"
|
||||
"traddr": "0000:00:04.0"
|
||||
"punits": "0-3"
|
||||
"uuid": "4a7481ce-786f-41a0-9b86-8f7465c8f4d3"
|
||||
},
|
||||
"jsonrpc": "2.0",
|
||||
"method": "construct_ftl_bdev",
|
||||
"id": 1
|
||||
}
|
||||
~~~
|
||||
|
||||
Example response:
|
||||
|
||||
~~~
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"result": {
|
||||
"name" : "nvme0"
|
||||
"uuid" : "4a7481ce-786f-41a0-9b86-8f7465c8f4d3"
|
||||
}
|
||||
}
|
||||
~~~
|
||||
|
||||
## delete_ftl_bdev {#rpc_delete_ftl_bdev}
|
||||
|
||||
Delete FTL bdev.
|
||||
|
||||
This RPC is subject to change.
|
||||
|
||||
### Parameters
|
||||
|
||||
Name | Optional | Type | Description
|
||||
----------------------- | -------- | ----------- | -----------
|
||||
name | Required | string | Bdev name
|
||||
|
||||
### Example
|
||||
|
||||
Example request:
|
||||
|
||||
~~~
|
||||
{
|
||||
"params": {
|
||||
"name": "nvme0"
|
||||
},
|
||||
"jsonrpc": "2.0",
|
||||
"method": "delete_ftl_bdev",
|
||||
"id": 1
|
||||
}
|
||||
~~~
|
||||
|
||||
Example response:
|
||||
|
||||
~~~
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"result": true
|
||||
}
|
||||
~~~
|
||||
|
||||
|
||||
## create_pmem_pool {#rpc_create_pmem_pool}
|
||||
|
||||
|
@ -56,5 +56,6 @@ DIRS-$(CONFIG_PMDK) += pmem
|
||||
endif
|
||||
|
||||
DIRS-$(CONFIG_RBD) += rbd
|
||||
DIRS-$(CONFIG_FTL) += ftl
|
||||
|
||||
include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk
|
||||
|
40
lib/bdev/ftl/Makefile
Normal file
40
lib/bdev/ftl/Makefile
Normal file
@ -0,0 +1,40 @@
|
||||
#
|
||||
# BSD LICENSE
|
||||
#
|
||||
# Copyright (c) Intel Corporation.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * 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.
|
||||
# * Neither the name of Intel Corporation nor the names of its
|
||||
# contributors may be used to endorse or promote products derived
|
||||
# from this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
|
||||
# OWNER 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.
|
||||
#
|
||||
|
||||
SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../..)
|
||||
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
|
||||
|
||||
C_SRCS = bdev_ftl.c bdev_ftl_rpc.c
|
||||
LIBNAME = bdev_ftl
|
||||
|
||||
include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk
|
928
lib/bdev/ftl/bdev_ftl.c
Normal file
928
lib/bdev/ftl/bdev_ftl.c
Normal file
@ -0,0 +1,928 @@
|
||||
/*-
|
||||
* BSD LICENSE
|
||||
*
|
||||
* Copyright (c) Intel Corporation.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * 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.
|
||||
* * Neither the name of Intel Corporation nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
|
||||
* OWNER 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.
|
||||
*/
|
||||
|
||||
#include "spdk/stdinc.h"
|
||||
#include "spdk/bdev.h"
|
||||
#include "spdk/conf.h"
|
||||
#include "spdk/env.h"
|
||||
#include "spdk/io_channel.h"
|
||||
#include "spdk/json.h"
|
||||
#include "spdk/string.h"
|
||||
#include "spdk/likely.h"
|
||||
#include "spdk/util.h"
|
||||
#include "spdk/ftl.h"
|
||||
#include "spdk_internal/log.h"
|
||||
|
||||
#include "bdev_ftl.h"
|
||||
|
||||
#define FTL_COMPLETION_RING_SIZE 4096
|
||||
|
||||
struct ftl_bdev_ctrlr {
|
||||
struct spdk_nvme_ctrlr *ctrlr;
|
||||
|
||||
struct spdk_nvme_transport_id trid;
|
||||
|
||||
size_t ref_cnt;
|
||||
|
||||
LIST_ENTRY(ftl_bdev_ctrlr) list_entry;
|
||||
};
|
||||
|
||||
struct ftl_bdev {
|
||||
struct spdk_bdev bdev;
|
||||
|
||||
struct ftl_bdev_ctrlr *ctrlr;
|
||||
|
||||
struct spdk_ftl_dev *dev;
|
||||
|
||||
ftl_bdev_init_fn init_cb;
|
||||
|
||||
void *init_arg;
|
||||
|
||||
LIST_ENTRY(ftl_bdev) list_entry;
|
||||
};
|
||||
|
||||
struct ftl_io_channel {
|
||||
struct spdk_ftl_dev *dev;
|
||||
|
||||
struct spdk_poller *poller;
|
||||
|
||||
#define FTL_MAX_COMPLETIONS 64
|
||||
struct ftl_bdev_io *io[FTL_MAX_COMPLETIONS];
|
||||
|
||||
/* Completion ring */
|
||||
struct spdk_ring *ring;
|
||||
|
||||
struct spdk_io_channel *ioch;
|
||||
};
|
||||
|
||||
struct ftl_bdev_io {
|
||||
struct ftl_bdev *bdev;
|
||||
|
||||
struct spdk_ring *ring;
|
||||
|
||||
int status;
|
||||
|
||||
struct spdk_thread *orig_thread;
|
||||
};
|
||||
|
||||
typedef void (*bdev_ftl_finish_fn)(void);
|
||||
|
||||
static LIST_HEAD(, ftl_bdev) g_ftl_bdevs = LIST_HEAD_INITIALIZER(g_ftl_bdevs);
|
||||
static LIST_HEAD(, ftl_bdev_ctrlr) g_ftl_bdev_ctrlrs =
|
||||
LIST_HEAD_INITIALIZER(g_ftl_bdev_ctrlrs);
|
||||
static bdev_ftl_finish_fn g_finish_cb;
|
||||
static size_t g_num_conf_bdevs;
|
||||
static size_t g_num_init_bdevs;
|
||||
static pthread_mutex_t g_ftl_bdev_lock;
|
||||
|
||||
static int bdev_ftl_initialize(void);
|
||||
static void bdev_ftl_finish(void);
|
||||
static void bdev_ftl_get_spdk_running_config(FILE *fp);
|
||||
|
||||
static int
|
||||
bdev_ftl_get_ctx_size(void)
|
||||
{
|
||||
return sizeof(struct ftl_bdev_io);
|
||||
}
|
||||
|
||||
static struct spdk_bdev_module g_ftl_if = {
|
||||
.name = "ftl",
|
||||
.async_init = true,
|
||||
.async_fini = true,
|
||||
.module_init = bdev_ftl_initialize,
|
||||
.module_fini = bdev_ftl_finish,
|
||||
/* TODO: Replace config_text with config_json */
|
||||
.config_text = bdev_ftl_get_spdk_running_config,
|
||||
.get_ctx_size = bdev_ftl_get_ctx_size,
|
||||
};
|
||||
|
||||
SPDK_BDEV_MODULE_REGISTER(&g_ftl_if)
|
||||
|
||||
static struct ftl_bdev_ctrlr *
|
||||
bdev_ftl_ctrlr_find(const struct spdk_nvme_transport_id *trid)
|
||||
{
|
||||
struct ftl_bdev_ctrlr *ftl_ctrlr = NULL;
|
||||
|
||||
LIST_FOREACH(ftl_ctrlr, &g_ftl_bdev_ctrlrs, list_entry) {
|
||||
if (!spdk_nvme_transport_id_compare(&ftl_ctrlr->trid, trid)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ftl_ctrlr;
|
||||
}
|
||||
|
||||
static struct ftl_bdev_ctrlr *
|
||||
bdev_ftl_add_ctrlr(struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_transport_id *trid)
|
||||
{
|
||||
struct ftl_bdev_ctrlr *ftl_ctrlr = NULL;
|
||||
|
||||
pthread_mutex_lock(&g_ftl_bdev_lock);
|
||||
|
||||
ftl_ctrlr = bdev_ftl_ctrlr_find(trid);
|
||||
if (ftl_ctrlr) {
|
||||
ftl_ctrlr->ref_cnt++;
|
||||
} else {
|
||||
ftl_ctrlr = calloc(1, sizeof(*ftl_ctrlr));
|
||||
if (!ftl_ctrlr) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
ftl_ctrlr->ctrlr = ctrlr;
|
||||
ftl_ctrlr->trid = *trid;
|
||||
ftl_ctrlr->ref_cnt = 1;
|
||||
|
||||
LIST_INSERT_HEAD(&g_ftl_bdev_ctrlrs, ftl_ctrlr, list_entry);
|
||||
}
|
||||
out:
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
return ftl_ctrlr;
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_remove_ctrlr(struct ftl_bdev_ctrlr *ctrlr)
|
||||
{
|
||||
pthread_mutex_lock(&g_ftl_bdev_lock);
|
||||
|
||||
if (--ctrlr->ref_cnt == 0) {
|
||||
if (spdk_nvme_detach(ctrlr->ctrlr)) {
|
||||
SPDK_ERRLOG("Failed to detach the controller\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
LIST_REMOVE(ctrlr, list_entry);
|
||||
free(ctrlr);
|
||||
}
|
||||
out:
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_free_cb(void *ctx, int status)
|
||||
{
|
||||
struct ftl_bdev *ftl_bdev = ctx;
|
||||
bool finish_done;
|
||||
|
||||
pthread_mutex_lock(&g_ftl_bdev_lock);
|
||||
LIST_REMOVE(ftl_bdev, list_entry);
|
||||
finish_done = LIST_EMPTY(&g_ftl_bdevs);
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
|
||||
spdk_io_device_unregister(ftl_bdev, NULL);
|
||||
|
||||
bdev_ftl_remove_ctrlr(ftl_bdev->ctrlr);
|
||||
|
||||
spdk_bdev_destruct_done(&ftl_bdev->bdev, status);
|
||||
free(ftl_bdev->bdev.name);
|
||||
free(ftl_bdev);
|
||||
|
||||
if (finish_done && g_finish_cb) {
|
||||
g_finish_cb();
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
bdev_ftl_destruct(void *ctx)
|
||||
{
|
||||
struct ftl_bdev *ftl_bdev = ctx;
|
||||
spdk_ftl_dev_free(ftl_bdev->dev, bdev_ftl_free_cb, ftl_bdev);
|
||||
|
||||
/* return 1 to indicate that the destruction is asynchronous */
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_complete_io(struct ftl_bdev_io *io, int rc)
|
||||
{
|
||||
enum spdk_bdev_io_status status;
|
||||
|
||||
switch (rc) {
|
||||
case 0:
|
||||
status = SPDK_BDEV_IO_STATUS_SUCCESS;
|
||||
break;
|
||||
case -ENOMEM:
|
||||
status = SPDK_BDEV_IO_STATUS_NOMEM;
|
||||
break;
|
||||
default:
|
||||
status = SPDK_BDEV_IO_STATUS_FAILED;
|
||||
break;
|
||||
}
|
||||
|
||||
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(io), status);
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_cb(void *arg, int status)
|
||||
{
|
||||
struct ftl_bdev_io *io = arg;
|
||||
size_t cnt __attribute__((unused));
|
||||
|
||||
io->status = status;
|
||||
|
||||
cnt = spdk_ring_enqueue(io->ring, (void **)&io, 1);
|
||||
assert(cnt == 1);
|
||||
}
|
||||
|
||||
static int
|
||||
bdev_ftl_fill_bio(struct ftl_bdev *ftl_bdev, struct spdk_io_channel *ch,
|
||||
struct ftl_bdev_io *io)
|
||||
{
|
||||
struct ftl_io_channel *ioch = spdk_io_channel_get_ctx(ch);
|
||||
|
||||
memset(io, 0, sizeof(*io));
|
||||
|
||||
io->orig_thread = spdk_io_channel_get_thread(ch);
|
||||
io->status = SPDK_BDEV_IO_STATUS_SUCCESS;
|
||||
io->ring = ioch->ring;
|
||||
io->bdev = ftl_bdev;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
bdev_ftl_readv(struct ftl_bdev *ftl_bdev, struct spdk_io_channel *ch,
|
||||
struct ftl_bdev_io *io)
|
||||
{
|
||||
struct spdk_bdev_io *bio;
|
||||
struct ftl_io_channel *ioch = spdk_io_channel_get_ctx(ch);
|
||||
int rc;
|
||||
|
||||
bio = spdk_bdev_io_from_ctx(io);
|
||||
|
||||
rc = bdev_ftl_fill_bio(ftl_bdev, ch, io);
|
||||
if (rc) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
return spdk_ftl_read(ftl_bdev->dev,
|
||||
ioch->ioch,
|
||||
bio->u.bdev.offset_blocks,
|
||||
bio->u.bdev.num_blocks,
|
||||
bio->u.bdev.iovs, bio->u.bdev.iovcnt, bdev_ftl_cb, io);
|
||||
}
|
||||
|
||||
static int
|
||||
bdev_ftl_writev(struct ftl_bdev *ftl_bdev, struct spdk_io_channel *ch,
|
||||
struct ftl_bdev_io *io)
|
||||
{
|
||||
struct spdk_bdev_io *bio;
|
||||
struct ftl_io_channel *ioch;
|
||||
int rc;
|
||||
|
||||
bio = spdk_bdev_io_from_ctx(io);
|
||||
ioch = spdk_io_channel_get_ctx(ch);
|
||||
|
||||
rc = bdev_ftl_fill_bio(ftl_bdev, ch, io);
|
||||
if (rc) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
return spdk_ftl_write(ftl_bdev->dev,
|
||||
ioch->ioch,
|
||||
bio->u.bdev.offset_blocks,
|
||||
bio->u.bdev.num_blocks,
|
||||
bio->u.bdev.iovs,
|
||||
bio->u.bdev.iovcnt, bdev_ftl_cb, io);
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
|
||||
{
|
||||
int rc = bdev_ftl_readv((struct ftl_bdev *)bdev_io->bdev->ctxt,
|
||||
ch, (struct ftl_bdev_io *)bdev_io->driver_ctx);
|
||||
|
||||
if (spdk_unlikely(rc != 0)) {
|
||||
bdev_ftl_complete_io((struct ftl_bdev_io *)bdev_io->driver_ctx, rc);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
bdev_ftl_flush(struct ftl_bdev *ftl_bdev, struct spdk_io_channel *ch, struct ftl_bdev_io *io)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = bdev_ftl_fill_bio(ftl_bdev, ch, io);
|
||||
if (rc) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
return spdk_ftl_flush(ftl_bdev->dev, bdev_ftl_cb, io);
|
||||
}
|
||||
|
||||
static int
|
||||
_bdev_ftl_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
|
||||
{
|
||||
struct ftl_bdev *ftl_bdev = (struct ftl_bdev *)bdev_io->bdev->ctxt;
|
||||
|
||||
switch (bdev_io->type) {
|
||||
case SPDK_BDEV_IO_TYPE_READ:
|
||||
spdk_bdev_io_get_buf(bdev_io, bdev_ftl_get_buf_cb,
|
||||
bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
|
||||
return 0;
|
||||
|
||||
case SPDK_BDEV_IO_TYPE_WRITE:
|
||||
return bdev_ftl_writev(ftl_bdev, ch, (struct ftl_bdev_io *)bdev_io->driver_ctx);
|
||||
|
||||
case SPDK_BDEV_IO_TYPE_FLUSH:
|
||||
return bdev_ftl_flush(ftl_bdev, ch, (struct ftl_bdev_io *)bdev_io->driver_ctx);
|
||||
|
||||
case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
|
||||
case SPDK_BDEV_IO_TYPE_RESET:
|
||||
case SPDK_BDEV_IO_TYPE_UNMAP:
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
|
||||
{
|
||||
int rc = _bdev_ftl_submit_request(ch, bdev_io);
|
||||
|
||||
if (spdk_unlikely(rc != 0)) {
|
||||
bdev_ftl_complete_io((struct ftl_bdev_io *)bdev_io->driver_ctx, rc);
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
bdev_ftl_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
|
||||
{
|
||||
switch (io_type) {
|
||||
case SPDK_BDEV_IO_TYPE_READ:
|
||||
case SPDK_BDEV_IO_TYPE_WRITE:
|
||||
case SPDK_BDEV_IO_TYPE_FLUSH:
|
||||
return true;
|
||||
case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
|
||||
case SPDK_BDEV_IO_TYPE_RESET:
|
||||
case SPDK_BDEV_IO_TYPE_UNMAP:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static struct spdk_io_channel *
|
||||
bdev_ftl_get_io_channel(void *ctx)
|
||||
{
|
||||
struct ftl_bdev *ftl_bdev = ctx;
|
||||
|
||||
return spdk_get_io_channel(ftl_bdev);
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
|
||||
{
|
||||
/* TODO: implement me! */
|
||||
}
|
||||
|
||||
static const struct spdk_bdev_fn_table ftl_fn_table = {
|
||||
.destruct = bdev_ftl_destruct,
|
||||
.submit_request = bdev_ftl_submit_request,
|
||||
.io_type_supported = bdev_ftl_io_type_supported,
|
||||
.get_io_channel = bdev_ftl_get_io_channel,
|
||||
.write_config_json = bdev_ftl_write_config_json,
|
||||
};
|
||||
|
||||
int
|
||||
bdev_ftl_parse_punits(struct spdk_ftl_punit_range *range, const char *range_string)
|
||||
{
|
||||
regex_t range_regex;
|
||||
regmatch_t range_match;
|
||||
unsigned long begin = 0, end = 0;
|
||||
char *str_ptr;
|
||||
int rc = -1;
|
||||
|
||||
if (regcomp(&range_regex, "\\b[[:digit:]]+-[[:digit:]]+\\b", REG_EXTENDED)) {
|
||||
SPDK_ERRLOG("Regex init error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (regexec(&range_regex, range_string, 1, &range_match, 0)) {
|
||||
SPDK_WARNLOG("Invalid range\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
begin = strtoul(range_string + range_match.rm_so, &str_ptr, 10);
|
||||
if ((begin == ULONG_MAX && errno == ERANGE) || (begin == 0 && errno == EINVAL)) {
|
||||
SPDK_WARNLOG("Invalid range '%s'\n", range_string);
|
||||
goto out;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
/* +1 to skip the '-' delimiter */
|
||||
end = strtoul(str_ptr + 1, NULL, 10);
|
||||
if ((end == ULONG_MAX && errno == ERANGE) || (end == 0 && errno == EINVAL)) {
|
||||
SPDK_WARNLOG("Invalid range '%s'\n", range_string);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (begin > UINT_MAX || end > UINT_MAX) {
|
||||
SPDK_WARNLOG("Invalid range '%s'\n", range_string);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (begin > end) {
|
||||
SPDK_WARNLOG("Invalid range '%s'\n", range_string);
|
||||
goto out;
|
||||
}
|
||||
|
||||
range->begin = (unsigned int)begin;
|
||||
range->end = (unsigned int)end;
|
||||
|
||||
rc = 0;
|
||||
out:
|
||||
regfree(&range_regex);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
bdev_ftl_read_bdev_config(struct spdk_conf_section *sp,
|
||||
struct ftl_bdev_init_opts *opts,
|
||||
size_t *num_bdevs)
|
||||
{
|
||||
const char *val;
|
||||
size_t i;
|
||||
int rc = 0;
|
||||
|
||||
*num_bdevs = 0;
|
||||
|
||||
for (i = 0; i < FTL_MAX_BDEVS; i++, opts++) {
|
||||
val = spdk_conf_section_get_nmval(sp, "TransportID", i, 0);
|
||||
if (!val) {
|
||||
break;
|
||||
}
|
||||
|
||||
rc = spdk_nvme_transport_id_parse(&opts->trid, val);
|
||||
if (rc < 0) {
|
||||
SPDK_ERRLOG("Unable to parse TransportID: %s\n", val);
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (opts->trid.trtype != SPDK_NVME_TRANSPORT_PCIE) {
|
||||
SPDK_ERRLOG("Unsupported transport type\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
opts->mode = SPDK_FTL_MODE_CREATE;
|
||||
|
||||
val = spdk_conf_section_get_nmval(sp, "TransportID", i, 1);
|
||||
if (!val) {
|
||||
SPDK_ERRLOG("No name provided for TransportID\n");
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
opts->name = val;
|
||||
|
||||
val = spdk_conf_section_get_nmval(sp, "TransportID", i, 2);
|
||||
if (!val) {
|
||||
SPDK_ERRLOG("No punit range provided for TransportID\n");
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (bdev_ftl_parse_punits(&opts->range, val)) {
|
||||
SPDK_ERRLOG("Invalid punit range\n");
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
val = spdk_conf_section_get_nmval(sp, "TransportID", i, 3);
|
||||
if (!val) {
|
||||
continue;
|
||||
}
|
||||
|
||||
rc = spdk_uuid_parse(&opts->uuid, val);
|
||||
if (rc < 0) {
|
||||
SPDK_ERRLOG("Failed to parse uuid: %s\n", val);
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
opts->mode = 0;
|
||||
}
|
||||
|
||||
if (!rc) {
|
||||
*num_bdevs = i;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
bdev_ftl_poll(void *arg)
|
||||
{
|
||||
struct ftl_io_channel *ch = arg;
|
||||
size_t cnt, i;
|
||||
|
||||
cnt = spdk_ring_dequeue(ch->ring, (void **)&ch->io, FTL_MAX_COMPLETIONS);
|
||||
|
||||
for (i = 0; i < cnt; ++i) {
|
||||
bdev_ftl_complete_io(ch->io[i], ch->io[i]->status);
|
||||
}
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
static int
|
||||
bdev_ftl_io_channel_create_cb(void *io_device, void *ctx)
|
||||
{
|
||||
struct ftl_io_channel *ch = ctx;
|
||||
struct ftl_bdev *ftl_bdev = (struct ftl_bdev *)io_device;
|
||||
|
||||
ch->dev = ftl_bdev->dev;
|
||||
ch->ring = spdk_ring_create(SPDK_RING_TYPE_MP_SC, FTL_COMPLETION_RING_SIZE,
|
||||
SPDK_ENV_SOCKET_ID_ANY);
|
||||
|
||||
if (!ch->ring) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
ch->poller = spdk_poller_register(bdev_ftl_poll, ch, 0);
|
||||
if (!ch->poller) {
|
||||
spdk_ring_free(ch->ring);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
ch->ioch = spdk_get_io_channel(ftl_bdev->dev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_io_channel_destroy_cb(void *io_device, void *ctx_buf)
|
||||
{
|
||||
struct ftl_io_channel *ch = ctx_buf;
|
||||
|
||||
spdk_ring_free(ch->ring);
|
||||
spdk_poller_unregister(&ch->poller);
|
||||
spdk_put_io_channel(ch->ioch);
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_create_cb(struct spdk_ftl_dev *dev, void *ctx, int status)
|
||||
{
|
||||
struct ftl_bdev *ftl_bdev = ctx;
|
||||
struct ftl_bdev_info info = {};
|
||||
struct spdk_ftl_attrs attrs;
|
||||
ftl_bdev_init_fn init_cb = ftl_bdev->init_cb;
|
||||
void *init_arg = ftl_bdev->init_arg;
|
||||
int rc = -ENODEV;
|
||||
|
||||
if (status) {
|
||||
SPDK_ERRLOG("Failed to create FTL device (%d)\n", status);
|
||||
rc = status;
|
||||
goto error_dev;
|
||||
}
|
||||
|
||||
spdk_ftl_dev_get_attrs(dev, &attrs);
|
||||
|
||||
ftl_bdev->dev = dev;
|
||||
ftl_bdev->bdev.product_name = "FTL disk";
|
||||
ftl_bdev->bdev.write_cache = 0;
|
||||
ftl_bdev->bdev.blocklen = attrs.lbk_size;
|
||||
ftl_bdev->bdev.blockcnt = attrs.lbk_cnt;
|
||||
/* TODO: Investigate why nbd test are failing without this alignment */
|
||||
ftl_bdev->bdev.required_alignment = spdk_u32log2(attrs.lbk_size);
|
||||
ftl_bdev->bdev.uuid = attrs.uuid;
|
||||
|
||||
SPDK_DEBUGLOG(SPDK_LOG_BDEV_FTL, "Creating bdev %s:\n", ftl_bdev->bdev.name);
|
||||
SPDK_DEBUGLOG(SPDK_LOG_BDEV_FTL, "\tblock_len:\t%zu\n", attrs.lbk_size);
|
||||
SPDK_DEBUGLOG(SPDK_LOG_BDEV_FTL, "\tblock_cnt:\t%"PRIu64"\n", attrs.lbk_cnt);
|
||||
SPDK_DEBUGLOG(SPDK_LOG_BDEV_FTL, "\tpunits:\t\t%u-%u\n", attrs.range.begin,
|
||||
attrs.range.end);
|
||||
|
||||
ftl_bdev->bdev.ctxt = ftl_bdev;
|
||||
ftl_bdev->bdev.fn_table = &ftl_fn_table;
|
||||
ftl_bdev->bdev.module = &g_ftl_if;
|
||||
|
||||
spdk_io_device_register(ftl_bdev, bdev_ftl_io_channel_create_cb,
|
||||
bdev_ftl_io_channel_destroy_cb,
|
||||
sizeof(struct ftl_io_channel),
|
||||
ftl_bdev->bdev.name);
|
||||
|
||||
if (spdk_bdev_register(&ftl_bdev->bdev)) {
|
||||
goto error_unregister;
|
||||
}
|
||||
|
||||
info.name = ftl_bdev->bdev.name;
|
||||
info.uuid = ftl_bdev->bdev.uuid;
|
||||
|
||||
pthread_mutex_lock(&g_ftl_bdev_lock);
|
||||
LIST_INSERT_HEAD(&g_ftl_bdevs, ftl_bdev, list_entry);
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
|
||||
init_cb(&info, init_arg, 0);
|
||||
return;
|
||||
|
||||
error_unregister:
|
||||
spdk_io_device_unregister(ftl_bdev, NULL);
|
||||
error_dev:
|
||||
bdev_ftl_remove_ctrlr(ftl_bdev->ctrlr);
|
||||
|
||||
free(ftl_bdev->bdev.name);
|
||||
free(ftl_bdev);
|
||||
|
||||
init_cb(NULL, init_arg, rc);
|
||||
}
|
||||
|
||||
static int
|
||||
bdev_ftl_create(struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_transport_id *trid,
|
||||
const char *name, struct spdk_ftl_punit_range *range, unsigned int mode,
|
||||
const struct spdk_uuid *uuid, ftl_bdev_init_fn cb, void *cb_arg)
|
||||
{
|
||||
struct ftl_bdev *ftl_bdev = NULL;
|
||||
struct ftl_bdev_ctrlr *ftl_ctrlr;
|
||||
struct spdk_ftl_dev_init_opts opts = {};
|
||||
int rc;
|
||||
|
||||
ftl_ctrlr = bdev_ftl_add_ctrlr(ctrlr, trid);
|
||||
if (!ftl_ctrlr) {
|
||||
spdk_nvme_detach(ctrlr);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
ftl_bdev = calloc(1, sizeof(*ftl_bdev));
|
||||
if (!ftl_bdev) {
|
||||
SPDK_ERRLOG("Could not allocate ftl_bdev\n");
|
||||
rc = -ENOMEM;
|
||||
goto error_ctrlr;
|
||||
}
|
||||
|
||||
ftl_bdev->bdev.name = strdup(name);
|
||||
if (!ftl_bdev->bdev.name) {
|
||||
rc = -ENOMEM;
|
||||
goto error_ctrlr;
|
||||
}
|
||||
|
||||
ftl_bdev->ctrlr = ftl_ctrlr;
|
||||
ftl_bdev->init_cb = cb;
|
||||
ftl_bdev->init_arg = cb_arg;
|
||||
|
||||
opts.conf = NULL;
|
||||
opts.ctrlr = ctrlr;
|
||||
opts.trid = *trid;
|
||||
opts.range = *range;
|
||||
opts.mode = mode;
|
||||
opts.uuid = *uuid;
|
||||
opts.name = ftl_bdev->bdev.name;
|
||||
/* TODO: set threads based on config */
|
||||
opts.core_thread = opts.read_thread = spdk_get_thread();
|
||||
|
||||
rc = spdk_ftl_dev_init(&opts, bdev_ftl_create_cb, ftl_bdev);
|
||||
if (rc) {
|
||||
SPDK_ERRLOG("Could not create FTL device\n");
|
||||
goto error_name;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_name:
|
||||
free(ftl_bdev->bdev.name);
|
||||
error_ctrlr:
|
||||
bdev_ftl_remove_ctrlr(ftl_ctrlr);
|
||||
free(ftl_bdev);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_bdev_init_done(void)
|
||||
{
|
||||
pthread_mutex_lock(&g_ftl_bdev_lock);
|
||||
|
||||
if (++g_num_init_bdevs != g_num_conf_bdevs) {
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
return;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
|
||||
spdk_bdev_module_init_done(&g_ftl_if);
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_init_cb(const struct ftl_bdev_info *info, void *ctx, int status)
|
||||
{
|
||||
if (status) {
|
||||
SPDK_ERRLOG("Failed to initialize FTL bdev\n");
|
||||
}
|
||||
|
||||
bdev_ftl_bdev_init_done();
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_initialize_cb(void *ctx, int status)
|
||||
{
|
||||
struct spdk_conf_section *sp;
|
||||
struct ftl_bdev_init_opts *opts = NULL;
|
||||
size_t i;
|
||||
|
||||
if (status) {
|
||||
SPDK_ERRLOG("Failed to initialize FTL module\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
sp = spdk_conf_find_section(NULL, "Ftl");
|
||||
if (!sp) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
opts = calloc(FTL_MAX_BDEVS, sizeof(*opts));
|
||||
if (!opts) {
|
||||
SPDK_ERRLOG("Failed to allocate bdev init opts\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (bdev_ftl_read_bdev_config(sp, opts, &g_num_conf_bdevs)) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
for (i = 0; i < g_num_conf_bdevs; ++i) {
|
||||
if (bdev_ftl_init_bdev(&opts[i], bdev_ftl_init_cb, NULL)) {
|
||||
SPDK_ERRLOG("Failed to create bdev '%s'\n", opts[i].name);
|
||||
bdev_ftl_bdev_init_done();
|
||||
}
|
||||
}
|
||||
out:
|
||||
if (g_num_conf_bdevs == 0) {
|
||||
spdk_bdev_module_init_done(&g_ftl_if);
|
||||
}
|
||||
|
||||
free(opts);
|
||||
}
|
||||
|
||||
static int
|
||||
bdev_ftl_initialize(void)
|
||||
{
|
||||
struct ftl_module_init_opts ftl_opts = {};
|
||||
pthread_mutexattr_t attr;
|
||||
int rc = 0;
|
||||
|
||||
if (pthread_mutexattr_init(&attr)) {
|
||||
SPDK_ERRLOG("Mutex initialization failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)) {
|
||||
SPDK_ERRLOG("Mutex initialization failed\n");
|
||||
rc = -1;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (pthread_mutex_init(&g_ftl_bdev_lock, &attr)) {
|
||||
SPDK_ERRLOG("Mutex initialization failed\n");
|
||||
rc = -1;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* TODO: retrieve this from config */
|
||||
ftl_opts.anm_thread = spdk_get_thread();
|
||||
rc = spdk_ftl_module_init(&ftl_opts, bdev_ftl_initialize_cb, NULL);
|
||||
|
||||
if (rc) {
|
||||
bdev_ftl_initialize_cb(NULL, rc);
|
||||
|
||||
}
|
||||
error:
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int
|
||||
bdev_ftl_init_bdev(struct ftl_bdev_init_opts *opts, ftl_bdev_init_fn cb, void *cb_arg)
|
||||
{
|
||||
struct ftl_bdev_ctrlr *ftl_ctrlr;
|
||||
struct spdk_nvme_ctrlr *ctrlr;
|
||||
int rc = 0;
|
||||
|
||||
assert(opts != NULL);
|
||||
assert(cb != NULL);
|
||||
|
||||
pthread_mutex_lock(&g_ftl_bdev_lock);
|
||||
|
||||
/* Check already attached controllers first */
|
||||
LIST_FOREACH(ftl_ctrlr, &g_ftl_bdev_ctrlrs, list_entry) {
|
||||
if (!spdk_nvme_transport_id_compare(&ftl_ctrlr->trid, &opts->trid)) {
|
||||
rc = bdev_ftl_create(ftl_ctrlr->ctrlr, &ftl_ctrlr->trid, opts->name,
|
||||
&opts->range, opts->mode, &opts->uuid, cb, cb_arg);
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
|
||||
ctrlr = spdk_nvme_connect(&opts->trid, NULL, 0);
|
||||
if (!ctrlr) {
|
||||
rc = -ENODEV;
|
||||
goto error;
|
||||
|
||||
}
|
||||
|
||||
if (!spdk_nvme_ctrlr_is_ocssd_supported(ctrlr)) {
|
||||
spdk_nvme_detach(ctrlr);
|
||||
rc = -EPERM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
rc = bdev_ftl_create(ctrlr, &opts->trid, opts->name, &opts->range,
|
||||
opts->mode, &opts->uuid, cb, cb_arg);
|
||||
|
||||
return rc;
|
||||
error:
|
||||
cb(NULL, cb_arg, rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
void
|
||||
bdev_ftl_delete_bdev(const char *name, spdk_bdev_unregister_cb cb_fn, void *cb_arg)
|
||||
{
|
||||
struct ftl_bdev *ftl_bdev, *tmp;
|
||||
|
||||
pthread_mutex_lock(&g_ftl_bdev_lock);
|
||||
|
||||
LIST_FOREACH_SAFE(ftl_bdev, &g_ftl_bdevs, list_entry, tmp) {
|
||||
if (strcmp(ftl_bdev->bdev.name, name) == 0) {
|
||||
spdk_bdev_unregister(&ftl_bdev->bdev, cb_fn, cb_arg);
|
||||
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
cb_fn(cb_arg, -ENODEV);
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_ftl_module_fini_cb(void *ctx, int status)
|
||||
{
|
||||
if (status) {
|
||||
SPDK_ERRLOG("Failed to deinitialize FTL module\n");
|
||||
assert(0);
|
||||
}
|
||||
|
||||
spdk_bdev_module_finish_done();
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_finish_cb(void)
|
||||
{
|
||||
if (spdk_ftl_module_fini(bdev_ftl_ftl_module_fini_cb, NULL)) {
|
||||
SPDK_ERRLOG("Failed to deinitialize FTL module\n");
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_finish(void)
|
||||
{
|
||||
pthread_mutex_lock(&g_ftl_bdev_lock);
|
||||
|
||||
if (LIST_EMPTY(&g_ftl_bdevs)) {
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
bdev_ftl_finish_cb();
|
||||
return;
|
||||
}
|
||||
|
||||
g_finish_cb = bdev_ftl_finish_cb;
|
||||
pthread_mutex_unlock(&g_ftl_bdev_lock);
|
||||
}
|
||||
|
||||
static void
|
||||
bdev_ftl_get_spdk_running_config(FILE *fp)
|
||||
{
|
||||
fprintf(fp, "\n[Ftl]\n");
|
||||
}
|
||||
|
||||
SPDK_LOG_REGISTER_COMPONENT("bdev_ftl", SPDK_LOG_BDEV_FTL)
|
74
lib/bdev/ftl/bdev_ftl.h
Normal file
74
lib/bdev/ftl/bdev_ftl.h
Normal file
@ -0,0 +1,74 @@
|
||||
/*-
|
||||
* BSD LICENSE
|
||||
*
|
||||
* Copyright (c) Intel Corporation.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * 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.
|
||||
* * Neither the name of Intel Corporation nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
|
||||
* OWNER 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.
|
||||
*/
|
||||
|
||||
#ifndef SPDK_BDEV_FTL_H
|
||||
#define SPDK_BDEV_FTL_H
|
||||
|
||||
#include <spdk/stdinc.h>
|
||||
#include <spdk/nvme.h>
|
||||
#include <spdk/bdev_module.h>
|
||||
#include <spdk/ftl.h>
|
||||
|
||||
#define FTL_MAX_CONTROLLERS 64
|
||||
#define FTL_MAX_BDEVS (FTL_MAX_CONTROLLERS * 128)
|
||||
#define FTL_RANGE_MAX_LENGTH 32
|
||||
|
||||
struct spdk_bdev;
|
||||
struct spdk_uuid;
|
||||
|
||||
struct ftl_bdev_info {
|
||||
const char *name;
|
||||
struct spdk_uuid uuid;
|
||||
};
|
||||
|
||||
struct ftl_bdev_init_opts {
|
||||
/* NVMe controller's transport ID */
|
||||
struct spdk_nvme_transport_id trid;
|
||||
/* Parallel unit range */
|
||||
struct spdk_ftl_punit_range range;
|
||||
/* Bdev's name */
|
||||
const char *name;
|
||||
/* Bdev's mode */
|
||||
uint32_t mode;
|
||||
/* UUID if device is restored from SSD */
|
||||
struct spdk_uuid uuid;
|
||||
};
|
||||
|
||||
typedef void (*ftl_bdev_init_fn)(const struct ftl_bdev_info *, void *, int);
|
||||
|
||||
int bdev_ftl_parse_punits(struct spdk_ftl_punit_range *range, const char *range_string);
|
||||
int bdev_ftl_init_bdev(struct ftl_bdev_init_opts *opts, ftl_bdev_init_fn cb,
|
||||
void *cb_arg);
|
||||
void bdev_ftl_delete_bdev(const char *name, spdk_bdev_unregister_cb cb_fn, void *cb_arg);
|
||||
|
||||
#endif /* SPDK_BDEV_FTL_H */
|
198
lib/bdev/ftl/bdev_ftl_rpc.c
Normal file
198
lib/bdev/ftl/bdev_ftl_rpc.c
Normal file
@ -0,0 +1,198 @@
|
||||
/*-
|
||||
* BSD LICENSE
|
||||
*
|
||||
* Copyright (c) Intel Corporation.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * 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.
|
||||
* * Neither the name of Intel Corporation nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
|
||||
* OWNER 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.
|
||||
*/
|
||||
|
||||
#include <spdk/rpc.h>
|
||||
#include <spdk/util.h>
|
||||
#include <spdk/bdev_module.h>
|
||||
#include <spdk_internal/log.h>
|
||||
#include "bdev_ftl.h"
|
||||
|
||||
struct rpc_construct_ftl {
|
||||
char *name;
|
||||
char *trtype;
|
||||
char *traddr;
|
||||
char *punits;
|
||||
char *uuid;
|
||||
};
|
||||
|
||||
static void
|
||||
free_rpc_construct_ftl(struct rpc_construct_ftl *req)
|
||||
{
|
||||
free(req->name);
|
||||
free(req->trtype);
|
||||
free(req->traddr);
|
||||
free(req->punits);
|
||||
free(req->uuid);
|
||||
}
|
||||
|
||||
static const struct spdk_json_object_decoder rpc_construct_ftl_decoders[] = {
|
||||
{"name", offsetof(struct rpc_construct_ftl, name), spdk_json_decode_string},
|
||||
{"trtype", offsetof(struct rpc_construct_ftl, trtype), spdk_json_decode_string},
|
||||
{"traddr", offsetof(struct rpc_construct_ftl, traddr), spdk_json_decode_string},
|
||||
{"punits", offsetof(struct rpc_construct_ftl, punits), spdk_json_decode_string},
|
||||
{"uuid", offsetof(struct rpc_construct_ftl, uuid), spdk_json_decode_string, true},
|
||||
};
|
||||
|
||||
#define FTL_RANGE_MAX_LENGTH 32
|
||||
|
||||
static void
|
||||
_spdk_rpc_construct_ftl_bdev_cb(const struct ftl_bdev_info *bdev_info, void *ctx, int status)
|
||||
{
|
||||
struct spdk_jsonrpc_request *request = ctx;
|
||||
char bdev_uuid[SPDK_UUID_STRING_LEN];
|
||||
struct spdk_json_write_ctx *w;
|
||||
|
||||
if (status) {
|
||||
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
|
||||
"Invalid parameters");
|
||||
return;
|
||||
}
|
||||
|
||||
w = spdk_jsonrpc_begin_result(request);
|
||||
if (!w) {
|
||||
SPDK_DEBUGLOG(SPDK_LOG_BDEV_FTL, "spdk_json_begin_result failed\n");
|
||||
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
|
||||
"Internal error");
|
||||
return;
|
||||
}
|
||||
|
||||
spdk_uuid_fmt_lower(bdev_uuid, sizeof(bdev_uuid), &bdev_info->uuid);
|
||||
spdk_json_write_object_begin(w);
|
||||
spdk_json_write_named_string(w, "name", bdev_info->name);
|
||||
spdk_json_write_named_string(w, "uuid", bdev_uuid);
|
||||
spdk_json_write_object_end(w);
|
||||
spdk_jsonrpc_end_result(request, w);
|
||||
}
|
||||
|
||||
static void
|
||||
spdk_rpc_construct_ftl_bdev(struct spdk_jsonrpc_request *request,
|
||||
const struct spdk_json_val *params)
|
||||
{
|
||||
struct rpc_construct_ftl req = {};
|
||||
struct ftl_bdev_init_opts opts = {};
|
||||
char range[FTL_RANGE_MAX_LENGTH];
|
||||
|
||||
if (spdk_json_decode_object(params, rpc_construct_ftl_decoders,
|
||||
SPDK_COUNTOF(rpc_construct_ftl_decoders),
|
||||
&req)) {
|
||||
SPDK_DEBUGLOG(SPDK_LOG_BDEV_FTL, "spdk_json_decode_object failed\n");
|
||||
goto invalid;
|
||||
}
|
||||
|
||||
opts.name = req.name;
|
||||
opts.mode = SPDK_FTL_MODE_CREATE;
|
||||
|
||||
/* Parse trtype */
|
||||
if (spdk_nvme_transport_id_parse_trtype(&opts.trid.trtype, req.trtype) < 0) {
|
||||
SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype);
|
||||
goto invalid;
|
||||
}
|
||||
|
||||
if (opts.trid.trtype != SPDK_NVME_TRANSPORT_PCIE) {
|
||||
SPDK_ERRLOG("Devices other than PCIe not supported %s\n", opts.trid.traddr);
|
||||
goto invalid;
|
||||
}
|
||||
|
||||
/* Parse traddr */
|
||||
snprintf(opts.trid.traddr, sizeof(opts.trid.traddr), "%s", req.traddr);
|
||||
snprintf(range, sizeof(range), "%s", req.punits);
|
||||
|
||||
if (bdev_ftl_parse_punits(&opts.range, req.punits)) {
|
||||
SPDK_ERRLOG("Failed to parse parallel unit range\n");
|
||||
goto invalid;
|
||||
}
|
||||
|
||||
if (req.uuid) {
|
||||
opts.mode = 0;
|
||||
if (spdk_uuid_parse(&opts.uuid, req.uuid) < 0) {
|
||||
SPDK_ERRLOG("Failed to parse uuid: %s\n", req.uuid);
|
||||
goto invalid;
|
||||
}
|
||||
}
|
||||
|
||||
if (bdev_ftl_init_bdev(&opts, _spdk_rpc_construct_ftl_bdev_cb, request)) {
|
||||
SPDK_ERRLOG("Failed to create FTL bdev\n");
|
||||
goto invalid;
|
||||
}
|
||||
|
||||
goto free_rpc;
|
||||
invalid:
|
||||
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
|
||||
"Invalid parameters");
|
||||
free_rpc:
|
||||
free_rpc_construct_ftl(&req);
|
||||
}
|
||||
|
||||
SPDK_RPC_REGISTER("construct_ftl_bdev", spdk_rpc_construct_ftl_bdev, SPDK_RPC_RUNTIME)
|
||||
|
||||
struct rpc_delete_ftl {
|
||||
char *name;
|
||||
};
|
||||
|
||||
static const struct spdk_json_object_decoder rpc_delete_ftl_decoders[] = {
|
||||
{"name", offsetof(struct rpc_construct_ftl, name), spdk_json_decode_string},
|
||||
};
|
||||
|
||||
static void
|
||||
_spdk_rpc_delete_ftl_bdev_cb(void *cb_arg, int bdeverrno)
|
||||
{
|
||||
struct spdk_jsonrpc_request *request = cb_arg;
|
||||
struct spdk_json_write_ctx *w;
|
||||
|
||||
w = spdk_jsonrpc_begin_result(request);
|
||||
if (w == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
spdk_json_write_bool(w, bdeverrno == 0);
|
||||
spdk_jsonrpc_end_result(request, w);
|
||||
}
|
||||
|
||||
static void
|
||||
spdk_rpc_delete_ftl_bdev(struct spdk_jsonrpc_request *request,
|
||||
const struct spdk_json_val *params)
|
||||
{
|
||||
struct rpc_delete_ftl attrs = {};
|
||||
|
||||
if (spdk_json_decode_object(params, rpc_delete_ftl_decoders,
|
||||
SPDK_COUNTOF(rpc_delete_ftl_decoders),
|
||||
&attrs)) {
|
||||
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
|
||||
"Invalid parameters");
|
||||
}
|
||||
|
||||
bdev_ftl_delete_bdev(attrs.name, _spdk_rpc_delete_ftl_bdev_cb, request);
|
||||
free(attrs.name);
|
||||
}
|
||||
|
||||
SPDK_RPC_REGISTER("delete_ftl_bdev", spdk_rpc_delete_ftl_bdev, SPDK_RPC_RUNTIME)
|
@ -67,7 +67,7 @@ SYS_LIBS += -lpmemblk
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_FTL),y)
|
||||
BLOCKDEV_MODULES_LIST += ftl
|
||||
BLOCKDEV_MODULES_LIST += ftl bdev_ftl
|
||||
endif
|
||||
|
||||
SOCK_MODULES_LIST = sock_posix
|
||||
|
@ -1215,6 +1215,35 @@ Format: 'user:u1 secret:s1 muser:mu1 msecret:ms1,user:u2 secret:s2 muser:mu2 mse
|
||||
p.add_argument('base_bdev', help='base bdev name')
|
||||
p.set_defaults(func=destruct_split_vbdev)
|
||||
|
||||
# ftl
|
||||
def construct_ftl_bdev(args):
|
||||
print_dict(rpc.bdev.construct_ftl_bdev(args.client,
|
||||
name=args.name,
|
||||
trtype=args.trtype,
|
||||
traddr=args.traddr,
|
||||
punits=args.punits,
|
||||
uuid=args.uuid))
|
||||
|
||||
p = subparsers.add_parser('construct_ftl_bdev',
|
||||
help='Add FTL bdev')
|
||||
p.add_argument('-b', '--name', help="Name of the bdev", required=True)
|
||||
p.add_argument('-t', '--trtype',
|
||||
help='NVMe target trtype: e.g., pcie', default='pcie')
|
||||
p.add_argument('-a', '--traddr',
|
||||
help='NVMe target address: e.g., an ip address or BDF', required=True)
|
||||
p.add_argument('-l', '--punits', help='Parallel unit range in the form of start-end: e.g. 4-8',
|
||||
required=True)
|
||||
p.add_argument('-u', '--uuid', help='UUID of restored bdev (not applicable when creating new '
|
||||
'instance): e.g. b286d19a-0059-4709-abcd-9f7732b1567d (optional)')
|
||||
p.set_defaults(func=construct_ftl_bdev)
|
||||
|
||||
def delete_ftl_bdev(args):
|
||||
print_dict(rpc.bdev.delete_ftl_bdev(args.client, name=args.name))
|
||||
|
||||
p = subparsers.add_parser('delete_ftl_bdev', help='Delete FTL bdev')
|
||||
p.add_argument('-b', '--name', help="Name of the bdev", required=True)
|
||||
p.set_defaults(func=delete_ftl_bdev)
|
||||
|
||||
# nbd
|
||||
def start_nbd_disk(args):
|
||||
print(rpc.nbd.start_nbd_disk(args.client,
|
||||
|
@ -445,6 +445,36 @@ def destruct_split_vbdev(client, base_bdev):
|
||||
return client.call('destruct_split_vbdev', params)
|
||||
|
||||
|
||||
def construct_ftl_bdev(client, name, trtype, traddr, punits, uuid=None):
|
||||
"""Construct FTL bdev
|
||||
|
||||
Args:
|
||||
name: name of the bdev
|
||||
trtype: transport type
|
||||
traddr: transport address
|
||||
punit: parallel unit range
|
||||
uuid: UUID of the device
|
||||
"""
|
||||
params = {'name': name,
|
||||
'trtype': trtype,
|
||||
'traddr': traddr,
|
||||
'punits': punits}
|
||||
if uuid:
|
||||
params['uuid'] = uuid
|
||||
return client.call('construct_ftl_bdev', params)
|
||||
|
||||
|
||||
def delete_ftl_bdev(client, name):
|
||||
"""Delete FTL bdev
|
||||
|
||||
Args:
|
||||
name: name of the bdev
|
||||
"""
|
||||
params = {'name': name}
|
||||
|
||||
return client.call('delete_ftl_bdev', params)
|
||||
|
||||
|
||||
def get_bdevs(client, name=None):
|
||||
"""Get information about block devices.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user