net/af_xdp: fix zero-copy Tx queue drain
[dpdk.git] / examples / ip_pipeline / cryptodev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #include <rte_cryptodev.h>
9 #include <rte_string_fns.h>
10
11 #include "cryptodev.h"
12
13 #define PIPELINE_CRYPTO_SESSION_CACHE_SIZE      128
14
15 static struct cryptodev_list cryptodev_list;
16
17 int
18 cryptodev_init(void)
19 {
20         TAILQ_INIT(&cryptodev_list);
21
22         return 0;
23 }
24
25 struct cryptodev *
26 cryptodev_find(const char *name)
27 {
28         struct cryptodev *cryptodev;
29
30         if (name == NULL)
31                 return NULL;
32
33         TAILQ_FOREACH(cryptodev, &cryptodev_list, node)
34                 if (strcmp(cryptodev->name, name) == 0)
35                         return cryptodev;
36
37         return NULL;
38 }
39
40 struct cryptodev *
41 cryptodev_next(struct cryptodev *cryptodev)
42 {
43         return (cryptodev == NULL) ?
44                         TAILQ_FIRST(&cryptodev_list) :
45                         TAILQ_NEXT(cryptodev, node);
46 }
47
48 struct cryptodev *
49 cryptodev_create(const char *name, struct cryptodev_params *params)
50 {
51         struct rte_cryptodev_info dev_info;
52         struct rte_cryptodev_config dev_conf;
53         struct rte_cryptodev_qp_conf queue_conf;
54         struct cryptodev *cryptodev;
55         uint32_t dev_id, i;
56         uint32_t socket_id;
57         uint32_t cache_size;
58         char mp_name[NAME_SIZE];
59         int status;
60
61         /* Check input params */
62         if ((name == NULL) ||
63                 cryptodev_find(name) ||
64                 (params->n_queues == 0) ||
65                 (params->queue_size == 0) ||
66                 (params->session_pool_size == 0))
67                 return NULL;
68
69         if (params->dev_name) {
70                 status = rte_cryptodev_get_dev_id(params->dev_name);
71                 if (status == -1)
72                         return NULL;
73
74                 dev_id = (uint32_t)status;
75         } else {
76                 if (rte_cryptodev_is_valid_dev(params->dev_id) == 0)
77                         return NULL;
78
79                 dev_id = params->dev_id;
80         }
81
82         cache_size = (params->session_pool_size / 2 <
83                         PIPELINE_CRYPTO_SESSION_CACHE_SIZE) ?
84                                         (params->session_pool_size / 2) :
85                                         PIPELINE_CRYPTO_SESSION_CACHE_SIZE;
86
87         socket_id = rte_cryptodev_socket_id(dev_id);
88         rte_cryptodev_info_get(dev_id, &dev_info);
89
90         if (dev_info.max_nb_queue_pairs < params->n_queues)
91                 return NULL;
92
93         dev_conf.socket_id = socket_id;
94         dev_conf.nb_queue_pairs = params->n_queues;
95         dev_conf.ff_disable = 0;
96
97         status = rte_cryptodev_configure(dev_id, &dev_conf);
98         if (status < 0)
99                 return NULL;
100
101         queue_conf.nb_descriptors = params->queue_size;
102         for (i = 0; i < params->n_queues; i++) {
103                 status = rte_cryptodev_queue_pair_setup(dev_id, i,
104                                 &queue_conf, socket_id);
105                 if (status < 0)
106                         return NULL;
107         }
108
109         if (rte_cryptodev_start(dev_id) < 0)
110                 return NULL;
111
112         cryptodev = calloc(1, sizeof(struct cryptodev));
113         if (cryptodev == NULL) {
114                 rte_cryptodev_stop(dev_id);
115                 return NULL;
116         }
117
118         strlcpy(cryptodev->name, name, sizeof(cryptodev->name));
119         cryptodev->dev_id = dev_id;
120         cryptodev->n_queues = params->n_queues;
121
122         snprintf(mp_name, NAME_SIZE, "%s_mp%u", name, dev_id);
123         cryptodev->mp_create = rte_cryptodev_sym_session_pool_create(
124                         mp_name,
125                         params->session_pool_size,
126                         0,
127                         cache_size,
128                         0,
129                         socket_id);
130         if (!cryptodev->mp_create)
131                 goto error_exit;
132
133         snprintf(mp_name, NAME_SIZE, "%s_mp_priv%u", name, dev_id);
134         cryptodev->mp_init = rte_mempool_create(
135                         NULL,
136                         params->session_pool_size,
137                         rte_cryptodev_sym_get_private_session_size(dev_id),
138                         cache_size,
139                         0,
140                         NULL,
141                         NULL,
142                         NULL,
143                         NULL,
144                         socket_id,
145                         0);
146         if (!cryptodev->mp_init)
147                 goto error_exit;
148
149         TAILQ_INSERT_TAIL(&cryptodev_list, cryptodev, node);
150
151         return cryptodev;
152
153 error_exit:
154         if (cryptodev->mp_create)
155                 rte_mempool_free(cryptodev->mp_create);
156         if (cryptodev->mp_init)
157                 rte_mempool_free(cryptodev->mp_init);
158
159         free(cryptodev);
160
161         return NULL;
162 }