2
0

cryptodev-vhost.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #include "qemu/osdep.h"
  25. #include "hw/virtio/virtio-bus.h"
  26. #include "sysemu/cryptodev-vhost.h"
  27. #ifdef CONFIG_VHOST_CRYPTO
  28. #include "qapi/error.h"
  29. #include "qemu/error-report.h"
  30. #include "hw/virtio/virtio-crypto.h"
  31. #include "sysemu/cryptodev-vhost-user.h"
  32. uint64_t
  33. cryptodev_vhost_get_max_queues(
  34. CryptoDevBackendVhost *crypto)
  35. {
  36. return crypto->dev.max_queues;
  37. }
  38. void cryptodev_vhost_cleanup(CryptoDevBackendVhost *crypto)
  39. {
  40. vhost_dev_cleanup(&crypto->dev);
  41. g_free(crypto);
  42. }
  43. struct CryptoDevBackendVhost *
  44. cryptodev_vhost_init(
  45. CryptoDevBackendVhostOptions *options)
  46. {
  47. int r;
  48. CryptoDevBackendVhost *crypto;
  49. Error *local_err = NULL;
  50. crypto = g_new(CryptoDevBackendVhost, 1);
  51. crypto->dev.max_queues = 1;
  52. crypto->dev.nvqs = 1;
  53. crypto->dev.vqs = crypto->vqs;
  54. crypto->cc = options->cc;
  55. crypto->dev.protocol_features = 0;
  56. crypto->backend = -1;
  57. /* vhost-user needs vq_index to initiate a specific queue pair */
  58. crypto->dev.vq_index = crypto->cc->queue_index * crypto->dev.nvqs;
  59. r = vhost_dev_init(&crypto->dev, options->opaque, options->backend_type, 0,
  60. &local_err);
  61. if (r < 0) {
  62. error_report_err(local_err);
  63. goto fail;
  64. }
  65. return crypto;
  66. fail:
  67. g_free(crypto);
  68. return NULL;
  69. }
  70. static int
  71. cryptodev_vhost_start_one(CryptoDevBackendVhost *crypto,
  72. VirtIODevice *dev)
  73. {
  74. int r;
  75. crypto->dev.nvqs = 1;
  76. crypto->dev.vqs = crypto->vqs;
  77. r = vhost_dev_enable_notifiers(&crypto->dev, dev);
  78. if (r < 0) {
  79. goto fail_notifiers;
  80. }
  81. r = vhost_dev_start(&crypto->dev, dev, false);
  82. if (r < 0) {
  83. goto fail_start;
  84. }
  85. return 0;
  86. fail_start:
  87. vhost_dev_disable_notifiers(&crypto->dev, dev);
  88. fail_notifiers:
  89. return r;
  90. }
  91. static void
  92. cryptodev_vhost_stop_one(CryptoDevBackendVhost *crypto,
  93. VirtIODevice *dev)
  94. {
  95. vhost_dev_stop(&crypto->dev, dev, false);
  96. vhost_dev_disable_notifiers(&crypto->dev, dev);
  97. }
  98. CryptoDevBackendVhost *
  99. cryptodev_get_vhost(CryptoDevBackendClient *cc,
  100. CryptoDevBackend *b,
  101. uint16_t queue)
  102. {
  103. CryptoDevBackendVhost *vhost_crypto = NULL;
  104. if (!cc) {
  105. return NULL;
  106. }
  107. switch (cc->type) {
  108. #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
  109. case QCRYPTODEV_BACKEND_TYPE_VHOST_USER:
  110. vhost_crypto = cryptodev_vhost_user_get_vhost(cc, b, queue);
  111. break;
  112. #endif
  113. default:
  114. break;
  115. }
  116. return vhost_crypto;
  117. }
  118. static void
  119. cryptodev_vhost_set_vq_index(CryptoDevBackendVhost *crypto,
  120. int vq_index)
  121. {
  122. crypto->dev.vq_index = vq_index;
  123. }
  124. static int
  125. vhost_set_vring_enable(CryptoDevBackendClient *cc,
  126. CryptoDevBackend *b,
  127. uint16_t queue, int enable)
  128. {
  129. CryptoDevBackendVhost *crypto =
  130. cryptodev_get_vhost(cc, b, queue);
  131. const VhostOps *vhost_ops;
  132. cc->vring_enable = enable;
  133. if (!crypto) {
  134. return 0;
  135. }
  136. vhost_ops = crypto->dev.vhost_ops;
  137. if (vhost_ops->vhost_set_vring_enable) {
  138. return vhost_ops->vhost_set_vring_enable(&crypto->dev, enable);
  139. }
  140. return 0;
  141. }
  142. int cryptodev_vhost_start(VirtIODevice *dev, int total_queues)
  143. {
  144. VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
  145. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
  146. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  147. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
  148. int r, e;
  149. int i;
  150. CryptoDevBackend *b = vcrypto->cryptodev;
  151. CryptoDevBackendVhost *vhost_crypto;
  152. CryptoDevBackendClient *cc;
  153. if (!k->set_guest_notifiers) {
  154. error_report("binding does not support guest notifiers");
  155. return -ENOSYS;
  156. }
  157. for (i = 0; i < total_queues; i++) {
  158. cc = b->conf.peers.ccs[i];
  159. vhost_crypto = cryptodev_get_vhost(cc, b, i);
  160. cryptodev_vhost_set_vq_index(vhost_crypto, i);
  161. /* Suppress the masking guest notifiers on vhost user
  162. * because vhost user doesn't interrupt masking/unmasking
  163. * properly.
  164. */
  165. if (cc->type == QCRYPTODEV_BACKEND_TYPE_VHOST_USER) {
  166. dev->use_guest_notifier_mask = false;
  167. }
  168. }
  169. r = k->set_guest_notifiers(qbus->parent, total_queues, true);
  170. if (r < 0) {
  171. error_report("error binding guest notifier: %d", -r);
  172. goto err;
  173. }
  174. for (i = 0; i < total_queues; i++) {
  175. cc = b->conf.peers.ccs[i];
  176. vhost_crypto = cryptodev_get_vhost(cc, b, i);
  177. r = cryptodev_vhost_start_one(vhost_crypto, dev);
  178. if (r < 0) {
  179. goto err_start;
  180. }
  181. if (cc->vring_enable) {
  182. /* restore vring enable state */
  183. r = vhost_set_vring_enable(cc, b, i, cc->vring_enable);
  184. if (r < 0) {
  185. goto err_start;
  186. }
  187. }
  188. }
  189. return 0;
  190. err_start:
  191. while (--i >= 0) {
  192. cc = b->conf.peers.ccs[i];
  193. vhost_crypto = cryptodev_get_vhost(cc, b, i);
  194. cryptodev_vhost_stop_one(vhost_crypto, dev);
  195. }
  196. e = k->set_guest_notifiers(qbus->parent, total_queues, false);
  197. if (e < 0) {
  198. error_report("vhost guest notifier cleanup failed: %d", e);
  199. }
  200. err:
  201. return r;
  202. }
  203. void cryptodev_vhost_stop(VirtIODevice *dev, int total_queues)
  204. {
  205. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
  206. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  207. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
  208. VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
  209. CryptoDevBackend *b = vcrypto->cryptodev;
  210. CryptoDevBackendVhost *vhost_crypto;
  211. CryptoDevBackendClient *cc;
  212. size_t i;
  213. int r;
  214. for (i = 0; i < total_queues; i++) {
  215. cc = b->conf.peers.ccs[i];
  216. vhost_crypto = cryptodev_get_vhost(cc, b, i);
  217. cryptodev_vhost_stop_one(vhost_crypto, dev);
  218. }
  219. r = k->set_guest_notifiers(qbus->parent, total_queues, false);
  220. if (r < 0) {
  221. error_report("vhost guest notifier cleanup failed: %d", r);
  222. }
  223. assert(r >= 0);
  224. }
  225. void cryptodev_vhost_virtqueue_mask(VirtIODevice *dev,
  226. int queue,
  227. int idx, bool mask)
  228. {
  229. VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
  230. CryptoDevBackend *b = vcrypto->cryptodev;
  231. CryptoDevBackendVhost *vhost_crypto;
  232. CryptoDevBackendClient *cc;
  233. assert(queue < MAX_CRYPTO_QUEUE_NUM);
  234. cc = b->conf.peers.ccs[queue];
  235. vhost_crypto = cryptodev_get_vhost(cc, b, queue);
  236. vhost_virtqueue_mask(&vhost_crypto->dev, dev, idx, mask);
  237. }
  238. bool cryptodev_vhost_virtqueue_pending(VirtIODevice *dev,
  239. int queue, int idx)
  240. {
  241. VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
  242. CryptoDevBackend *b = vcrypto->cryptodev;
  243. CryptoDevBackendVhost *vhost_crypto;
  244. CryptoDevBackendClient *cc;
  245. assert(queue < MAX_CRYPTO_QUEUE_NUM);
  246. cc = b->conf.peers.ccs[queue];
  247. vhost_crypto = cryptodev_get_vhost(cc, b, queue);
  248. return vhost_virtqueue_pending(&vhost_crypto->dev, idx);
  249. }
  250. #else
  251. uint64_t
  252. cryptodev_vhost_get_max_queues(CryptoDevBackendVhost *crypto)
  253. {
  254. return 0;
  255. }
  256. void cryptodev_vhost_cleanup(CryptoDevBackendVhost *crypto)
  257. {
  258. }
  259. struct CryptoDevBackendVhost *
  260. cryptodev_vhost_init(CryptoDevBackendVhostOptions *options)
  261. {
  262. return NULL;
  263. }
  264. CryptoDevBackendVhost *
  265. cryptodev_get_vhost(CryptoDevBackendClient *cc,
  266. CryptoDevBackend *b,
  267. uint16_t queue)
  268. {
  269. return NULL;
  270. }
  271. int cryptodev_vhost_start(VirtIODevice *dev, int total_queues)
  272. {
  273. return -1;
  274. }
  275. void cryptodev_vhost_stop(VirtIODevice *dev, int total_queues)
  276. {
  277. }
  278. void cryptodev_vhost_virtqueue_mask(VirtIODevice *dev,
  279. int queue,
  280. int idx, bool mask)
  281. {
  282. }
  283. bool cryptodev_vhost_virtqueue_pending(VirtIODevice *dev,
  284. int queue, int idx)
  285. {
  286. return false;
  287. }
  288. #endif