Commit c391fed3 authored by Changpeng Liu's avatar Changpeng Liu Committed by Jim Harris
Browse files

nvme: add nvme error injection support



Users can set specified Admin commands or IO commands with
error status, when submitting new commands which are already
set with error status, the commands will return to the caller
with specified error code. So that users can emulate some error
status for their error condition code path.

Change-Id: I4b93c7e4f2b15a659da73b39e26bfa162eb5214e
Signed-off-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
Reviewed-on: https://review.gerrithub.io/410870


Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
parent 5b822d4c
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -2,6 +2,12 @@

## v18.07: (Upcoming Release)

### NVMe Driver

New API function spdk_nvme_qpair_add_cmd_error_injection() and
spdk_nvme_qpair_remove_cmd_error_injection() have been added for NVMe error emulation,
users can set specified command with specified error status for error emulation.

### Build System

The build system now generates a combined shared library (libspdk.so) that may be used
+48 −0
Original line number Diff line number Diff line
@@ -1679,6 +1679,54 @@ int spdk_nvme_ns_cmd_compare_with_md(struct spdk_nvme_ns *ns, struct spdk_nvme_q
				     void *cb_arg, uint32_t io_flags,
				     uint16_t apptag_mask, uint16_t apptag);

/**
 * \brief Inject an error for the next request with a given opcode.
 *
 * \param ctrlr NVMe controller.
 * \param qpair I/O queue pair to add the error command,
 *              NULL for Admin queue pair.
 * \param opc Opcode for Admin or I/O commands.
 * \param do_not_submit True if matching requests should not be submitted
 *                      to the controller, but instead completed manually
 *                      after timeout_in_us has expired.  False if matching
 *                      requests should be submitted to the controller and
 *                      have their completion status modified after the
 *                      controller completes the request.
 * \param timeout_in_us Wait specified microseconds when do_not_submit is true.
 * \param err_count Number of matching requests to inject errors.
 * \param sct Status code type.
 * \param sc Status code.
 *
 * \return 0 if successfully enabled, ENOMEM if an error command
 *	     structure cannot be allocated.
 *
 * The function can be called multiple times to inject errors for different
 * commands.  If the opcode matches an existing entry, the existing entry
 * will be updated with the values specified.
 */
int spdk_nvme_qpair_add_cmd_error_injection(struct spdk_nvme_ctrlr *ctrlr,
		struct spdk_nvme_qpair *qpair,
		uint8_t opc,
		bool do_not_submit,
		uint64_t timeout_in_us,
		uint32_t err_count,
		uint8_t sct, uint8_t sc);

/**
 * \brief Clear the specified NVMe command with error status.
 *
 * \param ctrlr NVMe controller.
 * \param qpair I/O queue pair to remove the error command,
 * \            NULL for Admin queue pair.
 * \param opc Opcode for Admin or I/O commands.
 *
 * The function will remove specified command in the error list.
 */
void spdk_nvme_qpair_remove_cmd_error_injection(struct spdk_nvme_ctrlr *ctrlr,
		struct spdk_nvme_qpair *qpair,
		uint8_t opc);


#ifdef __cplusplus
}
#endif
+47 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
#ifndef __NVME_INTERNAL_H__
#define __NVME_INTERNAL_H__

#include "spdk/likely.h"
#include "spdk/stdinc.h"

#include "spdk/nvme.h"
@@ -186,6 +187,15 @@ nvme_payload_type(const struct nvme_payload *payload) {
	return payload->reset_sgl_fn ? NVME_PAYLOAD_TYPE_SGL : NVME_PAYLOAD_TYPE_CONTIG;
}

struct nvme_error_cmd {
	bool				do_not_submit;
	uint64_t			timeout_tsc;
	uint32_t			err_count;
	uint8_t				opc;
	struct spdk_nvme_status		status;
	TAILQ_ENTRY(nvme_error_cmd)	link;
};

