27cb9a2f9cf174b20e08e9f04d7e58e01e3815be
[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 -1;
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 -1;
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         /*
88          * if crypto session and operation are valid just enqueue the packet
89          * in the processed ring
90          */
91         return rte_ring_enqueue(qp->processed_pkts, (void *)op);
92 }
93
94 static struct null_crypto_session *
95 get_session(struct null_crypto_qp *qp, struct rte_crypto_op *op)
96 {
97         struct null_crypto_session *sess;
98         struct rte_crypto_sym_op *sym_op = op->sym;
99
100         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
101                 if (unlikely(sym_op->session == NULL))
102                         return NULL;
103
104                 sess = (struct null_crypto_session *)sym_op->session->_private;
105         } else  {
106                 struct rte_cryptodev_sym_session *c_sess = NULL;
107
108                 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
109                         return NULL;
110
111                 sess = (struct null_crypto_session *)c_sess->_private;
112
113                 if (null_crypto_set_session_parameters(sess, sym_op->xform) != 0)
114                         return NULL;
115         }
116
117         return sess;
118 }
119
120 /** Enqueue burst */
121 static uint16_t
122 null_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
123                 uint16_t nb_ops)
124 {
125         struct null_crypto_session *sess;
126         struct null_crypto_qp *qp = queue_pair;
127
128         int i, retval;
129
130         for (i = 0; i < nb_ops; i++) {
131                 sess = get_session(qp, ops[i]);
132                 if (unlikely(sess == NULL))
133                         goto enqueue_err;
134
135                 retval = process_op(qp, ops[i], sess);
136                 if (unlikely(retval < 0))
137                         goto enqueue_err;
138         }
139
140         qp->qp_stats.enqueued_count += i;
141         return i;
142
143 enqueue_err:
144         if (ops[i])
145                 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
146
147         qp->qp_stats.enqueue_err_count++;
148         return i;
149 }
150
151 /** Dequeue burst */
152 static uint16_t
153 null_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
154                 uint16_t nb_ops)
155 {
156         struct null_crypto_qp *qp = queue_pair;
157
158         unsigned nb_dequeued;
159
160         nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
161                         (void **)ops, nb_ops, NULL);
162         qp->qp_stats.dequeued_count += nb_dequeued;
163
164         return nb_dequeued;
165 }
166
167 static int cryptodev_null_remove(const char *name);
168
169 /** Create crypto device */
170 static int
171 cryptodev_null_create(const char *name,
172                 struct rte_vdev_device *vdev,
173                 struct rte_crypto_vdev_init_params *init_params)
174 {
175         struct rte_cryptodev *dev;
176         struct null_crypto_private *internals;
177
178         if (init_params->name[0] == '\0')
179                 snprintf(init_params->name, sizeof(init_params->name),
180                                 "%s", name);
181
182         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
183                         sizeof(struct null_crypto_private),
184                         init_params->socket_id,
185                         vdev);
186         if (dev == NULL) {
187                 NULL_CRYPTO_LOG_ERR("failed to create cryptodev vdev");
188                 goto init_error;
189         }
190
191         dev->driver_id = cryptodev_driver_id;
192         dev->dev_ops = null_crypto_pmd_ops;
193
194         /* register rx/tx burst functions for data path */
195         dev->dequeue_burst = null_crypto_pmd_dequeue_burst;
196         dev->enqueue_burst = null_crypto_pmd_enqueue_burst;
197
198         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
199                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
200                         RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
201
202         internals = dev->data->dev_private;
203
204         internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
205         internals->max_nb_sessions = init_params->max_nb_sessions;
206
207         return 0;
208
209 init_error:
210         NULL_CRYPTO_LOG_ERR("driver %s: cryptodev_null_create failed",
211                         init_params->name);
212         cryptodev_null_remove(init_params->name);
213
214         return -EFAULT;
215 }
216
217 /** Initialise null crypto device */
218 static int
219 cryptodev_null_probe(struct rte_vdev_device *dev)
220 {
221         struct rte_crypto_vdev_init_params init_params = {
222                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
223                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
224                 rte_socket_id(),
225                 {0}
226         };
227         const char *name;
228
229         name = rte_vdev_device_name(dev);
230         if (name == NULL)
231                 return -EINVAL;
232
233         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n",
234                 name, init_params.socket_id);
235         if (init_params.name[0] != '\0')
236                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
237                         init_params.name);
238         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
239                         init_params.max_nb_queue_pairs);
240         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
241                         init_params.max_nb_sessions);
242
243         return cryptodev_null_create(name, dev, &init_params);
244 }
245
246 /** Uninitialise null crypto device */
247 static int
248 cryptodev_null_remove(const char *name)
249 {
250         if (name == NULL)
251                 return -EINVAL;
252
253         RTE_LOG(INFO, PMD, "Closing null crypto device %s on numa socket %u\n",
254                         name, rte_socket_id());
255
256         return 0;
257 }
258
259 static int
260 cryptodev_null_remove_dev(struct rte_vdev_device *dev)
261 {
262         return cryptodev_null_remove(rte_vdev_device_name(dev));
263 }
264
265 static struct rte_vdev_driver cryptodev_null_pmd_drv = {
266         .probe = cryptodev_null_probe,
267         .remove = cryptodev_null_remove_dev,
268 };
269
270 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd_drv);
271 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd);
272 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_NULL_PMD,
273         "max_nb_queue_pairs=<int> "
274         "max_nb_sessions=<int> "
275         "socket_id=<int>");
276 RTE_PMD_REGISTER_CRYPTO_DRIVER(cryptodev_null_pmd_drv, cryptodev_driver_id);