2c8272579c74c657230edd002437cdff10c92a37
[deb_dpdk.git] / drivers / crypto / null / null_crypto_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <rte_common.h>
34 #include <rte_config.h>
35 #include <rte_cryptodev_pmd.h>
36 #include <rte_cryptodev_vdev.h>
37 #include <rte_vdev.h>
38 #include <rte_malloc.h>
39
40 #include "null_crypto_pmd_private.h"
41
42 static uint8_t cryptodev_driver_id;
43
44 /** verify and set session parameters */
45 int
46 null_crypto_set_session_parameters(
47                 struct null_crypto_session *sess __rte_unused,
48                 const struct rte_crypto_sym_xform *xform)
49 {
50         if (xform == NULL) {
51                 return -EINVAL;
52         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
53                         xform->next == NULL) {
54                 /* Authentication Only */
55                 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
56                         return 0;
57         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
58                         xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
59                 /* Authentication then Cipher */
60                 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
61                         xform->next->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
62                         return 0;
63         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
64                         xform->next == NULL) {
65                 /* Cipher Only */
66                 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
67                         return 0;
68         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
69                         xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
70                 /* Cipher then Authentication */
71                 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL &&
72                         xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
73                         return 0;
74         }
75
76         return -ENOTSUP;
77 }
78
79 /** Process crypto operation for mbuf */
80 static int
81 process_op(const struct null_crypto_qp *qp, struct rte_crypto_op *op,
82                 struct null_crypto_session *sess __rte_unused)
83 {
84         /* set status as successful by default */
85         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
86
87         /* Free session if a session-less crypto op. */
88         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
89                 memset(op->sym->session, 0,
90                                 sizeof(struct null_crypto_session));
91                 rte_cryptodev_sym_session_free(op->sym->session);
92                 op->sym->session = NULL;
93         }
94
95         /*
96          * if crypto session and operation are valid just enqueue the packet
97          * in the processed ring
98          */
99         return rte_ring_enqueue(qp->processed_pkts, (void *)op);
100 }
101
102 static struct null_crypto_session *
103 get_session(struct null_crypto_qp *qp, struct rte_crypto_op *op)
104 {
105         struct null_crypto_session *sess = NULL;
106         struct rte_crypto_sym_op *sym_op = op->sym;
107
108         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
109                 if (likely(sym_op->session != NULL))
110                         sess = (struct null_crypto_session *)
111                                         get_session_private_data(
112                                         sym_op->session, cryptodev_driver_id);
113         } else {
114                 void *_sess = NULL;
115                 void *_sess_private_data = NULL;
116
117                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
118                         return NULL;
119
120                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
121                         return NULL;
122
123                 sess = (struct null_crypto_session *)_sess_private_data;
124
125                 if (unlikely(null_crypto_set_session_parameters(sess,
126                                 sym_op->xform) != 0)) {
127                         rte_mempool_put(qp->sess_mp, _sess);
128                         rte_mempool_put(qp->sess_mp, _sess_private_data);
129                         sess = NULL;
130                 }
131                 sym_op->session = (struct rte_cryptodev_sym_session *)_sess;
132                 set_session_private_data(sym_op->session, cryptodev_driver_id,
133                         _sess_private_data);
134         }
135
136         return sess;
137 }
138
139 /** Enqueue burst */
140 static uint16_t
141 null_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
142                 uint16_t nb_ops)
143 {
144         struct null_crypto_session *sess;
145         struct null_crypto_qp *qp = queue_pair;
146
147         int i, retval;
148
149         for (i = 0; i < nb_ops; i++) {
150                 sess = get_session(qp, ops[i]);
151                 if (unlikely(sess == NULL))
152                         goto enqueue_err;
153
154                 retval = process_op(qp, ops[i], sess);
155                 if (unlikely(retval < 0))
156                         goto enqueue_err;
157         }
158
159         qp->qp_stats.enqueued_count += i;
160         return i;
161
162 enqueue_err:
163         if (ops[i])
164                 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
165
166         qp->qp_stats.enqueue_err_count++;
167         return i;
168 }
169
170 /** Dequeue burst */
171 static uint16_t
172 null_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
173                 uint16_t nb_ops)
174 {
175         struct null_crypto_qp *qp = queue_pair;
176
177         unsigned nb_dequeued;
178
179         nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
180                         (void **)ops, nb_ops, NULL);
181         qp->qp_stats.dequeued_count += nb_dequeued;
182
183         return nb_dequeued;
184 }
185
186 static int cryptodev_null_remove(const char *name);
187
188 /** Create crypto device */
189 static int
190 cryptodev_null_create(const char *name,
191                 struct rte_vdev_device *vdev,
192                 struct rte_crypto_vdev_init_params *init_params)
193 {
194         struct rte_cryptodev *dev;
195         struct null_crypto_private *internals;
196
197         if (init_params->name[0] == '\0')
198                 snprintf(init_params->name, sizeof(init_params->name),
199                                 "%s", name);
200
201         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
202                         sizeof(struct null_crypto_private),
203                         init_params->socket_id,
204                         vdev);
205         if (dev == NULL) {
206                 NULL_CRYPTO_LOG_ERR("failed to create cryptodev vdev");
207                 goto init_error;
208         }
209
210         dev->driver_id = cryptodev_driver_id;
211         dev->dev_ops = null_crypto_pmd_ops;
212
213         /* register rx/tx burst functions for data path */
214         dev->dequeue_burst = null_crypto_pmd_dequeue_burst;
215         dev->enqueue_burst = null_crypto_pmd_enqueue_burst;
216
217         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
218                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
219                         RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
220
221         internals = dev->data->dev_private;
222
223         internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
224         internals->max_nb_sessions = init_params->max_nb_sessions;
225
226         return 0;
227
228 init_error:
229         NULL_CRYPTO_LOG_ERR("driver %s: cryptodev_null_create failed",
230                         init_params->name);
231         cryptodev_null_remove(init_params->name);
232
233         return -EFAULT;
234 }
235
236 /** Initialise null crypto device */
237 static int
238 cryptodev_null_probe(struct rte_vdev_device *dev)
239 {
240         struct rte_crypto_vdev_init_params init_params = {
241                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
242                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
243                 rte_socket_id(),
244                 {0}
245         };
246         const char *name;
247
248         name = rte_vdev_device_name(dev);
249         if (name == NULL)
250                 return -EINVAL;
251
252         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n",
253                 name, init_params.socket_id);
254         if (init_params.name[0] != '\0')
255                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
256                         init_params.name);
257         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
258                         init_params.max_nb_queue_pairs);
259         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
260                         init_params.max_nb_sessions);
261
262         return cryptodev_null_create(name, dev, &init_params);
263 }
264
265 /** Uninitialise null crypto device */
266 static int
267 cryptodev_null_remove(const char *name)
268 {
269         if (name == NULL)
270                 return -EINVAL;
271
272         RTE_LOG(INFO, PMD, "Closing null crypto device %s on numa socket %u\n",
273                         name, rte_socket_id());
274
275         return 0;
276 }
277
278 static int
279 cryptodev_null_remove_dev(struct rte_vdev_device *dev)
280 {
281         return cryptodev_null_remove(rte_vdev_device_name(dev));
282 }
283
284 static struct rte_vdev_driver cryptodev_null_pmd_drv = {
285         .probe = cryptodev_null_probe,
286         .remove = cryptodev_null_remove_dev,
287 };
288
289 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd_drv);
290 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd);
291 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_NULL_PMD,
292         "max_nb_queue_pairs=<int> "
293         "max_nb_sessions=<int> "
294         "socket_id=<int>");
295 RTE_PMD_REGISTER_CRYPTO_DRIVER(cryptodev_null_pmd_drv, cryptodev_driver_id);