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 = NULL;
168 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
169 if (likely(op->sym->session != NULL))
170 sess = (struct snow3g_session *)
171 get_session_private_data(
173 cryptodev_driver_id);
176 void *_sess_private_data = NULL;
178 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
181 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
184 sess = (struct snow3g_session *)_sess_private_data;
186 if (unlikely(snow3g_set_session_parameters(sess,
187 op->sym->xform) != 0)) {
188 rte_mempool_put(qp->sess_mp, _sess);
189 rte_mempool_put(qp->sess_mp, _sess_private_data);
192 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
193 set_session_private_data(op->sym->session, cryptodev_driver_id,
197 if (unlikely(sess == NULL))
198 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
204 /** Encrypt/decrypt mbufs with same cipher key. */
206 process_snow3g_cipher_op(struct rte_crypto_op **ops,
207 struct snow3g_session *session,
211 uint8_t processed_ops = 0;
212 uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
213 uint8_t *iv[SNOW3G_MAX_BURST];
214 uint32_t num_bytes[SNOW3G_MAX_BURST];
216 for (i = 0; i < num_ops; i++) {
217 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
218 (ops[i]->sym->cipher.data.offset >> 3);
219 dst[i] = ops[i]->sym->m_dst ?
220 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
221 (ops[i]->sym->cipher.data.offset >> 3) :
222 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
223 (ops[i]->sym->cipher.data.offset >> 3);
224 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
225 session->cipher_iv_offset);
226 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
231 sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, iv, src, dst,
232 num_bytes, processed_ops);
234 return processed_ops;
237 /** Encrypt/decrypt mbuf (bit level function). */
239 process_snow3g_cipher_op_bit(struct rte_crypto_op *op,
240 struct snow3g_session *session)
244 uint32_t length_in_bits, offset_in_bits;
246 offset_in_bits = op->sym->cipher.data.offset;
247 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
248 if (op->sym->m_dst == NULL) {
249 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
250 SNOW3G_LOG_ERR("bit-level in-place not supported\n");
253 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
254 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
255 session->cipher_iv_offset);
256 length_in_bits = op->sym->cipher.data.length;
258 sso_snow3g_f8_1_buffer_bit(&session->pKeySched_cipher, iv,
259 src, dst, length_in_bits, offset_in_bits);
264 /** Generate/verify hash from mbufs with same hash key. */
266 process_snow3g_hash_op(struct rte_crypto_op **ops,
267 struct snow3g_session *session,
271 uint8_t processed_ops = 0;
273 uint32_t length_in_bits;
276 for (i = 0; i < num_ops; i++) {
277 /* Data must be byte aligned */
278 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
279 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
280 SNOW3G_LOG_ERR("Offset");
284 length_in_bits = ops[i]->sym->auth.data.length;
286 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
287 (ops[i]->sym->auth.data.offset >> 3);
288 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
289 session->auth_iv_offset);
291 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
292 dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
293 SNOW3G_DIGEST_LENGTH);
295 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
297 length_in_bits, dst);
299 if (memcmp(dst, ops[i]->sym->auth.digest.data,
300 SNOW3G_DIGEST_LENGTH) != 0)
301 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
303 /* Trim area used for digest from mbuf. */
304 rte_pktmbuf_trim(ops[i]->sym->m_src,
305 SNOW3G_DIGEST_LENGTH);
307 dst = ops[i]->sym->auth.digest.data;
309 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
311 length_in_bits, dst);
316 return processed_ops;
319 /** Process a batch of crypto ops which shares the same session. */
321 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
322 struct snow3g_qp *qp, uint8_t num_ops,
323 uint16_t *accumulated_enqueued_ops)
326 unsigned enqueued_ops, processed_ops;
328 #ifdef RTE_LIBRTE_PMD_SNOW3G_DEBUG
329 for (i = 0; i < num_ops; i++) {
330 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
331 (ops[i]->sym->m_dst != NULL &&
332 !rte_pktmbuf_is_contiguous(
333 ops[i]->sym->m_dst))) {
334 SNOW3G_LOG_ERR("PMD supports only contiguous mbufs, "
335 "op (%p) provides noncontiguous mbuf as "
336 "source/destination buffer.\n", ops[i]);
337 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
343 switch (session->op) {
344 case SNOW3G_OP_ONLY_CIPHER:
345 processed_ops = process_snow3g_cipher_op(ops,
348 case SNOW3G_OP_ONLY_AUTH:
349 processed_ops = process_snow3g_hash_op(ops, session,
352 case SNOW3G_OP_CIPHER_AUTH:
353 processed_ops = process_snow3g_cipher_op(ops, session,
355 process_snow3g_hash_op(ops, session, processed_ops);
357 case SNOW3G_OP_AUTH_CIPHER:
358 processed_ops = process_snow3g_hash_op(ops, session,
360 process_snow3g_cipher_op(ops, session, processed_ops);
363 /* Operation not supported. */
367 for (i = 0; i < num_ops; i++) {
369 * If there was no error/authentication failure,
370 * change status to successful.
372 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
373 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
374 /* Free session if a session-less crypto op. */
375 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
376 memset(session, 0, sizeof(struct snow3g_session));
377 memset(ops[i]->sym->session, 0,
378 rte_cryptodev_get_header_session_size());
379 rte_mempool_put(qp->sess_mp, session);
380 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
381 ops[i]->sym->session = NULL;
385 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
386 (void **)ops, processed_ops, NULL);
387 qp->qp_stats.enqueued_count += enqueued_ops;
388 *accumulated_enqueued_ops += enqueued_ops;
393 /** Process a crypto op with length/offset in bits. */
395 process_op_bit(struct rte_crypto_op *op, struct snow3g_session *session,
396 struct snow3g_qp *qp, uint16_t *accumulated_enqueued_ops)
398 unsigned enqueued_op, processed_op;
400 switch (session->op) {
401 case SNOW3G_OP_ONLY_CIPHER:
402 processed_op = process_snow3g_cipher_op_bit(op,
405 case SNOW3G_OP_ONLY_AUTH:
406 processed_op = process_snow3g_hash_op(&op, session, 1);
408 case SNOW3G_OP_CIPHER_AUTH:
409 processed_op = process_snow3g_cipher_op_bit(op, session);
410 if (processed_op == 1)
411 process_snow3g_hash_op(&op, session, 1);
413 case SNOW3G_OP_AUTH_CIPHER:
414 processed_op = process_snow3g_hash_op(&op, session, 1);
415 if (processed_op == 1)
416 process_snow3g_cipher_op_bit(op, session);
419 /* Operation not supported. */
424 * If there was no error/authentication failure,
425 * change status to successful.
427 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
428 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
430 /* Free session if a session-less crypto op. */
431 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
432 memset(op->sym->session, 0, sizeof(struct snow3g_session));
433 rte_cryptodev_sym_session_free(op->sym->session);
434 op->sym->session = NULL;
437 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops,
438 (void **)&op, processed_op, NULL);
439 qp->qp_stats.enqueued_count += enqueued_op;
440 *accumulated_enqueued_ops += enqueued_op;
446 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
449 struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
450 struct rte_crypto_op *curr_c_op;
452 struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
453 struct snow3g_qp *qp = queue_pair;
455 uint8_t burst_size = 0;
456 uint16_t enqueued_ops = 0;
457 uint8_t processed_ops;
459 for (i = 0; i < nb_ops; i++) {
462 /* Set status as enqueued (not processed yet) by default. */
463 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
465 curr_sess = snow3g_get_session(qp, curr_c_op);
466 if (unlikely(curr_sess == NULL ||
467 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
469 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
473 /* If length/offset is at bit-level, process this buffer alone. */
474 if (((curr_c_op->sym->cipher.data.length % BYTE_LEN) != 0)
475 || ((curr_c_op->sym->cipher.data.offset
477 /* Process the ops of the previous session. */
478 if (prev_sess != NULL) {
479 processed_ops = process_ops(c_ops, prev_sess,
480 qp, burst_size, &enqueued_ops);
481 if (processed_ops < burst_size) {
490 processed_ops = process_op_bit(curr_c_op, curr_sess,
492 if (processed_ops != 1)
498 /* Batch ops that share the same session. */
499 if (prev_sess == NULL) {
500 prev_sess = curr_sess;
501 c_ops[burst_size++] = curr_c_op;
502 } else if (curr_sess == prev_sess) {
503 c_ops[burst_size++] = curr_c_op;
505 * When there are enough ops to process in a batch,
506 * process them, and start a new batch.
508 if (burst_size == SNOW3G_MAX_BURST) {
509 processed_ops = process_ops(c_ops, prev_sess,
510 qp, burst_size, &enqueued_ops);
511 if (processed_ops < burst_size) {
521 * Different session, process the ops
522 * of the previous session.
524 processed_ops = process_ops(c_ops, prev_sess,
525 qp, burst_size, &enqueued_ops);
526 if (processed_ops < burst_size) {
532 prev_sess = curr_sess;
534 c_ops[burst_size++] = curr_c_op;
538 if (burst_size != 0) {
539 /* Process the crypto ops of the last session. */
540 processed_ops = process_ops(c_ops, prev_sess,
541 qp, burst_size, &enqueued_ops);
544 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
549 snow3g_pmd_dequeue_burst(void *queue_pair,
550 struct rte_crypto_op **c_ops, uint16_t nb_ops)
552 struct snow3g_qp *qp = queue_pair;
554 unsigned nb_dequeued;
556 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
557 (void **)c_ops, nb_ops, NULL);
558 qp->qp_stats.dequeued_count += nb_dequeued;
563 static int cryptodev_snow3g_remove(struct rte_vdev_device *vdev);
566 cryptodev_snow3g_create(const char *name,
567 struct rte_vdev_device *vdev,
568 struct rte_crypto_vdev_init_params *init_params)
570 struct rte_cryptodev *dev;
571 struct snow3g_private *internals;
572 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
574 if (init_params->name[0] == '\0')
575 snprintf(init_params->name, sizeof(init_params->name),
578 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
579 sizeof(struct snow3g_private), init_params->socket_id,
582 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
586 dev->driver_id = cryptodev_driver_id;
587 dev->dev_ops = rte_snow3g_pmd_ops;
589 /* Register RX/TX burst functions for data path. */
590 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
591 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
593 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
594 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
597 internals = dev->data->dev_private;
599 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
600 internals->max_nb_sessions = init_params->max_nb_sessions;
604 SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed",
607 cryptodev_snow3g_remove(vdev);
612 cryptodev_snow3g_probe(struct rte_vdev_device *vdev)
614 struct rte_crypto_vdev_init_params init_params = {
615 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
616 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
621 const char *input_args;
623 name = rte_vdev_device_name(vdev);
626 input_args = rte_vdev_device_args(vdev);
628 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
630 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
631 init_params.socket_id);
632 if (init_params.name[0] != '\0')
633 RTE_LOG(INFO, PMD, " User defined name = %s\n",
635 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
636 init_params.max_nb_queue_pairs);
637 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
638 init_params.max_nb_sessions);
640 return cryptodev_snow3g_create(name, vdev, &init_params);
644 cryptodev_snow3g_remove(struct rte_vdev_device *vdev)
648 name = rte_vdev_device_name(vdev);
652 RTE_LOG(INFO, PMD, "Closing SNOW 3G crypto device %s"
653 " on numa socket %u\n",
654 name, rte_socket_id());
659 static struct rte_vdev_driver cryptodev_snow3g_pmd_drv = {
660 .probe = cryptodev_snow3g_probe,
661 .remove = cryptodev_snow3g_remove
664 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd_drv);
665 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd);
666 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_SNOW3G_PMD,
667 "max_nb_queue_pairs=<int> "
668 "max_nb_sessions=<int> "
670 RTE_PMD_REGISTER_CRYPTO_DRIVER(cryptodev_snow3g_pmd_drv, cryptodev_driver_id);