2
0

cryptodev-vhost-user.c 11 KB

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