cryptodev-vhost-user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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, QEMUChrEvent 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. case CHR_EVENT_BREAK:
  149. case CHR_EVENT_MUX_IN:
  150. case CHR_EVENT_MUX_OUT:
  151. /* Ignore */
  152. break;
  153. }
  154. }
  155. static void cryptodev_vhost_user_init(
  156. CryptoDevBackend *backend, Error **errp)
  157. {
  158. int queues = backend->conf.peers.queues;
  159. size_t i;
  160. Error *local_err = NULL;
  161. Chardev *chr;
  162. CryptoDevBackendClient *cc;
  163. CryptoDevBackendVhostUser *s =
  164. CRYPTODEV_BACKEND_VHOST_USER(backend);
  165. chr = cryptodev_vhost_claim_chardev(s, &local_err);
  166. if (local_err) {
  167. error_propagate(errp, local_err);
  168. return;
  169. }
  170. s->opened = true;
  171. for (i = 0; i < queues; i++) {
  172. cc = cryptodev_backend_new_client(
  173. "cryptodev-vhost-user", NULL);
  174. cc->info_str = g_strdup_printf("cryptodev-vhost-user%zu to %s ",
  175. i, chr->label);
  176. cc->queue_index = i;
  177. cc->type = CRYPTODEV_BACKEND_TYPE_VHOST_USER;
  178. backend->conf.peers.ccs[i] = cc;
  179. if (i == 0) {
  180. if (!qemu_chr_fe_init(&s->chr, chr, errp)) {
  181. return;
  182. }
  183. }
  184. }
  185. if (!vhost_user_init(&s->vhost_user, &s->chr, errp)) {
  186. return;
  187. }
  188. qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
  189. cryptodev_vhost_user_event, NULL, s, NULL, true);
  190. backend->conf.crypto_services =
  191. 1u << VIRTIO_CRYPTO_SERVICE_CIPHER |
  192. 1u << VIRTIO_CRYPTO_SERVICE_HASH |
  193. 1u << VIRTIO_CRYPTO_SERVICE_MAC;
  194. backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
  195. backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
  196. backend->conf.max_size = UINT64_MAX;
  197. backend->conf.max_cipher_key_len = VHOST_USER_MAX_CIPHER_KEY_LEN;
  198. backend->conf.max_auth_key_len = VHOST_USER_MAX_AUTH_KEY_LEN;
  199. }
  200. static int64_t cryptodev_vhost_user_sym_create_session(
  201. CryptoDevBackend *backend,
  202. CryptoDevBackendSymSessionInfo *sess_info,
  203. uint32_t queue_index, Error **errp)
  204. {
  205. CryptoDevBackendClient *cc =
  206. backend->conf.peers.ccs[queue_index];
  207. CryptoDevBackendVhost *vhost_crypto;
  208. uint64_t session_id = 0;
  209. int ret;
  210. vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
  211. if (vhost_crypto) {
  212. struct vhost_dev *dev = &(vhost_crypto->dev);
  213. ret = dev->vhost_ops->vhost_crypto_create_session(dev,
  214. sess_info,
  215. &session_id);
  216. if (ret < 0) {
  217. return -1;
  218. } else {
  219. return session_id;
  220. }
  221. }
  222. return -1;
  223. }
  224. static int cryptodev_vhost_user_sym_close_session(
  225. CryptoDevBackend *backend,
  226. uint64_t session_id,
  227. uint32_t queue_index, Error **errp)
  228. {
  229. CryptoDevBackendClient *cc =
  230. backend->conf.peers.ccs[queue_index];
  231. CryptoDevBackendVhost *vhost_crypto;
  232. int ret;
  233. vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
  234. if (vhost_crypto) {
  235. struct vhost_dev *dev = &(vhost_crypto->dev);
  236. ret = dev->vhost_ops->vhost_crypto_close_session(dev,
  237. session_id);
  238. if (ret < 0) {
  239. return -1;
  240. } else {
  241. return 0;
  242. }
  243. }
  244. return -1;
  245. }
  246. static void cryptodev_vhost_user_cleanup(
  247. CryptoDevBackend *backend,
  248. Error **errp)
  249. {
  250. CryptoDevBackendVhostUser *s =
  251. CRYPTODEV_BACKEND_VHOST_USER(backend);
  252. size_t i;
  253. int queues = backend->conf.peers.queues;
  254. CryptoDevBackendClient *cc;
  255. cryptodev_vhost_user_stop(queues, s);
  256. for (i = 0; i < queues; i++) {
  257. cc = backend->conf.peers.ccs[i];
  258. if (cc) {
  259. cryptodev_backend_free_client(cc);
  260. backend->conf.peers.ccs[i] = NULL;
  261. }
  262. }
  263. vhost_user_cleanup(&s->vhost_user);
  264. }
  265. static void cryptodev_vhost_user_set_chardev(Object *obj,
  266. const char *value, Error **errp)
  267. {
  268. CryptoDevBackendVhostUser *s =
  269. CRYPTODEV_BACKEND_VHOST_USER(obj);
  270. if (s->opened) {
  271. error_setg(errp, QERR_PERMISSION_DENIED);
  272. } else {
  273. g_free(s->chr_name);
  274. s->chr_name = g_strdup(value);
  275. }
  276. }
  277. static char *
  278. cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
  279. {
  280. CryptoDevBackendVhostUser *s =
  281. CRYPTODEV_BACKEND_VHOST_USER(obj);
  282. Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
  283. if (chr && chr->label) {
  284. return g_strdup(chr->label);
  285. }
  286. return NULL;
  287. }
  288. static void cryptodev_vhost_user_instance_int(Object *obj)
  289. {
  290. object_property_add_str(obj, "chardev",
  291. cryptodev_vhost_user_get_chardev,
  292. cryptodev_vhost_user_set_chardev);
  293. }
  294. static void cryptodev_vhost_user_finalize(Object *obj)
  295. {
  296. CryptoDevBackendVhostUser *s =
  297. CRYPTODEV_BACKEND_VHOST_USER(obj);
  298. qemu_chr_fe_deinit(&s->chr, false);
  299. g_free(s->chr_name);
  300. }
  301. static void
  302. cryptodev_vhost_user_class_init(ObjectClass *oc, void *data)
  303. {
  304. CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
  305. bc->init = cryptodev_vhost_user_init;
  306. bc->cleanup = cryptodev_vhost_user_cleanup;
  307. bc->create_session = cryptodev_vhost_user_sym_create_session;
  308. bc->close_session = cryptodev_vhost_user_sym_close_session;
  309. bc->do_sym_op = NULL;
  310. }
  311. static const TypeInfo cryptodev_vhost_user_info = {
  312. .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER,
  313. .parent = TYPE_CRYPTODEV_BACKEND,
  314. .class_init = cryptodev_vhost_user_class_init,
  315. .instance_init = cryptodev_vhost_user_instance_int,
  316. .instance_finalize = cryptodev_vhost_user_finalize,
  317. .instance_size = sizeof(CryptoDevBackendVhostUser),
  318. };
  319. static void
  320. cryptodev_vhost_user_register_types(void)
  321. {
  322. type_register_static(&cryptodev_vhost_user_info);
  323. }
  324. type_init(cryptodev_vhost_user_register_types);