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