xref: /spdk/module/bdev/crypto/vbdev_crypto.c (revision 324e3261e6744af9613b13139266f6efa3276d58)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2018 Intel Corporation.
3  *   All rights reserved.
4  *   Copyright (c) 2022, 2023 NVIDIA CORPORATION & AFFILIATES.
5  *   All rights reserved.
6  */
7 
8 #include "vbdev_crypto.h"
9 
10 #include "spdk_internal/assert.h"
11 #include "spdk/thread.h"
12 #include "spdk/bdev_module.h"
13 #include "spdk/likely.h"
14 
15 struct bdev_names {
16 	struct vbdev_crypto_opts	*opts;
17 	TAILQ_ENTRY(bdev_names)		link;
18 };
19 
20 /* List of crypto_bdev names and their base bdevs via configuration file. */
21 static TAILQ_HEAD(, bdev_names) g_bdev_names = TAILQ_HEAD_INITIALIZER(g_bdev_names);
22 
23 struct vbdev_crypto {
24 	struct spdk_bdev		*base_bdev;		/* the thing we're attaching to */
25 	struct spdk_bdev_desc		*base_desc;		/* its descriptor we get from open */
26 	struct spdk_bdev		crypto_bdev;		/* the crypto virtual bdev */
27 	struct vbdev_crypto_opts	*opts;			/* crypto options such as names and DEK */
28 	TAILQ_ENTRY(vbdev_crypto)	link;
29 	struct spdk_thread		*thread;		/* thread where base device is opened */
30 };
31 
32 /* List of virtual bdevs and associated info for each. We keep the device friendly name here even
33  * though its also in the device struct because we use it early on.
34  */
35 static TAILQ_HEAD(, vbdev_crypto) g_vbdev_crypto = TAILQ_HEAD_INITIALIZER(g_vbdev_crypto);
36 
37 /* The crypto vbdev channel struct. It is allocated and freed on my behalf by the io channel code.
38  * We store things in here that are needed on per thread basis like the base_channel for this thread.
39  */
40 struct crypto_io_channel {
41 	struct spdk_io_channel		*base_ch;	/* IO channel of base device */
42 	struct spdk_io_channel		*accel_channel;	/* Accel engine channel used for crypto ops */
43 	struct spdk_accel_crypto_key	*crypto_key;
44 };
45 
46 enum crypto_io_resubmit_state {
47 	CRYPTO_IO_DECRYPT_DONE,	/* Appended decrypt, need to read */
48 	CRYPTO_IO_ENCRYPT_DONE,	/* Need to write */
49 };
50 
51 /* This is the crypto per IO context that the bdev layer allocates for us opaquely and attaches to
52  * each IO for us.
53  */
54 struct crypto_bdev_io {
55 	struct crypto_io_channel *crypto_ch;		/* need to store for crypto completion handling */
56 	struct vbdev_crypto *crypto_bdev;		/* the crypto node struct associated with this IO */
57 	/* Used for the single contiguous buffer that serves as the crypto destination target for writes */
58 	uint64_t aux_num_blocks;			/* num of blocks for the contiguous buffer */
59 	uint64_t aux_offset_blocks;			/* block offset on media */
60 	void *aux_buf_raw;				/* raw buffer that the bdev layer gave us for write buffer */
61 	struct iovec aux_buf_iov;			/* iov representing aligned contig write buffer */
62 	struct spdk_memory_domain *aux_domain;		/* memory domain of the aux buf */
63 	void *aux_domain_ctx;				/* memory domain ctx of the aux buf */
64 	struct spdk_accel_sequence *seq;		/* sequence of accel operations */
65 
66 	/* for bdev_io_wait */
67 	struct spdk_bdev_io_wait_entry bdev_io_wait;
68 	enum crypto_io_resubmit_state resubmit_state;
69 };
70 
71 static void vbdev_crypto_queue_io(struct spdk_bdev_io *bdev_io,
72 				  enum crypto_io_resubmit_state state);
73 static void _complete_internal_io(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg);
74 static void vbdev_crypto_examine(struct spdk_bdev *bdev);
75 static int vbdev_crypto_claim(const char *bdev_name);
76 static void vbdev_crypto_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io);
77 
78 static void
79 crypto_io_fail(struct crypto_bdev_io *crypto_io)
80 {
81 	struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(crypto_io);
82 
83 	/* This function can only be used to fail an IO that hasn't been sent to the base bdev,
84 	 * otherwise accel sequence might have already been executed/aborted. */
85 	spdk_accel_sequence_abort(crypto_io->seq);
86 	spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
87 }
88 
89 static void
90 crypto_write(struct crypto_io_channel *crypto_ch, struct spdk_bdev_io *bdev_io)
91 {
92 	struct vbdev_crypto *crypto_bdev = SPDK_CONTAINEROF(bdev_io->bdev, struct vbdev_crypto,
93 					   crypto_bdev);
94 	struct crypto_bdev_io *crypto_io = (struct crypto_bdev_io *)bdev_io->driver_ctx;
95 	struct spdk_bdev_ext_io_opts opts = {};
96 	int rc;
97 
98 	opts.size = sizeof(opts);
99 	opts.accel_sequence = crypto_io->seq;
100 	opts.memory_domain = crypto_io->aux_domain;
101 	opts.memory_domain_ctx = crypto_io->aux_domain_ctx;
102 
103 	/* Write the encrypted data. */
104 	rc = spdk_bdev_writev_blocks_ext(crypto_bdev->base_desc, crypto_ch->base_ch,
105 					 &crypto_io->aux_buf_iov, 1, crypto_io->aux_offset_blocks,
106 					 crypto_io->aux_num_blocks, _complete_internal_io,
107 					 bdev_io, &opts);
108 	if (spdk_unlikely(rc != 0)) {
109 		if (rc == -ENOMEM) {
110 			SPDK_DEBUGLOG(vbdev_crypto, "No memory, queue the IO.\n");
111 			vbdev_crypto_queue_io(bdev_io, CRYPTO_IO_ENCRYPT_DONE);
112 		} else {
113 			SPDK_ERRLOG("Failed to submit bdev_io!\n");
114 			crypto_io_fail(crypto_io);
115 		}
116 	}
117 }
118 
119 static void
120 crypto_encrypt_cb(void *cb_arg)
121 {
122 	struct crypto_bdev_io *crypto_io = cb_arg;
123 	struct crypto_io_channel *crypto_ch = crypto_io->crypto_ch;
124 
125 	spdk_accel_put_buf(crypto_ch->accel_channel, crypto_io->aux_buf_raw,
126 			   crypto_io->aux_domain, crypto_io->aux_domain_ctx);
127 }
128 
129 /* We're either encrypting on the way down or decrypting on the way back. */
130 static void
131 crypto_encrypt(struct crypto_io_channel *crypto_ch, struct spdk_bdev_io *bdev_io)
132 {
133 	struct crypto_bdev_io *crypto_io = (struct crypto_bdev_io *)bdev_io->driver_ctx;
134 	uint32_t crypto_len = crypto_io->crypto_bdev->crypto_bdev.blocklen;
135 	uint64_t total_length;
136 	uint64_t alignment;
137 	void *aux_buf = crypto_io->aux_buf_raw;
138 	int rc;
139 
140 	/* For encryption, we need to prepare a single contiguous buffer as the encryption
141 	 * destination, we'll then pass that along for the write after encryption is done.
142 	 * This is done to avoiding encrypting the provided write buffer which may be
143 	 * undesirable in some use cases.
144 	 */
145 	total_length = bdev_io->u.bdev.num_blocks * crypto_len;
146 	alignment = spdk_bdev_get_buf_align(&crypto_io->crypto_bdev->crypto_bdev);
147 	crypto_io->aux_buf_iov.iov_len = total_length;
148 	crypto_io->aux_buf_iov.iov_base  = (void *)(((uintptr_t)aux_buf + (alignment - 1)) & ~
149 					   (alignment - 1));
150 	crypto_io->aux_offset_blocks = bdev_io->u.bdev.offset_blocks;
151 	crypto_io->aux_num_blocks = bdev_io->u.bdev.num_blocks;
152 
153 	rc = spdk_accel_append_encrypt(&crypto_io->seq, crypto_ch->accel_channel,
154 				       crypto_ch->crypto_key, &crypto_io->aux_buf_iov, 1,
155 				       crypto_io->aux_domain, crypto_io->aux_domain_ctx,
156 				       bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
157 				       bdev_io->u.bdev.memory_domain,
158 				       bdev_io->u.bdev.memory_domain_ctx,
159 				       bdev_io->u.bdev.offset_blocks, crypto_len, 0,
160 				       crypto_encrypt_cb, crypto_io);
161 	if (spdk_unlikely(rc != 0)) {
162 		spdk_accel_put_buf(crypto_ch->accel_channel, crypto_io->aux_buf_raw,
163 				   crypto_io->aux_domain, crypto_io->aux_domain_ctx);
164 		if (rc == -ENOMEM) {
165 			SPDK_DEBUGLOG(vbdev_crypto, "No memory, queue the IO.\n");
166 			spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
167 		} else {
168 			SPDK_ERRLOG("Failed to submit bdev_io!\n");
169 			crypto_io_fail(crypto_io);
170 		}
171 
172 		return;
173 	}
174 
175 	crypto_write(crypto_ch, bdev_io);
176 }
177 
178 static void
179 _complete_internal_io(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
180 {
181 	struct spdk_bdev_io *orig_io = cb_arg;
182 	int status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
183 
184 	spdk_bdev_io_complete(orig_io, status);
185 	spdk_bdev_free_io(bdev_io);
186 }
187 
188 static void crypto_read(struct crypto_io_channel *crypto_ch, struct spdk_bdev_io *bdev_io);
189 
190 static void
191 vbdev_crypto_resubmit_io(void *arg)
192 {
193 	struct spdk_bdev_io *bdev_io = (struct spdk_bdev_io *)arg;
194 	struct crypto_bdev_io *crypto_io = (struct crypto_bdev_io *)bdev_io->driver_ctx;
195 
196 	switch (crypto_io->resubmit_state) {
197 	case CRYPTO_IO_ENCRYPT_DONE:
198 		crypto_write(crypto_io->crypto_ch, bdev_io);
199 		break;
200 	case CRYPTO_IO_DECRYPT_DONE:
201 		crypto_read(crypto_io->crypto_ch, bdev_io);
202 		break;
203 	default:
204 		SPDK_UNREACHABLE();
205 	}
206 }
207 
208 static void
209 vbdev_crypto_queue_io(struct spdk_bdev_io *bdev_io, enum crypto_io_resubmit_state state)
210 {
211 	struct crypto_bdev_io *crypto_io = (struct crypto_bdev_io *)bdev_io->driver_ctx;
212 	int rc;
213 
214 	crypto_io->bdev_io_wait.bdev = bdev_io->bdev;
215 	crypto_io->bdev_io_wait.cb_fn = vbdev_crypto_resubmit_io;
216 	crypto_io->bdev_io_wait.cb_arg = bdev_io;
217 	crypto_io->resubmit_state = state;
218 
219 	rc = spdk_bdev_queue_io_wait(bdev_io->bdev, crypto_io->crypto_ch->base_ch,
220 				     &crypto_io->bdev_io_wait);
221 	if (rc != 0) {
222 		SPDK_ERRLOG("Queue io failed in vbdev_crypto_queue_io, rc=%d.\n", rc);
223 		crypto_io_fail(crypto_io);
224 	}
225 }
226 
227 static void
228 crypto_read(struct crypto_io_channel *crypto_ch, struct spdk_bdev_io *bdev_io)
229 {
230 	struct crypto_bdev_io *crypto_io = (struct crypto_bdev_io *)bdev_io->driver_ctx;
231 	struct vbdev_crypto *crypto_bdev = SPDK_CONTAINEROF(bdev_io->bdev, struct vbdev_crypto,
232 					   crypto_bdev);
233 	struct spdk_bdev_ext_io_opts opts = {};
234 	int rc;
235 
236 	opts.size = sizeof(opts);
237 	opts.accel_sequence = crypto_io->seq;
238 	opts.memory_domain = bdev_io->u.bdev.memory_domain;
239 	opts.memory_domain_ctx = bdev_io->u.bdev.memory_domain_ctx;
240 
241 	rc = spdk_bdev_readv_blocks_ext(crypto_bdev->base_desc, crypto_ch->base_ch,
242 					bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
243 					bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
244 					_complete_internal_io, bdev_io, &opts);
245 	if (rc != 0) {
246 		if (rc == -ENOMEM) {
247 			SPDK_DEBUGLOG(vbdev_crypto, "No memory, queue the IO.\n");
248 			vbdev_crypto_queue_io(bdev_io, CRYPTO_IO_DECRYPT_DONE);
249 		} else {
250 			SPDK_ERRLOG("Failed to submit bdev_io!\n");
251 			crypto_io_fail(crypto_io);
252 		}
253 	}
254 }
255 
256 /* Callback for getting a buf from the bdev pool in the event that the caller passed
257  * in NULL, we need to own the buffer so it doesn't get freed by another vbdev module
258  * beneath us before we're done with it.
259  */
260 static void
261 crypto_read_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
262 		       bool success)
263 {
264 	struct crypto_io_channel *crypto_ch = spdk_io_channel_get_ctx(ch);
265 	struct crypto_bdev_io *crypto_io = (struct crypto_bdev_io *)bdev_io->driver_ctx;
266 	uint32_t blocklen = crypto_io->crypto_bdev->crypto_bdev.blocklen;
267 	int rc;
268 
269 	if (!success) {
270 		crypto_io_fail(crypto_io);
271 		return;
272 	}
273 
274 	rc = spdk_accel_append_decrypt(&crypto_io->seq, crypto_ch->accel_channel,
275 				       crypto_ch->crypto_key,
276 				       bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
277 				       bdev_io->u.bdev.memory_domain,
278 				       bdev_io->u.bdev.memory_domain_ctx,
279 				       bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
280 				       bdev_io->u.bdev.memory_domain,
281 				       bdev_io->u.bdev.memory_domain_ctx,
282 				       bdev_io->u.bdev.offset_blocks, blocklen, 0,
283 				       NULL, NULL);
284 	if (rc != 0) {
285 		if (rc == -ENOMEM) {
286 			SPDK_DEBUGLOG(vbdev_crypto, "No memory, queue the IO.\n");
287 			spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
288 		} else {
289 			SPDK_ERRLOG("Failed to submit bdev_io!\n");
290 			crypto_io_fail(crypto_io);
291 		}
292 
293 		return;
294 	}
295 
296 	crypto_read(crypto_ch, bdev_io);
297 }
298 
299 /* Called when someone submits IO to this crypto vbdev. For IO's not relevant to crypto,
300  * we're simply passing it on here via SPDK IO calls which in turn allocate another bdev IO
301  * and call our cpl callback provided below along with the original bdev_io so that we can
302  * complete it once this IO completes. For crypto operations, we'll either encrypt it first
303  * (writes) then call back into bdev to submit it or we'll submit a read and then catch it
304  * on the way back for decryption.
305  */
306 static void
307 vbdev_crypto_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
308 {
309 	struct vbdev_crypto *crypto_bdev = SPDK_CONTAINEROF(bdev_io->bdev, struct vbdev_crypto,
310 					   crypto_bdev);
311 	struct crypto_io_channel *crypto_ch = spdk_io_channel_get_ctx(ch);
312 	struct crypto_bdev_io *crypto_io = (struct crypto_bdev_io *)bdev_io->driver_ctx;
313 	int rc = 0;
314 
315 	memset(crypto_io, 0, sizeof(struct crypto_bdev_io));
316 	crypto_io->crypto_bdev = crypto_bdev;
317 	crypto_io->crypto_ch = crypto_ch;
318 	crypto_io->seq = bdev_io->u.bdev.accel_sequence;
319 
320 	switch (bdev_io->type) {
321 	case SPDK_BDEV_IO_TYPE_READ:
322 		spdk_bdev_io_get_buf(bdev_io, crypto_read_get_buf_cb,
323 				     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
324 		break;
325 	case SPDK_BDEV_IO_TYPE_WRITE:
326 		/* For encryption we don't want to encrypt the data in place as the host isn't
327 		 * expecting us to mangle its data buffers so we need to encrypt into the aux accel
328 		 * buffer, then we can use that as the source for the disk data transfer.
329 		 */
330 		rc = spdk_accel_get_buf(crypto_ch->accel_channel,
331 					bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen,
332 					&crypto_io->aux_buf_raw, &crypto_io->aux_domain,
333 					&crypto_io->aux_domain_ctx);
334 		if (rc == 0) {
335 			crypto_encrypt(crypto_ch, bdev_io);
336 		}
337 		break;
338 	case SPDK_BDEV_IO_TYPE_UNMAP:
339 		rc = spdk_bdev_unmap_blocks(crypto_bdev->base_desc, crypto_ch->base_ch,
340 					    bdev_io->u.bdev.offset_blocks,
341 					    bdev_io->u.bdev.num_blocks,
342 					    _complete_internal_io, bdev_io);
343 		break;
344 	case SPDK_BDEV_IO_TYPE_FLUSH:
345 		rc = spdk_bdev_flush_blocks(crypto_bdev->base_desc, crypto_ch->base_ch,
346 					    bdev_io->u.bdev.offset_blocks,
347 					    bdev_io->u.bdev.num_blocks,
348 					    _complete_internal_io, bdev_io);
349 		break;
350 	case SPDK_BDEV_IO_TYPE_RESET:
351 		rc = spdk_bdev_reset(crypto_bdev->base_desc, crypto_ch->base_ch,
352 				     _complete_internal_io, bdev_io);
353 		break;
354 	case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
355 	default:
356 		SPDK_ERRLOG("crypto: unknown I/O type %d\n", bdev_io->type);
357 		rc = -EINVAL;
358 		break;
359 	}
360 
361 	if (rc != 0) {
362 		if (rc == -ENOMEM) {
363 			SPDK_DEBUGLOG(vbdev_crypto, "No memory, queue the IO.\n");
364 			spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
365 		} else {
366 			SPDK_ERRLOG("Failed to submit bdev_io!\n");
367 			crypto_io_fail(crypto_io);
368 		}
369 	}
370 }
371 
372 /* We'll just call the base bdev and let it answer except for WZ command which
373  * we always say we don't support so that the bdev layer will actually send us
374  * real writes that we can encrypt.
375  */
376 static bool
377 vbdev_crypto_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
378 {
379 	struct vbdev_crypto *crypto_bdev = (struct vbdev_crypto *)ctx;
380 
381 	switch (io_type) {
382 	case SPDK_BDEV_IO_TYPE_WRITE:
383 	case SPDK_BDEV_IO_TYPE_UNMAP:
384 	case SPDK_BDEV_IO_TYPE_RESET:
385 	case SPDK_BDEV_IO_TYPE_READ:
386 	case SPDK_BDEV_IO_TYPE_FLUSH:
387 		return spdk_bdev_io_type_supported(crypto_bdev->base_bdev, io_type);
388 	case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
389 	/* Force the bdev layer to issue actual writes of zeroes so we can
390 	 * encrypt them as regular writes.
391 	 */
392 	default:
393 		return false;
394 	}
395 }
396 
397 /* Callback for unregistering the IO device. */
398 static void
399 _device_unregister_cb(void *io_device)
400 {
401 	struct vbdev_crypto *crypto_bdev = io_device;
402 
403 	/* Done with this crypto_bdev. */
404 	crypto_bdev->opts = NULL;
405 
406 	spdk_bdev_destruct_done(&crypto_bdev->crypto_bdev, 0);
407 	free(crypto_bdev->crypto_bdev.name);
408 	free(crypto_bdev);
409 }
410 
411 /* Wrapper for the bdev close operation. */
412 static void
413 _vbdev_crypto_destruct(void *ctx)
414 {
415 	struct spdk_bdev_desc *desc = ctx;
416 
417 	spdk_bdev_close(desc);
418 }
419 
420 /* Called after we've unregistered following a hot remove callback.
421  * Our finish entry point will be called next.
422  */
423 static int
424 vbdev_crypto_destruct(void *ctx)
425 {
426 	struct vbdev_crypto *crypto_bdev = (struct vbdev_crypto *)ctx;
427 
428 	/* Remove this device from the internal list */
429 	TAILQ_REMOVE(&g_vbdev_crypto, crypto_bdev, link);
430 
431 	/* Unclaim the underlying bdev. */
432 	spdk_bdev_module_release_bdev(crypto_bdev->base_bdev);
433 
434 	/* Close the underlying bdev on its same opened thread. */
435 	if (crypto_bdev->thread && crypto_bdev->thread != spdk_get_thread()) {
436 		spdk_thread_send_msg(crypto_bdev->thread, _vbdev_crypto_destruct, crypto_bdev->base_desc);
437 	} else {
438 		spdk_bdev_close(crypto_bdev->base_desc);
439 	}
440 
441 	/* Unregister the io_device. */
442 	spdk_io_device_unregister(crypto_bdev, _device_unregister_cb);
443 
444 	return 1;
445 }
446 
447 /* We supplied this as an entry point for upper layers who want to communicate to this
448  * bdev.  This is how they get a channel. We are passed the same context we provided when
449  * we created our crypto vbdev in examine() which, for this bdev, is the address of one of
450  * our context nodes. From here we'll ask the SPDK channel code to fill out our channel
451  * struct and we'll keep it in our crypto node.
452  */
453 static struct spdk_io_channel *
454 vbdev_crypto_get_io_channel(void *ctx)
455 {
456 	struct vbdev_crypto *crypto_bdev = (struct vbdev_crypto *)ctx;
457 
458 	/* The IO channel code will allocate a channel for us which consists of
459 	 * the SPDK channel structure plus the size of our crypto_io_channel struct
460 	 * that we passed in when we registered our IO device. It will then call
461 	 * our channel create callback to populate any elements that we need to
462 	 * update.
463 	 */
464 	return spdk_get_io_channel(crypto_bdev);
465 }
466 
467 /* This is the output for bdev_get_bdevs() for this vbdev */
468 static int
469 vbdev_crypto_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
470 {
471 	struct vbdev_crypto *crypto_bdev = (struct vbdev_crypto *)ctx;
472 
473 	spdk_json_write_name(w, "crypto");
474 	spdk_json_write_object_begin(w);
475 	spdk_json_write_named_string(w, "base_bdev_name", spdk_bdev_get_name(crypto_bdev->base_bdev));
476 	spdk_json_write_named_string(w, "name", spdk_bdev_get_name(&crypto_bdev->crypto_bdev));
477 	spdk_json_write_named_string(w, "key_name", crypto_bdev->opts->key->param.key_name);
478 	spdk_json_write_object_end(w);
479 
480 	return 0;
481 }
482 
483 static int
484 vbdev_crypto_config_json(struct spdk_json_write_ctx *w)
485 {
486 	struct vbdev_crypto *crypto_bdev;
487 
488 	TAILQ_FOREACH(crypto_bdev, &g_vbdev_crypto, link) {
489 		spdk_json_write_object_begin(w);
490 		spdk_json_write_named_string(w, "method", "bdev_crypto_create");
491 		spdk_json_write_named_object_begin(w, "params");
492 		spdk_json_write_named_string(w, "base_bdev_name", spdk_bdev_get_name(crypto_bdev->base_bdev));
493 		spdk_json_write_named_string(w, "name", spdk_bdev_get_name(&crypto_bdev->crypto_bdev));
494 		spdk_json_write_named_string(w, "key_name", crypto_bdev->opts->key->param.key_name);
495 		spdk_json_write_object_end(w);
496 		spdk_json_write_object_end(w);
497 	}
498 	return 0;
499 }
500 
501 /* We provide this callback for the SPDK channel code to create a channel using
502  * the channel struct we provided in our module get_io_channel() entry point. Here
503  * we get and save off an underlying base channel of the device below us so that
504  * we can communicate with the base bdev on a per channel basis. We also register the
505  * poller used to complete crypto operations from the device.
506  */
507 static int
508 crypto_bdev_ch_create_cb(void *io_device, void *ctx_buf)
509 {
510 	struct crypto_io_channel *crypto_ch = ctx_buf;
511 	struct vbdev_crypto *crypto_bdev = io_device;
512 
513 	crypto_ch->base_ch = spdk_bdev_get_io_channel(crypto_bdev->base_desc);
514 	crypto_ch->accel_channel = spdk_accel_get_io_channel();
515 	crypto_ch->crypto_key = crypto_bdev->opts->key;
516 
517 	return 0;
518 }
519 
520 /* We provide this callback for the SPDK channel code to destroy a channel
521  * created with our create callback. We just need to undo anything we did
522  * when we created.
523  */
524 static void
525 crypto_bdev_ch_destroy_cb(void *io_device, void *ctx_buf)
526 {
527 	struct crypto_io_channel *crypto_ch = ctx_buf;
528 
529 	spdk_put_io_channel(crypto_ch->base_ch);
530 	spdk_put_io_channel(crypto_ch->accel_channel);
531 }
532 
533 /* Create the association from the bdev and vbdev name and insert
534  * on the global list. */
535 static int
536 vbdev_crypto_insert_name(struct vbdev_crypto_opts *opts, struct bdev_names **out)
537 {
538 	struct bdev_names *name;
539 
540 	assert(opts);
541 	assert(out);
542 
543 	TAILQ_FOREACH(name, &g_bdev_names, link) {
544 		if (strcmp(opts->vbdev_name, name->opts->vbdev_name) == 0) {
545 			SPDK_ERRLOG("Crypto bdev %s already exists\n", opts->vbdev_name);
546 			return -EEXIST;
547 		}
548 	}
549 
550 	name = calloc(1, sizeof(struct bdev_names));
551 	if (!name) {
552 		SPDK_ERRLOG("Failed to allocate memory for bdev_names.\n");
553 		return -ENOMEM;
554 	}
555 
556 	name->opts = opts;
557 	TAILQ_INSERT_TAIL(&g_bdev_names, name, link);
558 	*out = name;
559 
560 	return 0;
561 }
562 
563 void
564 free_crypto_opts(struct vbdev_crypto_opts *opts)
565 {
566 	free(opts->bdev_name);
567 	free(opts->vbdev_name);
568 	free(opts);
569 }
570 
571 static void
572 vbdev_crypto_delete_name(struct bdev_names *name)
573 {
574 	TAILQ_REMOVE(&g_bdev_names, name, link);
575 	if (name->opts) {
576 		if (name->opts->key_owner && name->opts->key) {
577 			spdk_accel_crypto_key_destroy(name->opts->key);
578 		}
579 		free_crypto_opts(name->opts);
580 		name->opts = NULL;
581 	}
582 	free(name);
583 }
584 
585 /* RPC entry point for crypto creation. */
586 int
587 create_crypto_disk(struct vbdev_crypto_opts *opts)
588 {
589 	struct bdev_names *name = NULL;
590 	int rc;
591 
592 	rc = vbdev_crypto_insert_name(opts, &name);
593 	if (rc) {
594 		return rc;
595 	}
596 
597 	rc = vbdev_crypto_claim(opts->bdev_name);
598 	if (rc == -ENODEV) {
599 		SPDK_NOTICELOG("vbdev creation deferred pending base bdev arrival\n");
600 		rc = 0;
601 	}
602 
603 	if (rc) {
604 		assert(name != NULL);
605 		/* In case of error we let the caller function to deallocate @opts
606 		 * since it is its responsibility. Setting name->opts = NULL let's
607 		 * vbdev_crypto_delete_name() know it does not have to do anything
608 		 * about @opts.
609 		 */
610 		name->opts = NULL;
611 		vbdev_crypto_delete_name(name);
612 	}
613 	return rc;
614 }
615 
616 /* Called at driver init time, parses config file to prepare for examine calls,
617  * also fully initializes the crypto drivers.
618  */
619 static int
620 vbdev_crypto_init(void)
621 {
622 	return 0;
623 }
624 
625 /* Called when the entire module is being torn down. */
626 static void
627 vbdev_crypto_finish(void)
628 {
629 	struct bdev_names *name;
630 
631 	while ((name = TAILQ_FIRST(&g_bdev_names))) {
632 		vbdev_crypto_delete_name(name);
633 	}
634 }
635 
636 /* During init we'll be asked how much memory we'd like passed to us
637  * in bev_io structures as context. Here's where we specify how
638  * much context we want per IO.
639  */
640 static int
641 vbdev_crypto_get_ctx_size(void)
642 {
643 	return sizeof(struct crypto_bdev_io);
644 }
645 
646 static void
647 vbdev_crypto_base_bdev_hotremove_cb(struct spdk_bdev *bdev_find)
648 {
649 	struct vbdev_crypto *crypto_bdev, *tmp;
650 
651 	TAILQ_FOREACH_SAFE(crypto_bdev, &g_vbdev_crypto, link, tmp) {
652 		if (bdev_find == crypto_bdev->base_bdev) {
653 			spdk_bdev_unregister(&crypto_bdev->crypto_bdev, NULL, NULL);
654 		}
655 	}
656 }
657 
658 /* Called when the underlying base bdev triggers asynchronous event such as bdev removal. */
659 static void
660 vbdev_crypto_base_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev,
661 				void *event_ctx)
662 {
663 	switch (type) {
664 	case SPDK_BDEV_EVENT_REMOVE:
665 		vbdev_crypto_base_bdev_hotremove_cb(bdev);
666 		break;
667 	default:
668 		SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type);
669 		break;
670 	}
671 }
672 
673 static int
674 vbdev_crypto_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size)
675 {
676 	struct vbdev_crypto *crypto_bdev = ctx;
677 	int num_domains;
678 
679 	/* Report base bdev's memory domains plus accel memory domain */
680 	num_domains = spdk_bdev_get_memory_domains(crypto_bdev->base_bdev, domains, array_size);
681 	if (domains != NULL && num_domains < array_size) {
682 		domains[num_domains] = spdk_accel_get_memory_domain();
683 	}
684 
685 	return num_domains + 1;
686 }
687 
688 static bool
689 vbdev_crypto_sequence_supported(void *ctx, enum spdk_bdev_io_type type)
690 {
691 	switch (type) {
692 	case SPDK_BDEV_IO_TYPE_READ:
693 	case SPDK_BDEV_IO_TYPE_WRITE:
694 		return true;
695 	default:
696 		return false;
697 	}
698 }
699 
700 /* When we register our bdev this is how we specify our entry points. */
701 static const struct spdk_bdev_fn_table vbdev_crypto_fn_table = {
702 	.destruct			= vbdev_crypto_destruct,
703 	.submit_request			= vbdev_crypto_submit_request,
704 	.io_type_supported		= vbdev_crypto_io_type_supported,
705 	.get_io_channel			= vbdev_crypto_get_io_channel,
706 	.dump_info_json			= vbdev_crypto_dump_info_json,
707 	.get_memory_domains		= vbdev_crypto_get_memory_domains,
708 	.accel_sequence_supported	= vbdev_crypto_sequence_supported,
709 };
710 
711 static struct spdk_bdev_module crypto_if = {
712 	.name = "crypto",
713 	.module_init = vbdev_crypto_init,
714 	.get_ctx_size = vbdev_crypto_get_ctx_size,
715 	.examine_config = vbdev_crypto_examine,
716 	.module_fini = vbdev_crypto_finish,
717 	.config_json = vbdev_crypto_config_json
718 };
719 
720 SPDK_BDEV_MODULE_REGISTER(crypto, &crypto_if)
721 
722 static int
723 vbdev_crypto_claim(const char *bdev_name)
724 {
725 	struct bdev_names *name;
726 	struct vbdev_crypto *vbdev;
727 	struct spdk_bdev *bdev;
728 	struct spdk_iobuf_opts iobuf_opts;
729 	int rc = 0;
730 
731 	/* Limit the max IO size by some reasonable value. Since in write operation we use aux buffer,
732 	 * let's set the limit to the large_bufsize value */
733 	spdk_iobuf_get_opts(&iobuf_opts);
734 
735 	/* Check our list of names from config versus this bdev and if
736 	 * there's a match, create the crypto_bdev & bdev accordingly.
737 	 */
738 	TAILQ_FOREACH(name, &g_bdev_names, link) {
739 		if (strcmp(name->opts->bdev_name, bdev_name) != 0) {
740 			continue;
741 		}
742 		SPDK_DEBUGLOG(vbdev_crypto, "Match on %s\n", bdev_name);
743 
744 		vbdev = calloc(1, sizeof(struct vbdev_crypto));
745 		if (!vbdev) {
746 			SPDK_ERRLOG("Failed to allocate memory for crypto_bdev.\n");
747 			return -ENOMEM;
748 		}
749 		vbdev->crypto_bdev.product_name = "crypto";
750 
751 		vbdev->crypto_bdev.name = strdup(name->opts->vbdev_name);
752 		if (!vbdev->crypto_bdev.name) {
753 			SPDK_ERRLOG("Failed to allocate memory for crypto_bdev name.\n");
754 			rc = -ENOMEM;
755 			goto error_bdev_name;
756 		}
757 
758 		rc = spdk_bdev_open_ext(bdev_name, true, vbdev_crypto_base_bdev_event_cb,
759 					NULL, &vbdev->base_desc);
760 		if (rc) {
761 			if (rc != -ENODEV) {
762 				SPDK_ERRLOG("Failed to open bdev %s: error %d\n", bdev_name, rc);
763 			}
764 			goto error_open;
765 		}
766 
767 		bdev = spdk_bdev_desc_get_bdev(vbdev->base_desc);
768 		vbdev->base_bdev = bdev;
769 
770 		vbdev->crypto_bdev.write_cache = bdev->write_cache;
771 		if (bdev->optimal_io_boundary > 0) {
772 			vbdev->crypto_bdev.optimal_io_boundary =
773 				spdk_min((iobuf_opts.large_bufsize / bdev->blocklen), bdev->optimal_io_boundary);
774 		} else {
775 			vbdev->crypto_bdev.optimal_io_boundary = (iobuf_opts.large_bufsize / bdev->blocklen);
776 		}
777 		vbdev->crypto_bdev.split_on_optimal_io_boundary = true;
778 		if (bdev->required_alignment > 0) {
779 			vbdev->crypto_bdev.required_alignment = bdev->required_alignment;
780 		} else {
781 			/* Some accel modules may not support SGL input or output, if this module works with physical
782 			 * addresses, unaligned buffer may cross huge page boundary which leads to scattered payload.
783 			 * To avoid such cases, set required_alignment to the block size */
784 			vbdev->crypto_bdev.required_alignment = spdk_u32log2(bdev->blocklen);
785 		}
786 		vbdev->crypto_bdev.blocklen = bdev->blocklen;
787 		vbdev->crypto_bdev.blockcnt = bdev->blockcnt;
788 
789 		/* This is the context that is passed to us when the bdev
790 		 * layer calls in so we'll save our crypto_bdev node here.
791 		 */
792 		vbdev->crypto_bdev.ctxt = vbdev;
793 		vbdev->crypto_bdev.fn_table = &vbdev_crypto_fn_table;
794 		vbdev->crypto_bdev.module = &crypto_if;
795 
796 		/* Assign crypto opts from the name. The pointer is valid up to the point
797 		 * the module is unloaded and all names removed from the list. */
798 		vbdev->opts = name->opts;
799 
800 		TAILQ_INSERT_TAIL(&g_vbdev_crypto, vbdev, link);
801 
802 		spdk_io_device_register(vbdev, crypto_bdev_ch_create_cb, crypto_bdev_ch_destroy_cb,
803 					sizeof(struct crypto_io_channel), vbdev->crypto_bdev.name);
804 
805 		/* Save the thread where the base device is opened */
806 		vbdev->thread = spdk_get_thread();
807 
808 		rc = spdk_bdev_module_claim_bdev(bdev, vbdev->base_desc, vbdev->crypto_bdev.module);
809 		if (rc) {
810 			SPDK_ERRLOG("Failed to claim bdev %s\n", spdk_bdev_get_name(bdev));
811 			goto error_claim;
812 		}
813 
814 		rc = spdk_bdev_register(&vbdev->crypto_bdev);
815 		if (rc < 0) {
816 			SPDK_ERRLOG("Failed to register vbdev: error %d\n", rc);
817 			rc = -EINVAL;
818 			goto error_bdev_register;
819 		}
820 		SPDK_DEBUGLOG(vbdev_crypto, "Registered io_device and virtual bdev for: %s\n",
821 			      vbdev->opts->vbdev_name);
822 		break;
823 	}
824 
825 	return rc;
826 
827 	/* Error cleanup paths. */
828 error_bdev_register:
829 	spdk_bdev_module_release_bdev(vbdev->base_bdev);
830 error_claim:
831 	TAILQ_REMOVE(&g_vbdev_crypto, vbdev, link);
832 	spdk_io_device_unregister(vbdev, NULL);
833 	spdk_bdev_close(vbdev->base_desc);
834 error_open:
835 	free(vbdev->crypto_bdev.name);
836 error_bdev_name:
837 	free(vbdev);
838 
839 	return rc;
840 }
841 
842 struct crypto_delete_disk_ctx {
843 	spdk_delete_crypto_complete cb_fn;
844 	void *cb_arg;
845 	char *bdev_name;
846 };
847 
848 static void
849 delete_crypto_disk_bdev_name(void *ctx, int rc)
850 {
851 	struct bdev_names *name;
852 	struct crypto_delete_disk_ctx *disk_ctx = ctx;
853 
854 	/* Remove the association (vbdev, bdev) from g_bdev_names. This is required so that the
855 	 * vbdev does not get re-created if the same bdev is constructed at some other time,
856 	 * unless the underlying bdev was hot-removed. */
857 	TAILQ_FOREACH(name, &g_bdev_names, link) {
858 		if (strcmp(name->opts->vbdev_name, disk_ctx->bdev_name) == 0) {
859 			vbdev_crypto_delete_name(name);
860 			break;
861 		}
862 	}
863 
864 	disk_ctx->cb_fn(disk_ctx->cb_arg, rc);
865 
866 	free(disk_ctx->bdev_name);
867 	free(disk_ctx);
868 }
869 
870 /* RPC entry for deleting a crypto vbdev. */
871 void
872 delete_crypto_disk(const char *bdev_name, spdk_delete_crypto_complete cb_fn,
873 		   void *cb_arg)
874 {
875 	int rc;
876 	struct crypto_delete_disk_ctx *ctx;
877 
878 	ctx = calloc(1, sizeof(struct crypto_delete_disk_ctx));
879 	if (!ctx) {
880 		SPDK_ERRLOG("Failed to allocate delete crypto disk ctx\n");
881 		cb_fn(cb_arg, -ENOMEM);
882 		return;
883 	}
884 
885 	ctx->bdev_name = strdup(bdev_name);
886 	if (!ctx->bdev_name) {
887 		SPDK_ERRLOG("Failed to copy bdev_name\n");
888 		free(ctx);
889 		cb_fn(cb_arg, -ENOMEM);
890 		return;
891 	}
892 	ctx->cb_arg = cb_arg;
893 	ctx->cb_fn = cb_fn;
894 	/* Some cleanup happens in the destruct callback. */
895 	rc = spdk_bdev_unregister_by_name(bdev_name, &crypto_if, delete_crypto_disk_bdev_name, ctx);
896 	if (rc != 0) {
897 		SPDK_ERRLOG("Encountered an error during bdev unregistration\n");
898 		cb_fn(cb_arg, rc);
899 		free(ctx->bdev_name);
900 		free(ctx);
901 	}
902 }
903 
904 /* Because we specified this function in our crypto bdev function table when we
905  * registered our crypto bdev, we'll get this call anytime a new bdev shows up.
906  * Here we need to decide if we care about it and if so what to do. We
907  * parsed the config file at init so we check the new bdev against the list
908  * we built up at that time and if the user configured us to attach to this
909  * bdev, here's where we do it.
910  */
911 static void
912 vbdev_crypto_examine(struct spdk_bdev *bdev)
913 {
914 	vbdev_crypto_claim(spdk_bdev_get_name(bdev));
915 	spdk_bdev_module_examine_done(&crypto_if);
916 }
917 
918 SPDK_LOG_REGISTER_COMPONENT(vbdev_crypto)
919