master
c 342 lines 8.6 KB
Raw
1 /*
2 * QEMU Cryptodev backend for QEMU cipher APIs
3 *
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5 *
6 * Authors:
7 * Gonglei <arei.gonglei@huawei.com>
8 * Jay Zhou <jianjay.zhou@huawei.com>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25 #include "qemu/osdep.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "system/cryptodev-vhost.h"
28
29 #ifdef CONFIG_VHOST_CRYPTO
30 #include "qapi/error.h"
31 #include "qemu/error-report.h"
32 #include "hw/virtio/virtio-crypto.h"
33 #include "system/cryptodev-vhost-user.h"
34
35 uint64_t
36 cryptodev_vhost_get_max_queues(
37 CryptoDevBackendVhost *crypto)
38 {
39 return crypto->dev.max_queues;
40 }
41
42 void cryptodev_vhost_cleanup(CryptoDevBackendVhost *crypto)
43 {
44 vhost_dev_cleanup(&crypto->dev);
45 g_free(crypto);
46 }
47
48 struct CryptoDevBackendVhost *
49 cryptodev_vhost_init(
50 CryptoDevBackendVhostOptions *options)
51 {
52 int r;
53 CryptoDevBackendVhost *crypto;
54 Error *local_err = NULL;
55
56 crypto = g_new0(CryptoDevBackendVhost, 1);
57 crypto->dev.max_queues = 1;
58 crypto->dev.nvqs = 1;
59 crypto->dev.vqs = crypto->vqs;
60
61 crypto->cc = options->cc;
62
63 crypto->backend = -1;
64
65 /* vhost-user needs vq_index to initiate a specific queue pair */
66 crypto->dev.vq_index = crypto->cc->queue_index * crypto->dev.nvqs;
67
68 r = vhost_dev_init(&crypto->dev, options->opaque, options->backend_type, 0,
69 &local_err);
70 if (r < 0) {
71 error_report_err(local_err);
72 goto fail;
73 }
74
75 return crypto;
76 fail:
77 g_free(crypto);
78 return NULL;
79 }
80
81 static int
82 cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto,
83 VirtIODevice *dev)
84 {
85 int r;
86
87 crypto->dev.nvqs = 1;
88 crypto->dev.vqs = crypto->vqs;
89
90 r = vhost_dev_enable_notifiers(&crypto->dev, dev);
91 if (r < 0) {
92 goto fail_notifiers;
93 }
94
95 r = vhost_dev_start(&crypto->dev, dev, false);
96 if (r < 0) {
97 goto fail_start;
98 }
99
100 return 0;
101
102 fail_start:
103 vhost_dev_disable_notifiers(&crypto->dev, dev);
104 fail_notifiers:
105 return r;
106 }
107
108 static void
109 cryptodev_vhost_stop_one(CryptoDevBackendVhost *crypto,
110 VirtIODevice *dev)
111 {
112 vhost_dev_stop(&crypto->dev, dev, false);
113 vhost_dev_disable_notifiers(&crypto->dev, dev);
114 }
115
116 CryptoDevBackendVhost *
117 cryptodev_get_vhost(CryptoDevBackendClient *cc,
118 CryptoDevBackend *b,
119 uint16_t queue)
120 {
121 CryptoDevBackendVhost *vhost_crypto = NULL;
122
123 if (!cc) {
124 return NULL;
125 }
126
127 switch (cc->type) {
128 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
129 case QCRYPTODEV_BACKEND_TYPE_VHOST_USER:
130 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, b, queue);
131 break;
132 #endif
133 default:
134 break;
135 }
136
137 return vhost_crypto;
138 }
139
140 static void
141 cryptodev_vhost_set_vq_index(CryptoDevBackendVhost *crypto,
142 int vq_index)
143 {
144 crypto->dev.vq_index = vq_index;
145 }
146
147 static int
148 vhost_set_vring_enable(CryptoDevBackendClient *cc,
149 CryptoDevBackend *b,
150 uint16_t queue, int enable)
151 {
152 CryptoDevBackendVhost *crypto =
153 cryptodev_get_vhost(cc, b, queue);
154
155 cc->vring_enable = enable;
156
157 if (!crypto) {
158 return 0;
159 }
160
161 return vhost_dev_set_vring_enable(&crypto->dev, enable);
162 }
163
164 int cryptodev_vhost_start(VirtIODevice *dev, int total_queues)
165 {
166 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
167 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
168 VirtioBusState *vbus = VIRTIO_BUS(qbus);
169 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
170 int r, e;
171 int i;
172 CryptoDevBackend *b = vcrypto->cryptodev;
173 CryptoDevBackendVhost *vhost_crypto;
174 CryptoDevBackendClient *cc;
175
176 if (!k->set_guest_notifiers) {
177 error_report("binding does not support guest notifiers");
178 return -ENOSYS;
179 }
180
181 for (i = 0; i < total_queues; i++) {
182 cc = b->conf.peers.ccs[i];
183
184 vhost_crypto = cryptodev_get_vhost(cc, b, i);
185 cryptodev_vhost_set_vq_index(vhost_crypto, i);
186
187 /* Suppress the masking guest notifiers on vhost user
188 * because vhost user doesn't interrupt masking/unmasking
189 * properly.
190 */
191 if (cc->type == QCRYPTODEV_BACKEND_TYPE_VHOST_USER) {
192 dev->use_guest_notifier_mask = false;
193 }
194 }
195
196 r = k->set_guest_notifiers(qbus->parent, total_queues, true);
197 if (r < 0) {
198 error_report("error binding guest notifier: %d", -r);
199 goto err;
200 }
201
202 for (i = 0; i < total_queues; i++) {
203 cc = b->conf.peers.ccs[i];
204
205 vhost_crypto = cryptodev_get_vhost(cc, b, i);
206 r = cryptodev_vhost_start_one(vhost_crypto, dev);
207
208 if (r < 0) {
209 goto err_start;
210 }
211
212 if (cc->vring_enable) {
213 /* restore vring enable state */
214 r = vhost_set_vring_enable(cc, b, i, cc->vring_enable);
215
216 if (r < 0) {
217 goto err_start;
218 }
219 }
220 }
221
222 return 0;
223
224 err_start:
225 while (--i >= 0) {
226 cc = b->conf.peers.ccs[i];
227 vhost_crypto = cryptodev_get_vhost(cc, b, i);
228 cryptodev_vhost_stop_one(vhost_crypto, dev);
229 }
230 e = k->set_guest_notifiers(qbus->parent, total_queues, false);
231 if (e < 0) {
232 error_report("vhost guest notifier cleanup failed: %d", e);
233 }
234 err:
235 return r;
236 }
237
238 void cryptodev_vhost_stop(VirtIODevice *dev, int total_queues)
239 {
240 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
241 VirtioBusState *vbus = VIRTIO_BUS(qbus);
242 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
243 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
244 CryptoDevBackend *b = vcrypto->cryptodev;
245 CryptoDevBackendVhost *vhost_crypto;
246 CryptoDevBackendClient *cc;
247 size_t i;
248 int r;
249
250 for (i = 0; i < total_queues; i++) {
251 cc = b->conf.peers.ccs[i];
252
253 vhost_crypto = cryptodev_get_vhost(cc, b, i);
254 cryptodev_vhost_stop_one(vhost_crypto, dev);
255 }
256
257 r = k->set_guest_notifiers(qbus->parent, total_queues, false);
258 if (r < 0) {
259 error_report("vhost guest notifier cleanup failed: %d", r);
260 }
261 assert(r >= 0);
262 }
263
264 void cryptodev_vhost_virtqueue_mask(VirtIODevice *dev,
265 int queue,
266 int idx, bool mask)
267 {
268 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
269 CryptoDevBackend *b = vcrypto->cryptodev;
270 CryptoDevBackendVhost *vhost_crypto;
271 CryptoDevBackendClient *cc;
272
273 assert(queue < MAX_CRYPTO_QUEUE_NUM);
274
275 cc = b->conf.peers.ccs[queue];
276 vhost_crypto = cryptodev_get_vhost(cc, b, queue);
277
278 vhost_virtqueue_mask(&vhost_crypto->dev, dev, idx, mask);
279 }
280
281 bool cryptodev_vhost_virtqueue_pending(VirtIODevice *dev,
282 int queue, int idx)
283 {
284 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
285 CryptoDevBackend *b = vcrypto->cryptodev;
286 CryptoDevBackendVhost *vhost_crypto;
287 CryptoDevBackendClient *cc;
288
289 assert(queue < MAX_CRYPTO_QUEUE_NUM);
290
291 cc = b->conf.peers.ccs[queue];
292 vhost_crypto = cryptodev_get_vhost(cc, b, queue);
293
294 return vhost_virtqueue_pending(&vhost_crypto->dev, idx);
295 }
296
297 #else
298 uint64_t
299 cryptodev_vhost_get_max_queues(CryptoDevBackendVhost *crypto)
300 {
301 return 0;
302 }
303
304 void cryptodev_vhost_cleanup(CryptoDevBackendVhost *crypto)
305 {
306 }
307
308 struct CryptoDevBackendVhost *
309 cryptodev_vhost_init(CryptoDevBackendVhostOptions *options)
310 {
311 return NULL;
312 }
313
314 CryptoDevBackendVhost *
315 cryptodev_get_vhost(CryptoDevBackendClient *cc,
316 CryptoDevBackend *b,
317 uint16_t queue)
318 {
319 return NULL;
320 }
321
322 int cryptodev_vhost_start(VirtIODevice *dev, int total_queues)
323 {
324 return -1;
325 }
326
327 void cryptodev_vhost_stop(VirtIODevice *dev, int total_queues)
328 {
329 }
330
331 void cryptodev_vhost_virtqueue_mask(VirtIODevice *dev,
332 int queue,
333 int idx, bool mask)
334 {
335 }
336
337 bool cryptodev_vhost_virtqueue_pending(VirtIODevice *dev,
338 int queue, int idx)
339 {
340 return false;
341 }
342 #endif