4 * Copyright(c) 2016 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>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
42 #include "rte_snow3g_pmd_private.h"
44 #define SNOW3G_IV_LENGTH 16
45 #define SNOW3G_DIGEST_LENGTH 4
46 #define SNOW3G_MAX_BURST 8
50 * Global static parameter used to create a unique name for each SNOW 3G
53 static unsigned unique_name_id;
56 create_unique_device_name(char *name, size_t size)
63 ret = snprintf(name, size, "%s_%u", RTE_STR(CRYPTODEV_NAME_SNOW3G_PMD),
70 /** Get xform chain order. */
71 static enum snow3g_operation
72 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
75 return SNOW3G_OP_NOT_SUPPORTED;
78 if (xform->next->next != NULL)
79 return SNOW3G_OP_NOT_SUPPORTED;
81 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
82 if (xform->next == NULL)
83 return SNOW3G_OP_ONLY_AUTH;
84 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
85 return SNOW3G_OP_AUTH_CIPHER;
87 return SNOW3G_OP_NOT_SUPPORTED;
90 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
91 if (xform->next == NULL)
92 return SNOW3G_OP_ONLY_CIPHER;
93 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
94 return SNOW3G_OP_CIPHER_AUTH;
96 return SNOW3G_OP_NOT_SUPPORTED;
99 return SNOW3G_OP_NOT_SUPPORTED;
103 /** Parse crypto xform chain and set private session parameters. */
105 snow3g_set_session_parameters(struct snow3g_session *sess,
106 const struct rte_crypto_sym_xform *xform)
108 const struct rte_crypto_sym_xform *auth_xform = NULL;
109 const struct rte_crypto_sym_xform *cipher_xform = NULL;
110 enum snow3g_operation mode;
112 /* Select Crypto operation - hash then cipher / cipher then hash */
113 mode = snow3g_get_mode(xform);
116 case SNOW3G_OP_CIPHER_AUTH:
117 auth_xform = xform->next;
120 case SNOW3G_OP_ONLY_CIPHER:
121 cipher_xform = xform;
123 case SNOW3G_OP_AUTH_CIPHER:
124 cipher_xform = xform->next;
126 case SNOW3G_OP_ONLY_AUTH:
129 case SNOW3G_OP_NOT_SUPPORTED:
131 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
136 /* Only SNOW 3G UEA2 supported */
137 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
140 sso_snow3g_init_key_sched(xform->cipher.key.data,
141 &sess->pKeySched_cipher);
145 /* Only SNOW 3G UIA2 supported */
146 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
148 sess->auth_op = auth_xform->auth.op;
150 sso_snow3g_init_key_sched(xform->auth.key.data,
151 &sess->pKeySched_hash);
160 /** Get SNOW 3G session. */
161 static struct snow3g_session *
162 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
164 struct snow3g_session *sess;
166 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
167 if (unlikely(op->sym->session->dev_type !=
168 RTE_CRYPTODEV_SNOW3G_PMD))
171 sess = (struct snow3g_session *)op->sym->session->_private;
173 struct rte_cryptodev_session *c_sess = NULL;
175 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
178 sess = (struct snow3g_session *)c_sess->_private;
180 if (unlikely(snow3g_set_session_parameters(sess,
181 op->sym->xform) != 0))
188 /** Encrypt/decrypt mbufs with same cipher key. */
190 process_snow3g_cipher_op(struct rte_crypto_op **ops,
191 struct snow3g_session *session,
195 uint8_t processed_ops = 0;
196 uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
197 uint8_t *IV[SNOW3G_MAX_BURST];
198 uint32_t num_bytes[SNOW3G_MAX_BURST];
200 for (i = 0; i < num_ops; i++) {
202 if (unlikely(ops[i]->sym->cipher.iv.length != SNOW3G_IV_LENGTH)) {
203 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
204 SNOW3G_LOG_ERR("iv");
208 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
209 (ops[i]->sym->cipher.data.offset >> 3);
210 dst[i] = ops[i]->sym->m_dst ?
211 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
212 (ops[i]->sym->cipher.data.offset >> 3) :
213 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
214 (ops[i]->sym->cipher.data.offset >> 3);
215 IV[i] = ops[i]->sym->cipher.iv.data;
216 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
221 sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, IV, src, dst,
222 num_bytes, processed_ops);
224 return processed_ops;
227 /** Encrypt/decrypt mbuf (bit level function). */
229 process_snow3g_cipher_op_bit(struct rte_crypto_op *op,
230 struct snow3g_session *session)
234 uint32_t length_in_bits, offset_in_bits;
237 if (unlikely(op->sym->cipher.iv.length != SNOW3G_IV_LENGTH)) {
238 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
239 SNOW3G_LOG_ERR("iv");
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 = op->sym->cipher.iv.data;
252 length_in_bits = op->sym->cipher.data.length;
254 sso_snow3g_f8_1_buffer_bit(&session->pKeySched_cipher, IV,
255 src, dst, length_in_bits, offset_in_bits);
260 /** Generate/verify hash from mbufs with same hash key. */
262 process_snow3g_hash_op(struct rte_crypto_op **ops,
263 struct snow3g_session *session,
267 uint8_t processed_ops = 0;
269 uint32_t length_in_bits;
271 for (i = 0; i < num_ops; i++) {
272 if (unlikely(ops[i]->sym->auth.aad.length != SNOW3G_IV_LENGTH)) {
273 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
274 SNOW3G_LOG_ERR("aad");
278 if (unlikely(ops[i]->sym->auth.digest.length != SNOW3G_DIGEST_LENGTH)) {
279 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
280 SNOW3G_LOG_ERR("digest");
284 /* Data must be byte aligned */
285 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
286 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
287 SNOW3G_LOG_ERR("Offset");
291 length_in_bits = ops[i]->sym->auth.data.length;
293 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
294 (ops[i]->sym->auth.data.offset >> 3);
296 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
297 dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
298 ops[i]->sym->auth.digest.length);
300 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
301 ops[i]->sym->auth.aad.data, src,
302 length_in_bits, dst);
304 if (memcmp(dst, ops[i]->sym->auth.digest.data,
305 ops[i]->sym->auth.digest.length) != 0)
306 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
308 /* Trim area used for digest from mbuf. */
309 rte_pktmbuf_trim(ops[i]->sym->m_src,
310 ops[i]->sym->auth.digest.length);
312 dst = ops[i]->sym->auth.digest.data;
314 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
315 ops[i]->sym->auth.aad.data, src,
316 length_in_bits, dst);
321 return processed_ops;
324 /** Process a batch of crypto ops which shares the same session. */
326 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
327 struct snow3g_qp *qp, uint8_t num_ops,
328 uint16_t *accumulated_enqueued_ops)
331 unsigned enqueued_ops, processed_ops;
333 switch (session->op) {
334 case SNOW3G_OP_ONLY_CIPHER:
335 processed_ops = process_snow3g_cipher_op(ops,
338 case SNOW3G_OP_ONLY_AUTH:
339 processed_ops = process_snow3g_hash_op(ops, session,
342 case SNOW3G_OP_CIPHER_AUTH:
343 processed_ops = process_snow3g_cipher_op(ops, session,
345 process_snow3g_hash_op(ops, session, processed_ops);
347 case SNOW3G_OP_AUTH_CIPHER:
348 processed_ops = process_snow3g_hash_op(ops, session,
350 process_snow3g_cipher_op(ops, session, processed_ops);
353 /* Operation not supported. */
357 for (i = 0; i < num_ops; i++) {
359 * If there was no error/authentication failure,
360 * change status to successful.
362 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
363 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
364 /* Free session if a session-less crypto op. */
365 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
366 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
367 ops[i]->sym->session = NULL;
371 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
372 (void **)ops, processed_ops);
373 qp->qp_stats.enqueued_count += enqueued_ops;
374 *accumulated_enqueued_ops += enqueued_ops;
379 /** Process a crypto op with length/offset in bits. */
381 process_op_bit(struct rte_crypto_op *op, struct snow3g_session *session,
382 struct snow3g_qp *qp, uint16_t *accumulated_enqueued_ops)
384 unsigned enqueued_op, processed_op;
386 switch (session->op) {
387 case SNOW3G_OP_ONLY_CIPHER:
388 processed_op = process_snow3g_cipher_op_bit(op,
391 case SNOW3G_OP_ONLY_AUTH:
392 processed_op = process_snow3g_hash_op(&op, session, 1);
394 case SNOW3G_OP_CIPHER_AUTH:
395 processed_op = process_snow3g_cipher_op_bit(op, session);
396 if (processed_op == 1)
397 process_snow3g_hash_op(&op, session, 1);
399 case SNOW3G_OP_AUTH_CIPHER:
400 processed_op = process_snow3g_hash_op(&op, session, 1);
401 if (processed_op == 1)
402 process_snow3g_cipher_op_bit(op, session);
405 /* Operation not supported. */
410 * If there was no error/authentication failure,
411 * change status to successful.
413 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
414 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
416 /* Free session if a session-less crypto op. */
417 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
418 rte_mempool_put(qp->sess_mp, op->sym->session);
419 op->sym->session = NULL;
422 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops,
423 (void **)&op, processed_op);
424 qp->qp_stats.enqueued_count += enqueued_op;
425 *accumulated_enqueued_ops += enqueued_op;
431 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
434 struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
435 struct rte_crypto_op *curr_c_op;
437 struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
438 struct snow3g_qp *qp = queue_pair;
440 uint8_t burst_size = 0;
441 uint16_t enqueued_ops = 0;
442 uint8_t processed_ops;
444 for (i = 0; i < nb_ops; i++) {
447 /* Set status as enqueued (not processed yet) by default. */
448 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
450 curr_sess = snow3g_get_session(qp, curr_c_op);
451 if (unlikely(curr_sess == NULL ||
452 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
454 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
458 /* If length/offset is at bit-level, process this buffer alone. */
459 if (((curr_c_op->sym->cipher.data.length % BYTE_LEN) != 0)
460 || ((curr_c_op->sym->cipher.data.offset
462 /* Process the ops of the previous session. */
463 if (prev_sess != NULL) {
464 processed_ops = process_ops(c_ops, prev_sess,
465 qp, burst_size, &enqueued_ops);
466 if (processed_ops < burst_size) {
475 processed_ops = process_op_bit(curr_c_op, curr_sess,
477 if (processed_ops != 1)
483 /* Batch ops that share the same session. */
484 if (prev_sess == NULL) {
485 prev_sess = curr_sess;
486 c_ops[burst_size++] = curr_c_op;
487 } else if (curr_sess == prev_sess) {
488 c_ops[burst_size++] = curr_c_op;
490 * When there are enough ops to process in a batch,
491 * process them, and start a new batch.
493 if (burst_size == SNOW3G_MAX_BURST) {
494 processed_ops = process_ops(c_ops, prev_sess,
495 qp, burst_size, &enqueued_ops);
496 if (processed_ops < burst_size) {
506 * Different session, process the ops
507 * of the previous session.
509 processed_ops = process_ops(c_ops, prev_sess,
510 qp, burst_size, &enqueued_ops);
511 if (processed_ops < burst_size) {
517 prev_sess = curr_sess;
519 c_ops[burst_size++] = curr_c_op;
523 if (burst_size != 0) {
524 /* Process the crypto ops of the last session. */
525 processed_ops = process_ops(c_ops, prev_sess,
526 qp, burst_size, &enqueued_ops);
529 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
534 snow3g_pmd_dequeue_burst(void *queue_pair,
535 struct rte_crypto_op **c_ops, uint16_t nb_ops)
537 struct snow3g_qp *qp = queue_pair;
539 unsigned nb_dequeued;
541 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
542 (void **)c_ops, nb_ops);
543 qp->qp_stats.dequeued_count += nb_dequeued;
548 static int cryptodev_snow3g_remove(const char *name);
551 cryptodev_snow3g_create(const char *name,
552 struct rte_crypto_vdev_init_params *init_params)
554 struct rte_cryptodev *dev;
555 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
556 struct snow3g_private *internals;
557 uint64_t cpu_flags = 0;
559 /* Check CPU for supported vector instruction set */
560 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
561 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
563 SNOW3G_LOG_ERR("Vector instructions are not supported by CPU");
568 /* Create a unique device name. */
569 if (create_unique_device_name(crypto_dev_name,
570 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
571 SNOW3G_LOG_ERR("failed to create unique cryptodev name");
575 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
576 sizeof(struct snow3g_private), init_params->socket_id);
578 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
582 dev->dev_type = RTE_CRYPTODEV_SNOW3G_PMD;
583 dev->dev_ops = rte_snow3g_pmd_ops;
585 /* Register RX/TX burst functions for data path. */
586 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
587 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
589 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
590 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
593 internals = dev->data->dev_private;
595 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
596 internals->max_nb_sessions = init_params->max_nb_sessions;
600 SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed", name);
602 cryptodev_snow3g_remove(crypto_dev_name);
607 cryptodev_snow3g_probe(const char *name,
608 const char *input_args)
610 struct rte_crypto_vdev_init_params init_params = {
611 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
612 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
616 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
618 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
619 init_params.socket_id);
620 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
621 init_params.max_nb_queue_pairs);
622 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
623 init_params.max_nb_sessions);
625 return cryptodev_snow3g_create(name, &init_params);
629 cryptodev_snow3g_remove(const char *name)
634 RTE_LOG(INFO, PMD, "Closing SNOW 3G crypto device %s"
635 " on numa socket %u\n",
636 name, rte_socket_id());
641 static struct rte_vdev_driver cryptodev_snow3g_pmd_drv = {
642 .probe = cryptodev_snow3g_probe,
643 .remove = cryptodev_snow3g_remove
646 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd_drv);
647 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd);
648 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_SNOW3G_PMD,
649 "max_nb_queue_pairs=<int> "
650 "max_nb_sessions=<int> "