xref: /spdk/module/bdev/crypto/vbdev_crypto_rpc.c (revision 19d5c3ed8e87dbd240c77ae0ddb5eda25ae99b5f)
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_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
77 						 "Invalid parameters");
78 		goto cleanup;
79 	}
80 
81 	rc = create_crypto_disk(req.base_bdev_name, req.name,
82 				req.crypto_pmd, req.key);
83 	if (rc) {
84 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
85 		goto cleanup;
86 	}
87 
88 	w = spdk_jsonrpc_begin_result(request);
89 	spdk_json_write_string(w, req.name);
90 	spdk_jsonrpc_end_result(request, w);
91 	free_rpc_construct_crypto(&req);
92 	return;
93 
94 cleanup:
95 	free_rpc_construct_crypto(&req);
96 }
97 SPDK_RPC_REGISTER("bdev_crypto_create", spdk_rpc_bdev_crypto_create, SPDK_RPC_RUNTIME)
98 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_crypto_create, construct_crypto_bdev)
99 
100 struct rpc_delete_crypto {
101 	char *name;
102 };
103 
104 static void
105 free_rpc_delete_crypto(struct rpc_delete_crypto *req)
106 {
107 	free(req->name);
108 }
109 
110 static const struct spdk_json_object_decoder rpc_delete_crypto_decoders[] = {
111 	{"name", offsetof(struct rpc_delete_crypto, name), spdk_json_decode_string},
112 };
113 
114 static void
115 _spdk_rpc_bdev_crypto_delete_cb(void *cb_arg, int bdeverrno)
116 {
117 	struct spdk_jsonrpc_request *request = cb_arg;
118 	struct spdk_json_write_ctx *w = spdk_jsonrpc_begin_result(request);
119 
120 	spdk_json_write_bool(w, bdeverrno == 0);
121 	spdk_jsonrpc_end_result(request, w);
122 }
123 
124 static void
125 spdk_rpc_bdev_crypto_delete(struct spdk_jsonrpc_request *request,
126 			    const struct spdk_json_val *params)
127 {
128 	struct rpc_delete_crypto req = {NULL};
129 	struct spdk_bdev *bdev;
130 
131 	if (spdk_json_decode_object(params, rpc_delete_crypto_decoders,
132 				    SPDK_COUNTOF(rpc_delete_crypto_decoders),
133 				    &req)) {
134 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
135 						 "Invalid parameters");
136 		goto cleanup;
137 	}
138 
139 	bdev = spdk_bdev_get_by_name(req.name);
140 	if (bdev == NULL) {
141 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
142 		goto cleanup;
143 	}
144 
145 	delete_crypto_disk(bdev, _spdk_rpc_bdev_crypto_delete_cb, request);
146 
147 	free_rpc_delete_crypto(&req);
148 
149 	return;
150 
151 cleanup:
152 	free_rpc_delete_crypto(&req);
153 }
154 SPDK_RPC_REGISTER("bdev_crypto_delete", spdk_rpc_bdev_crypto_delete, SPDK_RPC_RUNTIME)
155 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_crypto_delete, delete_crypto_bdev)
156