xref: /spdk/module/bdev/malloc/bdev_malloc_rpc.c (revision c016e6ff3a96bf552b14b80f3a7efe7c30c9177a)
1488570ebSJim Harris /*   SPDX-License-Identifier: BSD-3-Clause
2a6dbe372Spaul luse  *   Copyright (C) 2016 Intel Corporation.
307fe6a43SSeth Howell  *   All rights reserved.
45e1e850bSAlexey Marchuk  *   Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
507fe6a43SSeth Howell  */
607fe6a43SSeth Howell 
707fe6a43SSeth Howell #include "bdev_malloc.h"
807fe6a43SSeth Howell #include "spdk/rpc.h"
907fe6a43SSeth Howell #include "spdk/string.h"
104e8e97c8STomasz Zawadzki #include "spdk/log.h"
1107fe6a43SSeth Howell 
1207fe6a43SSeth Howell static void
13e6b2b907SShuhei Matsumoto free_rpc_construct_malloc(struct malloc_bdev_opts *r)
1407fe6a43SSeth Howell {
1507fe6a43SSeth Howell 	free(r->name);
1683eff61dSShuhei Matsumoto }
1783eff61dSShuhei Matsumoto 
1807fe6a43SSeth Howell static const struct spdk_json_object_decoder rpc_construct_malloc_decoders[] = {
19e6b2b907SShuhei Matsumoto 	{"name", offsetof(struct malloc_bdev_opts, name), spdk_json_decode_string, true},
20e85f1f11SKonrad Sztyber 	{"uuid", offsetof(struct malloc_bdev_opts, uuid), spdk_json_decode_uuid, true},
21e6b2b907SShuhei Matsumoto 	{"num_blocks", offsetof(struct malloc_bdev_opts, num_blocks), spdk_json_decode_uint64},
22e6b2b907SShuhei Matsumoto 	{"block_size", offsetof(struct malloc_bdev_opts, block_size), spdk_json_decode_uint32},
231eb06bd6SPanfil, Wojciech 	{"physical_block_size", offsetof(struct malloc_bdev_opts, physical_block_size), spdk_json_decode_uint32, true},
24e6b2b907SShuhei Matsumoto 	{"optimal_io_boundary", offsetof(struct malloc_bdev_opts, optimal_io_boundary), spdk_json_decode_uint32, true},
25aef00d44SShuhei Matsumoto 	{"md_size", offsetof(struct malloc_bdev_opts, md_size), spdk_json_decode_uint32, true},
26aef00d44SShuhei Matsumoto 	{"md_interleave", offsetof(struct malloc_bdev_opts, md_interleave), spdk_json_decode_bool, true},
2700bff560SShuhei Matsumoto 	{"dif_type", offsetof(struct malloc_bdev_opts, dif_type), spdk_json_decode_int32, true},
2800bff560SShuhei Matsumoto 	{"dif_is_head_of_md", offsetof(struct malloc_bdev_opts, dif_is_head_of_md), spdk_json_decode_bool, true},
29*c016e6ffSShuhei Matsumoto 	{"dif_pi_format", offsetof(struct malloc_bdev_opts, dif_pi_format), spdk_json_decode_uint32, true},
3007fe6a43SSeth Howell };
3107fe6a43SSeth Howell 
3207fe6a43SSeth Howell static void
33f8acb893SSeth Howell rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request,
3407fe6a43SSeth Howell 		       const struct spdk_json_val *params)
3507fe6a43SSeth Howell {
36e6b2b907SShuhei Matsumoto 	struct malloc_bdev_opts req = {NULL};
3707fe6a43SSeth Howell 	struct spdk_json_write_ctx *w;
3807fe6a43SSeth Howell 	struct spdk_bdev *bdev;
3907fe6a43SSeth Howell 	int rc = 0;
4007fe6a43SSeth Howell 
4107fe6a43SSeth Howell 	if (spdk_json_decode_object(params, rpc_construct_malloc_decoders,
4207fe6a43SSeth Howell 				    SPDK_COUNTOF(rpc_construct_malloc_decoders),
4307fe6a43SSeth Howell 				    &req)) {
442172c432STomasz Zawadzki 		SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n");
4507fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
4607fe6a43SSeth Howell 						 "spdk_json_decode_object failed");
4707fe6a43SSeth Howell 		goto cleanup;
4807fe6a43SSeth Howell 	}
4907fe6a43SSeth Howell 
50e6b2b907SShuhei Matsumoto 	rc = create_malloc_disk(&bdev, &req);
5107fe6a43SSeth Howell 	if (rc) {
5207fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
5307fe6a43SSeth Howell 		goto cleanup;
5407fe6a43SSeth Howell 	}
5507fe6a43SSeth Howell 
5607fe6a43SSeth Howell 	free_rpc_construct_malloc(&req);
5707fe6a43SSeth Howell 
5807fe6a43SSeth Howell 	w = spdk_jsonrpc_begin_result(request);
5907fe6a43SSeth Howell 	spdk_json_write_string(w, spdk_bdev_get_name(bdev));
6007fe6a43SSeth Howell 	spdk_jsonrpc_end_result(request, w);
6107fe6a43SSeth Howell 	return;
6207fe6a43SSeth Howell 
6307fe6a43SSeth Howell cleanup:
6407fe6a43SSeth Howell 	free_rpc_construct_malloc(&req);
6507fe6a43SSeth Howell }
66f8acb893SSeth Howell SPDK_RPC_REGISTER("bdev_malloc_create", rpc_bdev_malloc_create, SPDK_RPC_RUNTIME)
6707fe6a43SSeth Howell 
6807fe6a43SSeth Howell struct rpc_delete_malloc {
6907fe6a43SSeth Howell 	char *name;
7007fe6a43SSeth Howell };
7107fe6a43SSeth Howell 
7207fe6a43SSeth Howell static void
7307fe6a43SSeth Howell free_rpc_delete_malloc(struct rpc_delete_malloc *r)
7407fe6a43SSeth Howell {
7507fe6a43SSeth Howell 	free(r->name);
7607fe6a43SSeth Howell }
7707fe6a43SSeth Howell 
7807fe6a43SSeth Howell static const struct spdk_json_object_decoder rpc_delete_malloc_decoders[] = {
7907fe6a43SSeth Howell 	{"name", offsetof(struct rpc_delete_malloc, name), spdk_json_decode_string},
8007fe6a43SSeth Howell };
8107fe6a43SSeth Howell 
8207fe6a43SSeth Howell static void
83f8acb893SSeth Howell rpc_bdev_malloc_delete_cb(void *cb_arg, int bdeverrno)
8407fe6a43SSeth Howell {
8507fe6a43SSeth Howell 	struct spdk_jsonrpc_request *request = cb_arg;
8607fe6a43SSeth Howell 
87d3e394aeSShuhei Matsumoto 	if (bdeverrno == 0) {
88d3e394aeSShuhei Matsumoto 		spdk_jsonrpc_send_bool_response(request, true);
89d3e394aeSShuhei Matsumoto 	} else {
90d3e394aeSShuhei Matsumoto 		spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
91d3e394aeSShuhei Matsumoto 	}
9207fe6a43SSeth Howell }
9307fe6a43SSeth Howell 
9407fe6a43SSeth Howell static void
95f8acb893SSeth Howell rpc_bdev_malloc_delete(struct spdk_jsonrpc_request *request,
9607fe6a43SSeth Howell 		       const struct spdk_json_val *params)
9707fe6a43SSeth Howell {
9807fe6a43SSeth Howell 	struct rpc_delete_malloc req = {NULL};
9907fe6a43SSeth Howell 
10007fe6a43SSeth Howell 	if (spdk_json_decode_object(params, rpc_delete_malloc_decoders,
10107fe6a43SSeth Howell 				    SPDK_COUNTOF(rpc_delete_malloc_decoders),
10207fe6a43SSeth Howell 				    &req)) {
1032172c432STomasz Zawadzki 		SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n");
10407fe6a43SSeth Howell 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
10507fe6a43SSeth Howell 						 "spdk_json_decode_object failed");
10607fe6a43SSeth Howell 		goto cleanup;
10707fe6a43SSeth Howell 	}
10807fe6a43SSeth Howell 
1094573e4ccSShuhei Matsumoto 	delete_malloc_disk(req.name, rpc_bdev_malloc_delete_cb, request);
11007fe6a43SSeth Howell 
11107fe6a43SSeth Howell cleanup:
11207fe6a43SSeth Howell 	free_rpc_delete_malloc(&req);
11307fe6a43SSeth Howell }
114f8acb893SSeth Howell SPDK_RPC_REGISTER("bdev_malloc_delete", rpc_bdev_malloc_delete, SPDK_RPC_RUNTIME)
115