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_zuc_pmd_private.h"
44 #define ZUC_DIGEST_LENGTH 4
45 #define ZUC_MAX_BURST 8
49 * Global static parameter used to create a unique name for each ZUC
52 static unsigned unique_name_id;
55 create_unique_device_name(char *name, size_t size)
62 ret = snprintf(name, size, "%s_%u", RTE_STR(CRYPTODEV_NAME_ZUC_PMD),
69 /** Get xform chain order. */
70 static enum zuc_operation
71 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
74 return ZUC_OP_NOT_SUPPORTED;
77 if (xform->next->next != NULL)
78 return ZUC_OP_NOT_SUPPORTED;
80 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
81 if (xform->next == NULL)
82 return ZUC_OP_ONLY_AUTH;
83 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
84 return ZUC_OP_AUTH_CIPHER;
86 return ZUC_OP_NOT_SUPPORTED;
89 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
90 if (xform->next == NULL)
91 return ZUC_OP_ONLY_CIPHER;
92 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
93 return ZUC_OP_CIPHER_AUTH;
95 return ZUC_OP_NOT_SUPPORTED;
98 return ZUC_OP_NOT_SUPPORTED;
102 /** Parse crypto xform chain and set private session parameters. */
104 zuc_set_session_parameters(struct zuc_session *sess,
105 const struct rte_crypto_sym_xform *xform)
107 const struct rte_crypto_sym_xform *auth_xform = NULL;
108 const struct rte_crypto_sym_xform *cipher_xform = NULL;
109 enum zuc_operation mode;
111 /* Select Crypto operation - hash then cipher / cipher then hash */
112 mode = zuc_get_mode(xform);
115 case ZUC_OP_CIPHER_AUTH:
116 auth_xform = xform->next;
119 case ZUC_OP_ONLY_CIPHER:
120 cipher_xform = xform;
122 case ZUC_OP_AUTH_CIPHER:
123 cipher_xform = xform->next;
125 case ZUC_OP_ONLY_AUTH:
128 case ZUC_OP_NOT_SUPPORTED:
130 ZUC_LOG_ERR("Unsupported operation chain order parameter");
135 /* Only ZUC EEA3 supported */
136 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
139 memcpy(sess->pKey_cipher, xform->cipher.key.data, ZUC_IV_KEY_LENGTH);
143 /* Only ZUC EIA3 supported */
144 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
146 sess->auth_op = auth_xform->auth.op;
148 memcpy(sess->pKey_hash, xform->auth.key.data, ZUC_IV_KEY_LENGTH);
157 /** Get ZUC session. */
158 static struct zuc_session *
159 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
161 struct zuc_session *sess;
163 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
164 if (unlikely(op->sym->session->dev_type !=
165 RTE_CRYPTODEV_ZUC_PMD))
168 sess = (struct zuc_session *)op->sym->session->_private;
170 struct rte_cryptodev_session *c_sess = NULL;
172 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
175 sess = (struct zuc_session *)c_sess->_private;
177 if (unlikely(zuc_set_session_parameters(sess,
178 op->sym->xform) != 0))
185 /** Encrypt/decrypt mbufs with same cipher key. */
187 process_zuc_cipher_op(struct rte_crypto_op **ops,
188 struct zuc_session *session,
192 uint8_t processed_ops = 0;
193 uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
194 uint8_t *IV[ZUC_MAX_BURST];
195 uint32_t num_bytes[ZUC_MAX_BURST];
196 uint8_t *cipher_keys[ZUC_MAX_BURST];
198 for (i = 0; i < num_ops; i++) {
200 if (unlikely(ops[i]->sym->cipher.iv.length != ZUC_IV_KEY_LENGTH)) {
201 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
206 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
207 || ((ops[i]->sym->cipher.data.offset
209 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
210 ZUC_LOG_ERR("Data Length or offset");
214 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
215 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
216 (ops[i]->sym->m_dst != NULL &&
217 !rte_pktmbuf_is_contiguous(
218 ops[i]->sym->m_dst))) {
219 ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
220 "op (%p) provides noncontiguous mbuf as "
221 "source/destination buffer.\n", ops[i]);
222 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
227 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
228 (ops[i]->sym->cipher.data.offset >> 3);
229 dst[i] = ops[i]->sym->m_dst ?
230 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
231 (ops[i]->sym->cipher.data.offset >> 3) :
232 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
233 (ops[i]->sym->cipher.data.offset >> 3);
234 IV[i] = ops[i]->sym->cipher.iv.data;
235 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
237 cipher_keys[i] = session->pKey_cipher;
242 sso_zuc_eea3_n_buffer(cipher_keys, IV, src, dst,
243 num_bytes, processed_ops);
245 return processed_ops;
248 /** Generate/verify hash from mbufs with same hash key. */
250 process_zuc_hash_op(struct rte_crypto_op **ops,
251 struct zuc_session *session,
255 uint8_t processed_ops = 0;
258 uint32_t length_in_bits;
260 for (i = 0; i < num_ops; i++) {
261 if (unlikely(ops[i]->sym->auth.aad.length != ZUC_IV_KEY_LENGTH)) {
262 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
267 if (unlikely(ops[i]->sym->auth.digest.length != ZUC_DIGEST_LENGTH)) {
268 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
269 ZUC_LOG_ERR("digest");
273 /* Data must be byte aligned */
274 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
275 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
276 ZUC_LOG_ERR("Offset");
280 length_in_bits = ops[i]->sym->auth.data.length;
282 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
283 (ops[i]->sym->auth.data.offset >> 3);
285 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
286 dst = (uint32_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
287 ops[i]->sym->auth.digest.length);
289 sso_zuc_eia3_1_buffer(session->pKey_hash,
290 ops[i]->sym->auth.aad.data, src,
291 length_in_bits, dst);
293 if (memcmp(dst, ops[i]->sym->auth.digest.data,
294 ops[i]->sym->auth.digest.length) != 0)
295 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
297 /* Trim area used for digest from mbuf. */
298 rte_pktmbuf_trim(ops[i]->sym->m_src,
299 ops[i]->sym->auth.digest.length);
301 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
303 sso_zuc_eia3_1_buffer(session->pKey_hash,
304 ops[i]->sym->auth.aad.data, src,
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 zuc_session *session,
316 struct zuc_qp *qp, uint8_t num_ops,
317 uint16_t *accumulated_enqueued_ops)
320 unsigned enqueued_ops, processed_ops;
322 switch (session->op) {
323 case ZUC_OP_ONLY_CIPHER:
324 processed_ops = process_zuc_cipher_op(ops,
327 case ZUC_OP_ONLY_AUTH:
328 processed_ops = process_zuc_hash_op(ops, session,
331 case ZUC_OP_CIPHER_AUTH:
332 processed_ops = process_zuc_cipher_op(ops, session,
334 process_zuc_hash_op(ops, session, processed_ops);
336 case ZUC_OP_AUTH_CIPHER:
337 processed_ops = process_zuc_hash_op(ops, session,
339 process_zuc_cipher_op(ops, session, processed_ops);
342 /* Operation not supported. */
346 for (i = 0; i < num_ops; i++) {
348 * If there was no error/authentication failure,
349 * change status to successful.
351 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
352 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
353 /* Free session if a session-less crypto op. */
354 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
355 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
356 ops[i]->sym->session = NULL;
360 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
361 (void **)ops, processed_ops);
362 qp->qp_stats.enqueued_count += enqueued_ops;
363 *accumulated_enqueued_ops += enqueued_ops;
369 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
372 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
373 struct rte_crypto_op *curr_c_op;
375 struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
376 struct zuc_qp *qp = queue_pair;
378 uint8_t burst_size = 0;
379 uint16_t enqueued_ops = 0;
380 uint8_t processed_ops;
382 for (i = 0; i < nb_ops; i++) {
385 /* Set status as enqueued (not processed yet) by default. */
386 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
388 curr_sess = zuc_get_session(qp, curr_c_op);
389 if (unlikely(curr_sess == NULL ||
390 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
392 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
396 /* Batch ops that share the same session. */
397 if (prev_sess == NULL) {
398 prev_sess = curr_sess;
399 c_ops[burst_size++] = curr_c_op;
400 } else if (curr_sess == prev_sess) {
401 c_ops[burst_size++] = curr_c_op;
403 * When there are enough ops to process in a batch,
404 * process them, and start a new batch.
406 if (burst_size == ZUC_MAX_BURST) {
407 processed_ops = process_ops(c_ops, prev_sess,
408 qp, burst_size, &enqueued_ops);
409 if (processed_ops < burst_size) {
419 * Different session, process the ops
420 * of the previous session.
422 processed_ops = process_ops(c_ops, prev_sess,
423 qp, burst_size, &enqueued_ops);
424 if (processed_ops < burst_size) {
430 prev_sess = curr_sess;
432 c_ops[burst_size++] = curr_c_op;
436 if (burst_size != 0) {
437 /* Process the crypto ops of the last session. */
438 processed_ops = process_ops(c_ops, prev_sess,
439 qp, burst_size, &enqueued_ops);
442 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
447 zuc_pmd_dequeue_burst(void *queue_pair,
448 struct rte_crypto_op **c_ops, uint16_t nb_ops)
450 struct zuc_qp *qp = queue_pair;
452 unsigned nb_dequeued;
454 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
455 (void **)c_ops, nb_ops);
456 qp->qp_stats.dequeued_count += nb_dequeued;
461 static int cryptodev_zuc_remove(const char *name);
464 cryptodev_zuc_create(const char *name,
465 struct rte_crypto_vdev_init_params *init_params)
467 struct rte_cryptodev *dev;
468 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
469 struct zuc_private *internals;
470 uint64_t cpu_flags = 0;
472 /* Check CPU for supported vector instruction set */
473 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
474 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
476 ZUC_LOG_ERR("Vector instructions are not supported by CPU");
481 /* Create a unique device name. */
482 if (create_unique_device_name(crypto_dev_name,
483 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
484 ZUC_LOG_ERR("failed to create unique cryptodev name");
488 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
489 sizeof(struct zuc_private), init_params->socket_id);
491 ZUC_LOG_ERR("failed to create cryptodev vdev");
495 dev->dev_type = RTE_CRYPTODEV_ZUC_PMD;
496 dev->dev_ops = rte_zuc_pmd_ops;
498 /* Register RX/TX burst functions for data path. */
499 dev->dequeue_burst = zuc_pmd_dequeue_burst;
500 dev->enqueue_burst = zuc_pmd_enqueue_burst;
502 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
503 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
506 internals = dev->data->dev_private;
508 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
509 internals->max_nb_sessions = init_params->max_nb_sessions;
513 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed", name);
515 cryptodev_zuc_remove(crypto_dev_name);
520 cryptodev_zuc_probe(const char *name,
521 const char *input_args)
523 struct rte_crypto_vdev_init_params init_params = {
524 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
525 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
529 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
531 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
532 init_params.socket_id);
533 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
534 init_params.max_nb_queue_pairs);
535 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
536 init_params.max_nb_sessions);
538 return cryptodev_zuc_create(name, &init_params);
542 cryptodev_zuc_remove(const char *name)
547 RTE_LOG(INFO, PMD, "Closing ZUC crypto device %s"
548 " on numa socket %u\n",
549 name, rte_socket_id());
554 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
555 .probe = cryptodev_zuc_probe,
556 .remove = cryptodev_zuc_remove
559 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
560 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
561 "max_nb_queue_pairs=<int> "
562 "max_nb_sessions=<int> "