PageRenderTime 109ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/backends/cryptodev-vhost-user.c

https://github.com/mstsirkin/qemu
C | 377 lines | 294 code | 57 blank | 26 comment | 33 complexity | 7f9f99265e527bfe3340c04583987008 MD5 | raw file
  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 "hw/boards.h"
  25. #include "qapi/error.h"
  26. #include "qapi/qmp/qerror.h"
  27. #include "qemu/error-report.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. CharBackend chr;
  43. char *chr_name;
  44. bool opened;
  45. CryptoDevBackendVhost *vhost_crypto[MAX_CRYPTO_QUEUE_NUM];
  46. } CryptoDevBackendVhostUser;
  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 == CRYPTODEV_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->chr;
  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, int event)
  131. {
  132. CryptoDevBackendVhostUser *s = opaque;
  133. CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
  134. Error *err = NULL;
  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. if (err) {
  150. error_report_err(err);
  151. }
  152. }
  153. static void cryptodev_vhost_user_init(
  154. CryptoDevBackend *backend, Error **errp)
  155. {
  156. int queues = backend->conf.peers.queues;
  157. size_t i;
  158. Error *local_err = NULL;
  159. Chardev *chr;
  160. CryptoDevBackendClient *cc;
  161. CryptoDevBackendVhostUser *s =
  162. CRYPTODEV_BACKEND_VHOST_USER(backend);
  163. chr = cryptodev_vhost_claim_chardev(s, &local_err);
  164. if (local_err) {
  165. error_propagate(errp, local_err);
  166. return;
  167. }
  168. s->opened = true;
  169. for (i = 0; i < queues; i++) {
  170. cc = cryptodev_backend_new_client(
  171. "cryptodev-vhost-user", NULL);
  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 = CRYPTODEV_BACKEND_TYPE_VHOST_USER;
  176. backend->conf.peers.ccs[i] = cc;
  177. if (i == 0) {
  178. if (!qemu_chr_fe_init(&s->chr, chr, &local_err)) {
  179. error_propagate(errp, local_err);
  180. return;
  181. }
  182. }
  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. }
  260. static void cryptodev_vhost_user_set_chardev(Object *obj,
  261. const char *value, Error **errp)
  262. {
  263. CryptoDevBackendVhostUser *s =
  264. CRYPTODEV_BACKEND_VHOST_USER(obj);
  265. if (s->opened) {
  266. error_setg(errp, QERR_PERMISSION_DENIED);
  267. } else {
  268. g_free(s->chr_name);
  269. s->chr_name = g_strdup(value);
  270. }
  271. }
  272. static char *
  273. cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
  274. {
  275. CryptoDevBackendVhostUser *s =
  276. CRYPTODEV_BACKEND_VHOST_USER(obj);
  277. Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
  278. if (chr && chr->label) {
  279. return g_strdup(chr->label);
  280. }
  281. return NULL;
  282. }
  283. static void cryptodev_vhost_user_instance_int(Object *obj)
  284. {
  285. object_property_add_str(obj, "chardev",
  286. cryptodev_vhost_user_get_chardev,
  287. cryptodev_vhost_user_set_chardev,
  288. NULL);
  289. }
  290. static void cryptodev_vhost_user_finalize(Object *obj)
  291. {
  292. CryptoDevBackendVhostUser *s =
  293. CRYPTODEV_BACKEND_VHOST_USER(obj);
  294. qemu_chr_fe_deinit(&s->chr, false);
  295. g_free(s->chr_name);
  296. }
  297. static void
  298. cryptodev_vhost_user_class_init(ObjectClass *oc, void *data)
  299. {
  300. CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
  301. bc->init = cryptodev_vhost_user_init;
  302. bc->cleanup = cryptodev_vhost_user_cleanup;
  303. bc->create_session = cryptodev_vhost_user_sym_create_session;
  304. bc->close_session = cryptodev_vhost_user_sym_close_session;
  305. bc->do_sym_op = NULL;
  306. }
  307. static const TypeInfo cryptodev_vhost_user_info = {
  308. .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER,
  309. .parent = TYPE_CRYPTODEV_BACKEND,
  310. .class_init = cryptodev_vhost_user_class_init,
  311. .instance_init = cryptodev_vhost_user_instance_int,
  312. .instance_finalize = cryptodev_vhost_user_finalize,
  313. .instance_size = sizeof(CryptoDevBackendVhostUser),
  314. };
  315. static void
  316. cryptodev_vhost_user_register_types(void)
  317. {
  318. type_register_static(&cryptodev_vhost_user_info);
  319. }
  320. type_init(cryptodev_vhost_user_register_types);