LCOV - code coverage report
Current view: top level - spdk/module/bdev/error - vbdev_error_rpc.c (source / functions) Hit Total Coverage
Test: Combined Lines: 64 87 73.6 %
Date: 2024-07-16 01:05:53 Functions: 12 12 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 18 32 56.2 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2017 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/log.h"
      11                 :            : #include "vbdev_error.h"
      12                 :            : 
      13                 :            : static int
      14                 :         81 : rpc_error_bdev_decode_io_type(const struct spdk_json_val *val, void *out)
      15                 :            : {
      16                 :         81 :         uint32_t *io_type = out;
      17                 :            : 
      18         [ +  + ]:         81 :         if (spdk_json_strequal(val, "read") == true) {
      19                 :         36 :                 *io_type = SPDK_BDEV_IO_TYPE_READ;
      20         [ +  + ]:         45 :         } else if (spdk_json_strequal(val, "write") == true) {
      21                 :         37 :                 *io_type = SPDK_BDEV_IO_TYPE_WRITE;
      22         [ -  + ]:          8 :         } else if (spdk_json_strequal(val, "flush") == true) {
      23                 :          0 :                 *io_type = SPDK_BDEV_IO_TYPE_FLUSH;
      24         [ -  + ]:          8 :         } else if (spdk_json_strequal(val, "unmap") == true) {
      25                 :          0 :                 *io_type = SPDK_BDEV_IO_TYPE_UNMAP;
      26         [ +  - ]:          8 :         } else if (spdk_json_strequal(val, "all") == true) {
      27                 :          8 :                 *io_type = 0xffffffff;
      28         [ #  # ]:          0 :         } else if (spdk_json_strequal(val, "clear") == true) {
      29                 :          0 :                 *io_type = 0;
      30                 :            :         } else {
      31                 :          0 :                 SPDK_NOTICELOG("Invalid parameter value: io_type\n");
      32                 :          0 :                 return -EINVAL;
      33                 :            :         }
      34                 :            : 
      35                 :         81 :         return 0;
      36                 :            : }
      37                 :            : 
      38                 :            : static int
      39                 :         81 : rpc_error_bdev_decode_error_type(const struct spdk_json_val *val, void *out)
      40                 :            : {
      41                 :         81 :         uint32_t *error_type = out;
      42                 :            : 
      43         [ +  + ]:         81 :         if (spdk_json_strequal(val, "failure") == true) {
      44                 :         80 :                 *error_type = VBDEV_IO_FAILURE;
      45         [ -  + ]:          1 :         } else if (spdk_json_strequal(val, "pending") == true) {
      46                 :          0 :                 *error_type = VBDEV_IO_PENDING;
      47         [ -  + ]:          1 :         } else if (spdk_json_strequal(val, "corrupt_data") == true) {
      48                 :          0 :                 *error_type = VBDEV_IO_CORRUPT_DATA;
      49         [ +  - ]:          1 :         } else if (spdk_json_strequal(val, "nomem") == true) {
      50                 :          1 :                 *error_type = VBDEV_IO_NOMEM;
      51                 :            :         } else {
      52                 :          0 :                 SPDK_NOTICELOG("Invalid parameter value: error_type\n");
      53                 :          0 :                 return -EINVAL;
      54                 :            :         }
      55                 :            : 
      56                 :         81 :         return 0;
      57                 :            : }
      58                 :            : 
      59                 :            : struct rpc_bdev_error_create {
      60                 :            :         char *base_name;
      61                 :            :         struct spdk_uuid uuid;
      62                 :            : };
      63                 :            : 
      64                 :            : static void
      65                 :        231 : free_rpc_bdev_error_create(struct rpc_bdev_error_create *req)
      66                 :            : {
      67                 :        231 :         free(req->base_name);
      68                 :        231 : }
      69                 :            : 
      70                 :            : static const struct spdk_json_object_decoder rpc_bdev_error_create_decoders[] = {
      71                 :            :         {"base_name", offsetof(struct rpc_bdev_error_create, base_name), spdk_json_decode_string},
      72                 :            :         {"uuid", offsetof(struct rpc_bdev_error_create, uuid), spdk_json_decode_uuid, true},
      73                 :            : };
      74                 :            : 
      75                 :            : static void
      76                 :        231 : rpc_bdev_error_create(struct spdk_jsonrpc_request *request,
      77                 :            :                       const struct spdk_json_val *params)
      78                 :            : {
      79                 :        231 :         struct rpc_bdev_error_create req = {};
      80                 :        231 :         int rc = 0;
      81                 :            : 
      82         [ -  + ]:        231 :         if (spdk_json_decode_object(params, rpc_bdev_error_create_decoders,
      83                 :            :                                     SPDK_COUNTOF(rpc_bdev_error_create_decoders),
      84                 :            :                                     &req)) {
      85                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
      86                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
      87                 :            :                                                  "spdk_json_decode_object failed");
      88                 :          0 :                 goto cleanup;
      89                 :            :         }
      90                 :            : 
      91                 :        231 :         rc = vbdev_error_create(req.base_name, &req.uuid);
      92         [ -  + ]:        231 :         if (rc) {
      93                 :          0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
      94                 :          0 :                 goto cleanup;
      95                 :            :         }
      96                 :            : 
      97                 :        231 :         spdk_jsonrpc_send_bool_response(request, true);
      98                 :            : 
      99                 :        231 : cleanup:
     100                 :        231 :         free_rpc_bdev_error_create(&req);
     101                 :        231 : }
     102                 :       2145 : SPDK_RPC_REGISTER("bdev_error_create", rpc_bdev_error_create, SPDK_RPC_RUNTIME)
     103                 :            : 
     104                 :            : struct rpc_delete_error {
     105                 :            :         char *name;
     106                 :            : };
     107                 :            : 
     108                 :            : static void
     109                 :         10 : free_rpc_delete_error(struct rpc_delete_error *r)
     110                 :            : {
     111                 :         10 :         free(r->name);
     112                 :         10 : }
     113                 :            : 
     114                 :            : static const struct spdk_json_object_decoder rpc_delete_error_decoders[] = {
     115                 :            :         {"name", offsetof(struct rpc_delete_error, name), spdk_json_decode_string},
     116                 :            : };
     117                 :            : 
     118                 :            : static void
     119                 :         10 : rpc_bdev_error_delete_cb(void *cb_arg, int bdeverrno)
     120                 :            : {
     121                 :         10 :         struct spdk_jsonrpc_request *request = cb_arg;
     122                 :            : 
     123         [ +  - ]:         10 :         if (bdeverrno == 0) {
     124                 :         10 :                 spdk_jsonrpc_send_bool_response(request, true);
     125                 :            :         } else {
     126                 :          0 :                 spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
     127                 :            :         }
     128                 :         10 : }
     129                 :            : 
     130                 :            : static void
     131                 :         10 : rpc_bdev_error_delete(struct spdk_jsonrpc_request *request,
     132                 :            :                       const struct spdk_json_val *params)
     133                 :            : {
     134                 :         10 :         struct rpc_delete_error req = {NULL};
     135                 :            : 
     136         [ -  + ]:         10 :         if (spdk_json_decode_object(params, rpc_delete_error_decoders,
     137                 :            :                                     SPDK_COUNTOF(rpc_delete_error_decoders),
     138                 :            :                                     &req)) {
     139                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     140                 :            :                                                  "spdk_json_decode_object failed");
     141                 :          0 :                 goto cleanup;
     142                 :            :         }
     143                 :            : 
     144                 :         10 :         vbdev_error_delete(req.name, rpc_bdev_error_delete_cb, request);
     145                 :            : 
     146                 :         10 : cleanup:
     147                 :         10 :         free_rpc_delete_error(&req);
     148                 :         10 : }
     149                 :       2145 : SPDK_RPC_REGISTER("bdev_error_delete", rpc_bdev_error_delete, SPDK_RPC_RUNTIME)
     150                 :            : 
     151                 :            : struct rpc_error_information {
     152                 :            :         char *name;
     153                 :            :         struct vbdev_error_inject_opts opts;
     154                 :            : };
     155                 :            : 
     156                 :            : static const struct spdk_json_object_decoder rpc_error_information_decoders[] = {
     157                 :            :         {"name", offsetof(struct rpc_error_information, name), spdk_json_decode_string},
     158                 :            :         {"io_type", offsetof(struct rpc_error_information, opts.io_type), rpc_error_bdev_decode_io_type},
     159                 :            :         {"error_type", offsetof(struct rpc_error_information, opts.error_type), rpc_error_bdev_decode_error_type},
     160                 :            :         {"num", offsetof(struct rpc_error_information, opts.error_num), spdk_json_decode_uint32, true},
     161                 :            :         {"queue_depth", offsetof(struct rpc_error_information, opts.error_qd), spdk_json_decode_uint64, true},
     162                 :            :         {"corrupt_offset", offsetof(struct rpc_error_information, opts.corrupt_offset), spdk_json_decode_uint64, true},
     163                 :            :         {"corrupt_value", offsetof(struct rpc_error_information, opts.corrupt_value), spdk_json_decode_uint8, true},
     164                 :            : };
     165                 :            : 
     166                 :            : static void
     167                 :         81 : free_rpc_error_information(struct rpc_error_information *p)
     168                 :            : {
     169                 :         81 :         free(p->name);
     170                 :         81 : }
     171                 :            : 
     172                 :            : static void
     173                 :         81 : rpc_bdev_error_inject_error(struct spdk_jsonrpc_request *request,
     174                 :            :                             const struct spdk_json_val *params)
     175                 :            : {
     176                 :         81 :         struct rpc_error_information req = {.opts.error_num = 1};
     177                 :         81 :         int rc = 0;
     178                 :            : 
     179         [ -  + ]:         81 :         if (spdk_json_decode_object(params, rpc_error_information_decoders,
     180                 :            :                                     SPDK_COUNTOF(rpc_error_information_decoders),
     181                 :            :                                     &req)) {
     182                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     183                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     184                 :            :                                                  "spdk_json_decode_object failed");
     185                 :          0 :                 goto cleanup;
     186                 :            :         }
     187                 :            : 
     188                 :         81 :         rc = vbdev_error_inject_error(req.name, &req.opts);
     189         [ -  + ]:         81 :         if (rc) {
     190                 :          0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
     191                 :          0 :                 goto cleanup;
     192                 :            :         }
     193                 :            : 
     194                 :         81 :         spdk_jsonrpc_send_bool_response(request, true);
     195                 :            : 
     196                 :         81 : cleanup:
     197                 :         81 :         free_rpc_error_information(&req);
     198                 :         81 : }
     199                 :       2145 : SPDK_RPC_REGISTER("bdev_error_inject_error", rpc_bdev_error_inject_error, SPDK_RPC_RUNTIME)

Generated by: LCOV version 1.14