xref: /spdk/module/bdev/crypto/vbdev_crypto_rpc.c (revision 9889ab2dc80e40dae92dcef361d53dcba722043d)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "vbdev_crypto.h"
35 
36 /* Structure to hold the parameters for this RPC method. */
37 struct rpc_construct_crypto {
38 	char *base_bdev_name;
39 	char *name;
40 	char *crypto_pmd;
41 	char *key;
42 };
43 
44 /* Free the allocated memory resource after the RPC handling. */
45 static void
46 free_rpc_construct_crypto(struct rpc_construct_crypto *r)
47 {
48 	free(r->base_bdev_name);
49 	free(r->name);
50 	free(r->crypto_pmd);
51 	free(r->key);
52 }
53 
54 /* Structure to decode the input parameters for this RPC method. */
55 static const struct spdk_json_object_decoder rpc_construct_crypto_decoders[] = {
56 	{"base_bdev_name", offsetof(struct rpc_construct_crypto, base_bdev_name), spdk_json_decode_string},
57 	{"name", offsetof(struct rpc_construct_crypto, name), spdk_json_decode_string},
58 	{"crypto_pmd", offsetof(struct rpc_construct_crypto, crypto_pmd), spdk_json_decode_string},
59 	{"key", offsetof(struct rpc_construct_crypto, key), spdk_json_decode_string},
60 };
61 
62 /* Decode the parameters for this RPC method and properly construct the crypto
63  * device. Error status returned in the failed cases.
64  */
65 static void
66 spdk_rpc_bdev_crypto_create(struct spdk_jsonrpc_request *request,
67 			    const struct spdk_json_val *params)
68 {
69 	struct rpc_construct_crypto req = {NULL};
70 	struct spdk_json_write_ctx *w;
71 	int rc;
72 
73 	if (spdk_json_decode_object(params, rpc_construct_crypto_decoders,
74 				    SPDK_COUNTOF(rpc_construct_crypto_decoders),
75 				    &req)) {
76 		SPDK_DEBUGLOG(SPDK_LOG_CRYPTO, "spdk_json_decode_object failed\n");
77 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
78 						 "spdk_json_decode_object failed");
79 		goto cleanup;
80 	}
81 
82 	rc = create_crypto_disk(req.base_bdev_name, req.name,
83 				req.crypto_pmd, req.key);
84 	if (rc) {
85 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
86 		goto cleanup;
87 	}
88 
89 	w = spdk_jsonrpc_begin_result(request);
90 	spdk_json_write_string(w, req.name);
91 	spdk_jsonrpc_end_result(request, w);
92 	free_rpc_construct_crypto(&req);
93 	return;
94 
95 cleanup:
96 	free_rpc_construct_crypto(&req);
97 }
98 SPDK_RPC_REGISTER("bdev_crypto_create", spdk_rpc_bdev_crypto_create, SPDK_RPC_RUNTIME)
99 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_crypto_create, construct_crypto_bdev)
100 
101 struct rpc_delete_crypto {
102 	char *name;
103 };
104 
105 static void
106 free_rpc_delete_crypto(struct rpc_delete_crypto *req)
107 {
108 	free(req->name);
109 }
110 
111 static const struct spdk_json_object_decoder rpc_delete_crypto_decoders[] = {
112 	{"name", offsetof(struct rpc_delete_crypto, name), spdk_json_decode_string},
113 };
114 
115 static void
116 _spdk_rpc_bdev_crypto_delete_cb(void *cb_arg, int bdeverrno)
117 {
118 	struct spdk_jsonrpc_request *request = cb_arg;
119 	struct spdk_json_write_ctx *w = spdk_jsonrpc_begin_result(request);
120 
121 	spdk_json_write_bool(w, bdeverrno == 0);
122 	spdk_jsonrpc_end_result(request, w);
123 }
124 
125 static void
126 spdk_rpc_bdev_crypto_delete(struct spdk_jsonrpc_request *request,
127 			    const struct spdk_json_val *params)
128 {
129 	struct rpc_delete_crypto req = {NULL};
130 	struct spdk_bdev *bdev;
131 
132 	if (spdk_json_decode_object(params, rpc_delete_crypto_decoders,
133 				    SPDK_COUNTOF(rpc_delete_crypto_decoders),
134 				    &req)) {
135 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
136 						 "spdk_json_decode_object failed");
137 		goto cleanup;
138 	}
139 
140 	bdev = spdk_bdev_get_by_name(req.name);
141 	if (bdev == NULL) {
142 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
143 		goto cleanup;
144 	}
145 
146 	delete_crypto_disk(bdev, _spdk_rpc_bdev_crypto_delete_cb, request);
147 
148 	free_rpc_delete_crypto(&req);
149 
150 	return;
151 
152 cleanup:
153 	free_rpc_delete_crypto(&req);
154 }
155 SPDK_RPC_REGISTER("bdev_crypto_delete", spdk_rpc_bdev_crypto_delete, SPDK_RPC_RUNTIME)
156 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_crypto_delete, delete_crypto_bdev)
157