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_config.h>
35 #include <rte_hexdump.h>
36 #include <rte_cryptodev.h>
37 #include <rte_cryptodev_pmd.h>
38 #include <rte_cryptodev_vdev.h>
40 #include <rte_malloc.h>
41 #include <rte_cpuflags.h>
43 #include "rte_snow3g_pmd_private.h"
45 #define SNOW3G_IV_LENGTH 16
46 #define SNOW3G_DIGEST_LENGTH 4
47 #define SNOW3G_MAX_BURST 8
50 static uint8_t cryptodev_driver_id;
52 /** Get xform chain order. */
53 static enum snow3g_operation
54 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
57 return SNOW3G_OP_NOT_SUPPORTED;
60 if (xform->next->next != NULL)
61 return SNOW3G_OP_NOT_SUPPORTED;
63 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
64 if (xform->next == NULL)
65 return SNOW3G_OP_ONLY_AUTH;
66 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
67 return SNOW3G_OP_AUTH_CIPHER;
69 return SNOW3G_OP_NOT_SUPPORTED;
72 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
73 if (xform->next == NULL)
74 return SNOW3G_OP_ONLY_CIPHER;
75 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
76 return SNOW3G_OP_CIPHER_AUTH;
78 return SNOW3G_OP_NOT_SUPPORTED;
81 return SNOW3G_OP_NOT_SUPPORTED;
85 /** Parse crypto xform chain and set private session parameters. */
87 snow3g_set_session_parameters(struct snow3g_session *sess,
88 const struct rte_crypto_sym_xform *xform)
90 const struct rte_crypto_sym_xform *auth_xform = NULL;
91 const struct rte_crypto_sym_xform *cipher_xform = NULL;
92 enum snow3g_operation mode;
94 /* Select Crypto operation - hash then cipher / cipher then hash */
95 mode = snow3g_get_mode(xform);
98 case SNOW3G_OP_CIPHER_AUTH:
99 auth_xform = xform->next;
102 case SNOW3G_OP_ONLY_CIPHER:
103 cipher_xform = xform;
105 case SNOW3G_OP_AUTH_CIPHER:
106 cipher_xform = xform->next;
108 case SNOW3G_OP_ONLY_AUTH:
111 case SNOW3G_OP_NOT_SUPPORTED:
113 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
118 /* Only SNOW 3G UEA2 supported */
119 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
122 if (cipher_xform->cipher.iv.length != SNOW3G_IV_LENGTH) {
123 SNOW3G_LOG_ERR("Wrong IV length");
126 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
129 sso_snow3g_init_key_sched(cipher_xform->cipher.key.data,
130 &sess->pKeySched_cipher);
134 /* Only SNOW 3G UIA2 supported */
135 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
138 if (auth_xform->auth.digest_length != SNOW3G_DIGEST_LENGTH) {
139 SNOW3G_LOG_ERR("Wrong digest length");
143 sess->auth_op = auth_xform->auth.op;
145 if (auth_xform->auth.iv.length != SNOW3G_IV_LENGTH) {
146 SNOW3G_LOG_ERR("Wrong IV length");
149 sess->auth_iv_offset = auth_xform->auth.iv.offset;
152 sso_snow3g_init_key_sched(auth_xform->auth.key.data,
153 &sess->pKeySched_hash);
162 /** Get SNOW 3G session. */
163 static struct snow3g_session *
164 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
166 struct snow3g_session *sess;
168 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
169 sess = (struct snow3g_session *)op->sym->session->_private;
171 struct rte_cryptodev_sym_session *c_sess = NULL;
173 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
176 sess = (struct snow3g_session *)c_sess->_private;
178 if (unlikely(snow3g_set_session_parameters(sess,
179 op->sym->xform) != 0))
186 /** Encrypt/decrypt mbufs with same cipher key. */
188 process_snow3g_cipher_op(struct rte_crypto_op **ops,
189 struct snow3g_session *session,
193 uint8_t processed_ops = 0;
194 uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
195 uint8_t *iv[SNOW3G_MAX_BURST];
196 uint32_t num_bytes[SNOW3G_MAX_BURST];
198 for (i = 0; i < num_ops; i++) {
199 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
200 (ops[i]->sym->cipher.data.offset >> 3);
201 dst[i] = ops[i]->sym->m_dst ?
202 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
203 (ops[i]->sym->cipher.data.offset >> 3) :
204 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
205 (ops[i]->sym->cipher.data.offset >> 3);
206 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
207 session->cipher_iv_offset);
208 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
213 sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, iv, src, dst,
214 num_bytes, processed_ops);
216 return processed_ops;
219 /** Encrypt/decrypt mbuf (bit level function). */
221 process_snow3g_cipher_op_bit(struct rte_crypto_op *op,
222 struct snow3g_session *session)
226 uint32_t length_in_bits, offset_in_bits;
228 offset_in_bits = op->sym->cipher.data.offset;
229 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
230 if (op->sym->m_dst == NULL) {
231 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
232 SNOW3G_LOG_ERR("bit-level in-place not supported\n");
235 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
236 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
237 session->cipher_iv_offset);
238 length_in_bits = op->sym->cipher.data.length;
240 sso_snow3g_f8_1_buffer_bit(&session->pKeySched_cipher, iv,
241 src, dst, length_in_bits, offset_in_bits);
246 /** Generate/verify hash from mbufs with same hash key. */
248 process_snow3g_hash_op(struct rte_crypto_op **ops,
249 struct snow3g_session *session,
253 uint8_t processed_ops = 0;
255 uint32_t length_in_bits;
258 for (i = 0; i < num_ops; i++) {
259 /* Data must be byte aligned */
260 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
261 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
262 SNOW3G_LOG_ERR("Offset");
266 length_in_bits = ops[i]->sym->auth.data.length;
268 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
269 (ops[i]->sym->auth.data.offset >> 3);
270 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
271 session->auth_iv_offset);
273 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
274 dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
275 SNOW3G_DIGEST_LENGTH);
277 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
279 length_in_bits, dst);
281 if (memcmp(dst, ops[i]->sym->auth.digest.data,
282 SNOW3G_DIGEST_LENGTH) != 0)
283 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
285 /* Trim area used for digest from mbuf. */
286 rte_pktmbuf_trim(ops[i]->sym->m_src,
287 SNOW3G_DIGEST_LENGTH);
289 dst = ops[i]->sym->auth.digest.data;
291 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
293 length_in_bits, dst);
298 return processed_ops;
301 /** Process a batch of crypto ops which shares the same session. */
303 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
304 struct snow3g_qp *qp, uint8_t num_ops,
305 uint16_t *accumulated_enqueued_ops)
308 unsigned enqueued_ops, processed_ops;
310 #ifdef RTE_LIBRTE_PMD_SNOW3G_DEBUG
311 for (i = 0; i < num_ops; i++) {
312 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
313 (ops[i]->sym->m_dst != NULL &&
314 !rte_pktmbuf_is_contiguous(
315 ops[i]->sym->m_dst))) {
316 SNOW3G_LOG_ERR("PMD supports only contiguous mbufs, "
317 "op (%p) provides noncontiguous mbuf as "
318 "source/destination buffer.\n", ops[i]);
319 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
325 switch (session->op) {
326 case SNOW3G_OP_ONLY_CIPHER:
327 processed_ops = process_snow3g_cipher_op(ops,
330 case SNOW3G_OP_ONLY_AUTH:
331 processed_ops = process_snow3g_hash_op(ops, session,
334 case SNOW3G_OP_CIPHER_AUTH:
335 processed_ops = process_snow3g_cipher_op(ops, session,
337 process_snow3g_hash_op(ops, session, processed_ops);
339 case SNOW3G_OP_AUTH_CIPHER:
340 processed_ops = process_snow3g_hash_op(ops, session,
342 process_snow3g_cipher_op(ops, session, processed_ops);
345 /* Operation not supported. */
349 for (i = 0; i < num_ops; i++) {
351 * If there was no error/authentication failure,
352 * change status to successful.
354 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
355 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
356 /* Free session if a session-less crypto op. */
357 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
358 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
359 ops[i]->sym->session = NULL;
363 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
364 (void **)ops, processed_ops, NULL);
365 qp->qp_stats.enqueued_count += enqueued_ops;
366 *accumulated_enqueued_ops += enqueued_ops;
371 /** Process a crypto op with length/offset in bits. */
373 process_op_bit(struct rte_crypto_op *op, struct snow3g_session *session,
374 struct snow3g_qp *qp, uint16_t *accumulated_enqueued_ops)
376 unsigned enqueued_op, processed_op;
378 switch (session->op) {
379 case SNOW3G_OP_ONLY_CIPHER:
380 processed_op = process_snow3g_cipher_op_bit(op,
383 case SNOW3G_OP_ONLY_AUTH:
384 processed_op = process_snow3g_hash_op(&op, session, 1);
386 case SNOW3G_OP_CIPHER_AUTH:
387 processed_op = process_snow3g_cipher_op_bit(op, session);
388 if (processed_op == 1)
389 process_snow3g_hash_op(&op, session, 1);
391 case SNOW3G_OP_AUTH_CIPHER:
392 processed_op = process_snow3g_hash_op(&op, session, 1);
393 if (processed_op == 1)
394 process_snow3g_cipher_op_bit(op, session);
397 /* Operation not supported. */
402 * If there was no error/authentication failure,
403 * change status to successful.
405 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
406 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
408 /* Free session if a session-less crypto op. */
409 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
410 rte_mempool_put(qp->sess_mp, op->sym->session);
411 op->sym->session = NULL;
414 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops,
415 (void **)&op, processed_op, NULL);
416 qp->qp_stats.enqueued_count += enqueued_op;
417 *accumulated_enqueued_ops += enqueued_op;
423 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
426 struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
427 struct rte_crypto_op *curr_c_op;
429 struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
430 struct snow3g_qp *qp = queue_pair;
432 uint8_t burst_size = 0;
433 uint16_t enqueued_ops = 0;
434 uint8_t processed_ops;
436 for (i = 0; i < nb_ops; i++) {
439 /* Set status as enqueued (not processed yet) by default. */
440 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
442 curr_sess = snow3g_get_session(qp, curr_c_op);
443 if (unlikely(curr_sess == NULL ||
444 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
446 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
450 /* If length/offset is at bit-level, process this buffer alone. */
451 if (((curr_c_op->sym->cipher.data.length % BYTE_LEN) != 0)
452 || ((curr_c_op->sym->cipher.data.offset
454 /* Process the ops of the previous session. */
455 if (prev_sess != NULL) {
456 processed_ops = process_ops(c_ops, prev_sess,
457 qp, burst_size, &enqueued_ops);
458 if (processed_ops < burst_size) {
467 processed_ops = process_op_bit(curr_c_op, curr_sess,
469 if (processed_ops != 1)
475 /* Batch ops that share the same session. */
476 if (prev_sess == NULL) {
477 prev_sess = curr_sess;
478 c_ops[burst_size++] = curr_c_op;
479 } else if (curr_sess == prev_sess) {
480 c_ops[burst_size++] = curr_c_op;
482 * When there are enough ops to process in a batch,
483 * process them, and start a new batch.
485 if (burst_size == SNOW3G_MAX_BURST) {
486 processed_ops = process_ops(c_ops, prev_sess,
487 qp, burst_size, &enqueued_ops);
488 if (processed_ops < burst_size) {
498 * Different session, process the ops
499 * of the previous session.
501 processed_ops = process_ops(c_ops, prev_sess,
502 qp, burst_size, &enqueued_ops);
503 if (processed_ops < burst_size) {
509 prev_sess = curr_sess;
511 c_ops[burst_size++] = curr_c_op;
515 if (burst_size != 0) {
516 /* Process the crypto ops of the last session. */
517 processed_ops = process_ops(c_ops, prev_sess,
518 qp, burst_size, &enqueued_ops);
521 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
526 snow3g_pmd_dequeue_burst(void *queue_pair,
527 struct rte_crypto_op **c_ops, uint16_t nb_ops)
529 struct snow3g_qp *qp = queue_pair;
531 unsigned nb_dequeued;
533 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
534 (void **)c_ops, nb_ops, NULL);
535 qp->qp_stats.dequeued_count += nb_dequeued;
540 static int cryptodev_snow3g_remove(struct rte_vdev_device *vdev);
543 cryptodev_snow3g_create(const char *name,
544 struct rte_vdev_device *vdev,
545 struct rte_crypto_vdev_init_params *init_params)
547 struct rte_cryptodev *dev;
548 struct snow3g_private *internals;
549 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
551 if (init_params->name[0] == '\0')
552 snprintf(init_params->name, sizeof(init_params->name),
555 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
556 sizeof(struct snow3g_private), init_params->socket_id,
559 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
563 dev->driver_id = cryptodev_driver_id;
564 dev->dev_ops = rte_snow3g_pmd_ops;
566 /* Register RX/TX burst functions for data path. */
567 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
568 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
570 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
571 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
574 internals = dev->data->dev_private;
576 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
577 internals->max_nb_sessions = init_params->max_nb_sessions;
581 SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed",
584 cryptodev_snow3g_remove(vdev);
589 cryptodev_snow3g_probe(struct rte_vdev_device *vdev)
591 struct rte_crypto_vdev_init_params init_params = {
592 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
593 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
598 const char *input_args;
600 name = rte_vdev_device_name(vdev);
603 input_args = rte_vdev_device_args(vdev);
605 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
607 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
608 init_params.socket_id);
609 if (init_params.name[0] != '\0')
610 RTE_LOG(INFO, PMD, " User defined name = %s\n",
612 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
613 init_params.max_nb_queue_pairs);
614 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
615 init_params.max_nb_sessions);
617 return cryptodev_snow3g_create(name, vdev, &init_params);
621 cryptodev_snow3g_remove(struct rte_vdev_device *vdev)
625 name = rte_vdev_device_name(vdev);
629 RTE_LOG(INFO, PMD, "Closing SNOW 3G crypto device %s"
630 " on numa socket %u\n",
631 name, rte_socket_id());
636 static struct rte_vdev_driver cryptodev_snow3g_pmd_drv = {
637 .probe = cryptodev_snow3g_probe,
638 .remove = cryptodev_snow3g_remove
641 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd_drv);
642 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd);
643 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_SNOW3G_PMD,
644 "max_nb_queue_pairs=<int> "
645 "max_nb_sessions=<int> "
647 RTE_PMD_REGISTER_CRYPTO_DRIVER(cryptodev_snow3g_pmd_drv, cryptodev_driver_id);