cryptodev: move vdev functions to a separate file
[dpdk.git] / drivers / crypto / aesni_gcm / aesni_gcm_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_hexdump.h>
36 #include <rte_cryptodev.h>
37 #include <rte_cryptodev_pmd.h>
38 #include <rte_cryptodev_vdev.h>
39 #include <rte_vdev.h>
40 #include <rte_malloc.h>
41 #include <rte_cpuflags.h>
42 #include <rte_byteorder.h>
43
44 #include "aesni_gcm_pmd_private.h"
45
46 /** GCM encode functions pointer table */
47 static const struct aesni_gcm_ops aesni_gcm_enc[] = {
48                 [AESNI_GCM_KEY_128] = {
49                                 aesni_gcm128_init,
50                                 aesni_gcm128_enc_update,
51                                 aesni_gcm128_enc_finalize
52                 },
53                 [AESNI_GCM_KEY_256] = {
54                                 aesni_gcm256_init,
55                                 aesni_gcm256_enc_update,
56                                 aesni_gcm256_enc_finalize
57                 }
58 };
59
60 /** GCM decode functions pointer table */
61 static const struct aesni_gcm_ops aesni_gcm_dec[] = {
62                 [AESNI_GCM_KEY_128] = {
63                                 aesni_gcm128_init,
64                                 aesni_gcm128_dec_update,
65                                 aesni_gcm128_dec_finalize
66                 },
67                 [AESNI_GCM_KEY_256] = {
68                                 aesni_gcm256_init,
69                                 aesni_gcm256_dec_update,
70                                 aesni_gcm256_dec_finalize
71                 }
72 };
73
74 /** Parse crypto xform chain and set private session parameters */
75 int
76 aesni_gcm_set_session_parameters(struct aesni_gcm_session *sess,
77                 const struct rte_crypto_sym_xform *xform)
78 {
79         const struct rte_crypto_sym_xform *auth_xform;
80         const struct rte_crypto_sym_xform *cipher_xform;
81
82         if (xform->next == NULL || xform->next->next != NULL) {
83                 GCM_LOG_ERR("Two and only two chained xform required");
84                 return -EINVAL;
85         }
86
87         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
88                         xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
89                 auth_xform = xform->next;
90                 cipher_xform = xform;
91         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
92                         xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
93                 auth_xform = xform;
94                 cipher_xform = xform->next;
95         } else {
96                 GCM_LOG_ERR("Cipher and auth xform required");
97                 return -EINVAL;
98         }
99
100         if (!(cipher_xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_GCM &&
101                 (auth_xform->auth.algo == RTE_CRYPTO_AUTH_AES_GCM ||
102                         auth_xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC))) {
103                 GCM_LOG_ERR("We only support AES GCM and AES GMAC");
104                 return -EINVAL;
105         }
106
107         /* Select Crypto operation */
108         if (cipher_xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
109                         auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
110                 sess->op = AESNI_GCM_OP_AUTHENTICATED_ENCRYPTION;
111         else if (cipher_xform->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT &&
112                         auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
113                 sess->op = AESNI_GCM_OP_AUTHENTICATED_DECRYPTION;
114         else {
115                 GCM_LOG_ERR("Cipher/Auth operations: Encrypt/Generate or"
116                                 " Decrypt/Verify are valid only");
117                 return -EINVAL;
118         }
119
120         /* Check key length and calculate GCM pre-compute. */
121         switch (cipher_xform->cipher.key.length) {
122         case 16:
123                 aesni_gcm128_pre(cipher_xform->cipher.key.data, &sess->gdata);
124                 sess->key = AESNI_GCM_KEY_128;
125
126                 break;
127         case 32:
128                 aesni_gcm256_pre(cipher_xform->cipher.key.data, &sess->gdata);
129                 sess->key = AESNI_GCM_KEY_256;
130
131                 break;
132         default:
133                 GCM_LOG_ERR("Unsupported cipher key length");
134                 return -EINVAL;
135         }
136
137         return 0;
138 }
139
140 /** Get gcm session */
141 static struct aesni_gcm_session *
142 aesni_gcm_get_session(struct aesni_gcm_qp *qp, struct rte_crypto_sym_op *op)
143 {
144         struct aesni_gcm_session *sess = NULL;
145
146         if (op->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
147                 if (unlikely(op->session->dev_type
148                                         != RTE_CRYPTODEV_AESNI_GCM_PMD))
149                         return sess;
150
151                 sess = (struct aesni_gcm_session *)op->session->_private;
152         } else  {
153                 void *_sess;
154
155                 if (rte_mempool_get(qp->sess_mp, &_sess))
156                         return sess;
157
158                 sess = (struct aesni_gcm_session *)
159                         ((struct rte_cryptodev_sym_session *)_sess)->_private;
160
161                 if (unlikely(aesni_gcm_set_session_parameters(sess,
162                                 op->xform) != 0)) {
163                         rte_mempool_put(qp->sess_mp, _sess);
164                         sess = NULL;
165                 }
166         }
167         return sess;
168 }
169
170 /**
171  * Process a crypto operation and complete a JOB_AES_HMAC job structure for
172  * submission to the multi buffer library for processing.
173  *
174  * @param       qp              queue pair
175  * @param       op              symmetric crypto operation
176  * @param       session         GCM session
177  *
178  * @return
179  *
180  */
181 static int
182 process_gcm_crypto_op(struct rte_crypto_sym_op *op,
183                 struct aesni_gcm_session *session)
184 {
185         uint8_t *src, *dst;
186         struct rte_mbuf *m_src = op->m_src;
187         uint32_t offset = op->cipher.data.offset;
188         uint32_t part_len, total_len, data_len;
189
190         RTE_ASSERT(m_src != NULL);
191
192         while (offset >= m_src->data_len) {
193                 offset -= m_src->data_len;
194                 m_src = m_src->next;
195
196                 RTE_ASSERT(m_src != NULL);
197         }
198
199         data_len = m_src->data_len - offset;
200         part_len = (data_len < op->cipher.data.length) ? data_len :
201                         op->cipher.data.length;
202
203         /* Destination buffer is required when segmented source buffer */
204         RTE_ASSERT((part_len == op->cipher.data.length) ||
205                         ((part_len != op->cipher.data.length) &&
206                                         (op->m_dst != NULL)));
207         /* Segmented destination buffer is not supported */
208         RTE_ASSERT((op->m_dst == NULL) ||
209                         ((op->m_dst != NULL) &&
210                                         rte_pktmbuf_is_contiguous(op->m_dst)));
211
212
213         dst = op->m_dst ?
214                         rte_pktmbuf_mtod_offset(op->m_dst, uint8_t *,
215                                         op->cipher.data.offset) :
216                         rte_pktmbuf_mtod_offset(op->m_src, uint8_t *,
217                                         op->cipher.data.offset);
218
219         src = rte_pktmbuf_mtod_offset(m_src, uint8_t *, offset);
220
221         /* sanity checks */
222         if (op->cipher.iv.length != 16 && op->cipher.iv.length != 12 &&
223                         op->cipher.iv.length != 0) {
224                 GCM_LOG_ERR("iv");
225                 return -1;
226         }
227
228         /*
229          * GCM working in 12B IV mode => 16B pre-counter block we need
230          * to set BE LSB to 1, driver expects that 16B is allocated
231          */
232         if (op->cipher.iv.length == 12) {
233                 uint32_t *iv_padd = (uint32_t *)&op->cipher.iv.data[12];
234                 *iv_padd = rte_bswap32(1);
235         }
236
237         if (op->auth.digest.length != 16 &&
238                         op->auth.digest.length != 12 &&
239                         op->auth.digest.length != 8) {
240                 GCM_LOG_ERR("digest");
241                 return -1;
242         }
243
244         if (session->op == AESNI_GCM_OP_AUTHENTICATED_ENCRYPTION) {
245
246                 aesni_gcm_enc[session->key].init(&session->gdata,
247                                 op->cipher.iv.data,
248                                 op->auth.aad.data,
249                                 (uint64_t)op->auth.aad.length);
250
251                 aesni_gcm_enc[session->key].update(&session->gdata, dst, src,
252                                 (uint64_t)part_len);
253                 total_len = op->cipher.data.length - part_len;
254
255                 while (total_len) {
256                         dst += part_len;
257                         m_src = m_src->next;
258
259                         RTE_ASSERT(m_src != NULL);
260
261                         src = rte_pktmbuf_mtod(m_src, uint8_t *);
262                         part_len = (m_src->data_len < total_len) ?
263                                         m_src->data_len : total_len;
264
265                         aesni_gcm_enc[session->key].update(&session->gdata,
266                                         dst, src,
267                                         (uint64_t)part_len);
268                         total_len -= part_len;
269                 }
270
271                 aesni_gcm_enc[session->key].finalize(&session->gdata,
272                                 op->auth.digest.data,
273                                 (uint64_t)op->auth.digest.length);
274         } else { /* session->op == AESNI_GCM_OP_AUTHENTICATED_DECRYPTION */
275                 uint8_t *auth_tag = (uint8_t *)rte_pktmbuf_append(op->m_dst ?
276                                 op->m_dst : op->m_src,
277                                 op->auth.digest.length);
278
279                 if (!auth_tag) {
280                         GCM_LOG_ERR("auth_tag");
281                         return -1;
282                 }
283
284                 aesni_gcm_dec[session->key].init(&session->gdata,
285                                 op->cipher.iv.data,
286                                 op->auth.aad.data,
287                                 (uint64_t)op->auth.aad.length);
288
289                 aesni_gcm_dec[session->key].update(&session->gdata, dst, src,
290                                 (uint64_t)part_len);
291                 total_len = op->cipher.data.length - part_len;
292
293                 while (total_len) {
294                         dst += part_len;
295                         m_src = m_src->next;
296
297                         RTE_ASSERT(m_src != NULL);
298
299                         src = rte_pktmbuf_mtod(m_src, uint8_t *);
300                         part_len = (m_src->data_len < total_len) ?
301                                         m_src->data_len : total_len;
302
303                         aesni_gcm_dec[session->key].update(&session->gdata,
304                                         dst, src,
305                                         (uint64_t)part_len);
306                         total_len -= part_len;
307                 }
308
309                 aesni_gcm_dec[session->key].finalize(&session->gdata,
310                                 auth_tag,
311                                 (uint64_t)op->auth.digest.length);
312         }
313
314         return 0;
315 }
316
317 /**
318  * Process a completed job and return rte_mbuf which job processed
319  *
320  * @param job   JOB_AES_HMAC job to process
321  *
322  * @return
323  * - Returns processed mbuf which is trimmed of output digest used in
324  * verification of supplied digest in the case of a HASH_CIPHER operation
325  * - Returns NULL on invalid job
326  */
327 static void
328 post_process_gcm_crypto_op(struct rte_crypto_op *op)
329 {
330         struct rte_mbuf *m = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
331
332         struct aesni_gcm_session *session =
333                 (struct aesni_gcm_session *)op->sym->session->_private;
334
335         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
336
337         /* Verify digest if required */
338         if (session->op == AESNI_GCM_OP_AUTHENTICATED_DECRYPTION) {
339
340                 uint8_t *tag = rte_pktmbuf_mtod_offset(m, uint8_t *,
341                                 m->data_len - op->sym->auth.digest.length);
342
343 #ifdef RTE_LIBRTE_PMD_AESNI_GCM_DEBUG
344                 rte_hexdump(stdout, "auth tag (orig):",
345                                 op->sym->auth.digest.data, op->sym->auth.digest.length);
346                 rte_hexdump(stdout, "auth tag (calc):",
347                                 tag, op->sym->auth.digest.length);
348 #endif
349
350                 if (memcmp(tag, op->sym->auth.digest.data,
351                                 op->sym->auth.digest.length) != 0)
352                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
353
354                 /* trim area used for digest from mbuf */
355                 rte_pktmbuf_trim(m, op->sym->auth.digest.length);
356         }
357 }
358
359 /**
360  * Process a completed GCM request
361  *
362  * @param qp            Queue Pair to process
363  * @param job           JOB_AES_HMAC job
364  *
365  * @return
366  * - Number of processed jobs
367  */
368 static void
369 handle_completed_gcm_crypto_op(struct aesni_gcm_qp *qp,
370                 struct rte_crypto_op *op)
371 {
372         post_process_gcm_crypto_op(op);
373
374         /* Free session if a session-less crypto op */
375         if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
376                 rte_mempool_put(qp->sess_mp, op->sym->session);
377                 op->sym->session = NULL;
378         }
379 }
380
381 static uint16_t
382 aesni_gcm_pmd_dequeue_burst(void *queue_pair,
383                 struct rte_crypto_op **ops, uint16_t nb_ops)
384 {
385         struct aesni_gcm_session *sess;
386         struct aesni_gcm_qp *qp = queue_pair;
387
388         int retval = 0;
389         unsigned int i, nb_dequeued;
390
391         nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
392                         (void **)ops, nb_ops, NULL);
393
394         for (i = 0; i < nb_dequeued; i++) {
395
396                 sess = aesni_gcm_get_session(qp, ops[i]->sym);
397                 if (unlikely(sess == NULL)) {
398                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
399                         qp->qp_stats.dequeue_err_count++;
400                         break;
401                 }
402
403                 retval = process_gcm_crypto_op(ops[i]->sym, sess);
404                 if (retval < 0) {
405                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
406                         qp->qp_stats.dequeue_err_count++;
407                         break;
408                 }
409
410                 handle_completed_gcm_crypto_op(qp, ops[i]);
411         }
412
413         qp->qp_stats.dequeued_count += i;
414
415         return i;
416 }
417
418 static uint16_t
419 aesni_gcm_pmd_enqueue_burst(void *queue_pair,
420                 struct rte_crypto_op **ops, uint16_t nb_ops)
421 {
422         struct aesni_gcm_qp *qp = queue_pair;
423
424         unsigned int nb_enqueued;
425
426         nb_enqueued = rte_ring_enqueue_burst(qp->processed_pkts,
427                         (void **)ops, nb_ops, NULL);
428         qp->qp_stats.enqueued_count += nb_enqueued;
429
430         return nb_enqueued;
431 }
432
433 static int aesni_gcm_remove(struct rte_vdev_device *vdev);
434
435 static int
436 aesni_gcm_create(const char *name,
437                 struct rte_vdev_device *vdev,
438                 struct rte_crypto_vdev_init_params *init_params)
439 {
440         struct rte_cryptodev *dev;
441         struct aesni_gcm_private *internals;
442
443         if (init_params->name[0] == '\0')
444                 snprintf(init_params->name, sizeof(init_params->name),
445                                 "%s", name);
446
447         /* Check CPU for support for AES instruction set */
448         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
449                 GCM_LOG_ERR("AES instructions not supported by CPU");
450                 return -EFAULT;
451         }
452
453         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
454                         sizeof(struct aesni_gcm_private), init_params->socket_id,
455                         vdev);
456         if (dev == NULL) {
457                 GCM_LOG_ERR("failed to create cryptodev vdev");
458                 goto init_error;
459         }
460
461         dev->dev_type = RTE_CRYPTODEV_AESNI_GCM_PMD;
462         dev->dev_ops = rte_aesni_gcm_pmd_ops;
463
464         /* register rx/tx burst functions for data path */
465         dev->dequeue_burst = aesni_gcm_pmd_dequeue_burst;
466         dev->enqueue_burst = aesni_gcm_pmd_enqueue_burst;
467
468         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
469                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
470                         RTE_CRYPTODEV_FF_CPU_AESNI |
471                         RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
472
473         internals = dev->data->dev_private;
474
475         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
476         internals->max_nb_sessions = init_params->max_nb_sessions;
477
478         return 0;
479
480 init_error:
481         GCM_LOG_ERR("driver %s: create failed", init_params->name);
482
483         aesni_gcm_remove(vdev);
484         return -EFAULT;
485 }
486
487 static int
488 aesni_gcm_probe(struct rte_vdev_device *vdev)
489 {
490         struct rte_crypto_vdev_init_params init_params = {
491                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
492                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
493                 rte_socket_id(),
494                 {0}
495         };
496         const char *name;
497         const char *input_args;
498
499         name = rte_vdev_device_name(vdev);
500         if (name == NULL)
501                 return -EINVAL;
502         input_args = rte_vdev_device_args(vdev);
503         rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
504
505         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
506                         init_params.socket_id);
507         if (init_params.name[0] != '\0')
508                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
509                         init_params.name);
510         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
511                         init_params.max_nb_queue_pairs);
512         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
513                         init_params.max_nb_sessions);
514
515         return aesni_gcm_create(name, vdev, &init_params);
516 }
517
518 static int
519 aesni_gcm_remove(struct rte_vdev_device *vdev)
520 {
521         const char *name;
522
523         name = rte_vdev_device_name(vdev);
524         if (name == NULL)
525                 return -EINVAL;
526
527         GCM_LOG_INFO("Closing AESNI crypto device %s on numa socket %u\n",
528                         name, rte_socket_id());
529
530         return 0;
531 }
532
533 static struct rte_vdev_driver aesni_gcm_pmd_drv = {
534         .probe = aesni_gcm_probe,
535         .remove = aesni_gcm_remove
536 };
537
538 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_GCM_PMD, aesni_gcm_pmd_drv);
539 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_GCM_PMD, cryptodev_aesni_gcm_pmd);
540 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_GCM_PMD,
541         "max_nb_queue_pairs=<int> "
542         "max_nb_sessions=<int> "
543         "socket_id=<int>");