4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
35 #include <rte_cryptodev.h>
36 #include <rte_cryptodev_pmd.h>
37 #include <rte_bus_vdev.h>
38 #include <rte_malloc.h>
39 #include <rte_cpuflags.h>
41 #include "rte_snow3g_pmd_private.h"
43 #define SNOW3G_IV_LENGTH 16
44 #define SNOW3G_MAX_BURST 8
47 static uint8_t cryptodev_driver_id;
49 /** Get xform chain order. */
50 static enum snow3g_operation
51 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
54 return SNOW3G_OP_NOT_SUPPORTED;
57 if (xform->next->next != NULL)
58 return SNOW3G_OP_NOT_SUPPORTED;
60 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
61 if (xform->next == NULL)
62 return SNOW3G_OP_ONLY_AUTH;
63 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
64 return SNOW3G_OP_AUTH_CIPHER;
66 return SNOW3G_OP_NOT_SUPPORTED;
69 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
70 if (xform->next == NULL)
71 return SNOW3G_OP_ONLY_CIPHER;
72 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
73 return SNOW3G_OP_CIPHER_AUTH;
75 return SNOW3G_OP_NOT_SUPPORTED;
78 return SNOW3G_OP_NOT_SUPPORTED;
82 /** Parse crypto xform chain and set private session parameters. */
84 snow3g_set_session_parameters(struct snow3g_session *sess,
85 const struct rte_crypto_sym_xform *xform)
87 const struct rte_crypto_sym_xform *auth_xform = NULL;
88 const struct rte_crypto_sym_xform *cipher_xform = NULL;
89 enum snow3g_operation mode;
91 /* Select Crypto operation - hash then cipher / cipher then hash */
92 mode = snow3g_get_mode(xform);
95 case SNOW3G_OP_CIPHER_AUTH:
96 auth_xform = xform->next;
99 case SNOW3G_OP_ONLY_CIPHER:
100 cipher_xform = xform;
102 case SNOW3G_OP_AUTH_CIPHER:
103 cipher_xform = xform->next;
105 case SNOW3G_OP_ONLY_AUTH:
108 case SNOW3G_OP_NOT_SUPPORTED:
110 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
115 /* Only SNOW 3G UEA2 supported */
116 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
119 if (cipher_xform->cipher.iv.length != SNOW3G_IV_LENGTH) {
120 SNOW3G_LOG_ERR("Wrong IV length");
123 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
126 sso_snow3g_init_key_sched(cipher_xform->cipher.key.data,
127 &sess->pKeySched_cipher);
131 /* Only SNOW 3G UIA2 supported */
132 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
135 if (auth_xform->auth.digest_length != SNOW3G_DIGEST_LENGTH) {
136 SNOW3G_LOG_ERR("Wrong digest length");
140 sess->auth_op = auth_xform->auth.op;
142 if (auth_xform->auth.iv.length != SNOW3G_IV_LENGTH) {
143 SNOW3G_LOG_ERR("Wrong IV length");
146 sess->auth_iv_offset = auth_xform->auth.iv.offset;
149 sso_snow3g_init_key_sched(auth_xform->auth.key.data,
150 &sess->pKeySched_hash);
159 /** Get SNOW 3G session. */
160 static struct snow3g_session *
161 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
163 struct snow3g_session *sess = NULL;
165 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
166 if (likely(op->sym->session != NULL))
167 sess = (struct snow3g_session *)
168 get_session_private_data(
170 cryptodev_driver_id);
173 void *_sess_private_data = NULL;
175 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
178 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
181 sess = (struct snow3g_session *)_sess_private_data;
183 if (unlikely(snow3g_set_session_parameters(sess,
184 op->sym->xform) != 0)) {
185 rte_mempool_put(qp->sess_mp, _sess);
186 rte_mempool_put(qp->sess_mp, _sess_private_data);
189 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
190 set_session_private_data(op->sym->session, cryptodev_driver_id,
194 if (unlikely(sess == NULL))
195 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
201 /** Encrypt/decrypt mbufs with same cipher key. */
203 process_snow3g_cipher_op(struct rte_crypto_op **ops,
204 struct snow3g_session *session,
208 uint8_t processed_ops = 0;
209 uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
210 uint8_t *iv[SNOW3G_MAX_BURST];
211 uint32_t num_bytes[SNOW3G_MAX_BURST];
213 for (i = 0; i < num_ops; i++) {
214 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
215 (ops[i]->sym->cipher.data.offset >> 3);
216 dst[i] = ops[i]->sym->m_dst ?
217 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
218 (ops[i]->sym->cipher.data.offset >> 3) :
219 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
220 (ops[i]->sym->cipher.data.offset >> 3);
221 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
222 session->cipher_iv_offset);
223 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
228 sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, iv, src, dst,
229 num_bytes, processed_ops);
231 return processed_ops;
234 /** Encrypt/decrypt mbuf (bit level function). */
236 process_snow3g_cipher_op_bit(struct rte_crypto_op *op,
237 struct snow3g_session *session)
241 uint32_t length_in_bits, offset_in_bits;
243 offset_in_bits = op->sym->cipher.data.offset;
244 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
245 if (op->sym->m_dst == NULL) {
246 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
247 SNOW3G_LOG_ERR("bit-level in-place not supported\n");
250 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
251 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
252 session->cipher_iv_offset);
253 length_in_bits = op->sym->cipher.data.length;
255 sso_snow3g_f8_1_buffer_bit(&session->pKeySched_cipher, iv,
256 src, dst, length_in_bits, offset_in_bits);
261 /** Generate/verify hash from mbufs with same hash key. */
263 process_snow3g_hash_op(struct snow3g_qp *qp, struct rte_crypto_op **ops,
264 struct snow3g_session *session,
268 uint8_t processed_ops = 0;
270 uint32_t length_in_bits;
273 for (i = 0; i < num_ops; i++) {
274 /* Data must be byte aligned */
275 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
276 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
277 SNOW3G_LOG_ERR("Offset");
281 length_in_bits = ops[i]->sym->auth.data.length;
283 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
284 (ops[i]->sym->auth.data.offset >> 3);
285 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
286 session->auth_iv_offset);
288 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
289 dst = qp->temp_digest;
291 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
293 length_in_bits, dst);
295 if (memcmp(dst, ops[i]->sym->auth.digest.data,
296 SNOW3G_DIGEST_LENGTH) != 0)
297 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
299 dst = ops[i]->sym->auth.digest.data;
301 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
303 length_in_bits, dst);
308 return processed_ops;
311 /** Process a batch of crypto ops which shares the same session. */
313 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
314 struct snow3g_qp *qp, uint8_t num_ops,
315 uint16_t *accumulated_enqueued_ops)
318 unsigned enqueued_ops, processed_ops;
320 #ifdef RTE_LIBRTE_PMD_SNOW3G_DEBUG
321 for (i = 0; i < num_ops; i++) {
322 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
323 (ops[i]->sym->m_dst != NULL &&
324 !rte_pktmbuf_is_contiguous(
325 ops[i]->sym->m_dst))) {
326 SNOW3G_LOG_ERR("PMD supports only contiguous mbufs, "
327 "op (%p) provides noncontiguous mbuf as "
328 "source/destination buffer.\n", ops[i]);
329 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
335 switch (session->op) {
336 case SNOW3G_OP_ONLY_CIPHER:
337 processed_ops = process_snow3g_cipher_op(ops,
340 case SNOW3G_OP_ONLY_AUTH:
341 processed_ops = process_snow3g_hash_op(qp, ops, session,
344 case SNOW3G_OP_CIPHER_AUTH:
345 processed_ops = process_snow3g_cipher_op(ops, session,
347 process_snow3g_hash_op(qp, ops, session, processed_ops);
349 case SNOW3G_OP_AUTH_CIPHER:
350 processed_ops = process_snow3g_hash_op(qp, ops, session,
352 process_snow3g_cipher_op(ops, session, processed_ops);
355 /* Operation not supported. */
359 for (i = 0; i < num_ops; i++) {
361 * If there was no error/authentication failure,
362 * change status to successful.
364 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
365 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
366 /* Free session if a session-less crypto op. */
367 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
368 memset(session, 0, sizeof(struct snow3g_session));
369 memset(ops[i]->sym->session, 0,
370 rte_cryptodev_get_header_session_size());
371 rte_mempool_put(qp->sess_mp, session);
372 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
373 ops[i]->sym->session = NULL;
377 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
378 (void **)ops, processed_ops, NULL);
379 qp->qp_stats.enqueued_count += enqueued_ops;
380 *accumulated_enqueued_ops += enqueued_ops;
385 /** Process a crypto op with length/offset in bits. */
387 process_op_bit(struct rte_crypto_op *op, struct snow3g_session *session,
388 struct snow3g_qp *qp, uint16_t *accumulated_enqueued_ops)
390 unsigned enqueued_op, processed_op;
392 switch (session->op) {
393 case SNOW3G_OP_ONLY_CIPHER:
394 processed_op = process_snow3g_cipher_op_bit(op,
397 case SNOW3G_OP_ONLY_AUTH:
398 processed_op = process_snow3g_hash_op(qp, &op, session, 1);
400 case SNOW3G_OP_CIPHER_AUTH:
401 processed_op = process_snow3g_cipher_op_bit(op, session);
402 if (processed_op == 1)
403 process_snow3g_hash_op(qp, &op, session, 1);
405 case SNOW3G_OP_AUTH_CIPHER:
406 processed_op = process_snow3g_hash_op(qp, &op, session, 1);
407 if (processed_op == 1)
408 process_snow3g_cipher_op_bit(op, session);
411 /* Operation not supported. */
416 * If there was no error/authentication failure,
417 * change status to successful.
419 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
420 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
422 /* Free session if a session-less crypto op. */
423 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
424 memset(op->sym->session, 0, sizeof(struct snow3g_session));
425 rte_cryptodev_sym_session_free(op->sym->session);
426 op->sym->session = NULL;
429 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops,
430 (void **)&op, processed_op, NULL);
431 qp->qp_stats.enqueued_count += enqueued_op;
432 *accumulated_enqueued_ops += enqueued_op;
438 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
441 struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
442 struct rte_crypto_op *curr_c_op;
444 struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
445 struct snow3g_qp *qp = queue_pair;
447 uint8_t burst_size = 0;
448 uint16_t enqueued_ops = 0;
449 uint8_t processed_ops;
451 for (i = 0; i < nb_ops; i++) {
454 /* Set status as enqueued (not processed yet) by default. */
455 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
457 curr_sess = snow3g_get_session(qp, curr_c_op);
458 if (unlikely(curr_sess == NULL ||
459 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
461 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
465 /* If length/offset is at bit-level, process this buffer alone. */
466 if (((curr_c_op->sym->cipher.data.length % BYTE_LEN) != 0)
467 || ((curr_c_op->sym->cipher.data.offset
469 /* Process the ops of the previous session. */
470 if (prev_sess != NULL) {
471 processed_ops = process_ops(c_ops, prev_sess,
472 qp, burst_size, &enqueued_ops);
473 if (processed_ops < burst_size) {
482 processed_ops = process_op_bit(curr_c_op, curr_sess,
484 if (processed_ops != 1)
490 /* Batch ops that share the same session. */
491 if (prev_sess == NULL) {
492 prev_sess = curr_sess;
493 c_ops[burst_size++] = curr_c_op;
494 } else if (curr_sess == prev_sess) {
495 c_ops[burst_size++] = curr_c_op;
497 * When there are enough ops to process in a batch,
498 * process them, and start a new batch.
500 if (burst_size == SNOW3G_MAX_BURST) {
501 processed_ops = process_ops(c_ops, prev_sess,
502 qp, burst_size, &enqueued_ops);
503 if (processed_ops < burst_size) {
513 * Different session, process the ops
514 * of the previous session.
516 processed_ops = process_ops(c_ops, prev_sess,
517 qp, burst_size, &enqueued_ops);
518 if (processed_ops < burst_size) {
524 prev_sess = curr_sess;
526 c_ops[burst_size++] = curr_c_op;
530 if (burst_size != 0) {
531 /* Process the crypto ops of the last session. */
532 processed_ops = process_ops(c_ops, prev_sess,
533 qp, burst_size, &enqueued_ops);
536 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
541 snow3g_pmd_dequeue_burst(void *queue_pair,
542 struct rte_crypto_op **c_ops, uint16_t nb_ops)
544 struct snow3g_qp *qp = queue_pair;
546 unsigned nb_dequeued;
548 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
549 (void **)c_ops, nb_ops, NULL);
550 qp->qp_stats.dequeued_count += nb_dequeued;
555 static int cryptodev_snow3g_remove(struct rte_vdev_device *vdev);
558 cryptodev_snow3g_create(const char *name,
559 struct rte_vdev_device *vdev,
560 struct rte_cryptodev_pmd_init_params *init_params)
562 struct rte_cryptodev *dev;
563 struct snow3g_private *internals;
564 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
566 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
568 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
572 dev->driver_id = cryptodev_driver_id;
573 dev->dev_ops = rte_snow3g_pmd_ops;
575 /* Register RX/TX burst functions for data path. */
576 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
577 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
579 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
580 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
583 internals = dev->data->dev_private;
585 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
586 internals->max_nb_sessions = init_params->max_nb_sessions;
590 SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed",
593 cryptodev_snow3g_remove(vdev);
598 cryptodev_snow3g_probe(struct rte_vdev_device *vdev)
600 struct rte_cryptodev_pmd_init_params init_params = {
602 sizeof(struct snow3g_private),
604 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
605 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
608 const char *input_args;
610 name = rte_vdev_device_name(vdev);
613 input_args = rte_vdev_device_args(vdev);
615 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
617 return cryptodev_snow3g_create(name, vdev, &init_params);
621 cryptodev_snow3g_remove(struct rte_vdev_device *vdev)
623 struct rte_cryptodev *cryptodev;
626 name = rte_vdev_device_name(vdev);
630 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
631 if (cryptodev == NULL)
634 return rte_cryptodev_pmd_destroy(cryptodev);
637 static struct rte_vdev_driver cryptodev_snow3g_pmd_drv = {
638 .probe = cryptodev_snow3g_probe,
639 .remove = cryptodev_snow3g_remove
642 static struct cryptodev_driver snow3g_crypto_drv;
644 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd_drv);
645 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd);
646 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_SNOW3G_PMD,
647 "max_nb_queue_pairs=<int> "
648 "max_nb_sessions=<int> "
650 RTE_PMD_REGISTER_CRYPTO_DRIVER(snow3g_crypto_drv, cryptodev_snow3g_pmd_drv,
651 cryptodev_driver_id);