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 "zuc_pmd_private.h"
14 #define ZUC_MAX_BURST 16
17 int zuc_logtype_driver;
18 static uint8_t cryptodev_driver_id;
20 /** Get xform chain order. */
21 static enum zuc_operation
22 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
25 return ZUC_OP_NOT_SUPPORTED;
28 if (xform->next->next != NULL)
29 return ZUC_OP_NOT_SUPPORTED;
31 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
32 if (xform->next == NULL)
33 return ZUC_OP_ONLY_AUTH;
34 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
35 return ZUC_OP_AUTH_CIPHER;
37 return ZUC_OP_NOT_SUPPORTED;
40 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
41 if (xform->next == NULL)
42 return ZUC_OP_ONLY_CIPHER;
43 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
44 return ZUC_OP_CIPHER_AUTH;
46 return ZUC_OP_NOT_SUPPORTED;
49 return ZUC_OP_NOT_SUPPORTED;
53 /** Parse crypto xform chain and set private session parameters. */
55 zuc_set_session_parameters(struct zuc_session *sess,
56 const struct rte_crypto_sym_xform *xform)
58 const struct rte_crypto_sym_xform *auth_xform = NULL;
59 const struct rte_crypto_sym_xform *cipher_xform = NULL;
60 enum zuc_operation mode;
62 /* Select Crypto operation - hash then cipher / cipher then hash */
63 mode = zuc_get_mode(xform);
66 case ZUC_OP_CIPHER_AUTH:
67 auth_xform = xform->next;
70 case ZUC_OP_ONLY_CIPHER:
73 case ZUC_OP_AUTH_CIPHER:
74 cipher_xform = xform->next;
76 case ZUC_OP_ONLY_AUTH:
79 case ZUC_OP_NOT_SUPPORTED:
81 ZUC_LOG(ERR, "Unsupported operation chain order parameter");
86 /* Only ZUC EEA3 supported */
87 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
90 if (cipher_xform->cipher.iv.length != ZUC_IV_KEY_LENGTH) {
91 ZUC_LOG(ERR, "Wrong IV length");
94 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
97 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
102 /* Only ZUC EIA3 supported */
103 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
106 if (auth_xform->auth.digest_length != ZUC_DIGEST_LENGTH) {
107 ZUC_LOG(ERR, "Wrong digest length");
111 sess->auth_op = auth_xform->auth.op;
113 if (auth_xform->auth.iv.length != ZUC_IV_KEY_LENGTH) {
114 ZUC_LOG(ERR, "Wrong IV length");
117 sess->auth_iv_offset = auth_xform->auth.iv.offset;
120 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
130 /** Get ZUC session. */
131 static struct zuc_session *
132 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
134 struct zuc_session *sess = NULL;
136 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
137 if (likely(op->sym->session != NULL))
138 sess = (struct zuc_session *)get_sym_session_private_data(
140 cryptodev_driver_id);
143 void *_sess_private_data = NULL;
145 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
148 if (rte_mempool_get(qp->sess_mp_priv,
149 (void **)&_sess_private_data))
152 sess = (struct zuc_session *)_sess_private_data;
154 if (unlikely(zuc_set_session_parameters(sess,
155 op->sym->xform) != 0)) {
156 rte_mempool_put(qp->sess_mp, _sess);
157 rte_mempool_put(qp->sess_mp_priv, _sess_private_data);
160 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
161 set_sym_session_private_data(op->sym->session,
162 cryptodev_driver_id, _sess_private_data);
165 if (unlikely(sess == NULL))
166 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
172 /** Encrypt/decrypt mbufs. */
174 process_zuc_cipher_op(struct zuc_qp *qp, struct rte_crypto_op **ops,
175 struct zuc_session **sessions,
179 uint8_t processed_ops = 0;
180 const void *src[ZUC_MAX_BURST];
181 void *dst[ZUC_MAX_BURST];
182 const void *iv[ZUC_MAX_BURST];
183 uint32_t num_bytes[ZUC_MAX_BURST];
184 const void *cipher_keys[ZUC_MAX_BURST];
185 struct zuc_session *sess;
187 for (i = 0; i < num_ops; i++) {
188 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
189 || ((ops[i]->sym->cipher.data.offset
191 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
192 ZUC_LOG(ERR, "Data Length or offset");
198 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
199 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
200 (ops[i]->sym->m_dst != NULL &&
201 !rte_pktmbuf_is_contiguous(
202 ops[i]->sym->m_dst))) {
203 ZUC_LOG(ERR, "PMD supports only contiguous mbufs, "
204 "op (%p) provides noncontiguous mbuf as "
205 "source/destination buffer.\n", ops[i]);
206 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
211 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
212 (ops[i]->sym->cipher.data.offset >> 3);
213 dst[i] = ops[i]->sym->m_dst ?
214 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
215 (ops[i]->sym->cipher.data.offset >> 3) :
216 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
217 (ops[i]->sym->cipher.data.offset >> 3);
218 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
219 sess->cipher_iv_offset);
220 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
222 cipher_keys[i] = sess->pKey_cipher;
227 IMB_ZUC_EEA3_N_BUFFER(qp->mb_mgr, (const void **)cipher_keys,
228 (const void **)iv, (const void **)src, (void **)dst,
229 num_bytes, processed_ops);
231 return processed_ops;
234 /** Generate/verify hash from mbufs. */
236 process_zuc_hash_op(struct zuc_qp *qp, struct rte_crypto_op **ops,
237 struct zuc_session **sessions,
241 uint8_t processed_ops = 0;
244 uint32_t length_in_bits;
246 struct zuc_session *sess;
248 for (i = 0; i < num_ops; i++) {
249 /* Data must be byte aligned */
250 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
251 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
252 ZUC_LOG(ERR, "Offset");
258 length_in_bits = ops[i]->sym->auth.data.length;
260 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
261 (ops[i]->sym->auth.data.offset >> 3);
262 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
263 sess->auth_iv_offset);
265 if (sess->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
266 dst = (uint32_t *)qp->temp_digest;
268 IMB_ZUC_EIA3_1_BUFFER(qp->mb_mgr, sess->pKey_hash,
270 length_in_bits, dst);
272 if (memcmp(dst, ops[i]->sym->auth.digest.data,
273 ZUC_DIGEST_LENGTH) != 0)
274 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
276 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
278 IMB_ZUC_EIA3_1_BUFFER(qp->mb_mgr, sess->pKey_hash,
280 length_in_bits, dst);
285 return processed_ops;
288 /** Process a batch of crypto ops which shares the same operation type. */
290 process_ops(struct rte_crypto_op **ops, enum zuc_operation op_type,
291 struct zuc_session **sessions,
292 struct zuc_qp *qp, uint8_t num_ops,
293 uint16_t *accumulated_enqueued_ops)
296 unsigned enqueued_ops, processed_ops;
299 case ZUC_OP_ONLY_CIPHER:
300 processed_ops = process_zuc_cipher_op(qp, ops,
303 case ZUC_OP_ONLY_AUTH:
304 processed_ops = process_zuc_hash_op(qp, ops, sessions,
307 case ZUC_OP_CIPHER_AUTH:
308 processed_ops = process_zuc_cipher_op(qp, ops, sessions,
310 process_zuc_hash_op(qp, ops, sessions, processed_ops);
312 case ZUC_OP_AUTH_CIPHER:
313 processed_ops = process_zuc_hash_op(qp, ops, sessions,
315 process_zuc_cipher_op(qp, ops, sessions, processed_ops);
318 /* Operation not supported. */
322 for (i = 0; i < num_ops; i++) {
324 * If there was no error/authentication failure,
325 * change status to successful.
327 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
328 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
329 /* Free session if a session-less crypto op. */
330 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
331 memset(sessions[i], 0, sizeof(struct zuc_session));
332 memset(ops[i]->sym->session, 0,
333 rte_cryptodev_sym_get_existing_header_session_size(
334 ops[i]->sym->session));
335 rte_mempool_put(qp->sess_mp_priv, sessions[i]);
336 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
337 ops[i]->sym->session = NULL;
341 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
342 (void **)ops, processed_ops, NULL);
343 qp->qp_stats.enqueued_count += enqueued_ops;
344 *accumulated_enqueued_ops += enqueued_ops;
350 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
353 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
354 struct rte_crypto_op *curr_c_op;
356 struct zuc_session *curr_sess;
357 struct zuc_session *sessions[ZUC_MAX_BURST];
358 enum zuc_operation prev_zuc_op = ZUC_OP_NOT_SUPPORTED;
359 enum zuc_operation curr_zuc_op;
360 struct zuc_qp *qp = queue_pair;
362 uint8_t burst_size = 0;
363 uint16_t enqueued_ops = 0;
364 uint8_t processed_ops;
366 for (i = 0; i < nb_ops; i++) {
369 curr_sess = zuc_get_session(qp, curr_c_op);
370 if (unlikely(curr_sess == NULL)) {
372 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
376 curr_zuc_op = curr_sess->op;
379 * Batch ops that share the same operation type
380 * (cipher only, auth only...).
382 if (burst_size == 0) {
383 prev_zuc_op = curr_zuc_op;
384 c_ops[0] = curr_c_op;
385 sessions[0] = curr_sess;
387 } else if (curr_zuc_op == prev_zuc_op) {
388 c_ops[burst_size] = curr_c_op;
389 sessions[burst_size] = curr_sess;
392 * When there are enough ops to process in a batch,
393 * process them, and start a new batch.
395 if (burst_size == ZUC_MAX_BURST) {
396 processed_ops = process_ops(c_ops, curr_zuc_op,
397 sessions, qp, burst_size,
399 if (processed_ops < burst_size) {
408 * Different operation type, process the ops
409 * of the previous type.
411 processed_ops = process_ops(c_ops, prev_zuc_op,
412 sessions, qp, burst_size,
414 if (processed_ops < burst_size) {
420 prev_zuc_op = curr_zuc_op;
422 c_ops[0] = curr_c_op;
423 sessions[0] = curr_sess;
428 if (burst_size != 0) {
429 /* Process the crypto ops of the last operation type. */
430 processed_ops = process_ops(c_ops, prev_zuc_op,
431 sessions, qp, burst_size,
435 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
440 zuc_pmd_dequeue_burst(void *queue_pair,
441 struct rte_crypto_op **c_ops, uint16_t nb_ops)
443 struct zuc_qp *qp = queue_pair;
445 unsigned nb_dequeued;
447 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
448 (void **)c_ops, nb_ops, NULL);
449 qp->qp_stats.dequeued_count += nb_dequeued;
454 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
457 cryptodev_zuc_create(const char *name,
458 struct rte_vdev_device *vdev,
459 struct rte_cryptodev_pmd_init_params *init_params)
461 struct rte_cryptodev *dev;
462 struct zuc_private *internals;
465 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
467 ZUC_LOG(ERR, "failed to create cryptodev vdev");
471 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
472 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
473 RTE_CRYPTODEV_FF_SYM_SESSIONLESS;
475 mb_mgr = alloc_mb_mgr(0);
479 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F)) {
480 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX512;
481 init_mb_mgr_avx512(mb_mgr);
482 } else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2)) {
483 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
484 init_mb_mgr_avx2(mb_mgr);
485 } else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX)) {
486 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
487 init_mb_mgr_avx(mb_mgr);
489 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
490 init_mb_mgr_sse(mb_mgr);
493 dev->driver_id = cryptodev_driver_id;
494 dev->dev_ops = rte_zuc_pmd_ops;
496 /* Register RX/TX burst functions for data path. */
497 dev->dequeue_burst = zuc_pmd_dequeue_burst;
498 dev->enqueue_burst = zuc_pmd_enqueue_burst;
500 internals = dev->data->dev_private;
501 internals->mb_mgr = mb_mgr;
503 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
507 ZUC_LOG(ERR, "driver %s: failed",
510 cryptodev_zuc_remove(vdev);
515 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
517 struct rte_cryptodev_pmd_init_params init_params = {
519 sizeof(struct zuc_private),
521 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
524 const char *input_args;
526 name = rte_vdev_device_name(vdev);
529 input_args = rte_vdev_device_args(vdev);
531 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
533 return cryptodev_zuc_create(name, vdev, &init_params);
537 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
540 struct rte_cryptodev *cryptodev;
542 struct zuc_private *internals;
544 name = rte_vdev_device_name(vdev);
548 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
549 if (cryptodev == NULL)
552 internals = cryptodev->data->dev_private;
554 free_mb_mgr(internals->mb_mgr);
556 return rte_cryptodev_pmd_destroy(cryptodev);
559 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
560 .probe = cryptodev_zuc_probe,
561 .remove = cryptodev_zuc_remove
564 static struct cryptodev_driver zuc_crypto_drv;
566 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
567 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
568 "max_nb_queue_pairs=<int> "
570 RTE_PMD_REGISTER_CRYPTO_DRIVER(zuc_crypto_drv, cryptodev_zuc_pmd_drv.driver,
571 cryptodev_driver_id);
573 RTE_INIT(zuc_init_log)
575 zuc_logtype_driver = rte_log_register("pmd.crypto.zuc");