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_MAX_BURST 8
49 static uint8_t cryptodev_driver_id;
51 /** Get xform chain order. */
52 static enum snow3g_operation
53 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
56 return SNOW3G_OP_NOT_SUPPORTED;
59 if (xform->next->next != NULL)
60 return SNOW3G_OP_NOT_SUPPORTED;
62 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
63 if (xform->next == NULL)
64 return SNOW3G_OP_ONLY_AUTH;
65 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
66 return SNOW3G_OP_AUTH_CIPHER;
68 return SNOW3G_OP_NOT_SUPPORTED;
71 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
72 if (xform->next == NULL)
73 return SNOW3G_OP_ONLY_CIPHER;
74 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
75 return SNOW3G_OP_CIPHER_AUTH;
77 return SNOW3G_OP_NOT_SUPPORTED;
80 return SNOW3G_OP_NOT_SUPPORTED;
84 /** Parse crypto xform chain and set private session parameters. */
86 snow3g_set_session_parameters(struct snow3g_session *sess,
87 const struct rte_crypto_sym_xform *xform)
89 const struct rte_crypto_sym_xform *auth_xform = NULL;
90 const struct rte_crypto_sym_xform *cipher_xform = NULL;
91 enum snow3g_operation mode;
93 /* Select Crypto operation - hash then cipher / cipher then hash */
94 mode = snow3g_get_mode(xform);
97 case SNOW3G_OP_CIPHER_AUTH:
98 auth_xform = xform->next;
101 case SNOW3G_OP_ONLY_CIPHER:
102 cipher_xform = xform;
104 case SNOW3G_OP_AUTH_CIPHER:
105 cipher_xform = xform->next;
107 case SNOW3G_OP_ONLY_AUTH:
110 case SNOW3G_OP_NOT_SUPPORTED:
112 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
117 /* Only SNOW 3G UEA2 supported */
118 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
121 if (cipher_xform->cipher.iv.length != SNOW3G_IV_LENGTH) {
122 SNOW3G_LOG_ERR("Wrong IV length");
125 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
128 sso_snow3g_init_key_sched(cipher_xform->cipher.key.data,
129 &sess->pKeySched_cipher);
133 /* Only SNOW 3G UIA2 supported */
134 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
137 if (auth_xform->auth.digest_length != SNOW3G_DIGEST_LENGTH) {
138 SNOW3G_LOG_ERR("Wrong digest length");
142 sess->auth_op = auth_xform->auth.op;
144 if (auth_xform->auth.iv.length != SNOW3G_IV_LENGTH) {
145 SNOW3G_LOG_ERR("Wrong IV length");
148 sess->auth_iv_offset = auth_xform->auth.iv.offset;
151 sso_snow3g_init_key_sched(auth_xform->auth.key.data,
152 &sess->pKeySched_hash);
161 /** Get SNOW 3G session. */
162 static struct snow3g_session *
163 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
165 struct snow3g_session *sess = NULL;
167 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
168 if (likely(op->sym->session != NULL))
169 sess = (struct snow3g_session *)
170 get_session_private_data(
172 cryptodev_driver_id);
175 void *_sess_private_data = NULL;
177 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
180 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
183 sess = (struct snow3g_session *)_sess_private_data;
185 if (unlikely(snow3g_set_session_parameters(sess,
186 op->sym->xform) != 0)) {
187 rte_mempool_put(qp->sess_mp, _sess);
188 rte_mempool_put(qp->sess_mp, _sess_private_data);
191 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
192 set_session_private_data(op->sym->session, cryptodev_driver_id,
196 if (unlikely(sess == NULL))
197 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
203 /** Encrypt/decrypt mbufs with same cipher key. */
205 process_snow3g_cipher_op(struct rte_crypto_op **ops,
206 struct snow3g_session *session,
210 uint8_t processed_ops = 0;
211 uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
212 uint8_t *iv[SNOW3G_MAX_BURST];
213 uint32_t num_bytes[SNOW3G_MAX_BURST];
215 for (i = 0; i < num_ops; i++) {
216 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
217 (ops[i]->sym->cipher.data.offset >> 3);
218 dst[i] = ops[i]->sym->m_dst ?
219 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
220 (ops[i]->sym->cipher.data.offset >> 3) :
221 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
222 (ops[i]->sym->cipher.data.offset >> 3);
223 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
224 session->cipher_iv_offset);
225 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
230 sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, iv, src, dst,
231 num_bytes, processed_ops);
233 return processed_ops;
236 /** Encrypt/decrypt mbuf (bit level function). */
238 process_snow3g_cipher_op_bit(struct rte_crypto_op *op,
239 struct snow3g_session *session)
243 uint32_t length_in_bits, offset_in_bits;
245 offset_in_bits = op->sym->cipher.data.offset;
246 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
247 if (op->sym->m_dst == NULL) {
248 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
249 SNOW3G_LOG_ERR("bit-level in-place not supported\n");
252 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
253 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
254 session->cipher_iv_offset);
255 length_in_bits = op->sym->cipher.data.length;
257 sso_snow3g_f8_1_buffer_bit(&session->pKeySched_cipher, iv,
258 src, dst, length_in_bits, offset_in_bits);
263 /** Generate/verify hash from mbufs with same hash key. */
265 process_snow3g_hash_op(struct snow3g_qp *qp, struct rte_crypto_op **ops,
266 struct snow3g_session *session,
270 uint8_t processed_ops = 0;
272 uint32_t length_in_bits;
275 for (i = 0; i < num_ops; i++) {
276 /* Data must be byte aligned */
277 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
278 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
279 SNOW3G_LOG_ERR("Offset");
283 length_in_bits = ops[i]->sym->auth.data.length;
285 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
286 (ops[i]->sym->auth.data.offset >> 3);
287 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
288 session->auth_iv_offset);
290 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
291 dst = qp->temp_digest;
293 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
295 length_in_bits, dst);
297 if (memcmp(dst, ops[i]->sym->auth.digest.data,
298 SNOW3G_DIGEST_LENGTH) != 0)
299 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
301 dst = ops[i]->sym->auth.digest.data;
303 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
305 length_in_bits, dst);
310 return processed_ops;
313 /** Process a batch of crypto ops which shares the same session. */
315 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
316 struct snow3g_qp *qp, uint8_t num_ops,
317 uint16_t *accumulated_enqueued_ops)
320 unsigned enqueued_ops, processed_ops;
322 #ifdef RTE_LIBRTE_PMD_SNOW3G_DEBUG
323 for (i = 0; i < num_ops; i++) {
324 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
325 (ops[i]->sym->m_dst != NULL &&
326 !rte_pktmbuf_is_contiguous(
327 ops[i]->sym->m_dst))) {
328 SNOW3G_LOG_ERR("PMD supports only contiguous mbufs, "
329 "op (%p) provides noncontiguous mbuf as "
330 "source/destination buffer.\n", ops[i]);
331 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
337 switch (session->op) {
338 case SNOW3G_OP_ONLY_CIPHER:
339 processed_ops = process_snow3g_cipher_op(ops,
342 case SNOW3G_OP_ONLY_AUTH:
343 processed_ops = process_snow3g_hash_op(qp, ops, session,
346 case SNOW3G_OP_CIPHER_AUTH:
347 processed_ops = process_snow3g_cipher_op(ops, session,
349 process_snow3g_hash_op(qp, ops, session, processed_ops);
351 case SNOW3G_OP_AUTH_CIPHER:
352 processed_ops = process_snow3g_hash_op(qp, ops, session,
354 process_snow3g_cipher_op(ops, session, processed_ops);
357 /* Operation not supported. */
361 for (i = 0; i < num_ops; i++) {
363 * If there was no error/authentication failure,
364 * change status to successful.
366 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
367 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
368 /* Free session if a session-less crypto op. */
369 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
370 memset(session, 0, sizeof(struct snow3g_session));
371 memset(ops[i]->sym->session, 0,
372 rte_cryptodev_get_header_session_size());
373 rte_mempool_put(qp->sess_mp, session);
374 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
375 ops[i]->sym->session = NULL;
379 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
380 (void **)ops, processed_ops, NULL);
381 qp->qp_stats.enqueued_count += enqueued_ops;
382 *accumulated_enqueued_ops += enqueued_ops;
387 /** Process a crypto op with length/offset in bits. */
389 process_op_bit(struct rte_crypto_op *op, struct snow3g_session *session,
390 struct snow3g_qp *qp, uint16_t *accumulated_enqueued_ops)
392 unsigned enqueued_op, processed_op;
394 switch (session->op) {
395 case SNOW3G_OP_ONLY_CIPHER:
396 processed_op = process_snow3g_cipher_op_bit(op,
399 case SNOW3G_OP_ONLY_AUTH:
400 processed_op = process_snow3g_hash_op(qp, &op, session, 1);
402 case SNOW3G_OP_CIPHER_AUTH:
403 processed_op = process_snow3g_cipher_op_bit(op, session);
404 if (processed_op == 1)
405 process_snow3g_hash_op(qp, &op, session, 1);
407 case SNOW3G_OP_AUTH_CIPHER:
408 processed_op = process_snow3g_hash_op(qp, &op, session, 1);
409 if (processed_op == 1)
410 process_snow3g_cipher_op_bit(op, session);
413 /* Operation not supported. */
418 * If there was no error/authentication failure,
419 * change status to successful.
421 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
422 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
424 /* Free session if a session-less crypto op. */
425 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
426 memset(op->sym->session, 0, sizeof(struct snow3g_session));
427 rte_cryptodev_sym_session_free(op->sym->session);
428 op->sym->session = NULL;
431 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops,
432 (void **)&op, processed_op, NULL);
433 qp->qp_stats.enqueued_count += enqueued_op;
434 *accumulated_enqueued_ops += enqueued_op;
440 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
443 struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
444 struct rte_crypto_op *curr_c_op;
446 struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
447 struct snow3g_qp *qp = queue_pair;
449 uint8_t burst_size = 0;
450 uint16_t enqueued_ops = 0;
451 uint8_t processed_ops;
453 for (i = 0; i < nb_ops; i++) {
456 /* Set status as enqueued (not processed yet) by default. */
457 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
459 curr_sess = snow3g_get_session(qp, curr_c_op);
460 if (unlikely(curr_sess == NULL ||
461 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
463 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
467 /* If length/offset is at bit-level, process this buffer alone. */
468 if (((curr_c_op->sym->cipher.data.length % BYTE_LEN) != 0)
469 || ((curr_c_op->sym->cipher.data.offset
471 /* Process the ops of the previous session. */
472 if (prev_sess != NULL) {
473 processed_ops = process_ops(c_ops, prev_sess,
474 qp, burst_size, &enqueued_ops);
475 if (processed_ops < burst_size) {
484 processed_ops = process_op_bit(curr_c_op, curr_sess,
486 if (processed_ops != 1)
492 /* Batch ops that share the same session. */
493 if (prev_sess == NULL) {
494 prev_sess = curr_sess;
495 c_ops[burst_size++] = curr_c_op;
496 } else if (curr_sess == prev_sess) {
497 c_ops[burst_size++] = curr_c_op;
499 * When there are enough ops to process in a batch,
500 * process them, and start a new batch.
502 if (burst_size == SNOW3G_MAX_BURST) {
503 processed_ops = process_ops(c_ops, prev_sess,
504 qp, burst_size, &enqueued_ops);
505 if (processed_ops < burst_size) {
515 * Different session, process the ops
516 * of the previous session.
518 processed_ops = process_ops(c_ops, prev_sess,
519 qp, burst_size, &enqueued_ops);
520 if (processed_ops < burst_size) {
526 prev_sess = curr_sess;
528 c_ops[burst_size++] = curr_c_op;
532 if (burst_size != 0) {
533 /* Process the crypto ops of the last session. */
534 processed_ops = process_ops(c_ops, prev_sess,
535 qp, burst_size, &enqueued_ops);
538 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
543 snow3g_pmd_dequeue_burst(void *queue_pair,
544 struct rte_crypto_op **c_ops, uint16_t nb_ops)
546 struct snow3g_qp *qp = queue_pair;
548 unsigned nb_dequeued;
550 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
551 (void **)c_ops, nb_ops, NULL);
552 qp->qp_stats.dequeued_count += nb_dequeued;
557 static int cryptodev_snow3g_remove(struct rte_vdev_device *vdev);
560 cryptodev_snow3g_create(const char *name,
561 struct rte_vdev_device *vdev,
562 struct rte_crypto_vdev_init_params *init_params)
564 struct rte_cryptodev *dev;
565 struct snow3g_private *internals;
566 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
568 if (init_params->name[0] == '\0')
569 snprintf(init_params->name, sizeof(init_params->name),
572 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
573 sizeof(struct snow3g_private), init_params->socket_id,
576 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
580 dev->driver_id = cryptodev_driver_id;
581 dev->dev_ops = rte_snow3g_pmd_ops;
583 /* Register RX/TX burst functions for data path. */
584 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
585 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
587 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
588 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
591 internals = dev->data->dev_private;
593 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
594 internals->max_nb_sessions = init_params->max_nb_sessions;
598 SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed",
601 cryptodev_snow3g_remove(vdev);
606 cryptodev_snow3g_probe(struct rte_vdev_device *vdev)
608 struct rte_crypto_vdev_init_params init_params = {
609 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
610 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
615 const char *input_args;
617 name = rte_vdev_device_name(vdev);
620 input_args = rte_vdev_device_args(vdev);
622 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
624 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
625 init_params.socket_id);
626 if (init_params.name[0] != '\0')
627 RTE_LOG(INFO, PMD, " User defined name = %s\n",
629 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
630 init_params.max_nb_queue_pairs);
631 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
632 init_params.max_nb_sessions);
634 return cryptodev_snow3g_create(name, vdev, &init_params);
638 cryptodev_snow3g_remove(struct rte_vdev_device *vdev)
642 name = rte_vdev_device_name(vdev);
646 RTE_LOG(INFO, PMD, "Closing SNOW 3G crypto device %s"
647 " on numa socket %u\n",
648 name, rte_socket_id());
653 static struct rte_vdev_driver cryptodev_snow3g_pmd_drv = {
654 .probe = cryptodev_snow3g_probe,
655 .remove = cryptodev_snow3g_remove
658 static struct cryptodev_driver snow3g_crypto_drv;
660 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd_drv);
661 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd);
662 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_SNOW3G_PMD,
663 "max_nb_queue_pairs=<int> "
664 "max_nb_sessions=<int> "
666 RTE_PMD_REGISTER_CRYPTO_DRIVER(snow3g_crypto_drv, cryptodev_snow3g_pmd_drv,
667 cryptodev_driver_id);