Spdk/lib/vhost/vhost_rpc.c
Pawel Wodkowski ff1863f428 vhost: interrupts coalescing
Virtio spec say that any IRQ requests are only hints. So try to limit
number of interrupts generated by vhost by defining minimum interval
between sending IRQ. Coalescing is disabled by default. Can be enabled
using RPC command 'set_vhost_controller_coalescing'.

Change-Id: I9b96014d004ea0ea022b4498c6b47d30d867091a
Signed-off-by: Pawel Wodkowski <pawelx.wodkowski@intel.com>
Reviewed-on: https://review.gerrithub.io/378130
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Daniel Verkamp <daniel.verkamp@intel.com>
2017-10-27 13:06:40 -04:00

627 lines
16 KiB
C

/*-
* BSD LICENSE
*
* Copyright(c) Intel Corporation. All rights reserved.
* 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_internal/log.h"
#include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk/string.h"
#include "spdk/env.h"
#include "spdk/scsi.h"
#include "spdk/vhost.h"
#include "vhost_internal.h"
#include "spdk/bdev.h"
struct rpc_vhost_scsi_ctrlr {
char *ctrlr;
char *cpumask;
};
static void
free_rpc_vhost_scsi_ctrlr(struct rpc_vhost_scsi_ctrlr *req)
{
free(req->ctrlr);
free(req->cpumask);
}
static const struct spdk_json_object_decoder rpc_construct_vhost_ctrlr[] = {
{"ctrlr", offsetof(struct rpc_vhost_scsi_ctrlr, ctrlr), spdk_json_decode_string },
{"cpumask", offsetof(struct rpc_vhost_scsi_ctrlr, cpumask), spdk_json_decode_string, true},
};
static void
spdk_rpc_construct_vhost_scsi_controller(struct spdk_jsonrpc_request *request,
const struct spdk_json_val *params)
{
struct rpc_vhost_scsi_ctrlr req = {0};
struct spdk_json_write_ctx *w;
int rc;
char buf[64];
if (spdk_json_decode_object(params, rpc_construct_vhost_ctrlr,
SPDK_COUNTOF(rpc_construct_vhost_ctrlr),
&req)) {
SPDK_DEBUGLOG(SPDK_TRACE_VHOST_RPC, "spdk_json_decode_object failed\n");
rc = -EINVAL;
goto invalid;
}
rc = spdk_vhost_scsi_dev_construct(req.ctrlr, req.cpumask);
if (rc < 0) {
goto invalid;
}
free_rpc_vhost_scsi_ctrlr(&req);
w = spdk_jsonrpc_begin_result(request);
if (w == NULL) {
return;
}
spdk_json_write_bool(w, true);
spdk_jsonrpc_end_result(request, w);
return;
invalid:
spdk_strerror_r(-rc, buf, sizeof(buf));
free_rpc_vhost_scsi_ctrlr(&req);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
}
SPDK_RPC_REGISTER("construct_vhost_scsi_controller", spdk_rpc_construct_vhost_scsi_controller)
struct rpc_add_vhost_scsi_ctrlr_lun {
char *ctrlr;
uint32_t scsi_dev_num;
char *lun_name;
struct spdk_jsonrpc_request *request;
};
static void
free_rpc_add_vhost_scsi_ctrlr_lun(struct rpc_add_vhost_scsi_ctrlr_lun *req)
{
free(req->ctrlr);
free(req->lun_name);
free(req);
}
static const struct spdk_json_object_decoder rpc_vhost_add_lun[] = {
{"ctrlr", offsetof(struct rpc_add_vhost_scsi_ctrlr_lun, ctrlr), spdk_json_decode_string },
{"scsi_dev_num", offsetof(struct rpc_add_vhost_scsi_ctrlr_lun, scsi_dev_num), spdk_json_decode_uint32},
{"lun_name", offsetof(struct rpc_add_vhost_scsi_ctrlr_lun, lun_name), spdk_json_decode_string },
};
static int
spdk_rpc_add_vhost_scsi_lun_cb(struct spdk_vhost_dev *vdev, void *arg)
{
struct rpc_add_vhost_scsi_ctrlr_lun *rpc = arg;
struct spdk_jsonrpc_request *request = rpc->request;
struct spdk_json_write_ctx *w;
int rc;
char buf[64];
if (vdev == NULL) {
rc = -ENODEV;
goto invalid;
}
rc = spdk_vhost_scsi_dev_add_dev(vdev, rpc->scsi_dev_num, rpc->lun_name);
if (rc < 0) {
goto invalid;
}
free_rpc_add_vhost_scsi_ctrlr_lun(rpc);
w = spdk_jsonrpc_begin_result(request);
if (w == NULL) {
return -1;
}
spdk_json_write_bool(w, true);
spdk_jsonrpc_end_result(request, w);
return 0;
invalid:
free_rpc_add_vhost_scsi_ctrlr_lun(rpc);
spdk_strerror_r(-rc, buf, sizeof(buf));
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
return rc;
}
static void
spdk_rpc_add_vhost_scsi_lun(struct spdk_jsonrpc_request *request,
const struct spdk_json_val *params)
{
struct rpc_add_vhost_scsi_ctrlr_lun *req;
char buf[64];
int rc;
req = calloc(1, sizeof(*req));
if (req == NULL) {
rc = -ENOMEM;
goto invalid;
}
req->request = request;
if (spdk_json_decode_object(params, rpc_vhost_add_lun,
SPDK_COUNTOF(rpc_vhost_add_lun),
req)) {
SPDK_DEBUGLOG(SPDK_TRACE_VHOST_RPC, "spdk_json_decode_object failed\n");
rc = -EINVAL;
goto invalid;
}
if (req->ctrlr == NULL) {
SPDK_ERRLOG("No controller name\n");
rc = -EINVAL;
goto invalid;
}
spdk_vhost_call_external_event(req->ctrlr, spdk_rpc_add_vhost_scsi_lun_cb, req);
return;
invalid:
if (req) {
free_rpc_add_vhost_scsi_ctrlr_lun(req);
}
spdk_strerror_r(-rc, buf, sizeof(buf));
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
}
SPDK_RPC_REGISTER("add_vhost_scsi_lun", spdk_rpc_add_vhost_scsi_lun)
struct rpc_remove_vhost_scsi_ctrlr_dev {
char *ctrlr;
uint32_t scsi_dev_num;
struct spdk_jsonrpc_request *request;
};
static void
free_rpc_remove_vhost_scsi_ctrlr_dev(struct rpc_remove_vhost_scsi_ctrlr_dev *req)
{
free(req->ctrlr);
free(req);
}
static const struct spdk_json_object_decoder rpc_vhost_remove_dev[] = {
{"ctrlr", offsetof(struct rpc_remove_vhost_scsi_ctrlr_dev, ctrlr), spdk_json_decode_string },
{"scsi_dev_num", offsetof(struct rpc_remove_vhost_scsi_ctrlr_dev, scsi_dev_num), spdk_json_decode_uint32},
};
static int
spdk_rpc_remove_vhost_scsi_dev_finish_cb(struct spdk_vhost_dev *vdev, void *arg)
{
struct rpc_remove_vhost_scsi_ctrlr_dev *rpc = arg;
struct spdk_jsonrpc_request *request = rpc->request;
struct spdk_json_write_ctx *w;
free_rpc_remove_vhost_scsi_ctrlr_dev(rpc);
w = spdk_jsonrpc_begin_result(request);
if (w == NULL) {
return -1;
}
spdk_json_write_bool(w, true);
spdk_jsonrpc_end_result(request, w);
return 0;
}
static int
spdk_rpc_remove_vhost_scsi_dev_cb(struct spdk_vhost_dev *vdev, void *arg)
{
struct rpc_remove_vhost_scsi_ctrlr_dev *rpc = arg;
struct spdk_jsonrpc_request *request = rpc->request;
char buf[64];
int rc;
if (vdev == NULL) {
rc = -ENODEV;
goto invalid;
}
rc = spdk_vhost_scsi_dev_remove_dev(vdev, rpc->scsi_dev_num,
spdk_rpc_remove_vhost_scsi_dev_finish_cb, rpc);
if (rc < 0) {
goto invalid;
}
return 0;
invalid:
free_rpc_remove_vhost_scsi_ctrlr_dev(rpc);
spdk_strerror_r(-rc, buf, sizeof(buf));
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
return rc;
}
static void
spdk_rpc_remove_vhost_scsi_dev(struct spdk_jsonrpc_request *request,
const struct spdk_json_val *params)
{
struct rpc_remove_vhost_scsi_ctrlr_dev *req;
int rc;
char buf[64];
req = calloc(1, sizeof(*req));
if (req == NULL) {
rc = -ENOMEM;
goto invalid;
}
req->request = request;
if (spdk_json_decode_object(params, rpc_vhost_remove_dev,
SPDK_COUNTOF(rpc_vhost_remove_dev),
req)) {
SPDK_DEBUGLOG(SPDK_TRACE_VHOST_RPC, "spdk_json_decode_object failed\n");
rc = -EINVAL;
goto invalid;
}
spdk_vhost_call_external_event(req->ctrlr, spdk_rpc_remove_vhost_scsi_dev_cb, req);
return;
invalid:
if (req) {
free_rpc_remove_vhost_scsi_ctrlr_dev(req);
}
spdk_strerror_r(-rc, buf, sizeof(buf));
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
}
SPDK_RPC_REGISTER("remove_vhost_scsi_dev", spdk_rpc_remove_vhost_scsi_dev)
struct rpc_vhost_blk_ctrlr {
char *ctrlr;
char *dev_name;
char *cpumask;
bool readonly;
};
static const struct spdk_json_object_decoder rpc_construct_vhost_blk_ctrlr[] = {
{"ctrlr", offsetof(struct rpc_vhost_blk_ctrlr, ctrlr), spdk_json_decode_string },
{"dev_name", offsetof(struct rpc_vhost_blk_ctrlr, dev_name), spdk_json_decode_string },
{"cpumask", offsetof(struct rpc_vhost_blk_ctrlr, cpumask), spdk_json_decode_string, true},
{"readonly", offsetof(struct rpc_vhost_blk_ctrlr, readonly), spdk_json_decode_bool, true},
};
static void
free_rpc_vhost_blk_ctrlr(struct rpc_vhost_blk_ctrlr *req)
{
free(req->ctrlr);
free(req->dev_name);
free(req->cpumask);
}
static void
spdk_rpc_construct_vhost_blk_controller(struct spdk_jsonrpc_request *request,
const struct spdk_json_val *params)
{
struct rpc_vhost_blk_ctrlr req = {0};
struct spdk_json_write_ctx *w;
int rc;
char buf[64];
if (spdk_json_decode_object(params, rpc_construct_vhost_blk_ctrlr,
SPDK_COUNTOF(rpc_construct_vhost_blk_ctrlr),
&req)) {
SPDK_DEBUGLOG(SPDK_TRACE_VHOST_RPC, "spdk_json_decode_object failed\n");
rc = -EINVAL;
goto invalid;
}
rc = spdk_vhost_blk_construct(req.ctrlr, req.cpumask, req.dev_name, req.readonly);
if (rc < 0) {
goto invalid;
}
free_rpc_vhost_blk_ctrlr(&req);
w = spdk_jsonrpc_begin_result(request);
if (w == NULL) {
return;
}
spdk_json_write_bool(w, true);
spdk_jsonrpc_end_result(request, w);
return;
invalid:
spdk_strerror_r(-rc, buf, sizeof(buf));
free_rpc_vhost_blk_ctrlr(&req);
spdk_jsonrpc_send_error_response(request,
SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
}
SPDK_RPC_REGISTER("construct_vhost_blk_controller", spdk_rpc_construct_vhost_blk_controller)
struct rpc_remove_vhost_ctrlr {
char *ctrlr;
struct spdk_jsonrpc_request *request;
};
static const struct spdk_json_object_decoder rpc_remove_vhost_ctrlr[] = {
{"ctrlr", offsetof(struct rpc_remove_vhost_ctrlr, ctrlr), spdk_json_decode_string },
};
static void
free_rpc_remove_vhost_ctrlr(struct rpc_remove_vhost_ctrlr *req)
{
free(req->ctrlr);
free(req);
}
static int
spdk_rpc_remove_vhost_controller_cb(struct spdk_vhost_dev *vdev, void *arg)
{
struct rpc_remove_vhost_ctrlr *ctx = arg;
struct spdk_jsonrpc_request *request = ctx->request;
struct spdk_json_write_ctx *w;
int rc;
char buf[64];
if (vdev == NULL) {
rc = -ENODEV;
goto invalid;
}
rc = spdk_remove_vhost_controller(vdev);
if (rc < 0) {
goto invalid;
}
free_rpc_remove_vhost_ctrlr(ctx);
w = spdk_jsonrpc_begin_result(request);
if (w == NULL) {
return 0;
}
spdk_json_write_bool(w, true);
spdk_jsonrpc_end_result(request, w);
return 0;
invalid:
free_rpc_remove_vhost_ctrlr(ctx);
spdk_strerror_r(-rc, buf, sizeof(buf));
spdk_jsonrpc_send_error_response(request,
SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
return -1;
}
static void
spdk_rpc_remove_vhost_controller(struct spdk_jsonrpc_request *request,
const struct spdk_json_val *params)
{
struct rpc_remove_vhost_ctrlr *req;
char buf[64];
int rc;
req = calloc(1, sizeof(*req));
if (req == NULL) {
rc = -ENOMEM;
goto invalid;
}
req->request = request;
if (spdk_json_decode_object(params, rpc_remove_vhost_ctrlr,
SPDK_COUNTOF(rpc_remove_vhost_ctrlr), req)) {
SPDK_DEBUGLOG(SPDK_TRACE_VHOST_RPC, "spdk_json_decode_object failed\n");
rc = -EINVAL;
goto invalid;
}
spdk_vhost_call_external_event(req->ctrlr, spdk_rpc_remove_vhost_controller_cb, req);
return;
invalid:
if (req) {
free_rpc_remove_vhost_ctrlr(req);
}
spdk_strerror_r(-rc, buf, sizeof(buf));
spdk_jsonrpc_send_error_response(request,
SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
}
SPDK_RPC_REGISTER("remove_vhost_controller", spdk_rpc_remove_vhost_controller)
struct rpc_get_vhost_ctrlrs {
struct spdk_json_write_ctx *w;
struct spdk_jsonrpc_request *request;
};
static int
spdk_rpc_get_vhost_controllers_cb(struct spdk_vhost_dev *vdev, void *arg)
{
struct rpc_get_vhost_ctrlrs *ctx = arg;
if (vdev == NULL) {
spdk_json_write_array_end(ctx->w);
spdk_jsonrpc_end_result(ctx->request, ctx->w);
free(ctx);
return 0;
}
spdk_json_write_object_begin(ctx->w);
spdk_json_write_name(ctx->w, "ctrlr");
spdk_json_write_string(ctx->w, spdk_vhost_dev_get_name(vdev));
spdk_json_write_name(ctx->w, "cpumask");
spdk_json_write_string_fmt(ctx->w, "%#" PRIx64, spdk_vhost_dev_get_cpumask(vdev));
spdk_json_write_name(ctx->w, "backend_specific");
spdk_json_write_object_begin(ctx->w);
spdk_vhost_dump_config_json(vdev, ctx->w);
spdk_json_write_object_end(ctx->w);
spdk_json_write_object_end(ctx->w); // ctrl
return 0;
}
static void
spdk_rpc_get_vhost_controllers(struct spdk_jsonrpc_request *request,
const struct spdk_json_val *params)
{
struct rpc_get_vhost_ctrlrs *ctx;
struct spdk_json_write_ctx *w;
char buf[64];
if (params != NULL) {
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
"get_vhost_controllers requires no parameters");
return;
}
w = spdk_jsonrpc_begin_result(request);
if (w == NULL) {
return;
}
spdk_json_write_array_begin(w);
ctx = calloc(1, sizeof(*ctx));
if (ctx == NULL) {
spdk_strerror_r(-ENOMEM, buf, sizeof(buf));
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, buf);
return;
}
ctx->w = w;
ctx->request = request;
spdk_vhost_call_external_event_foreach(spdk_rpc_get_vhost_controllers_cb, ctx);
}
SPDK_RPC_REGISTER("get_vhost_controllers", spdk_rpc_get_vhost_controllers)
struct rpc_vhost_ctrlr_coalescing {
char *ctrlr;
uint32_t delay_base_us;
uint32_t iops_threshold;
struct spdk_jsonrpc_request *request;
};
static const struct spdk_json_object_decoder rpc_set_vhost_ctrlr_coalescing[] = {
{"ctrlr", offsetof(struct rpc_vhost_ctrlr_coalescing, ctrlr), spdk_json_decode_string },
{"delay_base_us", offsetof(struct rpc_vhost_ctrlr_coalescing, delay_base_us), spdk_json_decode_uint32},
{"iops_threshold", offsetof(struct rpc_vhost_ctrlr_coalescing, iops_threshold), spdk_json_decode_uint32},
};
static void
free_rpc_set_vhost_controllers_event_coalescing(struct rpc_vhost_ctrlr_coalescing *req)
{
if (!req) {
return;
}
free(req->ctrlr);
free(req);
}
static int
spdk_rpc_set_vhost_controller_coalescing_cb(struct spdk_vhost_dev *vdev, void *arg)
{
struct rpc_vhost_ctrlr_coalescing *req = arg;
struct spdk_json_write_ctx *w;
char buf[64];
int rc;
if (vdev == NULL) {
rc = -ENODEV;
goto invalid;
}
rc = spdk_vhost_set_coalescing(vdev, req->delay_base_us, req->iops_threshold);
if (rc) {
goto invalid;
}
w = spdk_jsonrpc_begin_result(req->request);
if (w != NULL) {
spdk_json_write_bool(w, true);
spdk_jsonrpc_end_result(req->request, w);
}
free_rpc_set_vhost_controllers_event_coalescing(req);
return 0;
invalid:
spdk_strerror_r(-rc, buf, sizeof(buf));
spdk_jsonrpc_send_error_response(req->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
free_rpc_set_vhost_controllers_event_coalescing(req);
return 0;
}
static void
spdk_rpc_set_vhost_controller_coalescing(struct spdk_jsonrpc_request *request,
const struct spdk_json_val *params)
{
struct rpc_vhost_ctrlr_coalescing *req;
char buf[64];
int rc;
req = calloc(1, sizeof(struct rpc_vhost_ctrlr_coalescing));
if (!req) {
rc = -ENOMEM;
goto invalid;
}
if (spdk_json_decode_object(params, rpc_set_vhost_ctrlr_coalescing,
SPDK_COUNTOF(rpc_set_vhost_ctrlr_coalescing), req)) {
SPDK_DEBUGLOG(SPDK_TRACE_VHOST_RPC, "spdk_json_decode_object failed\n");
rc = -EINVAL;
goto invalid;
}
req->request = request;
spdk_vhost_call_external_event(req->ctrlr, spdk_rpc_set_vhost_controller_coalescing_cb, req);
return;
invalid:
spdk_strerror_r(-rc, buf, sizeof(buf));
free_rpc_set_vhost_controllers_event_coalescing(req);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
}
SPDK_RPC_REGISTER("set_vhost_controller_coalescing", spdk_rpc_set_vhost_controller_coalescing)
SPDK_LOG_REGISTER_TRACE_FLAG("vhost_rpc", SPDK_TRACE_VHOST_RPC)