From d2a8ea8f37a509ecbeeec74e67ce6909c2354347 Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Wed, 22 Dec 2021 22:33:25 +0000 Subject: [PATCH] bdev/nvme: add bdev_nvme_stop_discovery RPC This RPC will stop the specified discovery service, including detaching from any controllers that were attached as part of that discovery service. Signed-off-by: Jim Harris Change-Id: I9222876457fc45e1acde680a7bd1925917c22308 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/10832 Tested-by: SPDK CI Jenkins Community-CI: Mellanox Build Bot Reviewed-by: Changpeng Liu Reviewed-by: Shuhei Matsumoto --- doc/jsonrpc.md | 36 +++++++++++++++++++ module/bdev/nvme/bdev_nvme.c | 30 ++++++++++++++++ module/bdev/nvme/bdev_nvme.h | 2 ++ module/bdev/nvme/bdev_nvme_rpc.c | 61 ++++++++++++++++++++++++++++++++ scripts/rpc.py | 7 ++++ scripts/rpc/bdev.py | 11 ++++++ test/nvmf/host/discovery.sh | 2 +- 7 files changed, 148 insertions(+), 1 deletion(-) diff --git a/doc/jsonrpc.md b/doc/jsonrpc.md index d1ae9cc24..b56e056cf 100644 --- a/doc/jsonrpc.md +++ b/doc/jsonrpc.md @@ -3123,6 +3123,42 @@ Example response: } ~~~ +### bdev_nvme_stop_discovery {#rpc_bdev_nvme_stop_discovery} + +Stop a discovery service. This includes detaching any controllers that were +discovered via the service that is being stopped. + +#### Parameters + +Name | Optional | Type | Description +-------------------------- | -------- | ----------- | ----------- +name | Required | string | Name of service to stop + +#### Example + +Example request: + +~~~json +{ + "jsonrpc": "2.0", + "method": "bdev_nvme_stop_discovery", + "id": 1, + "params": { + "name": "nvme_auto" + } +} +~~~ + +Example response: + +~~~json +{ + "jsonrpc": "2.0", + "id": 1, + "result": true +} +~~~ + ### bdev_nvme_cuse_register {#rpc_bdev_nvme_cuse_register} Register CUSE device on NVMe controller. diff --git a/module/bdev/nvme/bdev_nvme.c b/module/bdev/nvme/bdev_nvme.c index c249ee94e..1b7792d43 100644 --- a/module/bdev/nvme/bdev_nvme.c +++ b/module/bdev/nvme/bdev_nvme.c @@ -4247,6 +4247,36 @@ bdev_nvme_start_discovery(struct spdk_nvme_transport_id *trid, return 0; } +int +bdev_nvme_stop_discovery(const char *name, spdk_bdev_nvme_stop_discovery_fn cb_fn, void *cb_ctx) +{ + struct discovery_ctx *ctx; + + TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) { + if (strcmp(name, ctx->name) == 0) { + if (ctx->detach) { + return -EALREADY; + } + ctx->detach = true; + ctx->stop_cb_fn = cb_fn; + ctx->cb_ctx = cb_ctx; + while (!TAILQ_EMPTY(&ctx->ctrlr_ctxs)) { + struct discovery_ctrlr_ctx *ctrlr_ctx; + struct nvme_path_id path = {}; + + ctrlr_ctx = TAILQ_FIRST(&ctx->ctrlr_ctxs); + path.trid = ctrlr_ctx->trid; + bdev_nvme_delete(ctrlr_ctx->name, &path); + TAILQ_REMOVE(&ctx->ctrlr_ctxs, ctrlr_ctx, tailq); + free(ctrlr_ctx); + } + return 0; + } + } + + return -ENOENT; +} + static int bdev_nvme_library_init(void) { diff --git a/module/bdev/nvme/bdev_nvme.h b/module/bdev/nvme/bdev_nvme.h index 8efcef5a4..0136d954d 100644 --- a/module/bdev/nvme/bdev_nvme.h +++ b/module/bdev/nvme/bdev_nvme.h @@ -260,6 +260,8 @@ int bdev_nvme_create(struct spdk_nvme_transport_id *trid, int bdev_nvme_start_discovery(struct spdk_nvme_transport_id *trid, const char *base_name, struct spdk_nvme_ctrlr_opts *opts, spdk_bdev_nvme_start_discovery_fn cb_fn, void *cb_ctx); +int bdev_nvme_stop_discovery(const char *name, spdk_bdev_nvme_stop_discovery_fn cb_fn, + void *cb_ctx); struct spdk_nvme_ctrlr *bdev_nvme_get_ctrlr(struct spdk_bdev *bdev); diff --git a/module/bdev/nvme/bdev_nvme_rpc.c b/module/bdev/nvme/bdev_nvme_rpc.c index 33fae0cf1..ba536fe60 100644 --- a/module/bdev/nvme/bdev_nvme_rpc.c +++ b/module/bdev/nvme/bdev_nvme_rpc.c @@ -1696,3 +1696,64 @@ cleanup: } SPDK_RPC_REGISTER("bdev_nvme_start_discovery", rpc_bdev_nvme_start_discovery, SPDK_RPC_RUNTIME) + +struct rpc_bdev_nvme_stop_discovery { + char *name; +}; + +static const struct spdk_json_object_decoder rpc_bdev_nvme_stop_discovery_decoders[] = { + {"name", offsetof(struct rpc_bdev_nvme_stop_discovery, name), spdk_json_decode_string}, +}; + +struct rpc_bdev_nvme_stop_discovery_ctx { + struct rpc_bdev_nvme_stop_discovery req; + struct spdk_jsonrpc_request *request; +}; + +static void +rpc_bdev_nvme_stop_discovery_done(void *cb_ctx) +{ + struct rpc_bdev_nvme_stop_discovery_ctx *ctx = cb_ctx; + + spdk_jsonrpc_send_bool_response(ctx->request, true); + free(ctx->req.name); + free(ctx); +} + +static void +rpc_bdev_nvme_stop_discovery(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct rpc_bdev_nvme_stop_discovery_ctx *ctx; + int rc; + + ctx = calloc(1, sizeof(*ctx)); + if (!ctx) { + spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); + return; + } + + if (spdk_json_decode_object(params, rpc_bdev_nvme_stop_discovery_decoders, + SPDK_COUNTOF(rpc_bdev_nvme_stop_discovery_decoders), + &ctx->req)) { + SPDK_ERRLOG("spdk_json_decode_object failed\n"); + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, + "spdk_json_decode_object failed"); + goto cleanup; + } + + ctx->request = request; + rc = bdev_nvme_stop_discovery(ctx->req.name, rpc_bdev_nvme_stop_discovery_done, ctx); + if (rc) { + spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); + goto cleanup; + } + + return; + +cleanup: + free(ctx->req.name); + free(ctx); +} +SPDK_RPC_REGISTER("bdev_nvme_stop_discovery", rpc_bdev_nvme_stop_discovery, + SPDK_RPC_RUNTIME) diff --git a/scripts/rpc.py b/scripts/rpc.py index 43e062974..7a5b53e7e 100755 --- a/scripts/rpc.py +++ b/scripts/rpc.py @@ -641,6 +641,13 @@ if __name__ == "__main__": p.add_argument('-q', '--hostnqn', help='NVMe-oF host subnqn') p.set_defaults(func=bdev_nvme_start_discovery) + def bdev_nvme_stop_discovery(args): + rpc.bdev.bdev_nvme_stop_discovery(args.client, name=args.name) + + p = subparsers.add_parser('bdev_nvme_stop_discovery', help='Stop automatic discovery') + p.add_argument('-b', '--name', help="Name of the service to stop", required=True) + p.set_defaults(func=bdev_nvme_stop_discovery) + def bdev_nvme_cuse_register(args): rpc.bdev.bdev_nvme_cuse_register(args.client, name=args.name) diff --git a/scripts/rpc/bdev.py b/scripts/rpc/bdev.py index f7c664726..0d1b33192 100644 --- a/scripts/rpc/bdev.py +++ b/scripts/rpc/bdev.py @@ -684,6 +684,17 @@ def bdev_nvme_start_discovery(client, name, trtype, traddr, adrfam=None, trsvcid return client.call('bdev_nvme_start_discovery', params) +def bdev_nvme_stop_discovery(client, name): + """Stop a previously started discovery service + + Args: + name: name of discovery service to start + """ + params = {'name': name} + + return client.call('bdev_nvme_stop_discovery', params) + + def bdev_nvme_cuse_register(client, name): """Register CUSE devices on NVMe controller. diff --git a/test/nvmf/host/discovery.sh b/test/nvmf/host/discovery.sh index d504231fd..d060893da 100755 --- a/test/nvmf/host/discovery.sh +++ b/test/nvmf/host/discovery.sh @@ -103,7 +103,7 @@ $rpc_py nvmf_subsystem_remove_listener ${NQN}0 -t $TEST_TRANSPORT -a $NVMF_FIRST [[ "$(get_bdev_list)" == "nvme0n1 nvme0n2" ]] [[ "$(get_subsystem_paths nvme0)" == "$NVMF_SECOND_PORT" ]] -$rpc_py nvmf_delete_subsystem ${NQN}0 +$rpc_py -s $HOST_SOCK bdev_nvme_stop_discovery -b nvme [[ "$(get_subsystem_names)" == "" ]] [[ "$(get_bdev_list)" == "" ]]