1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017-2018 Intel Corporation
13 #include <rte_malloc.h>
14 #include <rte_cycles.h>
15 #include <rte_vhost.h>
16 #include <rte_cryptodev.h>
17 #include <rte_vhost_crypto.h>
18 #include <rte_string_fns.h>
20 #include <cmdline_rdline.h>
21 #include <cmdline_parse.h>
22 #include <cmdline_parse_string.h>
25 #define NB_VIRTIO_QUEUES (1)
26 #define MAX_PKT_BURST (64)
27 #define MAX_IV_LEN (32)
28 #define NB_MEMPOOL_OBJS (8192)
29 #define NB_CRYPTO_DESCRIPTORS (4096)
30 #define NB_CACHE_OBJS (128)
31 #define SESSION_MAP_ENTRIES (1024)
32 #define REFRESH_TIME_SEC (3)
34 #define MAX_NB_SOCKETS (4)
35 #define MAX_NB_WORKER_CORES (16)
39 char *socket_files[MAX_NB_SOCKETS];
45 struct vhost_crypto_info {
46 int vids[MAX_NB_SOCKETS];
48 struct rte_mempool *sess_pool;
49 struct rte_mempool *sess_priv_pool;
50 struct rte_mempool *cop_pool;
53 uint32_t nb_inflight_ops;
54 volatile uint32_t initialized[MAX_NB_SOCKETS];
55 } __rte_cache_aligned;
57 struct vhost_crypto_options {
58 struct lcore_option los[MAX_NB_WORKER_CORES];
59 struct vhost_crypto_info *infos[MAX_NB_WORKER_CORES];
62 uint32_t guest_polling;
66 #define OPT_CONFIG "config"
68 #define OPT_SOCKET_FILE "socket-file"
70 #define OPT_ZERO_COPY "zero-copy"
72 #define OPT_POLLING "guest-polling"
76 #define NB_SOCKET_FIELDS (2)
79 find_lo(uint32_t lcore_id)
83 for (i = 0; i < options.nb_los; i++)
84 if (options.los[i].lcore_id == lcore_id)
90 /** support *SOCKET_FILE_PATH:CRYPTODEV_ID* format */
92 parse_socket_arg(char *arg)
96 char *str_fld[NB_SOCKET_FIELDS];
97 struct lcore_option *lo;
101 if (rte_strsplit(arg, strlen(arg), str_fld, NB_SOCKET_FIELDS, ',') !=
103 RTE_LOG(ERR, USER1, "Invalid socket parameter '%s'\n", arg);
108 lcore_id = strtoul(str_fld[0], &end, 0);
109 if (errno != 0 || end == str_fld[0] || lcore_id > 255)
112 idx = find_lo(lcore_id);
113 if (idx == UINT32_MAX) {
114 if (options.nb_los == MAX_NB_WORKER_CORES)
116 lo = &options.los[options.nb_los];
117 lo->lcore_id = lcore_id;
120 lo = &options.los[idx];
122 nb_sockets = lo->nb_sockets;
124 if (nb_sockets >= MAX_NB_SOCKETS) {
125 RTE_LOG(ERR, USER1, "Too many socket files!\n");
129 lo->socket_files[nb_sockets] = strdup(str_fld[1]);
130 if (!lo->socket_files[nb_sockets]) {
131 RTE_LOG(ERR, USER1, "Insufficient memory\n");
141 parse_config(char *q_arg)
143 struct lcore_option *lo;
145 const char *p, *p0 = q_arg;
153 uint32_t flds[_NUM_FLD];
154 char *str_fld[_NUM_FLD];
158 while ((p = strchr(p0, '(')) != NULL) {
165 if (size >= sizeof(s))
168 snprintf(s, sizeof(s), "%.*s", size, p);
169 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
172 for (i = 0; i < _NUM_FLD; i++) {
174 flds[i] = strtoul(str_fld[i], &end, 0);
175 if (errno != 0 || end == str_fld[i] || flds[i] > 255)
179 if (flds[FLD_LCORE] > RTE_MAX_LCORE)
182 i = find_lo(flds[FLD_LCORE]);
183 if (i == UINT32_MAX) {
184 if (options.nb_los == MAX_NB_WORKER_CORES)
186 lo = &options.los[options.nb_los];
189 lo = &options.los[i];
191 lo->lcore_id = flds[FLD_LCORE];
192 lo->cid = flds[FLD_CID];
193 lo->qid = flds[FLD_QID];
200 vhost_crypto_usage(const char *prgname)
202 printf("%s [EAL options] --\n"
203 " --%s <lcore>,SOCKET-FILE-PATH\n"
204 " --%s (lcore,cdev_id,queue_id)[,(lcore,cdev_id,queue_id)]\n"
206 " --%s: guest polling\n",
207 prgname, OPT_SOCKET_FILE, OPT_CONFIG,
208 OPT_ZERO_COPY, OPT_POLLING);
212 vhost_crypto_parse_args(int argc, char **argv)
215 char *prgname = argv[0];
218 struct option lgopts[] = {
219 {OPT_SOCKET_FILE, required_argument,
220 NULL, OPT_SOCKET_FILE_NUM},
221 {OPT_CONFIG, required_argument,
222 NULL, OPT_CONFIG_NUM},
223 {OPT_ZERO_COPY, no_argument,
224 NULL, OPT_ZERO_COPY_NUM},
225 {OPT_POLLING, no_argument,
226 NULL, OPT_POLLING_NUM},
232 while ((opt = getopt_long(argc, argvopt, "",
233 lgopts, &option_index)) != EOF) {
236 vhost_crypto_usage(prgname);
241 case OPT_SOCKET_FILE_NUM:
242 ret = parse_socket_arg(optarg);
244 vhost_crypto_usage(prgname);
250 ret = parse_config(optarg);
252 vhost_crypto_usage(prgname);
257 case OPT_ZERO_COPY_NUM:
259 RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE;
262 case OPT_POLLING_NUM:
263 options.guest_polling = 1;
267 vhost_crypto_usage(prgname);
278 struct vhost_crypto_info *info = NULL;
283 ret = rte_vhost_get_ifname(vid, path, PATH_MAX);
285 RTE_LOG(ERR, USER1, "Cannot find matched socket\n");
289 for (i = 0; i < options.nb_los; i++) {
290 for (j = 0; j < options.los[i].nb_sockets; j++) {
291 if (strcmp(path, options.los[i].socket_files[j]) == 0) {
292 info = options.infos[i];
302 RTE_LOG(ERR, USER1, "Cannot find recorded socket\n");
306 ret = rte_vhost_crypto_create(vid, info->cid, info->sess_pool,
307 info->sess_priv_pool,
308 rte_lcore_to_socket_id(options.los[i].lcore_id));
310 RTE_LOG(ERR, USER1, "Cannot create vhost crypto\n");
314 ret = rte_vhost_crypto_set_zero_copy(vid, options.zero_copy);
316 RTE_LOG(ERR, USER1, "Cannot %s zero copy feature\n",
317 options.zero_copy == 1 ? "enable" : "disable");
322 info->initialized[j] = 1;
326 RTE_LOG(INFO, USER1, "New Vhost-crypto Device %s, Device ID %d\n", path,
332 destroy_device(int vid)
334 struct vhost_crypto_info *info = NULL;
337 for (i = 0; i < options.nb_los; i++) {
338 for (j = 0; j < options.los[i].nb_sockets; j++) {
339 if (options.infos[i]->vids[j] == vid) {
340 info = options.infos[i];
349 RTE_LOG(ERR, USER1, "Cannot find socket file from list\n");
355 } while (info->nb_inflight_ops);
357 info->initialized[j] = 0;
361 rte_vhost_crypto_free(vid);
363 RTE_LOG(INFO, USER1, "Vhost Crypto Device %i Removed\n", vid);
366 static const struct rte_vhost_device_ops virtio_crypto_device_ops = {
367 .new_device = new_device,
368 .destroy_device = destroy_device,
372 vhost_crypto_worker(void *arg)
374 struct rte_crypto_op *ops[NB_VIRTIO_QUEUES][MAX_PKT_BURST + 1];
375 struct rte_crypto_op *ops_deq[NB_VIRTIO_QUEUES][MAX_PKT_BURST + 1];
376 struct vhost_crypto_info *info = arg;
378 int callfds[VIRTIO_CRYPTO_MAX_NUM_BURST_VQS];
379 uint32_t lcore_id = rte_lcore_id();
380 uint32_t burst_size = MAX_PKT_BURST;
382 uint32_t to_fetch, fetched;
386 RTE_LOG(INFO, USER1, "Processing on Core %u started\n", lcore_id);
388 for (i = 0; i < NB_VIRTIO_QUEUES; i++) {
389 if (rte_crypto_op_bulk_alloc(info->cop_pool,
390 RTE_CRYPTO_OP_TYPE_SYMMETRIC, ops[i],
391 burst_size) < burst_size) {
392 RTE_LOG(ERR, USER1, "Failed to alloc cops\n");
399 for (i = 0; i < info->nb_vids; i++) {
400 if (unlikely(info->initialized[i] == 0))
403 for (j = 0; j < NB_VIRTIO_QUEUES; j++) {
404 to_fetch = RTE_MIN(burst_size,
405 (NB_CRYPTO_DESCRIPTORS -
406 info->nb_inflight_ops));
407 fetched = rte_vhost_crypto_fetch_requests(
408 info->vids[i], j, ops[j],
410 info->nb_inflight_ops +=
411 rte_cryptodev_enqueue_burst(
412 info->cid, info->qid, ops[j],
414 if (unlikely(rte_crypto_op_bulk_alloc(
416 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
417 ops[j], fetched) < fetched)) {
418 RTE_LOG(ERR, USER1, "Failed realloc\n");
422 fetched = rte_cryptodev_dequeue_burst(
423 info->cid, info->qid,
424 ops_deq[j], RTE_MIN(burst_size,
425 info->nb_inflight_ops));
426 fetched = rte_vhost_crypto_finalize_requests(
427 ops_deq[j], fetched, callfds,
430 info->nb_inflight_ops -= fetched;
432 if (!options.guest_polling) {
433 for (k = 0; k < nb_callfds; k++)
434 eventfd_write(callfds[k],
438 rte_mempool_put_bulk(info->cop_pool,
439 (void **)ops_deq[j], fetched);
452 for (i = 0; i < options.nb_los; i++) {
453 struct lcore_option *lo = &options.los[i];
454 struct vhost_crypto_info *info = options.infos[i];
459 rte_mempool_free(info->cop_pool);
460 rte_mempool_free(info->sess_pool);
461 rte_mempool_free(info->sess_priv_pool);
463 for (j = 0; j < lo->nb_sockets; j++) {
464 rte_vhost_driver_unregister(lo->socket_files[i]);
465 free(lo->socket_files[i]);
471 memset(&options, 0, sizeof(options));
473 /* clean up the EAL */
478 main(int argc, char *argv[])
480 struct rte_cryptodev_qp_conf qp_conf;
481 struct rte_cryptodev_config config;
482 struct rte_cryptodev_info dev_info;
484 uint32_t i, j, lcore;
487 ret = rte_eal_init(argc, argv);
493 ret = vhost_crypto_parse_args(argc, argv);
495 rte_exit(EXIT_FAILURE, "Failed to parse arguments!\n");
497 for (i = 0; i < options.nb_los; i++) {
498 struct lcore_option *lo = &options.los[i];
499 struct vhost_crypto_info *info;
501 info = rte_zmalloc_socket(NULL, sizeof(*info),
502 RTE_CACHE_LINE_SIZE, rte_lcore_to_socket_id(
511 info->nb_vids = lo->nb_sockets;
513 rte_cryptodev_info_get(info->cid, &dev_info);
514 if (options.zero_copy == RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE) {
515 #define VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD crypto_aesni_mb
516 #define VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD crypto_aesni_gcm
517 if (strstr(dev_info.driver_name,
518 RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD)) ||
519 strstr(dev_info.driver_name,
520 RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD))) {
521 RTE_LOG(ERR, USER1, "Cannot enable zero-copy in %s\n",
522 dev_info.driver_name);
528 if (dev_info.max_nb_queue_pairs < info->qid + 1) {
529 RTE_LOG(ERR, USER1, "Number of queues cannot over %u",
530 dev_info.max_nb_queue_pairs);
534 config.nb_queue_pairs = dev_info.max_nb_queue_pairs;
535 config.socket_id = rte_lcore_to_socket_id(lo->lcore_id);
536 config.ff_disable = RTE_CRYPTODEV_FF_SECURITY;
538 ret = rte_cryptodev_configure(info->cid, &config);
540 RTE_LOG(ERR, USER1, "Failed to configure cryptodev %u",
545 snprintf(name, 127, "SESS_POOL_%u", lo->lcore_id);
546 info->sess_pool = rte_cryptodev_sym_session_pool_create(name,
547 SESSION_MAP_ENTRIES, 0, 0, 0,
548 rte_lcore_to_socket_id(lo->lcore_id));
550 snprintf(name, 127, "SESS_POOL_PRIV_%u", lo->lcore_id);
551 info->sess_priv_pool = rte_mempool_create(name,
553 rte_cryptodev_sym_get_private_session_size(
554 info->cid), 64, 0, NULL, NULL, NULL, NULL,
555 rte_lcore_to_socket_id(lo->lcore_id), 0);
556 if (!info->sess_priv_pool || !info->sess_pool) {
557 RTE_LOG(ERR, USER1, "Failed to create mempool");
561 snprintf(name, 127, "COPPOOL_%u", lo->lcore_id);
562 info->cop_pool = rte_crypto_op_pool_create(name,
563 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MEMPOOL_OBJS,
564 NB_CACHE_OBJS, VHOST_CRYPTO_MAX_IV_LEN,
565 rte_lcore_to_socket_id(lo->lcore_id));
567 if (!info->cop_pool) {
568 RTE_LOG(ERR, USER1, "Failed to create crypto pool");
573 options.infos[i] = info;
575 qp_conf.nb_descriptors = NB_CRYPTO_DESCRIPTORS;
576 qp_conf.mp_session = info->sess_pool;
577 qp_conf.mp_session_private = info->sess_priv_pool;
579 for (j = 0; j < dev_info.max_nb_queue_pairs; j++) {
580 ret = rte_cryptodev_queue_pair_setup(info->cid, j,
581 &qp_conf, rte_lcore_to_socket_id(
584 RTE_LOG(ERR, USER1, "Failed to configure qp\n");
590 for (i = 0; i < options.nb_los; i++) {
591 struct lcore_option *lo = &options.los[i];
592 struct vhost_crypto_info *info = options.infos[i];
594 ret = rte_cryptodev_start(lo->cid);
596 RTE_LOG(ERR, USER1, "Failed to start cryptodev\n");
600 if (rte_eal_remote_launch(vhost_crypto_worker, info,
602 RTE_LOG(ERR, USER1, "Failed to start worker lcore");
606 for (j = 0; j < lo->nb_sockets; j++) {
607 ret = rte_vhost_driver_register(lo->socket_files[j],
608 RTE_VHOST_USER_ASYNC_COPY);
610 RTE_LOG(ERR, USER1, "socket %s already exists\n",
611 lo->socket_files[j]);
615 rte_vhost_driver_callback_register(lo->socket_files[j],
616 &virtio_crypto_device_ops);
618 ret = rte_vhost_crypto_driver_start(
619 lo->socket_files[j]);
621 RTE_LOG(ERR, USER1, "failed to start vhost.\n");
627 RTE_LCORE_FOREACH(lcore)
628 rte_eal_wait_lcore(lcore);