]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
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_ocf.h" | |
35 | #include "stats.h" | |
36 | #include "spdk/log.h" | |
37 | #include "spdk/rpc.h" | |
38 | #include "spdk/string.h" | |
39 | ||
40 | /* Structure to hold the parameters for this RPC method. */ | |
41 | struct rpc_construct_ocf_bdev { | |
42 | char *name; /* master vbdev */ | |
43 | char *mode; /* OCF mode (choose one) */ | |
44 | char *cache_bdev_name; /* sub bdev */ | |
45 | char *core_bdev_name; /* sub bdev */ | |
46 | }; | |
47 | ||
48 | static void | |
49 | free_rpc_construct_ocf_bdev(struct rpc_construct_ocf_bdev *r) | |
50 | { | |
51 | free(r->name); | |
52 | free(r->core_bdev_name); | |
53 | free(r->cache_bdev_name); | |
54 | free(r->mode); | |
55 | } | |
56 | ||
57 | /* Structure to decode the input parameters for this RPC method. */ | |
58 | static const struct spdk_json_object_decoder rpc_construct_ocf_bdev_decoders[] = { | |
59 | {"name", offsetof(struct rpc_construct_ocf_bdev, name), spdk_json_decode_string}, | |
60 | {"mode", offsetof(struct rpc_construct_ocf_bdev, mode), spdk_json_decode_string}, | |
61 | {"cache_bdev_name", offsetof(struct rpc_construct_ocf_bdev, cache_bdev_name), spdk_json_decode_string}, | |
62 | {"core_bdev_name", offsetof(struct rpc_construct_ocf_bdev, core_bdev_name), spdk_json_decode_string}, | |
63 | }; | |
64 | ||
65 | static void | |
66 | construct_cb(int status, struct vbdev_ocf *vbdev, void *cb_arg) | |
67 | { | |
68 | struct spdk_jsonrpc_request *request = cb_arg; | |
69 | struct spdk_json_write_ctx *w; | |
70 | ||
71 | if (status) { | |
72 | spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, | |
73 | "Could not create OCF vbdev: %d", | |
74 | status); | |
75 | } else { | |
76 | w = spdk_jsonrpc_begin_result(request); | |
77 | if (w) { | |
78 | spdk_json_write_string(w, vbdev->name); | |
79 | spdk_jsonrpc_end_result(request, w); | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | static void | |
85 | spdk_rpc_construct_ocf_bdev(struct spdk_jsonrpc_request *request, | |
86 | const struct spdk_json_val *params) | |
87 | { | |
88 | struct rpc_construct_ocf_bdev req = {NULL}; | |
89 | int ret; | |
90 | ||
91 | ret = spdk_json_decode_object(params, rpc_construct_ocf_bdev_decoders, | |
92 | SPDK_COUNTOF(rpc_construct_ocf_bdev_decoders), | |
93 | &req); | |
94 | if (ret) { | |
95 | spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, | |
96 | "Invalid parameters"); | |
97 | free_rpc_construct_ocf_bdev(&req); | |
98 | return; | |
99 | } | |
100 | ||
101 | vbdev_ocf_construct(req.name, req.mode, req.cache_bdev_name, req.core_bdev_name, false, | |
102 | construct_cb, request); | |
103 | free_rpc_construct_ocf_bdev(&req); | |
104 | } | |
105 | SPDK_RPC_REGISTER("construct_ocf_bdev", spdk_rpc_construct_ocf_bdev, SPDK_RPC_RUNTIME) | |
106 | ||
107 | /* Structure to hold the parameters for this RPC method. */ | |
108 | struct rpc_delete_ocf_bdev { | |
109 | char *name; /* master vbdev name */ | |
110 | }; | |
111 | ||
112 | static void | |
113 | free_rpc_delete_ocf_bdev(struct rpc_delete_ocf_bdev *r) | |
114 | { | |
115 | free(r->name); | |
116 | } | |
117 | ||
118 | /* Structure to decode the input parameters for this RPC method. */ | |
119 | static const struct spdk_json_object_decoder rpc_delete_ocf_bdev_decoders[] = { | |
120 | {"name", offsetof(struct rpc_delete_ocf_bdev, name), spdk_json_decode_string}, | |
121 | }; | |
122 | ||
123 | static void | |
124 | delete_cb(void *cb_arg, int status) | |
125 | { | |
126 | struct spdk_jsonrpc_request *request = cb_arg; | |
127 | struct spdk_json_write_ctx *w; | |
128 | ||
129 | if (status) { | |
130 | spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, | |
131 | "Could not delete OCF vbdev: %d", | |
132 | status); | |
133 | } else { | |
134 | w = spdk_jsonrpc_begin_result(request); | |
135 | if (w) { | |
136 | spdk_json_write_bool(w, true); | |
137 | spdk_jsonrpc_end_result(request, w); | |
138 | } | |
139 | } | |
140 | } | |
141 | ||
142 | static void | |
143 | spdk_rpc_delete_ocf_bdev(struct spdk_jsonrpc_request *request, | |
144 | const struct spdk_json_val *params) | |
145 | { | |
146 | struct rpc_delete_ocf_bdev req = {NULL}; | |
147 | struct vbdev_ocf *vbdev; | |
148 | int status; | |
149 | ||
150 | status = spdk_json_decode_object(params, rpc_delete_ocf_bdev_decoders, | |
151 | SPDK_COUNTOF(rpc_delete_ocf_bdev_decoders), | |
152 | &req); | |
153 | if (status) { | |
154 | spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, | |
155 | "Invalid parameters"); | |
156 | goto end; | |
157 | } | |
158 | ||
159 | vbdev = vbdev_ocf_get_by_name(req.name); | |
160 | if (vbdev == NULL) { | |
161 | spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, | |
162 | spdk_strerror(ENODEV)); | |
163 | goto end; | |
164 | } | |
165 | ||
166 | status = vbdev_ocf_delete(vbdev, delete_cb, request); | |
167 | if (status) { | |
168 | spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, | |
169 | "Could not delete OCF vbdev: %s", | |
170 | spdk_strerror(-status)); | |
171 | goto end; | |
172 | } | |
173 | ||
174 | end: | |
175 | free_rpc_delete_ocf_bdev(&req); | |
176 | } | |
177 | SPDK_RPC_REGISTER("delete_ocf_bdev", spdk_rpc_delete_ocf_bdev, SPDK_RPC_RUNTIME) | |
178 | ||
179 | /* Structure to hold the parameters for this RPC method. */ | |
180 | struct rpc_get_ocf_stats { | |
181 | char *name; /* master vbdev name */ | |
182 | }; | |
183 | ||
184 | static void | |
185 | free_rpc_get_ocf_stats(struct rpc_get_ocf_stats *r) | |
186 | { | |
187 | free(r->name); | |
188 | } | |
189 | ||
190 | /* Structure to decode the input parameters for this RPC method. */ | |
191 | static const struct spdk_json_object_decoder rpc_get_ocf_stats_decoders[] = { | |
192 | {"name", offsetof(struct rpc_get_ocf_stats, name), spdk_json_decode_string}, | |
193 | }; | |
194 | ||
195 | static void | |
196 | spdk_rpc_get_ocf_stats(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params) | |
197 | { | |
198 | struct rpc_get_ocf_stats req = {NULL}; | |
199 | struct spdk_json_write_ctx *w; | |
200 | struct vbdev_ocf *vbdev; | |
201 | struct vbdev_ocf_stats stats; | |
202 | int status; | |
203 | ||
204 | if (spdk_json_decode_object(params, rpc_get_ocf_stats_decoders, | |
205 | SPDK_COUNTOF(rpc_get_ocf_stats_decoders), | |
206 | &req)) { | |
207 | spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, | |
208 | "Invalid parameters"); | |
209 | goto end; | |
210 | } | |
211 | ||
212 | vbdev = vbdev_ocf_get_by_name(req.name); | |
213 | if (vbdev == NULL) { | |
214 | spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, | |
215 | spdk_strerror(ENODEV)); | |
216 | goto end; | |
217 | } | |
218 | ||
219 | status = vbdev_ocf_stats_get(vbdev->ocf_cache, vbdev->core.id, &stats); | |
220 | if (status) { | |
221 | spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, | |
222 | "Could not get stats: %s", | |
223 | spdk_strerror(-status)); | |
224 | goto end; | |
225 | } | |
226 | ||
227 | w = spdk_jsonrpc_begin_result(request); | |
228 | if (w) { | |
229 | vbdev_ocf_stats_write_json(w, &stats); | |
230 | spdk_jsonrpc_end_result(request, w); | |
231 | } | |
232 | ||
233 | end: | |
234 | free_rpc_get_ocf_stats(&req); | |
235 | } | |
236 | SPDK_RPC_REGISTER("get_ocf_stats", spdk_rpc_get_ocf_stats, SPDK_RPC_RUNTIME) | |
237 | ||
238 | /* Structure to hold the parameters for this RPC method. */ | |
239 | struct rpc_get_ocf_bdevs { | |
240 | char *name; | |
241 | }; | |
242 | ||
243 | static void | |
244 | free_rpc_get_ocf_bdevs(struct rpc_get_ocf_bdevs *r) | |
245 | { | |
246 | free(r->name); | |
247 | } | |
248 | ||
249 | /* Structure to decode the input parameters for this RPC method. */ | |
250 | static const struct spdk_json_object_decoder rpc_get_ocf_bdevs_decoders[] = { | |
251 | {"name", offsetof(struct rpc_get_ocf_bdevs, name), spdk_json_decode_string, true}, | |
252 | }; | |
253 | ||
254 | struct get_bdevs_ctx { | |
255 | char *name; | |
256 | struct spdk_json_write_ctx *w; | |
257 | }; | |
258 | ||
259 | static void | |
260 | get_bdevs_fn(struct vbdev_ocf *vbdev, void *ctx) | |
261 | { | |
262 | struct get_bdevs_ctx *cctx = ctx; | |
263 | struct spdk_json_write_ctx *w = cctx->w; | |
264 | ||
265 | if (cctx->name != NULL && | |
266 | strcmp(vbdev->name, cctx->name) && | |
267 | strcmp(vbdev->cache.name, cctx->name) && | |
268 | strcmp(vbdev->core.name, cctx->name)) { | |
269 | return; | |
270 | } | |
271 | ||
272 | spdk_json_write_object_begin(w); | |
273 | spdk_json_write_named_string(w, "name", vbdev->name); | |
274 | spdk_json_write_named_bool(w, "started", vbdev->state.started); | |
275 | ||
276 | spdk_json_write_named_object_begin(w, "cache"); | |
277 | spdk_json_write_named_string(w, "name", vbdev->cache.name); | |
278 | spdk_json_write_named_bool(w, "attached", vbdev->cache.attached); | |
279 | spdk_json_write_object_end(w); | |
280 | ||
281 | spdk_json_write_named_object_begin(w, "core"); | |
282 | spdk_json_write_named_string(w, "name", vbdev->core.name); | |
283 | spdk_json_write_named_bool(w, "attached", vbdev->core.attached); | |
284 | spdk_json_write_object_end(w); | |
285 | ||
286 | spdk_json_write_object_end(w); | |
287 | } | |
288 | ||
289 | static void | |
290 | spdk_rpc_get_ocf_bdevs(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params) | |
291 | { | |
292 | struct spdk_json_write_ctx *w; | |
293 | struct rpc_get_ocf_bdevs req = {NULL}; | |
294 | struct get_bdevs_ctx cctx; | |
295 | ||
296 | if (params && spdk_json_decode_object(params, rpc_get_ocf_bdevs_decoders, | |
297 | SPDK_COUNTOF(rpc_get_ocf_bdevs_decoders), | |
298 | &req)) { | |
299 | spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, | |
300 | "Invalid parameters"); | |
301 | goto end; | |
302 | } | |
303 | ||
304 | if (req.name) { | |
305 | if (!(vbdev_ocf_get_by_name(req.name) || vbdev_ocf_get_base_by_name(req.name))) { | |
306 | spdk_jsonrpc_send_error_response(request, | |
307 | SPDK_JSONRPC_ERROR_INVALID_PARAMS, | |
308 | spdk_strerror(ENODEV)); | |
309 | goto end; | |
310 | } | |
311 | } | |
312 | ||
313 | w = spdk_jsonrpc_begin_result(request); | |
314 | if (w == NULL) { | |
315 | return; | |
316 | } | |
317 | ||
318 | cctx.name = req.name; | |
319 | cctx.w = w; | |
320 | ||
321 | spdk_json_write_array_begin(w); | |
322 | vbdev_ocf_foreach(get_bdevs_fn, &cctx); | |
323 | spdk_json_write_array_end(w); | |
324 | spdk_jsonrpc_end_result(request, w); | |
325 | ||
326 | end: | |
327 | free_rpc_get_ocf_bdevs(&req); | |
328 | } | |
329 | SPDK_RPC_REGISTER("get_ocf_bdevs", spdk_rpc_get_ocf_bdevs, SPDK_RPC_RUNTIME) |