LCOV - code coverage report
Current view: top level - spdk/module/bdev/iscsi - bdev_iscsi_rpc.c (source / functions) Hit Total Coverage
Test: Combined Lines: 34 63 54.0 %
Date: 2024-08-14 09:16:26 Functions: 7 10 70.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 8 20 40.0 %

           Branch data     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 "bdev_iscsi.h"
       7                 :            : #include "spdk/rpc.h"
       8                 :            : #include "spdk/util.h"
       9                 :            : #include "spdk/string.h"
      10                 :            : 
      11                 :            : #include "spdk/log.h"
      12                 :            : 
      13                 :            : static const struct spdk_json_object_decoder rpc_bdev_iscsi_options_decoders[] = {
      14                 :            :         {"timeout_sec", offsetof(struct spdk_bdev_iscsi_opts, timeout_sec), spdk_json_decode_uint64, true},
      15                 :            : };
      16                 :            : 
      17                 :            : static void
      18                 :        242 : rpc_bdev_iscsi_set_options(struct spdk_jsonrpc_request *request,
      19                 :            :                            const struct spdk_json_val *params)
      20                 :            : {
      21                 :        126 :         struct spdk_bdev_iscsi_opts opts;
      22                 :            :         int rc;
      23                 :            : 
      24                 :        242 :         bdev_iscsi_get_opts(&opts);
      25   [ +  -  -  + ]:        242 :         if (params && spdk_json_decode_object(params, rpc_bdev_iscsi_options_decoders,
      26                 :            :                                               SPDK_COUNTOF(rpc_bdev_iscsi_options_decoders),
      27                 :            :                                               &opts)) {
      28                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
      29                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
      30                 :            :                                                  "spdk_json_decode_object failed");
      31                 :          0 :                 return;
      32                 :            :         }
      33                 :            : 
      34                 :        242 :         rc = bdev_iscsi_set_opts(&opts);
      35         [ -  + ]:        242 :         if (rc == -EPERM) {
      36                 :          0 :                 spdk_jsonrpc_send_error_response(request, -EPERM,
      37                 :            :                                                  "RPC not permitted with iscsi already connected");
      38         [ -  + ]:        242 :         } else if (rc) {
      39                 :          0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
      40                 :            :         } else {
      41                 :        242 :                 spdk_jsonrpc_send_bool_response(request, true);
      42                 :            :         }
      43                 :            : 
      44                 :        242 :         return;
      45                 :            : }
      46                 :       1994 : SPDK_RPC_REGISTER("bdev_iscsi_set_options", rpc_bdev_iscsi_set_options,
      47                 :            :                   SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
      48                 :            : 
      49                 :            : struct rpc_bdev_iscsi_create {
      50                 :            :         char *name;
      51                 :            :         char *initiator_iqn;
      52                 :            :         char *url;
      53                 :            : };
      54                 :            : 
      55                 :            : static const struct spdk_json_object_decoder rpc_bdev_iscsi_create_decoders[] = {
      56                 :            :         {"name", offsetof(struct rpc_bdev_iscsi_create, name), spdk_json_decode_string},
      57                 :            :         {"initiator_iqn", offsetof(struct rpc_bdev_iscsi_create, initiator_iqn), spdk_json_decode_string},
      58                 :            :         {"url", offsetof(struct rpc_bdev_iscsi_create, url), spdk_json_decode_string},
      59                 :            : };
      60                 :            : 
      61                 :            : static void
      62                 :          9 : free_rpc_bdev_iscsi_create(struct rpc_bdev_iscsi_create *req)
      63                 :            : {
      64                 :          9 :         free(req->name);
      65                 :          9 :         free(req->initiator_iqn);
      66                 :          9 :         free(req->url);
      67                 :          9 : }
      68                 :            : 
      69                 :            : static void
      70                 :          9 : bdev_iscsi_create_cb(void *cb_arg, struct spdk_bdev *bdev, int status)
      71                 :            : {
      72                 :          9 :         struct spdk_jsonrpc_request *request = cb_arg;
      73                 :            :         struct spdk_json_write_ctx *w;
      74                 :            : 
      75         [ -  + ]:          9 :         if (status > 0) {
      76                 :          0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
      77                 :            :                                                      "iSCSI error (%d).", status);
      78         [ -  + ]:          9 :         } else if (status < 0) {
      79                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
      80                 :            :                                                  spdk_strerror(-status));
      81                 :            :         } else {
      82                 :          9 :                 w = spdk_jsonrpc_begin_result(request);
      83                 :          9 :                 spdk_json_write_string(w, spdk_bdev_get_name(bdev));
      84                 :          9 :                 spdk_jsonrpc_end_result(request, w);
      85                 :            :         }
      86                 :          9 : }
      87                 :            : 
      88                 :            : static void
      89                 :          9 : rpc_bdev_iscsi_create(struct spdk_jsonrpc_request *request,
      90                 :            :                       const struct spdk_json_val *params)
      91                 :            : {
      92                 :          9 :         struct rpc_bdev_iscsi_create req = {};
      93                 :          9 :         int rc = 0;
      94                 :            : 
      95         [ -  + ]:          9 :         if (spdk_json_decode_object(params, rpc_bdev_iscsi_create_decoders,
      96                 :            :                                     SPDK_COUNTOF(rpc_bdev_iscsi_create_decoders),
      97                 :            :                                     &req)) {
      98                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
      99                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     100                 :            :                                                  "spdk_json_decode_object failed");
     101                 :          0 :                 goto cleanup;
     102                 :            :         }
     103                 :            : 
     104                 :          9 :         rc = create_iscsi_disk(req.name, req.url, req.initiator_iqn, bdev_iscsi_create_cb, request);
     105         [ +  - ]:          9 :         if (rc) {
     106                 :          0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
     107                 :            :         }
     108                 :            : 
     109                 :          9 : cleanup:
     110                 :          9 :         free_rpc_bdev_iscsi_create(&req);
     111                 :          9 : }
     112                 :       1994 : SPDK_RPC_REGISTER("bdev_iscsi_create", rpc_bdev_iscsi_create, SPDK_RPC_RUNTIME)
     113                 :            : 
     114                 :            : struct rpc_delete_iscsi {
     115                 :            :         char *name;
     116                 :            : };
     117                 :            : 
     118                 :            : static void
     119                 :          0 : free_rpc_delete_iscsi(struct rpc_delete_iscsi *r)
     120                 :            : {
     121                 :          0 :         free(r->name);
     122                 :          0 : }
     123                 :            : 
     124                 :            : static const struct spdk_json_object_decoder rpc_delete_iscsi_decoders[] = {
     125                 :            :         {"name", offsetof(struct rpc_delete_iscsi, name), spdk_json_decode_string},
     126                 :            : };
     127                 :            : 
     128                 :            : static void
     129                 :          0 : rpc_bdev_iscsi_delete_cb(void *cb_arg, int bdeverrno)
     130                 :            : {
     131                 :          0 :         struct spdk_jsonrpc_request *request = cb_arg;
     132                 :            : 
     133         [ #  # ]:          0 :         if (bdeverrno == 0) {
     134                 :          0 :                 spdk_jsonrpc_send_bool_response(request, true);
     135                 :            :         } else {
     136                 :          0 :                 spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
     137                 :            :         }
     138                 :          0 : }
     139                 :            : 
     140                 :            : static void
     141                 :          0 : rpc_bdev_iscsi_delete(struct spdk_jsonrpc_request *request,
     142                 :            :                       const struct spdk_json_val *params)
     143                 :            : {
     144                 :          0 :         struct rpc_delete_iscsi req = {NULL};
     145                 :            : 
     146         [ #  # ]:          0 :         if (spdk_json_decode_object(params, rpc_delete_iscsi_decoders,
     147                 :            :                                     SPDK_COUNTOF(rpc_delete_iscsi_decoders),
     148                 :            :                                     &req)) {
     149                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     150                 :            :                                                  "spdk_json_decode_object failed");
     151                 :          0 :                 goto cleanup;
     152                 :            :         }
     153                 :            : 
     154                 :          0 :         delete_iscsi_disk(req.name, rpc_bdev_iscsi_delete_cb, request);
     155                 :            : 
     156                 :          0 : cleanup:
     157                 :          0 :         free_rpc_delete_iscsi(&req);
     158                 :          0 : }
     159                 :       1994 : SPDK_RPC_REGISTER("bdev_iscsi_delete", rpc_bdev_iscsi_delete, SPDK_RPC_RUNTIME)

Generated by: LCOV version 1.14