LCOV - code coverage report
Current view: top level - module/bdev/nvme - nvme_rpc.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 0 219 0.0 %
Date: 2024-12-03 11:27:30 Functions: 0 16 0.0 %

          Line data    Source code
       1             : /*   SPDX-License-Identifier: BSD-3-Clause
       2             :  *   Copyright (C) 2018 Intel Corporation.
       3             :  *   All rights reserved.
       4             :  */
       5             : 
       6             : #include "spdk/stdinc.h"
       7             : #include "spdk/string.h"
       8             : #include "spdk/rpc.h"
       9             : #include "spdk/util.h"
      10             : #include "spdk/bdev_module.h"
      11             : #include "spdk/log.h"
      12             : 
      13             : #include "bdev_nvme.h"
      14             : #include "spdk/base64.h"
      15             : 
      16             : enum spdk_nvme_rpc_type {
      17             :         NVME_ADMIN_CMD = 1,
      18             :         NVME_IO_CMD,
      19             : };
      20             : 
      21             : struct rpc_bdev_nvme_send_cmd_req {
      22             :         char                    *name;
      23             :         int                     cmd_type;
      24             :         int                     data_direction;
      25             :         uint32_t                timeout_ms;
      26             :         uint32_t                data_len;
      27             :         uint32_t                md_len;
      28             : 
      29             :         struct spdk_nvme_cmd    *cmdbuf;
      30             :         char                    *data;
      31             :         char                    *md;
      32             : };
      33             : 
      34             : struct rpc_bdev_nvme_send_cmd_resp {
      35             :         char    *cpl_text;
      36             :         char    *data_text;
      37             :         char    *md_text;
      38             : };
      39             : 
      40             : struct rpc_bdev_nvme_send_cmd_ctx {
      41             :         struct spdk_jsonrpc_request     *jsonrpc_request;
      42             :         struct rpc_bdev_nvme_send_cmd_req       req;
      43             :         struct rpc_bdev_nvme_send_cmd_resp      resp;
      44             :         struct nvme_ctrlr               *nvme_ctrlr;
      45             :         struct spdk_io_channel          *ctrlr_io_ch;
      46             : };
      47             : 
      48             : static void
      49           0 : free_rpc_bdev_nvme_send_cmd_ctx(struct rpc_bdev_nvme_send_cmd_ctx *ctx)
      50             : {
      51           0 :         assert(ctx != NULL);
      52             : 
      53           0 :         free(ctx->req.name);
      54           0 :         free(ctx->req.cmdbuf);
      55           0 :         spdk_free(ctx->req.data);
      56           0 :         spdk_free(ctx->req.md);
      57           0 :         free(ctx->resp.cpl_text);
      58           0 :         free(ctx->resp.data_text);
      59           0 :         free(ctx->resp.md_text);
      60           0 :         free(ctx);
      61           0 : }
      62             : 
      63             : static int
      64           0 : rpc_bdev_nvme_send_cmd_resp_construct(struct rpc_bdev_nvme_send_cmd_resp *resp,
      65             :                                       struct rpc_bdev_nvme_send_cmd_req *req,
      66             :                                       const struct spdk_nvme_cpl *cpl)
      67             : {
      68           0 :         resp->cpl_text = malloc(spdk_base64_get_encoded_strlen(sizeof(*cpl)) + 1);
      69           0 :         if (!resp->cpl_text) {
      70           0 :                 return -ENOMEM;
      71             :         }
      72           0 :         spdk_base64_urlsafe_encode(resp->cpl_text, cpl, sizeof(*cpl));
      73             : 
      74           0 :         if (req->data_direction == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
      75           0 :                 if (req->data_len) {
      76           0 :                         resp->data_text = malloc(spdk_base64_get_encoded_strlen(req->data_len) + 1);
      77           0 :                         if (!resp->data_text) {
      78           0 :                                 return -ENOMEM;
      79             :                         }
      80           0 :                         spdk_base64_urlsafe_encode(resp->data_text, req->data, req->data_len);
      81             :                 }
      82           0 :                 if (req->md_len) {
      83           0 :                         resp->md_text = malloc(spdk_base64_get_encoded_strlen(req->md_len) + 1);
      84           0 :                         if (!resp->md_text) {
      85           0 :                                 return -ENOMEM;
      86             :                         }
      87           0 :                         spdk_base64_urlsafe_encode(resp->md_text, req->md, req->md_len);
      88             :                 }
      89             :         }
      90             : 
      91           0 :         return 0;
      92             : }
      93             : 
      94             : static void
      95           0 : rpc_bdev_nvme_send_cmd_complete(struct rpc_bdev_nvme_send_cmd_ctx *ctx,
      96             :                                 const struct spdk_nvme_cpl *cpl)
      97             : {
      98           0 :         struct spdk_jsonrpc_request *request = ctx->jsonrpc_request;
      99             :         struct spdk_json_write_ctx *w;
     100             :         int ret;
     101             : 
     102           0 :         ret = rpc_bdev_nvme_send_cmd_resp_construct(&ctx->resp, &ctx->req, cpl);
     103           0 :         if (ret) {
     104           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     105             :                                                  spdk_strerror(-ret));
     106           0 :                 goto out;
     107             :         }
     108             : 
     109           0 :         w = spdk_jsonrpc_begin_result(request);
     110           0 :         spdk_json_write_object_begin(w);
     111           0 :         spdk_json_write_named_string(w, "cpl", ctx->resp.cpl_text);
     112             : 
     113           0 :         if (ctx->resp.data_text) {
     114           0 :                 spdk_json_write_named_string(w, "data", ctx->resp.data_text);
     115             :         }
     116             : 
     117           0 :         if (ctx->resp.md_text) {
     118           0 :                 spdk_json_write_named_string(w, "metadata", ctx->resp.md_text);
     119             :         }
     120             : 
     121           0 :         spdk_json_write_object_end(w);
     122           0 :         spdk_jsonrpc_end_result(request, w);
     123             : 
     124           0 : out:
     125           0 :         free_rpc_bdev_nvme_send_cmd_ctx(ctx);
     126           0 :         return;
     127             : }
     128             : 
     129             : static void
     130           0 : nvme_rpc_bdev_nvme_cb(void *ref, const struct spdk_nvme_cpl *cpl)
     131             : {
     132           0 :         struct rpc_bdev_nvme_send_cmd_ctx *ctx = (struct rpc_bdev_nvme_send_cmd_ctx *)ref;
     133             : 
     134           0 :         if (ctx->ctrlr_io_ch) {
     135           0 :                 spdk_put_io_channel(ctx->ctrlr_io_ch);
     136           0 :                 ctx->ctrlr_io_ch = NULL;
     137             :         }
     138             : 
     139           0 :         rpc_bdev_nvme_send_cmd_complete(ctx, cpl);
     140           0 : }
     141             : 
     142             : static int
     143           0 : nvme_rpc_admin_cmd_bdev_nvme(struct rpc_bdev_nvme_send_cmd_ctx *ctx, struct spdk_nvme_cmd *cmd,
     144             :                              void *buf, uint32_t nbytes, uint32_t timeout_ms)
     145             : {
     146           0 :         struct nvme_ctrlr *_nvme_ctrlr = ctx->nvme_ctrlr;
     147             :         int ret;
     148             : 
     149           0 :         ret = spdk_nvme_ctrlr_cmd_admin_raw(_nvme_ctrlr->ctrlr, cmd, buf,
     150             :                                             nbytes, nvme_rpc_bdev_nvme_cb, ctx);
     151             : 
     152           0 :         return ret;
     153             : }
     154             : 
     155             : static int
     156           0 : nvme_rpc_io_cmd_bdev_nvme(struct rpc_bdev_nvme_send_cmd_ctx *ctx, struct spdk_nvme_cmd *cmd,
     157             :                           void *buf, uint32_t nbytes, void *md_buf, uint32_t md_len,
     158             :                           uint32_t timeout_ms)
     159             : {
     160           0 :         struct nvme_ctrlr *_nvme_ctrlr = ctx->nvme_ctrlr;
     161             :         struct spdk_nvme_qpair *io_qpair;
     162             :         int ret;
     163             : 
     164           0 :         ctx->ctrlr_io_ch = spdk_get_io_channel(_nvme_ctrlr);
     165           0 :         io_qpair = bdev_nvme_get_io_qpair(ctx->ctrlr_io_ch);
     166             : 
     167           0 :         ret = spdk_nvme_ctrlr_cmd_io_raw_with_md(_nvme_ctrlr->ctrlr, io_qpair,
     168             :                         cmd, buf, nbytes, md_buf, nvme_rpc_bdev_nvme_cb, ctx);
     169           0 :         if (ret) {
     170           0 :                 spdk_put_io_channel(ctx->ctrlr_io_ch);
     171             :         }
     172             : 
     173           0 :         return ret;
     174             : 
     175             : }
     176             : 
     177             : static int
     178           0 : rpc_bdev_nvme_send_cmd_exec(struct rpc_bdev_nvme_send_cmd_ctx *ctx)
     179             : {
     180           0 :         struct rpc_bdev_nvme_send_cmd_req *req = &ctx->req;
     181           0 :         int ret = -EINVAL;
     182             : 
     183           0 :         switch (req->cmd_type) {
     184           0 :         case NVME_ADMIN_CMD:
     185           0 :                 ret = nvme_rpc_admin_cmd_bdev_nvme(ctx, req->cmdbuf, req->data,
     186             :                                                    req->data_len, req->timeout_ms);
     187           0 :                 break;
     188           0 :         case NVME_IO_CMD:
     189           0 :                 ret = nvme_rpc_io_cmd_bdev_nvme(ctx, req->cmdbuf, req->data,
     190           0 :                                                 req->data_len, req->md, req->md_len, req->timeout_ms);
     191           0 :                 break;
     192             :         }
     193             : 
     194           0 :         return ret;
     195             : }
     196             : 
     197             : static int
     198           0 : rpc_decode_cmd_type(const struct spdk_json_val *val, void *out)
     199             : {
     200           0 :         int *cmd_type = out;
     201             : 
     202           0 :         if (spdk_json_strequal(val, "admin") == true) {
     203           0 :                 *cmd_type = NVME_ADMIN_CMD;
     204           0 :         } else if (spdk_json_strequal(val, "io") == true) {
     205           0 :                 *cmd_type = NVME_IO_CMD;
     206             :         } else {
     207           0 :                 SPDK_NOTICELOG("Invalid parameter value: cmd_type\n");
     208           0 :                 return -EINVAL;
     209             :         }
     210             : 
     211           0 :         return 0;
     212             : }
     213             : 
     214             : static int
     215           0 : rpc_decode_data_direction(const struct spdk_json_val *val, void *out)
     216             : {
     217           0 :         int *data_direction = out;
     218             : 
     219           0 :         if (spdk_json_strequal(val, "h2c") == true) {
     220           0 :                 *data_direction = SPDK_NVME_DATA_HOST_TO_CONTROLLER;
     221           0 :         } else if (spdk_json_strequal(val, "c2h") == true) {
     222           0 :                 *data_direction = SPDK_NVME_DATA_CONTROLLER_TO_HOST;
     223             :         } else {
     224           0 :                 SPDK_NOTICELOG("Invalid parameter value: data_direction\n");
     225           0 :                 return -EINVAL;
     226             :         }
     227             : 
     228           0 :         return 0;
     229             : }
     230             : 
     231             : static int
     232           0 : rpc_decode_cmdbuf(const struct spdk_json_val *val, void *out)
     233             : {
     234           0 :         char *text = NULL;
     235           0 :         size_t text_strlen, raw_len;
     236           0 :         struct spdk_nvme_cmd *cmdbuf, **_cmdbuf = out;
     237             :         int rc;
     238             : 
     239           0 :         rc = spdk_json_decode_string(val, &text);
     240           0 :         if (rc) {
     241           0 :                 return val->type == SPDK_JSON_VAL_STRING ? -ENOMEM : -EINVAL;
     242             :         }
     243             : 
     244           0 :         text_strlen = strlen(text);
     245           0 :         raw_len = spdk_base64_get_decoded_len(text_strlen);
     246           0 :         cmdbuf = malloc(raw_len);
     247           0 :         if (!cmdbuf) {
     248           0 :                 rc = -ENOMEM;
     249           0 :                 goto out;
     250             :         }
     251             : 
     252           0 :         rc = spdk_base64_urlsafe_decode(cmdbuf, &raw_len, text);
     253           0 :         if (rc) {
     254           0 :                 free(cmdbuf);
     255           0 :                 goto out;
     256             :         }
     257           0 :         if (raw_len != sizeof(*cmdbuf)) {
     258           0 :                 rc = -EINVAL;
     259           0 :                 free(cmdbuf);
     260           0 :                 goto out;
     261             :         }
     262             : 
     263           0 :         *_cmdbuf = cmdbuf;
     264             : 
     265           0 : out:
     266           0 :         free(text);
     267           0 :         return rc;
     268             : }
     269             : 
     270             : static int
     271           0 : rpc_decode_data(const struct spdk_json_val *val, void *out)
     272             : {
     273           0 :         struct rpc_bdev_nvme_send_cmd_req *req = (struct rpc_bdev_nvme_send_cmd_req *)out;
     274           0 :         char *text = NULL;
     275             :         size_t text_strlen;
     276             :         int rc;
     277             : 
     278           0 :         rc = spdk_json_decode_string(val, &text);
     279           0 :         if (rc) {
     280           0 :                 return val->type == SPDK_JSON_VAL_STRING ? -ENOMEM : -EINVAL;
     281             :         }
     282           0 :         text_strlen = strlen(text);
     283             : 
     284           0 :         if (req->data_len) {
     285             :                 /* data_len is decoded by param "data_len" */
     286           0 :                 if (req->data_len != spdk_base64_get_decoded_len(text_strlen)) {
     287           0 :                         rc = -EINVAL;
     288           0 :                         goto out;
     289             :                 }
     290             :         } else {
     291           0 :                 req->data_len = spdk_base64_get_decoded_len(text_strlen);
     292           0 :                 req->data = spdk_malloc(req->data_len > 0x1000 ? req->data_len : 0x1000, 0x1000,
     293             :                                         NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
     294           0 :                 if (!req->data) {
     295           0 :                         rc = -ENOMEM;
     296           0 :                         goto out;
     297             :                 }
     298             :         }
     299             : 
     300           0 :         rc = spdk_base64_urlsafe_decode(req->data, (size_t *)&req->data_len, text);
     301             : 
     302           0 : out:
     303           0 :         free(text);
     304           0 :         return rc;
     305             : }
     306             : 
     307             : static int
     308           0 : rpc_decode_data_len(const struct spdk_json_val *val, void *out)
     309             : {
     310           0 :         struct rpc_bdev_nvme_send_cmd_req *req = (struct rpc_bdev_nvme_send_cmd_req *)out;
     311           0 :         uint32_t data_len;
     312             :         int rc;
     313             : 
     314           0 :         rc = spdk_json_decode_uint32(val, &data_len);
     315           0 :         if (rc) {
     316           0 :                 return rc;
     317             :         }
     318             : 
     319           0 :         if (req->data_len) {
     320             :                 /* data_len is decoded by param "data" */
     321           0 :                 if (req->data_len != data_len) {
     322           0 :                         rc = -EINVAL;
     323             :                 }
     324             :         } else {
     325           0 :                 req->data_len = data_len;
     326           0 :                 req->data = spdk_malloc(req->data_len > 0x1000 ? req->data_len : 0x1000, 0x1000,
     327             :                                         NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
     328           0 :                 if (!req->data) {
     329           0 :                         rc = -ENOMEM;
     330             :                 }
     331             :         }
     332             : 
     333           0 :         return rc;
     334             : }
     335             : 
     336             : static int
     337           0 : rpc_decode_metadata(const struct spdk_json_val *val, void *out)
     338             : {
     339           0 :         struct rpc_bdev_nvme_send_cmd_req *req = (struct rpc_bdev_nvme_send_cmd_req *)out;
     340           0 :         char *text = NULL;
     341             :         size_t text_strlen;
     342             :         int rc;
     343             : 
     344           0 :         rc = spdk_json_decode_string(val, &text);
     345           0 :         if (rc) {
     346           0 :                 return val->type == SPDK_JSON_VAL_STRING ? -ENOMEM : -EINVAL;
     347             :         }
     348           0 :         text_strlen = strlen(text);
     349             : 
     350           0 :         if (req->md_len) {
     351             :                 /* md_len is decoded by param "metadata_len" */
     352           0 :                 if (req->md_len != spdk_base64_get_decoded_len(text_strlen)) {
     353           0 :                         rc = -EINVAL;
     354           0 :                         goto out;
     355             :                 }
     356             :         } else {
     357           0 :                 req->md_len = spdk_base64_get_decoded_len(text_strlen);
     358           0 :                 req->md = spdk_malloc(req->md_len, 0x1000, NULL,
     359             :                                       SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
     360           0 :                 if (!req->md) {
     361           0 :                         rc = -ENOMEM;
     362           0 :                         goto out;
     363             :                 }
     364             :         }
     365             : 
     366           0 :         rc = spdk_base64_urlsafe_decode(req->md, (size_t *)&req->md_len, text);
     367             : 
     368           0 : out:
     369           0 :         free(text);
     370           0 :         return rc;
     371             : }
     372             : 
     373             : static int
     374           0 : rpc_decode_metadata_len(const struct spdk_json_val *val, void *out)
     375             : {
     376           0 :         struct rpc_bdev_nvme_send_cmd_req *req = (struct rpc_bdev_nvme_send_cmd_req *)out;
     377           0 :         uint32_t md_len;
     378             :         int rc;
     379             : 
     380           0 :         rc = spdk_json_decode_uint32(val, &md_len);
     381           0 :         if (rc) {
     382           0 :                 return rc;
     383             :         }
     384             : 
     385           0 :         if (req->md_len) {
     386             :                 /* md_len is decoded by param "metadata" */
     387           0 :                 if (req->md_len != md_len) {
     388           0 :                         rc = -EINVAL;
     389             :                 }
     390             :         } else {
     391           0 :                 req->md_len = md_len;
     392           0 :                 req->md = spdk_malloc(req->md_len, 0x1000, NULL,
     393             :                                       SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
     394           0 :                 if (!req->md) {
     395           0 :                         rc = -ENOMEM;
     396             :                 }
     397             :         }
     398             : 
     399           0 :         return rc;
     400             : }
     401             : 
     402             : static const struct spdk_json_object_decoder rpc_bdev_nvme_send_cmd_req_decoders[] = {
     403             :         {"name", offsetof(struct rpc_bdev_nvme_send_cmd_req, name), spdk_json_decode_string},
     404             :         {"cmd_type", offsetof(struct rpc_bdev_nvme_send_cmd_req, cmd_type), rpc_decode_cmd_type},
     405             :         {"data_direction", offsetof(struct rpc_bdev_nvme_send_cmd_req, data_direction), rpc_decode_data_direction},
     406             :         {"cmdbuf", offsetof(struct rpc_bdev_nvme_send_cmd_req, cmdbuf), rpc_decode_cmdbuf},
     407             :         {"timeout_ms", offsetof(struct rpc_bdev_nvme_send_cmd_req, timeout_ms), spdk_json_decode_uint32, true},
     408             :         {"data_len", 0, rpc_decode_data_len, true},
     409             :         {"metadata_len", 0, rpc_decode_metadata_len, true},
     410             :         {"data", 0, rpc_decode_data, true},
     411             :         {"metadata", 0, rpc_decode_metadata, true},
     412             : };
     413             : 
     414             : static void
     415           0 : rpc_bdev_nvme_send_cmd(struct spdk_jsonrpc_request *request,
     416             :                        const struct spdk_json_val *params)
     417             : {
     418             :         struct rpc_bdev_nvme_send_cmd_ctx *ctx;
     419             :         int ret, error_code;
     420             : 
     421           0 :         ctx = calloc(1, sizeof(*ctx));
     422           0 :         if (!ctx) {
     423           0 :                 SPDK_ERRLOG("Failed at Malloc ctx\n");
     424           0 :                 error_code = SPDK_JSONRPC_ERROR_INTERNAL_ERROR;
     425           0 :                 ret = -ENOMEM;
     426           0 :                 goto invalid;
     427             :         }
     428             : 
     429           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_send_cmd_req_decoders,
     430             :                                     SPDK_COUNTOF(rpc_bdev_nvme_send_cmd_req_decoders),
     431           0 :                                     &ctx->req)) {
     432           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     433           0 :                 error_code = SPDK_JSONRPC_ERROR_INVALID_PARAMS;
     434           0 :                 ret = -EINVAL;
     435           0 :                 goto invalid;
     436             :         }
     437             : 
     438           0 :         ctx->nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->req.name);
     439           0 :         if (ctx->nvme_ctrlr == NULL) {
     440           0 :                 SPDK_ERRLOG("Failed at device lookup\n");
     441           0 :                 error_code = SPDK_JSONRPC_ERROR_INVALID_PARAMS;
     442           0 :                 ret = -EINVAL;
     443           0 :                 goto invalid;
     444             :         }
     445             : 
     446           0 :         ctx->jsonrpc_request = request;
     447             : 
     448           0 :         ret = rpc_bdev_nvme_send_cmd_exec(ctx);
     449           0 :         if (ret < 0) {
     450           0 :                 SPDK_NOTICELOG("Failed at rpc_bdev_nvme_send_cmd_exec\n");
     451           0 :                 error_code = SPDK_JSONRPC_ERROR_INTERNAL_ERROR;
     452           0 :                 goto invalid;
     453             :         }
     454             : 
     455           0 :         return;
     456             : 
     457           0 : invalid:
     458           0 :         if (ctx != NULL) {
     459           0 :                 free_rpc_bdev_nvme_send_cmd_ctx(ctx);
     460             :         }
     461           0 :         spdk_jsonrpc_send_error_response(request, error_code, spdk_strerror(-ret));
     462             : }
     463           0 : SPDK_RPC_REGISTER("bdev_nvme_send_cmd", rpc_bdev_nvme_send_cmd, SPDK_RPC_RUNTIME)

Generated by: LCOV version 1.15