xref: /spdk/module/bdev/malloc/bdev_malloc_rpc.c (revision 0098e636761237b77c12c30c2408263a5d2260cc)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (c) Intel Corporation.
3  *   All rights reserved.
4  *   Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  */
6 
7 #include "bdev_malloc.h"
8 #include "spdk/rpc.h"
9 #include "spdk/util.h"
10 #include "spdk/uuid.h"
11 #include "spdk/string.h"
12 #include "spdk/log.h"
13 
14 struct rpc_construct_malloc {
15 	char *name;
16 	char *uuid;
17 	uint64_t num_blocks;
18 	uint32_t block_size;
19 	uint32_t optimal_io_boundary;
20 };
21 
22 static void
23 free_rpc_construct_malloc(struct rpc_construct_malloc *r)
24 {
25 	free(r->name);
26 	free(r->uuid);
27 }
28 
29 static const struct spdk_json_object_decoder rpc_construct_malloc_decoders[] = {
30 	{"name", offsetof(struct rpc_construct_malloc, name), spdk_json_decode_string, true},
31 	{"uuid", offsetof(struct rpc_construct_malloc, uuid), spdk_json_decode_string, true},
32 	{"num_blocks", offsetof(struct rpc_construct_malloc, num_blocks), spdk_json_decode_uint64},
33 	{"block_size", offsetof(struct rpc_construct_malloc, block_size), spdk_json_decode_uint32},
34 	{"optimal_io_boundary", offsetof(struct rpc_construct_malloc, optimal_io_boundary), spdk_json_decode_uint32, true},
35 };
36 
37 static void
38 rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request,
39 		       const struct spdk_json_val *params)
40 {
41 	struct rpc_construct_malloc req = {NULL};
42 	struct spdk_json_write_ctx *w;
43 	struct spdk_uuid *uuid = NULL;
44 	struct spdk_uuid decoded_uuid;
45 	struct spdk_bdev *bdev;
46 	int rc = 0;
47 
48 	if (spdk_json_decode_object(params, rpc_construct_malloc_decoders,
49 				    SPDK_COUNTOF(rpc_construct_malloc_decoders),
50 				    &req)) {
51 		SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n");
52 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
53 						 "spdk_json_decode_object failed");
54 		goto cleanup;
55 	}
56 
57 	if (req.num_blocks == 0) {
58 		spdk_jsonrpc_send_error_response(request, -EINVAL,
59 						 "Disk num_blocks must be greater than 0");
60 		goto cleanup;
61 	}
62 
63 	if (req.uuid) {
64 		if (spdk_uuid_parse(&decoded_uuid, req.uuid)) {
65 			spdk_jsonrpc_send_error_response(request, -EINVAL,
66 							 "Failed to parse bdev UUID");
67 			goto cleanup;
68 		}
69 		uuid = &decoded_uuid;
70 	}
71 
72 	rc = create_malloc_disk(&bdev, req.name, uuid, req.num_blocks, req.block_size,
73 				req.optimal_io_boundary);
74 	if (rc) {
75 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
76 		goto cleanup;
77 	}
78 
79 	free_rpc_construct_malloc(&req);
80 
81 	w = spdk_jsonrpc_begin_result(request);
82 	spdk_json_write_string(w, spdk_bdev_get_name(bdev));
83 	spdk_jsonrpc_end_result(request, w);
84 	return;
85 
86 cleanup:
87 	free_rpc_construct_malloc(&req);
88 }
89 SPDK_RPC_REGISTER("bdev_malloc_create", rpc_bdev_malloc_create, SPDK_RPC_RUNTIME)
90 
91 struct rpc_delete_malloc {
92 	char *name;
93 };
94 
95 static void
96 free_rpc_delete_malloc(struct rpc_delete_malloc *r)
97 {
98 	free(r->name);
99 }
100 
101 static const struct spdk_json_object_decoder rpc_delete_malloc_decoders[] = {
102 	{"name", offsetof(struct rpc_delete_malloc, name), spdk_json_decode_string},
103 };
104 
105 static void
106 rpc_bdev_malloc_delete_cb(void *cb_arg, int bdeverrno)
107 {
108 	struct spdk_jsonrpc_request *request = cb_arg;
109 
110 	if (bdeverrno == 0) {
111 		spdk_jsonrpc_send_bool_response(request, true);
112 	} else {
113 		spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
114 	}
115 }
116 
117 static void
118 rpc_bdev_malloc_delete(struct spdk_jsonrpc_request *request,
119 		       const struct spdk_json_val *params)
120 {
121 	struct rpc_delete_malloc req = {NULL};
122 
123 	if (spdk_json_decode_object(params, rpc_delete_malloc_decoders,
124 				    SPDK_COUNTOF(rpc_delete_malloc_decoders),
125 				    &req)) {
126 		SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n");
127 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
128 						 "spdk_json_decode_object failed");
129 		goto cleanup;
130 	}
131 
132 	delete_malloc_disk(req.name, rpc_bdev_malloc_delete_cb, request);
133 
134 cleanup:
135 	free_rpc_delete_malloc(&req);
136 }
137 SPDK_RPC_REGISTER("bdev_malloc_delete", rpc_bdev_malloc_delete, SPDK_RPC_RUNTIME)
138