cryptodev.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "qapi/error.h"
  26. #include "qapi/visitor.h"
  27. #include "qemu/config-file.h"
  28. #include "qom/object_interfaces.h"
  29. #include "hw/virtio/virtio-crypto.h"
  30. static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
  31. CryptoDevBackendClient *
  32. cryptodev_backend_new_client(const char *model,
  33. const char *name)
  34. {
  35. CryptoDevBackendClient *cc;
  36. cc = g_malloc0(sizeof(CryptoDevBackendClient));
  37. cc->model = g_strdup(model);
  38. if (name) {
  39. cc->name = g_strdup(name);
  40. }
  41. QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
  42. return cc;
  43. }
  44. void cryptodev_backend_free_client(
  45. CryptoDevBackendClient *cc)
  46. {
  47. QTAILQ_REMOVE(&crypto_clients, cc, next);
  48. g_free(cc->name);
  49. g_free(cc->model);
  50. g_free(cc->info_str);
  51. g_free(cc);
  52. }
  53. void cryptodev_backend_cleanup(
  54. CryptoDevBackend *backend,
  55. Error **errp)
  56. {
  57. CryptoDevBackendClass *bc =
  58. CRYPTODEV_BACKEND_GET_CLASS(backend);
  59. if (bc->cleanup) {
  60. bc->cleanup(backend, errp);
  61. }
  62. }
  63. int64_t cryptodev_backend_sym_create_session(
  64. CryptoDevBackend *backend,
  65. CryptoDevBackendSymSessionInfo *sess_info,
  66. uint32_t queue_index, Error **errp)
  67. {
  68. CryptoDevBackendClass *bc =
  69. CRYPTODEV_BACKEND_GET_CLASS(backend);
  70. if (bc->create_session) {
  71. return bc->create_session(backend, sess_info, queue_index, errp);
  72. }
  73. return -1;
  74. }
  75. int cryptodev_backend_sym_close_session(
  76. CryptoDevBackend *backend,
  77. uint64_t session_id,
  78. uint32_t queue_index, Error **errp)
  79. {
  80. CryptoDevBackendClass *bc =
  81. CRYPTODEV_BACKEND_GET_CLASS(backend);
  82. if (bc->close_session) {
  83. return bc->close_session(backend, session_id, queue_index, errp);
  84. }
  85. return -1;
  86. }
  87. static int cryptodev_backend_sym_operation(
  88. CryptoDevBackend *backend,
  89. CryptoDevBackendSymOpInfo *op_info,
  90. uint32_t queue_index, Error **errp)
  91. {
  92. CryptoDevBackendClass *bc =
  93. CRYPTODEV_BACKEND_GET_CLASS(backend);
  94. if (bc->do_sym_op) {
  95. return bc->do_sym_op(backend, op_info, queue_index, errp);
  96. }
  97. return -VIRTIO_CRYPTO_ERR;
  98. }
  99. int cryptodev_backend_crypto_operation(
  100. CryptoDevBackend *backend,
  101. void *opaque,
  102. uint32_t queue_index, Error **errp)
  103. {
  104. VirtIOCryptoReq *req = opaque;
  105. if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
  106. CryptoDevBackendSymOpInfo *op_info;
  107. op_info = req->u.sym_op_info;
  108. return cryptodev_backend_sym_operation(backend,
  109. op_info, queue_index, errp);
  110. } else {
  111. error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "",
  112. req->flags);
  113. return -VIRTIO_CRYPTO_NOTSUPP;
  114. }
  115. return -VIRTIO_CRYPTO_ERR;
  116. }
  117. static void
  118. cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
  119. void *opaque, Error **errp)
  120. {
  121. CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
  122. uint32_t value = backend->conf.peers.queues;
  123. visit_type_uint32(v, name, &value, errp);
  124. }
  125. static void
  126. cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
  127. void *opaque, Error **errp)
  128. {
  129. CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
  130. uint32_t value;
  131. if (!visit_type_uint32(v, name, &value, errp)) {
  132. return;
  133. }
  134. if (!value) {
  135. error_setg(errp, "Property '%s.%s' doesn't take value '%" PRIu32 "'",
  136. object_get_typename(obj), name, value);
  137. return;
  138. }
  139. backend->conf.peers.queues = value;
  140. }
  141. static void
  142. cryptodev_backend_complete(UserCreatable *uc, Error **errp)
  143. {
  144. CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
  145. CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
  146. if (bc->init) {
  147. bc->init(backend, errp);
  148. }
  149. }
  150. void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
  151. {
  152. backend->is_used = used;
  153. }
  154. bool cryptodev_backend_is_used(CryptoDevBackend *backend)
  155. {
  156. return backend->is_used;
  157. }
  158. void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready)
  159. {
  160. backend->ready = ready;
  161. }
  162. bool cryptodev_backend_is_ready(CryptoDevBackend *backend)
  163. {
  164. return backend->ready;
  165. }
  166. static bool
  167. cryptodev_backend_can_be_deleted(UserCreatable *uc)
  168. {
  169. return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc));
  170. }
  171. static void cryptodev_backend_instance_init(Object *obj)
  172. {
  173. object_property_add(obj, "queues", "uint32",
  174. cryptodev_backend_get_queues,
  175. cryptodev_backend_set_queues,
  176. NULL, NULL);
  177. /* Initialize devices' queues property to 1 */
  178. object_property_set_int(obj, "queues", 1, NULL);
  179. }
  180. static void cryptodev_backend_finalize(Object *obj)
  181. {
  182. CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
  183. cryptodev_backend_cleanup(backend, NULL);
  184. }
  185. static void
  186. cryptodev_backend_class_init(ObjectClass *oc, void *data)
  187. {
  188. UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
  189. ucc->complete = cryptodev_backend_complete;
  190. ucc->can_be_deleted = cryptodev_backend_can_be_deleted;
  191. QTAILQ_INIT(&crypto_clients);
  192. }
  193. static const TypeInfo cryptodev_backend_info = {
  194. .name = TYPE_CRYPTODEV_BACKEND,
  195. .parent = TYPE_OBJECT,
  196. .instance_size = sizeof(CryptoDevBackend),
  197. .instance_init = cryptodev_backend_instance_init,
  198. .instance_finalize = cryptodev_backend_finalize,
  199. .class_size = sizeof(CryptoDevBackendClass),
  200. .class_init = cryptodev_backend_class_init,
  201. .interfaces = (InterfaceInfo[]) {
  202. { TYPE_USER_CREATABLE },
  203. { }
  204. }
  205. };
  206. static void
  207. cryptodev_backend_register_types(void)
  208. {
  209. type_register_static(&cryptodev_backend_info);
  210. }
  211. type_init(cryptodev_backend_register_types);