LCOV - code coverage report
Current view: top level - spdk/module/vfu_device - vfu_virtio_rpc.c (source / functions) Hit Total Coverage
Test: Combined Lines: 83 148 56.1 %
Date: 2024-08-13 00:13:26 Functions: 17 19 89.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 18 40 45.0 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2022 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/bdev.h"
       7                 :            : #include "spdk/log.h"
       8                 :            : #include "spdk/rpc.h"
       9                 :            : #include "spdk/env.h"
      10                 :            : #include "spdk/string.h"
      11                 :            : #include "spdk/util.h"
      12                 :            : #include "spdk/thread.h"
      13                 :            : #include "spdk/config.h"
      14                 :            : 
      15                 :            : #include "vfu_virtio_internal.h"
      16                 :            : 
      17                 :            : struct rpc_delete_vfu_endpoint {
      18                 :            :         char            *name;
      19                 :            : };
      20                 :            : 
      21                 :            : static const struct spdk_json_object_decoder rpc_delete_vfu_endpoint_decode[] = {
      22                 :            :         {"name", offsetof(struct rpc_delete_vfu_endpoint, name), spdk_json_decode_string }
      23                 :            : };
      24                 :            : 
      25                 :            : static void
      26                 :          2 : free_rpc_delete_vfu_endpoint(struct rpc_delete_vfu_endpoint *req)
      27                 :            : {
      28                 :          2 :         free(req->name);
      29                 :          2 : }
      30                 :            : 
      31                 :            : static void
      32                 :          2 : rpc_vfu_virtio_delete_endpoint(struct spdk_jsonrpc_request *request,
      33                 :            :                                const struct spdk_json_val *params)
      34                 :            : {
      35                 :          2 :         struct rpc_delete_vfu_endpoint req = {0};
      36                 :            :         int rc;
      37                 :            : 
      38         [ -  + ]:          2 :         if (spdk_json_decode_object(params, rpc_delete_vfu_endpoint_decode,
      39                 :            :                                     SPDK_COUNTOF(rpc_delete_vfu_endpoint_decode),
      40                 :            :                                     &req)) {
      41                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
      42                 :          0 :                 rc = -EINVAL;
      43                 :          0 :                 goto invalid;
      44                 :            :         }
      45                 :            : 
      46                 :          2 :         rc = spdk_vfu_delete_endpoint(req.name);
      47         [ -  + ]:          2 :         if (rc < 0) {
      48                 :          0 :                 goto invalid;
      49                 :            :         }
      50                 :          2 :         free_rpc_delete_vfu_endpoint(&req);
      51                 :            : 
      52                 :          2 :         spdk_jsonrpc_send_bool_response(request, true);
      53                 :          2 :         return;
      54                 :            : 
      55                 :          0 : invalid:
      56                 :          0 :         free_rpc_delete_vfu_endpoint(&req);
      57                 :          0 :         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
      58                 :            :                                          spdk_strerror(-rc));
      59                 :            : }
      60                 :        199 : SPDK_RPC_REGISTER("vfu_virtio_delete_endpoint", rpc_vfu_virtio_delete_endpoint,
      61                 :            :                   SPDK_RPC_RUNTIME)
      62                 :            : 
      63                 :            : struct rpc_vfu_virtio_create_blk {
      64                 :            :         char            *name;
      65                 :            :         char            *bdev_name;
      66                 :            :         char            *cpumask;
      67                 :            :         uint16_t        num_queues;
      68                 :            :         uint16_t        qsize;
      69                 :            :         bool            packed_ring;
      70                 :            : };
      71                 :            : 
      72                 :            : static const struct spdk_json_object_decoder rpc_construct_vfu_virtio_create_blk[] = {
      73                 :            :         {"name", offsetof(struct rpc_vfu_virtio_create_blk, name), spdk_json_decode_string },
      74                 :            :         {"bdev_name", offsetof(struct rpc_vfu_virtio_create_blk, bdev_name), spdk_json_decode_string },
      75                 :            :         {"cpumask", offsetof(struct rpc_vfu_virtio_create_blk, cpumask), spdk_json_decode_string, true},
      76                 :            :         {"num_queues", offsetof(struct rpc_vfu_virtio_create_blk, num_queues), spdk_json_decode_uint16, true },
      77                 :            :         {"qsize", offsetof(struct rpc_vfu_virtio_create_blk, qsize), spdk_json_decode_uint16, true },
      78                 :            :         {"packed_ring", offsetof(struct rpc_vfu_virtio_create_blk, packed_ring), spdk_json_decode_bool, true},
      79                 :            : };
      80                 :            : 
      81                 :            : static void
      82                 :          2 : free_rpc_vfu_virtio_create_blk(struct rpc_vfu_virtio_create_blk *req)
      83                 :            : {
      84                 :          2 :         free(req->name);
      85                 :          2 :         free(req->bdev_name);
      86                 :          2 :         free(req->cpumask);
      87                 :          2 : }
      88                 :            : 
      89                 :            : static void
      90                 :          2 : rpc_vfu_virtio_create_blk_endpoint(struct spdk_jsonrpc_request *request,
      91                 :            :                                    const struct spdk_json_val *params)
      92                 :            : {
      93                 :          2 :         struct rpc_vfu_virtio_create_blk req = {0};
      94                 :            :         int rc;
      95                 :            : 
      96         [ -  + ]:          2 :         if (spdk_json_decode_object(params, rpc_construct_vfu_virtio_create_blk,
      97                 :            :                                     SPDK_COUNTOF(rpc_construct_vfu_virtio_create_blk),
      98                 :            :                                     &req)) {
      99                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     100                 :          0 :                 rc = -EINVAL;
     101                 :          0 :                 goto invalid;
     102                 :            :         }
     103                 :            : 
     104                 :          2 :         rc = spdk_vfu_create_endpoint(req.name, req.cpumask, "virtio_blk");
     105         [ -  + ]:          2 :         if (rc) {
     106                 :          0 :                 SPDK_ERRLOG("Failed to create virtio_blk endpoint\n");
     107                 :          0 :                 goto invalid;
     108                 :            :         }
     109                 :            : 
     110                 :          2 :         rc = vfu_virtio_blk_add_bdev(req.name, req.bdev_name, req.num_queues, req.qsize,
     111         [ -  + ]:          2 :                                      req.packed_ring);
     112         [ -  + ]:          2 :         if (rc < 0) {
     113                 :          0 :                 spdk_vfu_delete_endpoint(req.name);
     114                 :          0 :                 goto invalid;
     115                 :            :         }
     116                 :          2 :         free_rpc_vfu_virtio_create_blk(&req);
     117                 :            : 
     118                 :          2 :         spdk_jsonrpc_send_bool_response(request, true);
     119                 :          2 :         return;
     120                 :            : 
     121                 :          0 : invalid:
     122                 :          0 :         free_rpc_vfu_virtio_create_blk(&req);
     123                 :          0 :         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     124                 :            :                                          spdk_strerror(-rc));
     125                 :            : }
     126                 :        199 : SPDK_RPC_REGISTER("vfu_virtio_create_blk_endpoint", rpc_vfu_virtio_create_blk_endpoint,
     127                 :            :                   SPDK_RPC_RUNTIME)
     128                 :            : 
     129                 :            : struct rpc_vfu_virtio_scsi {
     130                 :            :         char            *name;
     131                 :            :         uint8_t         scsi_target_num;
     132                 :            :         char            *bdev_name;
     133                 :            : };
     134                 :            : 
     135                 :            : static const struct spdk_json_object_decoder rpc_construct_vfu_virtio_scsi[] = {
     136                 :            :         {"name", offsetof(struct rpc_vfu_virtio_scsi, name), spdk_json_decode_string },
     137                 :            :         {"scsi_target_num", offsetof(struct rpc_vfu_virtio_scsi, scsi_target_num), spdk_json_decode_uint8 },
     138                 :            :         {"bdev_name", offsetof(struct rpc_vfu_virtio_scsi, bdev_name), spdk_json_decode_string },
     139                 :            : };
     140                 :            : 
     141                 :            : static void
     142                 :          3 : free_rpc_vfu_virtio_scsi(struct rpc_vfu_virtio_scsi *req)
     143                 :            : {
     144                 :          3 :         free(req->name);
     145                 :          3 :         free(req->bdev_name);
     146                 :          3 : }
     147                 :            : 
     148                 :            : static void
     149                 :          3 : rpc_vfu_virtio_scsi_add_target(struct spdk_jsonrpc_request *request,
     150                 :            :                                const struct spdk_json_val *params)
     151                 :            : {
     152                 :          3 :         struct rpc_vfu_virtio_scsi req = {0};
     153                 :            :         int rc;
     154                 :            : 
     155         [ -  + ]:          3 :         if (spdk_json_decode_object(params, rpc_construct_vfu_virtio_scsi,
     156                 :            :                                     SPDK_COUNTOF(rpc_construct_vfu_virtio_scsi),
     157                 :            :                                     &req)) {
     158                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     159                 :          0 :                 rc = -EINVAL;
     160                 :          0 :                 goto invalid;
     161                 :            :         }
     162                 :            : 
     163                 :          3 :         rc = vfu_virtio_scsi_add_target(req.name, req.scsi_target_num, req.bdev_name);;
     164         [ -  + ]:          3 :         if (rc < 0) {
     165                 :          0 :                 goto invalid;
     166                 :            :         }
     167                 :            : 
     168                 :          3 :         free_rpc_vfu_virtio_scsi(&req);
     169                 :          3 :         spdk_jsonrpc_send_bool_response(request, true);
     170                 :          3 :         return;
     171                 :            : 
     172                 :          0 : invalid:
     173                 :          0 :         free_rpc_vfu_virtio_scsi(&req);
     174                 :          0 :         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     175                 :            :                                          spdk_strerror(-rc));
     176                 :            : }
     177                 :        199 : SPDK_RPC_REGISTER("vfu_virtio_scsi_add_target", rpc_vfu_virtio_scsi_add_target,
     178                 :            :                   SPDK_RPC_RUNTIME)
     179                 :            : 
     180                 :            : struct rpc_vfu_virtio_scsi_remove {
     181                 :            :         char            *name;
     182                 :            :         uint8_t         scsi_target_num;
     183                 :            : };
     184                 :            : 
     185                 :            : static const struct spdk_json_object_decoder rpc_remove_vfu_virtio_scsi_target[] = {
     186                 :            :         {"name", offsetof(struct rpc_vfu_virtio_scsi_remove, name), spdk_json_decode_string },
     187                 :            :         {"scsi_target_num", offsetof(struct rpc_vfu_virtio_scsi_remove, scsi_target_num), spdk_json_decode_uint8 },
     188                 :            : };
     189                 :            : 
     190                 :            : static void
     191                 :          0 : free_rpc_vfu_virtio_scsi_remove(struct rpc_vfu_virtio_scsi_remove *req)
     192                 :            : {
     193                 :          0 :         free(req->name);
     194                 :          0 : }
     195                 :            : 
     196                 :            : static void
     197                 :          0 : rpc_vfu_virtio_scsi_remove_target(struct spdk_jsonrpc_request *request,
     198                 :            :                                   const struct spdk_json_val *params)
     199                 :            : {
     200                 :          0 :         struct rpc_vfu_virtio_scsi_remove req = {0};
     201                 :            :         int rc;
     202                 :            : 
     203         [ #  # ]:          0 :         if (spdk_json_decode_object(params, rpc_remove_vfu_virtio_scsi_target,
     204                 :            :                                     SPDK_COUNTOF(rpc_remove_vfu_virtio_scsi_target),
     205                 :            :                                     &req)) {
     206                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     207                 :          0 :                 rc = -EINVAL;
     208                 :          0 :                 goto invalid;
     209                 :            :         }
     210                 :            : 
     211                 :          0 :         rc = vfu_virtio_scsi_remove_target(req.name, req.scsi_target_num);
     212         [ #  # ]:          0 :         if (rc < 0) {
     213                 :          0 :                 goto invalid;
     214                 :            :         }
     215                 :            : 
     216                 :          0 :         free_rpc_vfu_virtio_scsi_remove(&req);
     217                 :          0 :         spdk_jsonrpc_send_bool_response(request, true);
     218                 :          0 :         return;
     219                 :            : 
     220                 :          0 : invalid:
     221                 :          0 :         free_rpc_vfu_virtio_scsi_remove(&req);
     222                 :          0 :         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     223                 :            :                                          spdk_strerror(-rc));
     224                 :            : }
     225                 :        199 : SPDK_RPC_REGISTER("vfu_virtio_scsi_remove_target", rpc_vfu_virtio_scsi_remove_target,
     226                 :            :                   SPDK_RPC_RUNTIME)
     227                 :            : 
     228                 :            : struct rpc_vfu_virtio_create_scsi {
     229                 :            :         char            *name;
     230                 :            :         char            *cpumask;
     231                 :            :         uint16_t        num_io_queues;
     232                 :            :         uint16_t        qsize;
     233                 :            :         bool            packed_ring;
     234                 :            : };
     235                 :            : 
     236                 :            : static const struct spdk_json_object_decoder rpc_construct_vfu_virtio_create_scsi[] = {
     237                 :            :         {"name", offsetof(struct rpc_vfu_virtio_create_scsi, name), spdk_json_decode_string },
     238                 :            :         {"cpumask", offsetof(struct rpc_vfu_virtio_create_scsi, cpumask), spdk_json_decode_string, true},
     239                 :            :         {"num_io_queues", offsetof(struct rpc_vfu_virtio_create_scsi, num_io_queues), spdk_json_decode_uint16, true },
     240                 :            :         {"qsize", offsetof(struct rpc_vfu_virtio_create_scsi, qsize), spdk_json_decode_uint16, true },
     241                 :            :         {"packed_ring", offsetof(struct rpc_vfu_virtio_create_scsi, packed_ring), spdk_json_decode_bool, true},
     242                 :            : };
     243                 :            : 
     244                 :            : static void
     245                 :          2 : free_rpc_vfu_virtio_create_scsi(struct rpc_vfu_virtio_create_scsi *req)
     246                 :            : {
     247                 :          2 :         free(req->name);
     248                 :          2 :         free(req->cpumask);
     249                 :          2 : }
     250                 :            : 
     251                 :            : static void
     252                 :          2 : rpc_vfu_virtio_create_scsi_endpoint(struct spdk_jsonrpc_request *request,
     253                 :            :                                     const struct spdk_json_val *params)
     254                 :            : {
     255                 :          2 :         struct rpc_vfu_virtio_create_scsi req = {0};
     256                 :            :         int rc;
     257                 :            : 
     258         [ -  + ]:          2 :         if (spdk_json_decode_object(params, rpc_construct_vfu_virtio_create_scsi,
     259                 :            :                                     SPDK_COUNTOF(rpc_construct_vfu_virtio_create_scsi),
     260                 :            :                                     &req)) {
     261                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     262                 :          0 :                 rc = -EINVAL;
     263                 :          0 :                 goto invalid;
     264                 :            :         }
     265                 :            : 
     266                 :          2 :         rc = spdk_vfu_create_endpoint(req.name, req.cpumask, "virtio_scsi");
     267         [ -  + ]:          2 :         if (rc) {
     268                 :          0 :                 SPDK_ERRLOG("Failed to create virtio_blk endpoint\n");
     269                 :          0 :                 goto invalid;
     270                 :            :         }
     271                 :            : 
     272         [ -  + ]:          2 :         rc = vfu_virtio_scsi_set_options(req.name, req.num_io_queues, req.qsize, req.packed_ring);
     273         [ -  + ]:          2 :         if (rc < 0) {
     274                 :          0 :                 spdk_vfu_delete_endpoint(req.name);
     275                 :          0 :                 goto invalid;
     276                 :            :         }
     277                 :          2 :         free_rpc_vfu_virtio_create_scsi(&req);
     278                 :            : 
     279                 :          2 :         spdk_jsonrpc_send_bool_response(request, true);
     280                 :          2 :         return;
     281                 :            : 
     282                 :          0 : invalid:
     283                 :          0 :         free_rpc_vfu_virtio_create_scsi(&req);
     284                 :          0 :         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     285                 :            :                                          spdk_strerror(-rc));
     286                 :            : }
     287                 :        199 : SPDK_RPC_REGISTER("vfu_virtio_create_scsi_endpoint", rpc_vfu_virtio_create_scsi_endpoint,
     288                 :            :                   SPDK_RPC_RUNTIME)
     289                 :            : 
     290                 :            : #ifdef SPDK_CONFIG_FSDEV
     291                 :            : struct rpc_vfu_virtio_create_fs {
     292                 :            :         char            *name;
     293                 :            :         char            *fsdev_name;
     294                 :            :         char            *tag;
     295                 :            :         char            *cpumask;
     296                 :            :         uint16_t        num_queues;
     297                 :            :         uint16_t        qsize;
     298                 :            :         bool            packed_ring;
     299                 :            :         struct spdk_jsonrpc_request *request;
     300                 :            : };
     301                 :            : 
     302                 :            : static const struct spdk_json_object_decoder rpc_construct_vfu_virtio_create_fs[] = {
     303                 :            :         {"name", offsetof(struct rpc_vfu_virtio_create_fs, name), spdk_json_decode_string },
     304                 :            :         {"fsdev_name", offsetof(struct rpc_vfu_virtio_create_fs, fsdev_name), spdk_json_decode_string },
     305                 :            :         {"tag", offsetof(struct rpc_vfu_virtio_create_fs, tag), spdk_json_decode_string },
     306                 :            :         {"cpumask", offsetof(struct rpc_vfu_virtio_create_fs, cpumask), spdk_json_decode_string, true},
     307                 :            :         {"num_queues", offsetof(struct rpc_vfu_virtio_create_fs, num_queues), spdk_json_decode_uint16, true },
     308                 :            :         {"qsize", offsetof(struct rpc_vfu_virtio_create_fs, qsize), spdk_json_decode_uint16, true },
     309                 :            :         {"packed_ring", offsetof(struct rpc_vfu_virtio_create_fs, packed_ring), spdk_json_decode_bool, true},
     310                 :            : };
     311                 :            : 
     312                 :            : static void
     313                 :          1 : free_rpc_vfu_virtio_create_fs(struct rpc_vfu_virtio_create_fs *req)
     314                 :            : {
     315         [ +  - ]:          1 :         if (req) {
     316                 :          1 :                 free(req->name);
     317                 :          1 :                 free(req->fsdev_name);
     318                 :          1 :                 free(req->tag);
     319                 :          1 :                 free(req->cpumask);
     320                 :          1 :                 free(req);
     321                 :            :         }
     322                 :          1 : }
     323                 :            : 
     324                 :            : static void
     325                 :          1 : rpc_vfu_virtio_create_fs_endpoint_cpl(void *cb_arg, int status)
     326                 :            : {
     327                 :          1 :         struct rpc_vfu_virtio_create_fs *req = cb_arg;
     328                 :            : 
     329                 :          1 :         spdk_jsonrpc_send_bool_response(req->request, true);
     330                 :            : 
     331                 :          1 :         free_rpc_vfu_virtio_create_fs(req);
     332                 :          1 : }
     333                 :            : 
     334                 :            : static void
     335                 :          1 : rpc_vfu_virtio_create_fs_endpoint(struct spdk_jsonrpc_request *request,
     336                 :            :                                   const struct spdk_json_val *params)
     337                 :            : {
     338                 :            :         struct rpc_vfu_virtio_create_fs *req;
     339                 :            :         int rc;
     340                 :            : 
     341                 :          1 :         req = calloc(1, sizeof(*req));
     342         [ -  + ]:          1 :         if (!req)  {
     343                 :          0 :                 SPDK_ERRLOG("cannot allocate req\n");
     344                 :          0 :                 rc = -ENOMEM;
     345                 :          0 :                 goto invalid;
     346                 :            :         }
     347                 :            : 
     348         [ -  + ]:          1 :         if (spdk_json_decode_object(params, rpc_construct_vfu_virtio_create_fs,
     349                 :            :                                     SPDK_COUNTOF(rpc_construct_vfu_virtio_create_fs),
     350                 :            :                                     req)) {
     351                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     352                 :          0 :                 rc = -EINVAL;
     353                 :          0 :                 goto invalid;
     354                 :            :         }
     355                 :            : 
     356                 :          1 :         rc = spdk_vfu_create_endpoint(req->name, req->cpumask, "virtio_fs");
     357         [ -  + ]:          1 :         if (rc) {
     358                 :          0 :                 SPDK_ERRLOG("Failed to create virtio_blk endpoint\n");
     359                 :          0 :                 goto invalid;
     360                 :            :         }
     361                 :            : 
     362                 :          1 :         req->request = request;
     363                 :            : 
     364                 :          1 :         rc = vfu_virtio_fs_add_fsdev(req->name, req->fsdev_name, req->tag, req->num_queues, req->qsize,
     365         [ -  + ]:          1 :                                      req->packed_ring, rpc_vfu_virtio_create_fs_endpoint_cpl, req);
     366         [ -  + ]:          1 :         if (rc < 0) {
     367                 :          0 :                 spdk_vfu_delete_endpoint(req->name);
     368                 :          0 :                 goto invalid;
     369                 :            :         }
     370                 :            : 
     371                 :          1 :         return;
     372                 :            : 
     373                 :          0 : invalid:
     374                 :          0 :         free_rpc_vfu_virtio_create_fs(req);
     375                 :          0 :         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     376                 :            :                                          spdk_strerror(-rc));
     377                 :            : }
     378                 :        199 : SPDK_RPC_REGISTER("vfu_virtio_create_fs_endpoint", rpc_vfu_virtio_create_fs_endpoint,
     379                 :            :                   SPDK_RPC_RUNTIME)
     380                 :            : #endif

Generated by: LCOV version 1.14