per Intel policy to include file commit date using git cmd below. The policy does not apply to non-Intel (C) notices. git log --follow -C90% --format=%ad --date default <file> | tail -1 and then pull just the 4 digit year from the result. Intel copyrights were not added to files where Intel either had no contribution ot the contribution lacked substance (ie license header updates, formatting changes, etc). Contribution date used "--follow -C95%" to get the most accurate date. Note that several files in this patch didn't end the license/(c) block with a blank comment line so these were added as the vast majority of files do have this last blank line. Simply there for consistency. Signed-off-by: paul luse <paul.e.luse@intel.com> Change-Id: Id5b7ce4f658fe87132f14139ead58d6e285c04d4 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/15192 Tested-by: SPDK CI Jenkins <sys_sgci@intel.com> Reviewed-by: Jim Harris <james.r.harris@intel.com> Reviewed-by: Ben Walker <benjamin.walker@intel.com> Community-CI: Mellanox Build Bot
392 lines
8.7 KiB
C
392 lines
8.7 KiB
C
/* SPDX-License-Identifier: BSD-3-Clause
|
|
* Copyright (C) 2020 Intel Corporation.
|
|
* Copyright (c) Eideticom Inc.
|
|
* All rights reserved.
|
|
*/
|
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
#include "spdk/env.h"
|
|
#include "spdk/nvme.h"
|
|
#include "spdk/string.h"
|
|
|
|
#define CMB_COPY_DELIM "-"
|
|
#define CMB_COPY_READ 0
|
|
#define CMB_COPY_WRITE 1
|
|
|
|
struct nvme_io {
|
|
struct spdk_nvme_ctrlr *ctrlr;
|
|
struct spdk_nvme_transport_id trid;
|
|
struct spdk_nvme_qpair *qpair;
|
|
struct spdk_nvme_ns *ns;
|
|
unsigned nsid;
|
|
unsigned slba;
|
|
unsigned nlbas;
|
|
uint32_t lba_size;
|
|
unsigned done;
|
|
};
|
|
|
|
struct cmb_t {
|
|
struct spdk_nvme_transport_id trid;
|
|
struct spdk_nvme_ctrlr *ctrlr;
|
|
};
|
|
|
|
struct config {
|
|
struct nvme_io read;
|
|
struct nvme_io write;
|
|
struct cmb_t cmb;
|
|
size_t copy_size;
|
|
};
|
|
|
|
static struct config g_config;
|
|
|
|
/* Namespaces index from 1. Return 0 to invoke an error */
|
|
static unsigned
|
|
get_nsid(const struct spdk_nvme_transport_id *trid)
|
|
{
|
|
if (!strcmp(trid->traddr, g_config.read.trid.traddr)) {
|
|
return g_config.read.nsid;
|
|
}
|
|
if (!strcmp(trid->traddr, g_config.write.trid.traddr)) {
|
|
return g_config.write.nsid;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
get_rw(const struct spdk_nvme_transport_id *trid)
|
|
{
|
|
if (!strcmp(trid->traddr, g_config.read.trid.traddr)) {
|
|
return CMB_COPY_READ;
|
|
}
|
|
if (!strcmp(trid->traddr, g_config.write.trid.traddr)) {
|
|
return CMB_COPY_WRITE;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static void
|
|
check_io(void *arg, const struct spdk_nvme_cpl *completion)
|
|
{
|
|
int *rw = (unsigned *)arg;
|
|
|
|
if (*rw == CMB_COPY_READ) {
|
|
g_config.read.done = 1;
|
|
} else {
|
|
g_config.write.done = 1;
|
|
}
|
|
}
|
|
|
|
static int
|
|
cmb_copy(void)
|
|
{
|
|
int rc = 0, rw;
|
|
void *buf;
|
|
size_t sz;
|
|
|
|
/* Allocate QPs for the read and write controllers */
|
|
g_config.read.qpair = spdk_nvme_ctrlr_alloc_io_qpair(g_config.read.ctrlr, NULL, 0);
|
|
g_config.write.qpair = spdk_nvme_ctrlr_alloc_io_qpair(g_config.write.ctrlr, NULL, 0);
|
|
if (g_config.read.qpair == NULL || g_config.read.qpair == NULL) {
|
|
printf("ERROR: spdk_nvme_ctrlr_alloc_io_qpair() failed\n");
|
|
return -ENOMEM;
|
|
}
|
|
|
|
/* Allocate a buffer from our CMB */
|
|
buf = spdk_nvme_ctrlr_map_cmb(g_config.cmb.ctrlr, &sz);
|
|
if (buf == NULL || sz < g_config.copy_size) {
|
|
printf("ERROR: buffer allocation failed\n");
|
|
printf("Are you sure %s has a valid CMB?\n",
|
|
g_config.cmb.trid.traddr);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
/* Clear the done flags */
|
|
g_config.read.done = 0;
|
|
g_config.write.done = 0;
|
|
|
|
rw = CMB_COPY_READ;
|
|
/* Do the read to the CMB IO buffer */
|
|
rc = spdk_nvme_ns_cmd_read(g_config.read.ns, g_config.read.qpair, buf,
|
|
g_config.read.slba, g_config.read.nlbas,
|
|
check_io, &rw, 0);
|
|
if (rc != 0) {
|
|
fprintf(stderr, "starting read I/O failed\n");
|
|
return -EIO;
|
|
}
|
|
while (!g_config.read.done) {
|
|
spdk_nvme_qpair_process_completions(g_config.read.qpair, 0);
|
|
}
|
|
|
|
/* Do the write from the CMB IO buffer */
|
|
rw = CMB_COPY_WRITE;
|
|
rc = spdk_nvme_ns_cmd_write(g_config.write.ns, g_config.write.qpair, buf,
|
|
g_config.write.slba, g_config.write.nlbas,
|
|
check_io, &rw, 0);
|
|
if (rc != 0) {
|
|
fprintf(stderr, "starting write I/O failed\n");
|
|
return -EIO;
|
|
}
|
|
while (!g_config.write.done) {
|
|
spdk_nvme_qpair_process_completions(g_config.write.qpair, 0);
|
|
}
|
|
|
|
/* Clear the done flags */
|
|
g_config.read.done = 0;
|
|
g_config.write.done = 0;
|
|
|
|
/* Free CMB buffer */
|
|
spdk_nvme_ctrlr_unmap_cmb(g_config.cmb.ctrlr);
|
|
|
|
/* Free the queues */
|
|
spdk_nvme_ctrlr_free_io_qpair(g_config.read.qpair);
|
|
spdk_nvme_ctrlr_free_io_qpair(g_config.write.qpair);
|
|
|
|
return rc;
|
|
}
|
|
|
|
static bool
|
|
probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
|
|
struct spdk_nvme_ctrlr_opts *opts)
|
|
{
|
|
/* We will only attach to the read or write controller */
|
|
if (strcmp(trid->traddr, g_config.read.trid.traddr) &&
|
|
strcmp(trid->traddr, g_config.write.trid.traddr)) {
|
|
printf("%s - not probed %s!\n", __func__, trid->traddr);
|
|
return 0;
|
|
}
|
|
|
|
opts->use_cmb_sqs = false;
|
|
|
|
printf("%s - probed %s!\n", __func__, trid->traddr);
|
|
return 1;
|
|
}
|
|
|
|
static void
|
|
attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
|
|
struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
|
|
{
|
|
struct spdk_nvme_ns *ns;
|
|
|
|
ns = spdk_nvme_ctrlr_get_ns(ctrlr, get_nsid(trid));
|
|
if (ns == NULL) {
|
|
fprintf(stderr, "Could not locate namespace %d on controller %s.\n",
|
|
get_nsid(trid), trid->traddr);
|
|
exit(-1);
|
|
}
|
|
if (get_rw(trid) == CMB_COPY_READ) {
|
|
g_config.read.ctrlr = ctrlr;
|
|
g_config.read.ns = ns;
|
|
g_config.read.lba_size = spdk_nvme_ns_get_sector_size(ns);
|
|
} else {
|
|
g_config.write.ctrlr = ctrlr;
|
|
g_config.write.ns = ns;
|
|
g_config.write.lba_size = spdk_nvme_ns_get_sector_size(ns);
|
|
}
|
|
printf("%s - attached %s!\n", __func__, trid->traddr);
|
|
|
|
return;
|
|
}
|
|
|
|
static void
|
|
usage(char *program_name)
|
|
{
|
|
printf("%s options (all mandatory)", program_name);
|
|
printf("\n");
|
|
printf("\t[-r NVMe read parameters]\n");
|
|
printf("\t[-w NVMe write parameters]\n");
|
|
printf("\t[-c CMB to use for data buffers]\n");
|
|
printf("\n");
|
|
printf("Read/Write params:\n");
|
|
printf(" <pci id>-<namespace>-<start LBA>-<number of LBAs>\n");
|
|
}
|
|
|
|
static void
|
|
parse(char *in, struct nvme_io *io)
|
|
{
|
|
char *tok = NULL;
|
|
long int val;
|
|
|
|
tok = strtok(in, CMB_COPY_DELIM);
|
|
if (tok == NULL) {
|
|
goto err;
|
|
}
|
|
snprintf(&io->trid.traddr[0], SPDK_NVMF_TRADDR_MAX_LEN + 1,
|
|
"%s", tok);
|
|
|
|
tok = strtok(NULL, CMB_COPY_DELIM);
|
|
if (tok == NULL) {
|
|
goto err;
|
|
}
|
|
val = spdk_strtol(tok, 10);
|
|
if (val < 0) {
|
|
goto err;
|
|
}
|
|
io->nsid = (unsigned)val;
|
|
|
|
tok = strtok(NULL, CMB_COPY_DELIM);
|
|
if (tok == NULL) {
|
|
goto err;
|
|
}
|
|
val = spdk_strtol(tok, 10);
|
|
if (val < 0) {
|
|
goto err;
|
|
}
|
|
io->slba = (unsigned)val;
|
|
|
|
tok = strtok(NULL, CMB_COPY_DELIM);
|
|
if (tok == NULL) {
|
|
goto err;
|
|
}
|
|
val = spdk_strtol(tok, 10);
|
|
if (val < 0) {
|
|
goto err;
|
|
}
|
|
io->nlbas = (unsigned)val;
|
|
|
|
tok = strtok(NULL, CMB_COPY_DELIM);
|
|
if (tok != NULL) {
|
|
goto err;
|
|
}
|
|
return;
|
|
|
|
err:
|
|
fprintf(stderr, "%s: error parsing %s\n", __func__, in);
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
static int
|
|
parse_args(int argc, char **argv)
|
|
{
|
|
int op;
|
|
unsigned read = 0, write = 0, cmb = 0;
|
|
|
|
while ((op = getopt(argc, argv, "r:w:c:")) != -1) {
|
|
switch (op) {
|
|
case 'r':
|
|
parse(optarg, &g_config.read);
|
|
read = 1;
|
|
break;
|
|
case 'w':
|
|
parse(optarg, &g_config.write);
|
|
write = 1;
|
|
break;
|
|
case 'c':
|
|
snprintf(g_config.cmb.trid.traddr, SPDK_NVMF_TRADDR_MAX_LEN + 1,
|
|
"%s", optarg);
|
|
cmb = 1;
|
|
break;
|
|
default:
|
|
usage(argv[0]);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
if ((!read || !write || !cmb)) {
|
|
usage(argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
int rc = 0;
|
|
struct spdk_env_opts opts;
|
|
|
|
/*
|
|
* Parse the input arguments. For now we use the following
|
|
* format list:
|
|
*
|
|
* <pci id>-<namespace>-<start LBA>-<number of LBAs>
|
|
*
|
|
*/
|
|
rc = parse_args(argc, argv);
|
|
if (rc) {
|
|
fprintf(stderr, "Error in parse_args(): %d\n",
|
|
rc);
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
* SPDK relies on an abstraction around the local environment
|
|
* named env that handles memory allocation and PCI device operations.
|
|
* This library must be initialized first.
|
|
*
|
|
*/
|
|
spdk_env_opts_init(&opts);
|
|
opts.name = "cmb_copy";
|
|
opts.shm_id = 0;
|
|
if (spdk_env_init(&opts) < 0) {
|
|
fprintf(stderr, "Unable to initialize SPDK env\n");
|
|
return 1;
|
|
}
|
|
|
|
/*
|
|
* CMBs only apply to PCIe attached NVMe controllers so we
|
|
* only probe the PCIe bus. This is the default when we pass
|
|
* in NULL for the first argument.
|
|
*/
|
|
|
|
rc = spdk_nvme_probe(NULL, NULL, probe_cb, attach_cb, NULL);
|
|
if (rc) {
|
|
fprintf(stderr, "Error in spdk_nvme_probe(): %d\n",
|
|
rc);
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
* For now enforce that the read and write controller are not
|
|
* the same. This avoids an internal only DMA.
|
|
*/
|
|
if (!strcmp(g_config.write.trid.traddr, g_config.read.trid.traddr)) {
|
|
fprintf(stderr, "Read and Write controllers must differ!\n");
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
* Perform a few sanity checks and set the buffer size for the
|
|
* CMB.
|
|
*/
|
|
if (g_config.read.nlbas * g_config.read.lba_size !=
|
|
g_config.write.nlbas * g_config.write.lba_size) {
|
|
fprintf(stderr, "Read and write sizes do not match!\n");
|
|
return -1;
|
|
}
|
|
g_config.copy_size = g_config.read.nlbas * g_config.read.lba_size;
|
|
|
|
/*
|
|
* Get the ctrlr pointer for the CMB. For now we assume this
|
|
* is either the read or write NVMe controller though in
|
|
* theory that is not a necessary condition.
|
|
*/
|
|
|
|
if (!strcmp(g_config.cmb.trid.traddr, g_config.read.trid.traddr)) {
|
|
g_config.cmb.ctrlr = g_config.read.ctrlr;
|
|
}
|
|
if (!strcmp(g_config.cmb.trid.traddr, g_config.write.trid.traddr)) {
|
|
g_config.cmb.ctrlr = g_config.write.ctrlr;
|
|
}
|
|
|
|
if (!g_config.read.ctrlr || !g_config.write.ctrlr) {
|
|
fprintf(stderr, "No NVMe controller that support CMB was found!\n");
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
* Call the cmb_copy() function which performs the CMB
|
|
* based copy or returns an error code if it fails.
|
|
*/
|
|
rc = cmb_copy();
|
|
if (rc) {
|
|
fprintf(stderr, "Error in spdk_cmb_copy(): %d\n",
|
|
rc);
|
|
return -1;
|
|
}
|
|
|
|
return rc;
|
|
}
|