Imported Upstream version 16.11
[deb_dpdk.git] / drivers / crypto / null / null_crypto_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 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_vdev.h>
37 #include <rte_malloc.h>
38
39 #include "null_crypto_pmd_private.h"
40
41 /**
42  * Global static parameter used to create a unique name for each crypto device.
43  */
44 static unsigned unique_name_id;
45
46 static inline int
47 create_unique_device_name(char *name, size_t size)
48 {
49         int ret;
50
51         if (name == NULL)
52                 return -EINVAL;
53
54         ret = snprintf(name, size, "%s_%u", RTE_STR(CRYPTODEV_NAME_NULL_PMD),
55                         unique_name_id++);
56         if (ret < 0)
57                 return ret;
58         return 0;
59 }
60
61
62 /** verify and set session parameters */
63 int
64 null_crypto_set_session_parameters(
65                 struct null_crypto_session *sess __rte_unused,
66                 const struct rte_crypto_sym_xform *xform)
67 {
68         if (xform == NULL) {
69                 return -1;
70         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
71                         xform->next == NULL) {
72                 /* Authentication Only */
73                 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL)
74                         return 0;
75         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
76                         xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
77                 /* Authentication then Cipher */
78                 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL &&
79                         xform->next->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
80                         return 0;
81         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
82                         xform->next == NULL) {
83                 /* Cipher Only */
84                 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL)
85                         return 0;
86         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
87                         xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
88                 /* Cipher then Authentication */
89                 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL &&
90                         xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL)
91                         return 0;
92         }
93
94         return -1;
95 }
96
97 /** Process crypto operation for mbuf */
98 static int
99 process_op(const struct null_crypto_qp *qp, struct rte_crypto_op *op,
100                 struct null_crypto_session *sess __rte_unused)
101 {
102         /* set status as successful by default */
103         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
104
105         /*
106          * if crypto session and operation are valid just enqueue the packet
107          * in the processed ring
108          */
109         return rte_ring_enqueue(qp->processed_pkts, (void *)op);
110 }
111
112 static struct null_crypto_session *
113 get_session(struct null_crypto_qp *qp, struct rte_crypto_sym_op *op)
114 {
115         struct null_crypto_session *sess;
116
117         if (op->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
118                 if (unlikely(op->session == NULL ||
119                              op->session->dev_type != RTE_CRYPTODEV_NULL_PMD))
120                         return NULL;
121
122                 sess = (struct null_crypto_session *)op->session->_private;
123         } else  {
124                 struct rte_cryptodev_session *c_sess = NULL;
125
126                 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
127                         return NULL;
128
129                 sess = (struct null_crypto_session *)c_sess->_private;
130
131                 if (null_crypto_set_session_parameters(sess, op->xform) != 0)
132                         return NULL;
133         }
134
135         return sess;
136 }
137
138 /** Enqueue burst */
139 static uint16_t
140 null_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
141                 uint16_t nb_ops)
142 {
143         struct null_crypto_session *sess;
144         struct null_crypto_qp *qp = queue_pair;
145
146         int i, retval;
147
148         for (i = 0; i < nb_ops; i++) {
149                 sess = get_session(qp, ops[i]->sym);
150                 if (unlikely(sess == NULL))
151                         goto enqueue_err;
152
153                 retval = process_op(qp, ops[i], sess);
154                 if (unlikely(retval < 0))
155                         goto enqueue_err;
156         }
157
158         qp->qp_stats.enqueued_count += i;
159         return i;
160
161 enqueue_err:
162         if (ops[i])
163                 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
164
165         qp->qp_stats.enqueue_err_count++;
166         return i;
167 }
168
169 /** Dequeue burst */
170 static uint16_t
171 null_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
172                 uint16_t nb_ops)
173 {
174         struct null_crypto_qp *qp = queue_pair;
175
176         unsigned nb_dequeued;
177
178         nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
179                         (void **)ops, nb_ops);
180         qp->qp_stats.dequeued_count += nb_dequeued;
181
182         return nb_dequeued;
183 }
184
185 static int cryptodev_null_remove(const char *name);
186
187 /** Create crypto device */
188 static int
189 cryptodev_null_create(const char *name,
190                 struct rte_crypto_vdev_init_params *init_params)
191 {
192         struct rte_cryptodev *dev;
193         char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
194         struct null_crypto_private *internals;
195
196         /* create a unique device name */
197         if (create_unique_device_name(crypto_dev_name,
198                         RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
199                 NULL_CRYPTO_LOG_ERR("failed to create unique cryptodev name");
200                 return -EINVAL;
201         }
202
203         dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
204                         sizeof(struct null_crypto_private),
205                         init_params->socket_id);
206         if (dev == NULL) {
207                 NULL_CRYPTO_LOG_ERR("failed to create cryptodev vdev");
208                 goto init_error;
209         }
210
211         dev->dev_type = RTE_CRYPTODEV_NULL_PMD;
212         dev->dev_ops = null_crypto_pmd_ops;
213
214         /* register rx/tx burst functions for data path */
215         dev->dequeue_burst = null_crypto_pmd_dequeue_burst;
216         dev->enqueue_burst = null_crypto_pmd_enqueue_burst;
217
218         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
219                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING;
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", name);
230         cryptodev_null_remove(crypto_dev_name);
231
232         return -EFAULT;
233 }
234
235 /** Initialise null crypto device */
236 static int
237 cryptodev_null_probe(const char *name,
238                 const char *input_args)
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         };
245
246         rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
247
248         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
249                         init_params.socket_id);
250         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
251                         init_params.max_nb_queue_pairs);
252         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
253                         init_params.max_nb_sessions);
254
255         return cryptodev_null_create(name, &init_params);
256 }
257
258 /** Uninitialise null crypto device */
259 static int
260 cryptodev_null_remove(const char *name)
261 {
262         if (name == NULL)
263                 return -EINVAL;
264
265         RTE_LOG(INFO, PMD, "Closing null crypto device %s on numa socket %u\n",
266                         name, rte_socket_id());
267
268         return 0;
269 }
270
271 static struct rte_vdev_driver cryptodev_null_pmd_drv = {
272         .probe = cryptodev_null_probe,
273         .remove = cryptodev_null_remove
274 };
275
276 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd_drv);
277 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd);
278 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_NULL_PMD,
279         "max_nb_queue_pairs=<int> "
280         "max_nb_sessions=<int> "
281         "socket_id=<int>");