LCOV - code coverage report
Current view: top level - spdk/module/bdev/error - vbdev_error_rpc.c (source / functions) Hit Total Coverage
Test: Combined Lines: 65 87 74.7 %
Date: 2024-07-12 13:06:24 Functions: 12 12 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 19 32 59.4 %

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

Generated by: LCOV version 1.14