cryptodev.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * QEMU Crypto Device Implementation
  3. *
  4. * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
  5. *
  6. * Authors:
  7. * Gonglei <arei.gonglei@huawei.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #include "qemu/osdep.h"
  24. #include "sysemu/cryptodev.h"
  25. #include "hw/boards.h"
  26. #include "qapi/error.h"
  27. #include "qapi/visitor.h"
  28. #include "qapi-types.h"
  29. #include "qapi-visit.h"
  30. #include "qemu/config-file.h"
  31. #include "qom/object_interfaces.h"
  32. #include "hw/virtio/virtio-crypto.h"
  33. static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
  34. CryptoDevBackendClient *
  35. cryptodev_backend_new_client(const char *model,
  36. const char *name)
  37. {
  38. CryptoDevBackendClient *cc;
  39. cc = g_malloc0(sizeof(CryptoDevBackendClient));
  40. cc->model = g_strdup(model);
  41. if (name) {
  42. cc->name = g_strdup(name);
  43. }
  44. QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
  45. return cc;
  46. }
  47. void cryptodev_backend_free_client(
  48. CryptoDevBackendClient *cc)
  49. {
  50. QTAILQ_REMOVE(&crypto_clients, cc, next);
  51. g_free(cc->name);
  52. g_free(cc->model);
  53. g_free(cc->info_str);
  54. g_free(cc);
  55. }
  56. void cryptodev_backend_cleanup(
  57. CryptoDevBackend *backend,
  58. Error **errp)
  59. {
  60. CryptoDevBackendClass *bc =
  61. CRYPTODEV_BACKEND_GET_CLASS(backend);
  62. if (bc->cleanup) {
  63. bc->cleanup(backend, errp);
  64. }
  65. }
  66. int64_t cryptodev_backend_sym_create_session(
  67. CryptoDevBackend *backend,
  68. CryptoDevBackendSymSessionInfo *sess_info,
  69. uint32_t queue_index, Error **errp)
  70. {
  71. CryptoDevBackendClass *bc =
  72. CRYPTODEV_BACKEND_GET_CLASS(backend);
  73. if (bc->create_session) {
  74. return bc->create_session(backend, sess_info, queue_index, errp);
  75. }
  76. return -1;
  77. }
  78. int cryptodev_backend_sym_close_session(
  79. CryptoDevBackend *backend,
  80. uint64_t session_id,
  81. uint32_t queue_index, Error **errp)
  82. {
  83. CryptoDevBackendClass *bc =
  84. CRYPTODEV_BACKEND_GET_CLASS(backend);
  85. if (bc->close_session) {
  86. return bc->close_session(backend, session_id, queue_index, errp);
  87. }
  88. return -1;
  89. }
  90. static int cryptodev_backend_sym_operation(
  91. CryptoDevBackend *backend,
  92. CryptoDevBackendSymOpInfo *op_info,
  93. uint32_t queue_index, Error **errp)
  94. {
  95. CryptoDevBackendClass *bc =
  96. CRYPTODEV_BACKEND_GET_CLASS(backend);
  97. if (bc->do_sym_op) {
  98. return bc->do_sym_op(backend, op_info, queue_index, errp);
  99. }
  100. return -VIRTIO_CRYPTO_ERR;
  101. }
  102. int cryptodev_backend_crypto_operation(
  103. CryptoDevBackend *backend,
  104. void *opaque,
  105. uint32_t queue_index, Error **errp)
  106. {
  107. VirtIOCryptoReq *req = opaque;
  108. if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
  109. CryptoDevBackendSymOpInfo *op_info;
  110. op_info = req->u.sym_op_info;
  111. return cryptodev_backend_sym_operation(backend,
  112. op_info, queue_index, errp);
  113. } else {
  114. error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "",
  115. req->flags);
  116. return -VIRTIO_CRYPTO_NOTSUPP;
  117. }
  118. return -VIRTIO_CRYPTO_ERR;
  119. }
  120. static void
  121. cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
  122. void *opaque, Error **errp)
  123. {
  124. CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
  125. uint32_t value = backend->conf.peers.queues;
  126. visit_type_uint32(v, name, &value, errp);
  127. }
  128. static void
  129. cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
  130. void *opaque, Error **errp)
  131. {
  132. CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
  133. Error *local_err = NULL;
  134. uint32_t value;
  135. visit_type_uint32(v, name, &value, &local_err);
  136. if (local_err) {
  137. goto out;
  138. }
  139. if (!value) {
  140. error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
  141. PRIu32 "'", object_get_typename(obj), name, value);
  142. goto out;
  143. }
  144. backend->conf.peers.queues = value;
  145. out:
  146. error_propagate(errp, local_err);
  147. }
  148. static void
  149. cryptodev_backend_complete(UserCreatable *uc, Error **errp)
  150. {
  151. CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
  152. CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
  153. Error *local_err = NULL;
  154. if (bc->init) {
  155. bc->init(backend, &local_err);
  156. if (local_err) {
  157. goto out;
  158. }
  159. }
  160. return;
  161. out:
  162. error_propagate(errp, local_err);
  163. }
  164. void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
  165. {
  166. backend->is_used = used;
  167. }
  168. bool cryptodev_backend_is_used(CryptoDevBackend *backend)
  169. {
  170. return backend->is_used;
  171. }
  172. void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready)
  173. {
  174. backend->ready = ready;
  175. }
  176. bool cryptodev_backend_is_ready(CryptoDevBackend *backend)
  177. {
  178. return backend->ready;
  179. }
  180. static bool
  181. cryptodev_backend_can_be_deleted(UserCreatable *uc, Error **errp)
  182. {
  183. return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc));
  184. }
  185. static void cryptodev_backend_instance_init(Object *obj)
  186. {
  187. object_property_add(obj, "queues", "int",
  188. cryptodev_backend_get_queues,
  189. cryptodev_backend_set_queues,
  190. NULL, NULL, NULL);
  191. /* Initialize devices' queues property to 1 */
  192. object_property_set_int(obj, 1, "queues", NULL);
  193. }
  194. static void cryptodev_backend_finalize(Object *obj)
  195. {
  196. CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
  197. cryptodev_backend_cleanup(backend, NULL);
  198. }
  199. static void
  200. cryptodev_backend_class_init(ObjectClass *oc, void *data)
  201. {
  202. UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
  203. ucc->complete = cryptodev_backend_complete;
  204. ucc->can_be_deleted = cryptodev_backend_can_be_deleted;
  205. QTAILQ_INIT(&crypto_clients);
  206. }
  207. static const TypeInfo cryptodev_backend_info = {
  208. .name = TYPE_CRYPTODEV_BACKEND,
  209. .parent = TYPE_OBJECT,
  210. .instance_size = sizeof(CryptoDevBackend),
  211. .instance_init = cryptodev_backend_instance_init,
  212. .instance_finalize = cryptodev_backend_finalize,
  213. .class_size = sizeof(CryptoDevBackendClass),
  214. .class_init = cryptodev_backend_class_init,
  215. .interfaces = (InterfaceInfo[]) {
  216. { TYPE_USER_CREATABLE },
  217. { }
  218. }
  219. };
  220. static void
  221. cryptodev_backend_register_types(void)
  222. {
  223. type_register_static(&cryptodev_backend_info);
  224. }
  225. type_init(cryptodev_backend_register_types);