xref: /spdk/module/bdev/malloc/bdev_malloc_rpc.c (revision fecffda6ecf8853b82edccde429b68252f0a62c5)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2016 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/string.h"
10 #include "spdk/log.h"
11 
12 static void
13 free_rpc_construct_malloc(struct malloc_bdev_opts *r)
14 {
15 	free(r->name);
16 }
17 
18 static int
19 decode_mdisk_uuid(const struct spdk_json_val *val, void *out)
20 {
21 	char *str = NULL;
22 	int rc;
23 
24 	rc = spdk_json_decode_string(val, &str);
25 	if (rc == 0) {
26 		rc = spdk_uuid_parse(out, str);
27 	}
28 
29 	free(str);
30 	return rc;
31 }
32 
33 static const struct spdk_json_object_decoder rpc_construct_malloc_decoders[] = {
34 	{"name", offsetof(struct malloc_bdev_opts, name), spdk_json_decode_string, true},
35 	{"uuid", offsetof(struct malloc_bdev_opts, uuid), decode_mdisk_uuid, true},
36 	{"num_blocks", offsetof(struct malloc_bdev_opts, num_blocks), spdk_json_decode_uint64},
37 	{"block_size", offsetof(struct malloc_bdev_opts, block_size), spdk_json_decode_uint32},
38 	{"optimal_io_boundary", offsetof(struct malloc_bdev_opts, optimal_io_boundary), spdk_json_decode_uint32, true},
39 	{"md_size", offsetof(struct malloc_bdev_opts, md_size), spdk_json_decode_uint32, true},
40 	{"md_interleave", offsetof(struct malloc_bdev_opts, md_interleave), spdk_json_decode_bool, true},
41 	{"dif_type", offsetof(struct malloc_bdev_opts, dif_type), spdk_json_decode_int32, true},
42 	{"dif_is_head_of_md", offsetof(struct malloc_bdev_opts, dif_is_head_of_md), spdk_json_decode_bool, true},
43 };
44 
45 static void
46 rpc_bdev_malloc_create(struct spdk_jsonrpc_request *request,
47 		       const struct spdk_json_val *params)
48 {
49 	struct malloc_bdev_opts req = {NULL};
50 	struct spdk_json_write_ctx *w;
51 	struct spdk_bdev *bdev;
52 	int rc = 0;
53 
54 	if (spdk_json_decode_object(params, rpc_construct_malloc_decoders,
55 				    SPDK_COUNTOF(rpc_construct_malloc_decoders),
56 				    &req)) {
57 		SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n");
58 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
59 						 "spdk_json_decode_object failed");
60 		goto cleanup;
61 	}
62 
63 	rc = create_malloc_disk(&bdev, &req);
64 	if (rc) {
65 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
66 		goto cleanup;
67 	}
68 
69 	free_rpc_construct_malloc(&req);
70 
71 	w = spdk_jsonrpc_begin_result(request);
72 	spdk_json_write_string(w, spdk_bdev_get_name(bdev));
73 	spdk_jsonrpc_end_result(request, w);
74 	return;
75 
76 cleanup:
77 	free_rpc_construct_malloc(&req);
78 }
79 SPDK_RPC_REGISTER("bdev_malloc_create", rpc_bdev_malloc_create, SPDK_RPC_RUNTIME)
80 
81 struct rpc_delete_malloc {
82 	char *name;
83 };
84 
85 static void
86 free_rpc_delete_malloc(struct rpc_delete_malloc *r)
87 {
88 	free(r->name);
89 }
90 
91 static const struct spdk_json_object_decoder rpc_delete_malloc_decoders[] = {
92 	{"name", offsetof(struct rpc_delete_malloc, name), spdk_json_decode_string},
93 };
94 
95 static void
96 rpc_bdev_malloc_delete_cb(void *cb_arg, int bdeverrno)
97 {
98 	struct spdk_jsonrpc_request *request = cb_arg;
99 
100 	if (bdeverrno == 0) {
101 		spdk_jsonrpc_send_bool_response(request, true);
102 	} else {
103 		spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
104 	}
105 }
106 
107 static void
108 rpc_bdev_malloc_delete(struct spdk_jsonrpc_request *request,
109 		       const struct spdk_json_val *params)
110 {
111 	struct rpc_delete_malloc req = {NULL};
112 
113 	if (spdk_json_decode_object(params, rpc_delete_malloc_decoders,
114 				    SPDK_COUNTOF(rpc_delete_malloc_decoders),
115 				    &req)) {
116 		SPDK_DEBUGLOG(bdev_malloc, "spdk_json_decode_object failed\n");
117 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
118 						 "spdk_json_decode_object failed");
119 		goto cleanup;
120 	}
121 
122 	delete_malloc_disk(req.name, rpc_bdev_malloc_delete_cb, request);
123 
124 cleanup:
125 	free_rpc_delete_malloc(&req);
126 }
127 SPDK_RPC_REGISTER("bdev_malloc_delete", rpc_bdev_malloc_delete, SPDK_RPC_RUNTIME)
128