struct nvme_request {
	struct spdk_nvme_cmd		cmd;

@@ -208,6 +218,12 @@ struct nvme_request {

	uint32_t			payload_size;

	/**
	 * Timeout ticks for error injection requests, can be extended in future
	 * to support per-request timeout feature.
	 */
	uint64_t			timeout_tsc;

	/**
	 * Data payload for this request's command.
	 */
@@ -293,6 +309,10 @@ struct nvme_async_event_request {
struct spdk_nvme_qpair {
	STAILQ_HEAD(, nvme_request)	free_req;
	STAILQ_HEAD(, nvme_request)	queued_req;
	/** Commands opcode in this list will return error */
	TAILQ_HEAD(, nvme_error_cmd)	err_cmd_head;
	/** Requests in this list will return error */
	STAILQ_HEAD(, nvme_request)	err_req_head;

	enum spdk_nvme_transport_type	trtype;

@@ -722,6 +742,33 @@ struct nvme_request *nvme_allocate_request_user_copy(struct spdk_nvme_qpair *qpa
static inline void
nvme_complete_request(struct nvme_request *req, struct spdk_nvme_cpl *cpl)
{
	struct spdk_nvme_qpair          *qpair = req->qpair;
	struct spdk_nvme_cpl            err_cpl;
	struct nvme_error_cmd           *cmd;

	/* error injection at completion path,
	 * only inject for successful completed commands
	 */
	if (spdk_unlikely(!TAILQ_EMPTY(&qpair->err_cmd_head) &&
			  !spdk_nvme_cpl_is_error(cpl))) {
		TAILQ_FOREACH(cmd, &qpair->err_cmd_head, link) {

			if (cmd->do_not_submit) {
				continue;
			}

			if ((cmd->opc == req->cmd.opc) && cmd->err_count) {

				err_cpl = *cpl;
				err_cpl.status.sct = cmd->status.sct;
				err_cpl.status.sc = cmd->status.sc;

				cpl = &err_cpl;
				cmd->err_count--;
			}
		}
	}

	if (req->cb_fn) {
		req->cb_fn(req->cb_arg, cpl);
	}
+123 −0
Original line number Diff line number Diff line
@@ -377,12 +377,25 @@ int32_t
spdk_nvme_qpair_process_completions(struct spdk_nvme_qpair *qpair, uint32_t max_completions)
{
	int32_t ret;
	struct nvme_request *req, *tmp;

	if (qpair->ctrlr->is_failed) {
		nvme_qpair_fail(qpair);
		return 0;
	}

	/* error injection for those queued error requests */
	if (spdk_unlikely(!STAILQ_EMPTY(&qpair->err_req_head))) {
		STAILQ_FOREACH_SAFE(req, &qpair->err_req_head, stailq, tmp) {
			if (spdk_get_ticks() - req->submit_tick > req->timeout_tsc) {
				STAILQ_REMOVE(&qpair->err_req_head, req, nvme_request, stailq);
				nvme_qpair_manual_complete_request(qpair, req,
								   req->cpl.status.sct,
								   req->cpl.status.sc, true);
			}
		}
	}

	qpair->in_completion_context = 1;
	ret = nvme_transport_qpair_process_completions(qpair, max_completions);
	qpair->in_completion_context = 0;
@@ -417,6 +430,8 @@ nvme_qpair_init(struct spdk_nvme_qpair *qpair, uint16_t id,

	STAILQ_INIT(&qpair->free_req);
	STAILQ_INIT(&qpair->queued_req);
	TAILQ_INIT(&qpair->err_cmd_head);
	STAILQ_INIT(&qpair->err_req_head);

	req_size_padded = (sizeof(struct nvme_request) + 63) & ~(size_t)63;

@@ -437,6 +452,22 @@ nvme_qpair_init(struct spdk_nvme_qpair *qpair, uint16_t id,
void
nvme_qpair_deinit(struct spdk_nvme_qpair *qpair)
{
	struct nvme_request *req;
	struct nvme_error_cmd *cmd, *entry;

	while (!STAILQ_EMPTY(&qpair->err_req_head)) {
		req = STAILQ_FIRST(&qpair->err_req_head);
		STAILQ_REMOVE_HEAD(&qpair->err_req_head, stailq);
		nvme_qpair_manual_complete_request(qpair, req,
						   req->cpl.status.sct,
						   req->cpl.status.sc, true);
	}

	TAILQ_FOREACH_SAFE(cmd, &qpair->err_cmd_head, link, entry) {
		TAILQ_REMOVE(&qpair->err_cmd_head, cmd, link);
		spdk_dma_free(cmd);
	}

	spdk_dma_free(qpair->req_buf);
}

@@ -445,6 +476,7 @@ nvme_qpair_submit_request(struct spdk_nvme_qpair *qpair, struct nvme_request *re
{
	int			rc = 0;
	struct nvme_request	*child_req, *tmp;
	struct nvme_error_cmd	*cmd;
	struct spdk_nvme_ctrlr	*ctrlr = qpair->ctrlr;
	bool			child_req_failed = false;

@@ -473,6 +505,26 @@ nvme_qpair_submit_request(struct spdk_nvme_qpair *qpair, struct nvme_request *re
		return rc;
	}

	/* queue those requests which matches with opcode in err_cmd list */
	if (spdk_unlikely(!TAILQ_EMPTY(&qpair->err_cmd_head))) {
		TAILQ_FOREACH(cmd, &qpair->err_cmd_head, link) {
			if (!cmd->do_not_submit) {
				continue;
			}

			if ((cmd->opc == req->cmd.opc) && cmd->err_count) {
				/* add to error request list and set cpl */
				req->timeout_tsc = cmd->timeout_tsc;
				req->submit_tick = spdk_get_ticks();
				req->cpl.status.sct = cmd->status.sct;
				req->cpl.status.sc = cmd->status.sc;
				STAILQ_INSERT_TAIL(&qpair->err_req_head, req, stailq);
				cmd->err_count--;
				return 0;
			}
		}
	}

	return nvme_transport_qpair_submit_request(qpair, req);
}

@@ -504,6 +556,16 @@ nvme_qpair_enable(struct spdk_nvme_qpair *qpair)
void
nvme_qpair_disable(struct spdk_nvme_qpair *qpair)
{
	struct nvme_request		*req;

	while (!STAILQ_EMPTY(&qpair->err_req_head)) {
		req = STAILQ_FIRST(&qpair->err_req_head);
		STAILQ_REMOVE_HEAD(&qpair->err_req_head, stailq);
		nvme_qpair_manual_complete_request(qpair, req,
						   req->cpl.status.sct,
						   req->cpl.status.sc, true);
	}

	nvme_transport_qpair_disable(qpair);
}

@@ -522,3 +584,64 @@ nvme_qpair_fail(struct spdk_nvme_qpair *qpair)

	nvme_transport_qpair_fail(qpair);
}

int
spdk_nvme_qpair_add_cmd_error_injection(struct spdk_nvme_ctrlr *ctrlr,
					struct spdk_nvme_qpair *qpair,
					uint8_t opc, bool do_not_submit,
					uint64_t timeout_in_us,
					uint32_t err_count,
					uint8_t sct, uint8_t sc)
{
	struct nvme_error_cmd *entry, *cmd = NULL;

	if (qpair == NULL) {
		qpair = ctrlr->adminq;
	}

	TAILQ_FOREACH(entry, &qpair->err_cmd_head, link) {
		if (entry->opc == opc) {
			cmd = entry;
			break;
		}
	}

	if (cmd == NULL) {
		cmd = spdk_dma_zmalloc(sizeof(*cmd), 64, NULL);
		if (!cmd) {
			return -ENOMEM;
		}
		TAILQ_INSERT_TAIL(&qpair->err_cmd_head, cmd, link);
	}

	cmd->do_not_submit = do_not_submit;
	cmd->err_count = err_count;
	cmd->timeout_tsc = timeout_in_us * spdk_get_ticks_hz() / 1000000ULL;
	cmd->opc = opc;
	cmd->status.sct = sct;
	cmd->status.sc = sc;

	return 0;
}

void
spdk_nvme_qpair_remove_cmd_error_injection(struct spdk_nvme_ctrlr *ctrlr,
		struct spdk_nvme_qpair *qpair,
		uint8_t opc)
{
	struct nvme_error_cmd *cmd, *entry;

	if (qpair == NULL) {
		qpair = ctrlr->adminq;
	}

	TAILQ_FOREACH_SAFE(cmd, &qpair->err_cmd_head, link, entry) {
		if (cmd->opc == opc) {
			TAILQ_REMOVE(&qpair->err_cmd_head, cmd, link);
			spdk_dma_free(cmd);
			return;
		}
	}

	return;
}
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@
SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

DIRS-y = aer reset sgl e2edp overhead deallocated_value
DIRS-y = aer reset sgl e2edp overhead deallocated_value err_injection

.PHONY: all clean $(DIRS-y)

Loading