cryptodev-vhost-user.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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_setg(&local_error, "Unsupported opcode :%" PRIu32 "",
  244. sess_info->op_code);
  245. return -VIRTIO_CRYPTO_NOTSUPP;
  246. }
  247. if (local_error) {
  248. error_report_err(local_error);
  249. }
  250. if (ret < 0) {
  251. status = -VIRTIO_CRYPTO_ERR;
  252. } else {
  253. sess_info->session_id = ret;
  254. status = VIRTIO_CRYPTO_OK;
  255. }
  256. if (cb) {
  257. cb(opaque, status);
  258. }
  259. return 0;
  260. }
  261. static int cryptodev_vhost_user_close_session(
  262. CryptoDevBackend *backend,
  263. uint64_t session_id,
  264. uint32_t queue_index,
  265. CryptoDevCompletionFunc cb,
  266. void *opaque)
  267. {
  268. CryptoDevBackendClient *cc =
  269. backend->conf.peers.ccs[queue_index];
  270. CryptoDevBackendVhost *vhost_crypto;
  271. int ret = -1, status;
  272. vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
  273. if (vhost_crypto) {
  274. struct vhost_dev *dev = &(vhost_crypto->dev);
  275. ret = dev->vhost_ops->vhost_crypto_close_session(dev,
  276. session_id);
  277. if (ret < 0) {
  278. status = -VIRTIO_CRYPTO_ERR;
  279. } else {
  280. status = VIRTIO_CRYPTO_OK;
  281. }
  282. } else {
  283. status = -VIRTIO_CRYPTO_NOTSUPP;
  284. }
  285. if (cb) {
  286. cb(opaque, status);
  287. }
  288. return 0;
  289. }
  290. static void cryptodev_vhost_user_cleanup(
  291. CryptoDevBackend *backend,
  292. Error **errp)
  293. {
  294. CryptoDevBackendVhostUser *s =
  295. CRYPTODEV_BACKEND_VHOST_USER(backend);
  296. size_t i;
  297. int queues = backend->conf.peers.queues;
  298. CryptoDevBackendClient *cc;
  299. cryptodev_vhost_user_stop(queues, s);
  300. for (i = 0; i < queues; i++) {
  301. cc = backend->conf.peers.ccs[i];
  302. if (cc) {
  303. cryptodev_backend_free_client(cc);
  304. backend->conf.peers.ccs[i] = NULL;
  305. }
  306. }
  307. vhost_user_cleanup(&s->vhost_user);
  308. }
  309. static void cryptodev_vhost_user_set_chardev(Object *obj,
  310. const char *value, Error **errp)
  311. {
  312. CryptoDevBackendVhostUser *s =
  313. CRYPTODEV_BACKEND_VHOST_USER(obj);
  314. if (s->opened) {
  315. error_setg(errp, "Property 'chardev' can no longer be set");
  316. } else {
  317. g_free(s->chr_name);
  318. s->chr_name = g_strdup(value);
  319. }
  320. }
  321. static char *
  322. cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
  323. {
  324. CryptoDevBackendVhostUser *s =
  325. CRYPTODEV_BACKEND_VHOST_USER(obj);
  326. Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
  327. if (chr && chr->label) {
  328. return g_strdup(chr->label);
  329. }
  330. return NULL;
  331. }
  332. static void cryptodev_vhost_user_finalize(Object *obj)
  333. {
  334. CryptoDevBackendVhostUser *s =
  335. CRYPTODEV_BACKEND_VHOST_USER(obj);
  336. qemu_chr_fe_deinit(&s->chr, false);
  337. g_free(s->chr_name);
  338. }
  339. static void
  340. cryptodev_vhost_user_class_init(ObjectClass *oc, void *data)
  341. {
  342. CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
  343. bc->init = cryptodev_vhost_user_init;
  344. bc->cleanup = cryptodev_vhost_user_cleanup;
  345. bc->create_session = cryptodev_vhost_user_create_session;
  346. bc->close_session = cryptodev_vhost_user_close_session;
  347. bc->do_op = NULL;
  348. object_class_property_add_str(oc, "chardev",
  349. cryptodev_vhost_user_get_chardev,
  350. cryptodev_vhost_user_set_chardev);
  351. }
  352. static const TypeInfo cryptodev_vhost_user_info = {
  353. .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER,
  354. .parent = TYPE_CRYPTODEV_BACKEND,
  355. .class_init = cryptodev_vhost_user_class_init,
  356. .instance_finalize = cryptodev_vhost_user_finalize,
  357. .instance_size = sizeof(CryptoDevBackendVhostUser),
  358. };
  359. static void
  360. cryptodev_vhost_user_register_types(void)
  361. {
  362. type_register_static(&cryptodev_vhost_user_info);
  363. }
  364. type_init(cryptodev_vhost_user_register_types);