cryptodev-vhost-user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. *
  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 "qapi/error.h"
  25. #include "qapi/qmp/qerror.h"
  26. #include "qemu/error-report.h"
  27. #include "hw/virtio/vhost-user.h"
  28. #include "standard-headers/linux/virtio_crypto.h"
  29. #include "sysemu/cryptodev-vhost.h"
  30. #include "chardev/char-fe.h"
  31. #include "sysemu/cryptodev-vhost-user.h"
  32. /**
  33. * @TYPE_CRYPTODEV_BACKEND_VHOST_USER:
  34. * name of backend that uses vhost user server
  35. */
  36. #define TYPE_CRYPTODEV_BACKEND_VHOST_USER "cryptodev-vhost-user"
  37. #define CRYPTODEV_BACKEND_VHOST_USER(obj) \
  38. OBJECT_CHECK(CryptoDevBackendVhostUser, \
  39. (obj), TYPE_CRYPTODEV_BACKEND_VHOST_USER)
  40. typedef struct CryptoDevBackendVhostUser {
  41. CryptoDevBackend parent_obj;
  42. VhostUserState vhost_user;
  43. CharBackend chr;
  44. char *chr_name;
  45. bool opened;
  46. CryptoDevBackendVhost *vhost_crypto[MAX_CRYPTO_QUEUE_NUM];
  47. } CryptoDevBackendVhostUser;
  48. static int
  49. cryptodev_vhost_user_running(
  50. CryptoDevBackendVhost *crypto)
  51. {
  52. return crypto ? 1 : 0;
  53. }
  54. CryptoDevBackendVhost *
  55. cryptodev_vhost_user_get_vhost(
  56. CryptoDevBackendClient *cc,
  57. CryptoDevBackend *b,
  58. uint16_t queue)
  59. {
  60. CryptoDevBackendVhostUser *s =
  61. CRYPTODEV_BACKEND_VHOST_USER(b);
  62. assert(cc->type == CRYPTODEV_BACKEND_TYPE_VHOST_USER);
  63. assert(queue < MAX_CRYPTO_QUEUE_NUM);
  64. return s->vhost_crypto[queue];
  65. }
  66. static void cryptodev_vhost_user_stop(int queues,
  67. CryptoDevBackendVhostUser *s)
  68. {
  69. size_t i;
  70. for (i = 0; i < queues; i++) {
  71. if (!cryptodev_vhost_user_running(s->vhost_crypto[i])) {
  72. continue;
  73. }
  74. cryptodev_vhost_cleanup(s->vhost_crypto[i]);
  75. s->vhost_crypto[i] = NULL;
  76. }
  77. }
  78. static int
  79. cryptodev_vhost_user_start(int queues,
  80. CryptoDevBackendVhostUser *s)
  81. {
  82. CryptoDevBackendVhostOptions options;
  83. CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
  84. int max_queues;
  85. size_t i;
  86. for (i = 0; i < queues; i++) {
  87. if (cryptodev_vhost_user_running(s->vhost_crypto[i])) {
  88. continue;
  89. }
  90. options.opaque = &s->vhost_user;
  91. options.backend_type = VHOST_BACKEND_TYPE_USER;
  92. options.cc = b->conf.peers.ccs[i];
  93. s->vhost_crypto[i] = cryptodev_vhost_init(&options);
  94. if (!s->vhost_crypto[i]) {
  95. error_report("failed to init vhost_crypto for queue %zu", i);
  96. goto err;
  97. }
  98. if (i == 0) {
  99. max_queues =
  100. cryptodev_vhost_get_max_queues(s->vhost_crypto[i]);
  101. if (queues > max_queues) {
  102. error_report("you are asking more queues than supported: %d",
  103. max_queues);
  104. goto err;
  105. }
  106. }
  107. }
  108. return 0;
  109. err:
  110. cryptodev_vhost_user_stop(i + 1, s);
  111. return -1;
  112. }
  113. static Chardev *
  114. cryptodev_vhost_claim_chardev(CryptoDevBackendVhostUser *s,
  115. Error **errp)
  116. {
  117. Chardev *chr;
  118. if (s->chr_name == NULL) {
  119. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  120. "chardev", "a valid character device");
  121. return NULL;
  122. }
  123. chr = qemu_chr_find(s->chr_name);
  124. if (chr == NULL) {
  125. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  126. "Device '%s' not found", s->chr_name);
  127. return NULL;
  128. }
  129. return chr;
  130. }
  131. static void cryptodev_vhost_user_event(void *opaque, int event)
  132. {
  133. CryptoDevBackendVhostUser *s = opaque;
  134. CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
  135. int queues = b->conf.peers.queues;
  136. assert(queues < MAX_CRYPTO_QUEUE_NUM);
  137. switch (event) {
  138. case CHR_EVENT_OPENED:
  139. if (cryptodev_vhost_user_start(queues, s) < 0) {
  140. exit(1);
  141. }
  142. b->ready = true;
  143. break;
  144. case CHR_EVENT_CLOSED:
  145. b->ready = false;
  146. cryptodev_vhost_user_stop(queues, s);
  147. break;
  148. }
  149. }
  150. static void cryptodev_vhost_user_init(
  151. CryptoDevBackend *backend, Error **errp)
  152. {
  153. int queues = backend->conf.peers.queues;
  154. size_t i;
  155. Error *local_err = NULL;
  156. Chardev *chr;
  157. CryptoDevBackendClient *cc;
  158. CryptoDevBackendVhostUser *s =
  159. CRYPTODEV_BACKEND_VHOST_USER(backend);
  160. chr = cryptodev_vhost_claim_chardev(s, &local_err);
  161. if (local_err) {
  162. error_propagate(errp, local_err);
  163. return;
  164. }
  165. s->opened = true;
  166. for (i = 0; i < queues; i++) {
  167. cc = cryptodev_backend_new_client(
  168. "cryptodev-vhost-user", NULL);
  169. cc->info_str = g_strdup_printf("cryptodev-vhost-user%zu to %s ",
  170. i, chr->label);
  171. cc->queue_index = i;
  172. cc->type = CRYPTODEV_BACKEND_TYPE_VHOST_USER;
  173. backend->conf.peers.ccs[i] = cc;
  174. if (i == 0) {
  175. if (!qemu_chr_fe_init(&s->chr, chr, &local_err)) {
  176. error_propagate(errp, local_err);
  177. return;
  178. }
  179. }
  180. }
  181. if (!vhost_user_init(&s->vhost_user, &s->chr, errp)) {
  182. return;
  183. }
  184. qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
  185. cryptodev_vhost_user_event, NULL, s, NULL, true);
  186. backend->conf.crypto_services =
  187. 1u << VIRTIO_CRYPTO_SERVICE_CIPHER |
  188. 1u << VIRTIO_CRYPTO_SERVICE_HASH |
  189. 1u << VIRTIO_CRYPTO_SERVICE_MAC;
  190. backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
  191. backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
  192. backend->conf.max_size = UINT64_MAX;
  193. backend->conf.max_cipher_key_len = VHOST_USER_MAX_CIPHER_KEY_LEN;
  194. backend->conf.max_auth_key_len = VHOST_USER_MAX_AUTH_KEY_LEN;
  195. }
  196. static int64_t cryptodev_vhost_user_sym_create_session(
  197. CryptoDevBackend *backend,
  198. CryptoDevBackendSymSessionInfo *sess_info,
  199. uint32_t queue_index, Error **errp)
  200. {
  201. CryptoDevBackendClient *cc =
  202. backend->conf.peers.ccs[queue_index];
  203. CryptoDevBackendVhost *vhost_crypto;
  204. uint64_t session_id = 0;
  205. int ret;
  206. vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
  207. if (vhost_crypto) {
  208. struct vhost_dev *dev = &(vhost_crypto->dev);
  209. ret = dev->vhost_ops->vhost_crypto_create_session(dev,
  210. sess_info,
  211. &session_id);
  212. if (ret < 0) {
  213. return -1;
  214. } else {
  215. return session_id;
  216. }
  217. }
  218. return -1;
  219. }
  220. static int cryptodev_vhost_user_sym_close_session(
  221. CryptoDevBackend *backend,
  222. uint64_t session_id,
  223. uint32_t queue_index, Error **errp)
  224. {
  225. CryptoDevBackendClient *cc =
  226. backend->conf.peers.ccs[queue_index];
  227. CryptoDevBackendVhost *vhost_crypto;
  228. int ret;
  229. vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
  230. if (vhost_crypto) {
  231. struct vhost_dev *dev = &(vhost_crypto->dev);
  232. ret = dev->vhost_ops->vhost_crypto_close_session(dev,
  233. session_id);
  234. if (ret < 0) {
  235. return -1;
  236. } else {
  237. return 0;
  238. }
  239. }
  240. return -1;
  241. }
  242. static void cryptodev_vhost_user_cleanup(
  243. CryptoDevBackend *backend,
  244. Error **errp)
  245. {
  246. CryptoDevBackendVhostUser *s =
  247. CRYPTODEV_BACKEND_VHOST_USER(backend);
  248. size_t i;
  249. int queues = backend->conf.peers.queues;
  250. CryptoDevBackendClient *cc;
  251. cryptodev_vhost_user_stop(queues, s);
  252. for (i = 0; i < queues; i++) {
  253. cc = backend->conf.peers.ccs[i];
  254. if (cc) {
  255. cryptodev_backend_free_client(cc);
  256. backend->conf.peers.ccs[i] = NULL;
  257. }
  258. }
  259. vhost_user_cleanup(&s->vhost_user);
  260. }
  261. static void cryptodev_vhost_user_set_chardev(Object *obj,
  262. const char *value, Error **errp)
  263. {
  264. CryptoDevBackendVhostUser *s =
  265. CRYPTODEV_BACKEND_VHOST_USER(obj);
  266. if (s->opened) {
  267. error_setg(errp, QERR_PERMISSION_DENIED);
  268. } else {
  269. g_free(s->chr_name);
  270. s->chr_name = g_strdup(value);
  271. }
  272. }
  273. static char *
  274. cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
  275. {
  276. CryptoDevBackendVhostUser *s =
  277. CRYPTODEV_BACKEND_VHOST_USER(obj);
  278. Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
  279. if (chr && chr->label) {
  280. return g_strdup(chr->label);
  281. }
  282. return NULL;
  283. }
  284. static void cryptodev_vhost_user_instance_int(Object *obj)
  285. {
  286. object_property_add_str(obj, "chardev",
  287. cryptodev_vhost_user_get_chardev,
  288. cryptodev_vhost_user_set_chardev,
  289. NULL);
  290. }
  291. static void cryptodev_vhost_user_finalize(Object *obj)
  292. {
  293. CryptoDevBackendVhostUser *s =
  294. CRYPTODEV_BACKEND_VHOST_USER(obj);
  295. qemu_chr_fe_deinit(&s->chr, false);
  296. g_free(s->chr_name);
  297. }
  298. static void
  299. cryptodev_vhost_user_class_init(ObjectClass *oc, void *data)
  300. {
  301. CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
  302. bc->init = cryptodev_vhost_user_init;
  303. bc->cleanup = cryptodev_vhost_user_cleanup;
  304. bc->create_session = cryptodev_vhost_user_sym_create_session;
  305. bc->close_session = cryptodev_vhost_user_sym_close_session;
  306. bc->do_sym_op = NULL;
  307. }
  308. static const TypeInfo cryptodev_vhost_user_info = {
  309. .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER,
  310. .parent = TYPE_CRYPTODEV_BACKEND,
  311. .class_init = cryptodev_vhost_user_class_init,
  312. .instance_init = cryptodev_vhost_user_instance_int,
  313. .instance_finalize = cryptodev_vhost_user_finalize,
  314. .instance_size = sizeof(CryptoDevBackendVhostUser),
  315. };
  316. static void
  317. cryptodev_vhost_user_register_types(void)
  318. {
  319. type_register_static(&cryptodev_vhost_user_info);
  320. }
  321. type_init(cryptodev_vhost_user_register_types);