cryptodev-vhost-user.c 12 KB

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