From 1519aa47156bf28adff9a2e2967e9ca5ad67654e Mon Sep 17 00:00:00 2001 From: gongwei Date: Thu, 18 Aug 2022 09:48:32 +0000 Subject: [PATCH] bdev_iscsi: support iscsi timeout setting Change-Id: I189ae677dfb13feb834b73fd1f55bf2545679213 Signed-off-by: gongwei Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14110 Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Ben Walker Reviewed-by: Changpeng Liu Reviewed-by: Shuhei Matsumoto Community-CI: Mellanox Build Bot --- doc/jsonrpc.md | 36 ++++++++++++++++++++++++ module/bdev/iscsi/bdev_iscsi.c | 44 ++++++++++++++++++++++++++++++ module/bdev/iscsi/bdev_iscsi.h | 8 +++++- module/bdev/iscsi/bdev_iscsi_rpc.c | 36 ++++++++++++++++++++++++ python/spdk/rpc/bdev.py | 14 ++++++++++ scripts/rpc.py | 8 ++++++ 6 files changed, 145 insertions(+), 1 deletion(-) diff --git a/doc/jsonrpc.md b/doc/jsonrpc.md index 34e63f6cc..a0dfdd087 100644 --- a/doc/jsonrpc.md +++ b/doc/jsonrpc.md @@ -4608,6 +4608,42 @@ Example response: } ~~~ +### bdev_iscsi_set_options {#rpc_bdev_iscsi_set_options} + +This RPC can be called at any time, but the new value will only take effect for new iSCSI bdevs. + +#### Parameters + +Name | Optional | Type | Description +-------------------------- | -------- | ----------- | ----------- +timeout | Optional | number | Timeout for command, in seconds, if 0, don't track timeout + +#### Example + +Example request: + +~~~json +request: +{ + "params": { + "timeout": 30 + }, + "jsonrpc": "2.0", + "method": "bdev_iscsi_set_options", + "id": 1 +} +~~~ + +Example response: + +~~~json +{ + "jsonrpc": "2.0", + "id": 1, + "result": true +} +~~~ + ### bdev_iscsi_create {#rpc_bdev_iscsi_create} Connect to iSCSI target and create bdev backed by this connection. diff --git a/module/bdev/iscsi/bdev_iscsi.c b/module/bdev/iscsi/bdev_iscsi.c index 190a5c66f..97876a7ca 100644 --- a/module/bdev/iscsi/bdev_iscsi.c +++ b/module/bdev/iscsi/bdev_iscsi.c @@ -28,6 +28,10 @@ struct bdev_iscsi_lun; #define BDEV_ISCSI_CONNECTION_POLL_US 500 /* 0.5 ms */ #define BDEV_ISCSI_NO_MAIN_CH_POLL_US 10000 /* 10ms */ +#define BDEV_ISCSI_TIMEOUT_POLL_PERIOD_DEFAULT 1000000ULL /* 1 s */ +#define BDEV_ISCSI_TIMEOUT_DEFAULT 30 /* 30 s */ +#define BDEV_ISCSI_TIMEOUT_POLL_PERIOD_DIVISOR 30 + #define DEFAULT_INITIATOR_NAME "iqn.2016-06.io.spdk:init" /* MAXIMUM UNMAP LBA COUNT: @@ -75,6 +79,7 @@ struct bdev_iscsi_lun { bool unmap_supported; uint32_t max_unmap; struct spdk_poller *poller; + struct spdk_poller *timeout_poller; }; struct bdev_iscsi_io_channel { @@ -95,6 +100,29 @@ struct bdev_iscsi_conn_req { TAILQ_ENTRY(bdev_iscsi_conn_req) link; }; +static struct spdk_bdev_iscsi_opts g_opts = { + .timeout = BDEV_ISCSI_TIMEOUT_DEFAULT, + .timeout_poller_period_us = BDEV_ISCSI_TIMEOUT_POLL_PERIOD_DEFAULT, +}; + +void +bdev_iscsi_get_opts(struct spdk_bdev_iscsi_opts *opts) +{ + *opts = g_opts; +} + +int +bdev_iscsi_set_opts(struct spdk_bdev_iscsi_opts *opts) +{ + /* make the poller period equal to timeout / 30 */ + opts->timeout_poller_period_us = (opts->timeout * 1000000ULL) / + BDEV_ISCSI_TIMEOUT_POLL_PERIOD_DIVISOR; + + g_opts = *opts; + + return 0; +} + static void complete_conn_req(struct bdev_iscsi_conn_req *req, struct spdk_bdev *bdev, int status) @@ -498,6 +526,15 @@ bdev_iscsi_poll_lun(void *_lun) return SPDK_POLLER_IDLE; } +static int +bdev_iscsi_poll_lun_timeout(void *_lun) +{ + struct bdev_iscsi_lun *lun = _lun; + /* passing 0 here to iscsi_service means do nothing except for timeout checks */ + iscsi_service(lun->context, 0); + return SPDK_POLLER_BUSY; +} + static int bdev_iscsi_no_main_ch_poll(void *arg) { @@ -624,6 +661,10 @@ bdev_iscsi_create_cb(void *io_device, void *ctx_buf) assert(lun->main_td == NULL); lun->main_td = spdk_get_thread(); lun->poller = SPDK_POLLER_REGISTER(bdev_iscsi_poll_lun, lun, 0); + if (g_opts.timeout > 0) { + lun->timeout_poller = SPDK_POLLER_REGISTER(bdev_iscsi_poll_lun_timeout, lun, + g_opts.timeout_poller_period_us); + } ch->lun = lun; } lun->ch_count++; @@ -650,6 +691,7 @@ _iscsi_destroy_cb(void *ctx) lun->main_td = NULL; spdk_poller_unregister(&lun->poller); + spdk_poller_unregister(&lun->timeout_poller); pthread_mutex_unlock(&lun->mutex); } @@ -678,6 +720,7 @@ bdev_iscsi_destroy_cb(void *io_device, void *ctx_buf) lun->main_td = NULL; spdk_poller_unregister(&lun->poller); + spdk_poller_unregister(&lun->timeout_poller); } pthread_mutex_unlock(&lun->mutex); } @@ -1001,6 +1044,7 @@ create_iscsi_disk(const char *bdev_name, const char *url, const char *initiator_ rc = iscsi_set_session_type(req->context, ISCSI_SESSION_NORMAL); rc = rc ? rc : iscsi_set_header_digest(req->context, ISCSI_HEADER_DIGEST_NONE); rc = rc ? rc : iscsi_set_targetname(req->context, iscsi_url->target); + rc = rc ? rc : iscsi_set_timeout(req->context, g_opts.timeout); rc = rc ? rc : iscsi_full_connect_async(req->context, iscsi_url->portal, iscsi_url->lun, iscsi_connect_cb, req); if (rc == 0 && iscsi_url->user[0] != '\0') { diff --git a/module/bdev/iscsi/bdev_iscsi.h b/module/bdev/iscsi/bdev_iscsi.h index 5b2d81d3f..0956b3334 100644 --- a/module/bdev/iscsi/bdev_iscsi.h +++ b/module/bdev/iscsi/bdev_iscsi.h @@ -8,6 +8,11 @@ #include "spdk/bdev.h" +struct spdk_bdev_iscsi_opts { + uint64_t timeout; + uint64_t timeout_poller_period_us; +}; + typedef void (*spdk_delete_iscsi_complete)(void *cb_arg, int bdeverrno); /** @@ -43,5 +48,6 @@ int create_iscsi_disk(const char *bdev_name, const char *url, const char *initia * \param cb_arg Completion callback custom arguments */ void delete_iscsi_disk(const char *bdev_name, spdk_delete_iscsi_complete cb_fn, void *cb_arg); - +void bdev_iscsi_get_opts(struct spdk_bdev_iscsi_opts *opts); +int bdev_iscsi_set_opts(struct spdk_bdev_iscsi_opts *opts); #endif /* SPDK_BDEV_ISCSI_H */ diff --git a/module/bdev/iscsi/bdev_iscsi_rpc.c b/module/bdev/iscsi/bdev_iscsi_rpc.c index affc49068..eb5386c91 100644 --- a/module/bdev/iscsi/bdev_iscsi_rpc.c +++ b/module/bdev/iscsi/bdev_iscsi_rpc.c @@ -10,6 +10,42 @@ #include "spdk/log.h" +static const struct spdk_json_object_decoder rpc_bdev_iscsi_options_decoders[] = { + {"timeout", offsetof(struct spdk_bdev_iscsi_opts, timeout), spdk_json_decode_uint64, true}, +}; + +static void +rpc_bdev_iscsi_set_options(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct spdk_bdev_iscsi_opts opts; + int rc; + + bdev_iscsi_get_opts(&opts); + if (params && spdk_json_decode_object(params, rpc_bdev_iscsi_options_decoders, + SPDK_COUNTOF(rpc_bdev_iscsi_options_decoders), + &opts)) { + SPDK_ERRLOG("spdk_json_decode_object failed\n"); + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, + "spdk_json_decode_object failed"); + return; + } + + rc = bdev_iscsi_set_opts(&opts); + if (rc == -EPERM) { + spdk_jsonrpc_send_error_response(request, -EPERM, + "RPC not permitted with iscsi already connected"); + } else if (rc) { + spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); + } else { + spdk_jsonrpc_send_bool_response(request, true); + } + + return; +} +SPDK_RPC_REGISTER("bdev_iscsi_set_options", rpc_bdev_iscsi_set_options, + SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) + struct rpc_bdev_iscsi_create { char *name; char *initiator_iqn; diff --git a/python/spdk/rpc/bdev.py b/python/spdk/rpc/bdev.py index 213990d7d..db77a232e 100644 --- a/python/spdk/rpc/bdev.py +++ b/python/spdk/rpc/bdev.py @@ -1116,6 +1116,20 @@ def bdev_error_delete(client, name): return client.call('bdev_error_delete', params) +def bdev_iscsi_set_options(client, timeout): + """Set options for the bdev iscsi. + + Args: + timeout: Timeout for command, in seconds, if 0, don't track timeout + """ + params = {} + + if timeout is not None: + params['timeout'] = timeout + + return client.call('bdev_iscsi_set_options', params) + + def bdev_iscsi_create(client, name, url, initiator_iqn): """Construct an iSCSI block device. diff --git a/scripts/rpc.py b/scripts/rpc.py index 2b6511ea1..cd3309913 100755 --- a/scripts/rpc.py +++ b/scripts/rpc.py @@ -996,6 +996,14 @@ if __name__ == "__main__": p.add_argument('name', help='error bdev name') p.set_defaults(func=bdev_error_delete) + def bdev_iscsi_set_options(args): + rpc.bdev.bdev_iscsi_set_options(args.client, + timeout=args.timeout) + + p = subparsers.add_parser('bdev_iscsi_set_options', help='Set options for the bdev iscsi type.') + p.add_argument('-t', '--timeout', help="Timeout for command, in seconds, if 0, don't track timeout.", type=int) + p.set_defaults(func=bdev_iscsi_set_options) + def bdev_iscsi_create(args): print_json(rpc.bdev.bdev_iscsi_create(args.client, name=args.name,