1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2018 Intel Corporation
5 #include <rte_common.h>
6 #include <rte_hexdump.h>
7 #include <rte_cryptodev.h>
8 #include <rte_cryptodev_pmd.h>
9 #include <rte_bus_vdev.h>
10 #include <rte_malloc.h>
11 #include <rte_cpuflags.h>
13 #include "snow3g_pmd_private.h"
15 #define SNOW3G_IV_LENGTH 16
16 #define SNOW3G_MAX_BURST 8
19 int snow3g_logtype_driver;
20 static uint8_t cryptodev_driver_id;
22 /** Get xform chain order. */
23 static enum snow3g_operation
24 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
27 return SNOW3G_OP_NOT_SUPPORTED;
30 if (xform->next->next != NULL)
31 return SNOW3G_OP_NOT_SUPPORTED;
33 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
34 if (xform->next == NULL)
35 return SNOW3G_OP_ONLY_AUTH;
36 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
37 return SNOW3G_OP_AUTH_CIPHER;
39 return SNOW3G_OP_NOT_SUPPORTED;
42 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
43 if (xform->next == NULL)
44 return SNOW3G_OP_ONLY_CIPHER;
45 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
46 return SNOW3G_OP_CIPHER_AUTH;
48 return SNOW3G_OP_NOT_SUPPORTED;
51 return SNOW3G_OP_NOT_SUPPORTED;
55 /** Parse crypto xform chain and set private session parameters. */
57 snow3g_set_session_parameters(MB_MGR *mgr, struct snow3g_session *sess,
58 const struct rte_crypto_sym_xform *xform)
60 const struct rte_crypto_sym_xform *auth_xform = NULL;
61 const struct rte_crypto_sym_xform *cipher_xform = NULL;
62 enum snow3g_operation mode;
64 /* Select Crypto operation - hash then cipher / cipher then hash */
65 mode = snow3g_get_mode(xform);
68 case SNOW3G_OP_CIPHER_AUTH:
69 auth_xform = xform->next;
72 case SNOW3G_OP_ONLY_CIPHER:
75 case SNOW3G_OP_AUTH_CIPHER:
76 cipher_xform = xform->next;
78 case SNOW3G_OP_ONLY_AUTH:
81 case SNOW3G_OP_NOT_SUPPORTED:
83 SNOW3G_LOG(ERR, "Unsupported operation chain order parameter");
88 /* Only SNOW 3G UEA2 supported */
89 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
92 if (cipher_xform->cipher.iv.length != SNOW3G_IV_LENGTH) {
93 SNOW3G_LOG(ERR, "Wrong IV length");
96 if (cipher_xform->cipher.key.length > SNOW3G_MAX_KEY_SIZE) {
97 SNOW3G_LOG(ERR, "Not enough memory to store the key");
101 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
104 IMB_SNOW3G_INIT_KEY_SCHED(mgr, cipher_xform->cipher.key.data,
105 &sess->pKeySched_cipher);
109 /* Only SNOW 3G UIA2 supported */
110 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
113 if (auth_xform->auth.digest_length != SNOW3G_DIGEST_LENGTH) {
114 SNOW3G_LOG(ERR, "Wrong digest length");
117 if (auth_xform->auth.key.length > SNOW3G_MAX_KEY_SIZE) {
118 SNOW3G_LOG(ERR, "Not enough memory to store the key");
122 sess->auth_op = auth_xform->auth.op;
124 if (auth_xform->auth.iv.length != SNOW3G_IV_LENGTH) {
125 SNOW3G_LOG(ERR, "Wrong IV length");
128 sess->auth_iv_offset = auth_xform->auth.iv.offset;
131 IMB_SNOW3G_INIT_KEY_SCHED(mgr, auth_xform->auth.key.data,
132 &sess->pKeySched_hash);
140 /** Get SNOW 3G session. */
141 static struct snow3g_session *
142 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
144 struct snow3g_session *sess = NULL;
146 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
147 if (likely(op->sym->session != NULL))
148 sess = (struct snow3g_session *)
149 get_sym_session_private_data(
151 cryptodev_driver_id);
154 void *_sess_private_data = NULL;
156 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
159 if (rte_mempool_get(qp->sess_mp_priv,
160 (void **)&_sess_private_data))
163 sess = (struct snow3g_session *)_sess_private_data;
165 if (unlikely(snow3g_set_session_parameters(qp->mgr, sess,
166 op->sym->xform) != 0)) {
167 rte_mempool_put(qp->sess_mp, _sess);
168 rte_mempool_put(qp->sess_mp_priv, _sess_private_data);
171 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
172 set_sym_session_private_data(op->sym->session,
173 cryptodev_driver_id, _sess_private_data);
176 if (unlikely(sess == NULL))
177 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
183 /** Encrypt/decrypt mbufs with same cipher key. */
185 process_snow3g_cipher_op(struct snow3g_qp *qp, struct rte_crypto_op **ops,
186 struct snow3g_session *session,
190 uint8_t processed_ops = 0;
191 const void *src[SNOW3G_MAX_BURST];
192 void *dst[SNOW3G_MAX_BURST];
193 const void *iv[SNOW3G_MAX_BURST];
194 uint32_t num_bytes[SNOW3G_MAX_BURST];
196 for (i = 0; i < num_ops; i++) {
197 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
198 (ops[i]->sym->cipher.data.offset >> 3);
199 dst[i] = ops[i]->sym->m_dst ?
200 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
201 (ops[i]->sym->cipher.data.offset >> 3) :
202 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
203 (ops[i]->sym->cipher.data.offset >> 3);
204 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
205 session->cipher_iv_offset);
206 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
211 IMB_SNOW3G_F8_N_BUFFER(qp->mgr, &session->pKeySched_cipher, iv,
212 src, dst, num_bytes, processed_ops);
214 return processed_ops;
217 /** Encrypt/decrypt mbuf (bit level function). */
219 process_snow3g_cipher_op_bit(struct snow3g_qp *qp,
220 struct rte_crypto_op *op,
221 struct snow3g_session *session)
225 uint32_t length_in_bits, offset_in_bits;
227 offset_in_bits = op->sym->cipher.data.offset;
228 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
229 if (op->sym->m_dst == NULL) {
230 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
231 SNOW3G_LOG(ERR, "bit-level in-place not supported\n");
234 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
235 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
236 session->cipher_iv_offset);
237 length_in_bits = op->sym->cipher.data.length;
239 IMB_SNOW3G_F8_1_BUFFER_BIT(qp->mgr, &session->pKeySched_cipher, iv,
240 src, dst, length_in_bits, offset_in_bits);
245 /** Generate/verify hash from mbufs with same hash key. */
247 process_snow3g_hash_op(struct snow3g_qp *qp, struct rte_crypto_op **ops,
248 struct snow3g_session *session,
252 uint8_t processed_ops = 0;
254 uint32_t length_in_bits;
257 for (i = 0; i < num_ops; i++) {
258 /* Data must be byte aligned */
259 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
260 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
261 SNOW3G_LOG(ERR, "Offset");
265 length_in_bits = ops[i]->sym->auth.data.length;
267 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
268 (ops[i]->sym->auth.data.offset >> 3);
269 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
270 session->auth_iv_offset);
272 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
273 dst = qp->temp_digest;
275 IMB_SNOW3G_F9_1_BUFFER(qp->mgr,
276 &session->pKeySched_hash,
277 iv, src, length_in_bits, dst);
279 if (memcmp(dst, ops[i]->sym->auth.digest.data,
280 SNOW3G_DIGEST_LENGTH) != 0)
281 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
283 dst = ops[i]->sym->auth.digest.data;
285 IMB_SNOW3G_F9_1_BUFFER(qp->mgr,
286 &session->pKeySched_hash,
287 iv, src, length_in_bits, dst);
292 return processed_ops;
295 /** Process a batch of crypto ops which shares the same session. */
297 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
298 struct snow3g_qp *qp, uint8_t num_ops,
299 uint16_t *accumulated_enqueued_ops)
302 unsigned enqueued_ops, processed_ops;
304 #ifdef RTE_LIBRTE_PMD_SNOW3G_DEBUG
305 for (i = 0; i < num_ops; i++) {
306 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
307 (ops[i]->sym->m_dst != NULL &&
308 !rte_pktmbuf_is_contiguous(
309 ops[i]->sym->m_dst))) {
310 SNOW3G_LOG(ERR, "PMD supports only contiguous mbufs, "
311 "op (%p) provides noncontiguous mbuf as "
312 "source/destination buffer.\n", ops[i]);
313 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
319 switch (session->op) {
320 case SNOW3G_OP_ONLY_CIPHER:
321 processed_ops = process_snow3g_cipher_op(qp, ops,
324 case SNOW3G_OP_ONLY_AUTH:
325 processed_ops = process_snow3g_hash_op(qp, ops, session,
328 case SNOW3G_OP_CIPHER_AUTH:
329 processed_ops = process_snow3g_cipher_op(qp, ops, session,
331 process_snow3g_hash_op(qp, ops, session, processed_ops);
333 case SNOW3G_OP_AUTH_CIPHER:
334 processed_ops = process_snow3g_hash_op(qp, ops, session,
336 process_snow3g_cipher_op(qp, ops, session, processed_ops);
339 /* Operation not supported. */
343 for (i = 0; i < num_ops; i++) {
345 * If there was no error/authentication failure,
346 * change status to successful.
348 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
349 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
350 /* Free session if a session-less crypto op. */
351 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
352 memset(session, 0, sizeof(struct snow3g_session));
353 memset(ops[i]->sym->session, 0,
354 rte_cryptodev_sym_get_existing_header_session_size(
355 ops[i]->sym->session));
356 rte_mempool_put(qp->sess_mp_priv, session);
357 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
358 ops[i]->sym->session = NULL;
362 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
363 (void **)ops, processed_ops, NULL);
364 qp->qp_stats.enqueued_count += enqueued_ops;
365 *accumulated_enqueued_ops += enqueued_ops;
370 /** Process a crypto op with length/offset in bits. */
372 process_op_bit(struct rte_crypto_op *op, struct snow3g_session *session,
373 struct snow3g_qp *qp, uint16_t *accumulated_enqueued_ops)
375 unsigned enqueued_op, processed_op;
377 switch (session->op) {
378 case SNOW3G_OP_ONLY_CIPHER:
379 processed_op = process_snow3g_cipher_op_bit(qp, op,
382 case SNOW3G_OP_ONLY_AUTH:
383 processed_op = process_snow3g_hash_op(qp, &op, session, 1);
385 case SNOW3G_OP_CIPHER_AUTH:
386 processed_op = process_snow3g_cipher_op_bit(qp, op, session);
387 if (processed_op == 1)
388 process_snow3g_hash_op(qp, &op, session, 1);
390 case SNOW3G_OP_AUTH_CIPHER:
391 processed_op = process_snow3g_hash_op(qp, &op, session, 1);
392 if (processed_op == 1)
393 process_snow3g_cipher_op_bit(qp, op, session);
396 /* Operation not supported. */
401 * If there was no error/authentication failure,
402 * change status to successful.
404 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
405 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
407 /* Free session if a session-less crypto op. */
408 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
409 memset(op->sym->session, 0, sizeof(struct snow3g_session));
410 rte_cryptodev_sym_session_free(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_cryptodev_pmd_init_params *init_params)
547 struct rte_cryptodev *dev;
548 struct snow3g_private *internals;
551 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
553 SNOW3G_LOG(ERR, "failed to create cryptodev vdev");
557 dev->driver_id = cryptodev_driver_id;
558 dev->dev_ops = rte_snow3g_pmd_ops;
560 /* Register RX/TX burst functions for data path. */
561 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
562 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
564 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
565 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
566 RTE_CRYPTODEV_FF_SYM_SESSIONLESS;
568 mgr = alloc_mb_mgr(0);
572 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2)) {
573 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
574 init_mb_mgr_avx2(mgr);
575 } else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX)) {
576 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
577 init_mb_mgr_avx(mgr);
579 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
580 init_mb_mgr_sse(mgr);
583 internals = dev->data->dev_private;
584 internals->mgr = mgr;
586 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
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
607 const char *input_args;
609 name = rte_vdev_device_name(vdev);
612 input_args = rte_vdev_device_args(vdev);
614 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
616 return cryptodev_snow3g_create(name, vdev, &init_params);
620 cryptodev_snow3g_remove(struct rte_vdev_device *vdev)
622 struct rte_cryptodev *cryptodev;
624 struct snow3g_private *internals;
626 name = rte_vdev_device_name(vdev);
630 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
631 if (cryptodev == NULL)
634 internals = cryptodev->data->dev_private;
636 free_mb_mgr(internals->mgr);
638 return rte_cryptodev_pmd_destroy(cryptodev);
641 static struct rte_vdev_driver cryptodev_snow3g_pmd_drv = {
642 .probe = cryptodev_snow3g_probe,
643 .remove = cryptodev_snow3g_remove
646 static struct cryptodev_driver snow3g_crypto_drv;
648 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd_drv);
649 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd);
650 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_SNOW3G_PMD,
651 "max_nb_queue_pairs=<int> "
653 RTE_PMD_REGISTER_CRYPTO_DRIVER(snow3g_crypto_drv,
654 cryptodev_snow3g_pmd_drv.driver, cryptodev_driver_id);
656 RTE_INIT(snow3g_init_log)
658 snow3g_logtype_driver = rte_log_register("pmd.crypto.snow3g");