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_zuc_pmd_private.h"
45 #define ZUC_DIGEST_LENGTH 4
46 #define ZUC_MAX_BURST 8
49 /** Get xform chain order. */
50 static enum zuc_operation
51 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
54 return ZUC_OP_NOT_SUPPORTED;
57 if (xform->next->next != NULL)
58 return ZUC_OP_NOT_SUPPORTED;
60 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
61 if (xform->next == NULL)
62 return ZUC_OP_ONLY_AUTH;
63 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
64 return ZUC_OP_AUTH_CIPHER;
66 return ZUC_OP_NOT_SUPPORTED;
69 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
70 if (xform->next == NULL)
71 return ZUC_OP_ONLY_CIPHER;
72 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
73 return ZUC_OP_CIPHER_AUTH;
75 return ZUC_OP_NOT_SUPPORTED;
78 return ZUC_OP_NOT_SUPPORTED;
82 /** Parse crypto xform chain and set private session parameters. */
84 zuc_set_session_parameters(struct zuc_session *sess,
85 const struct rte_crypto_sym_xform *xform)
87 const struct rte_crypto_sym_xform *auth_xform = NULL;
88 const struct rte_crypto_sym_xform *cipher_xform = NULL;
89 enum zuc_operation mode;
91 /* Select Crypto operation - hash then cipher / cipher then hash */
92 mode = zuc_get_mode(xform);
95 case ZUC_OP_CIPHER_AUTH:
96 auth_xform = xform->next;
99 case ZUC_OP_ONLY_CIPHER:
100 cipher_xform = xform;
102 case ZUC_OP_AUTH_CIPHER:
103 cipher_xform = xform->next;
105 case ZUC_OP_ONLY_AUTH:
108 case ZUC_OP_NOT_SUPPORTED:
110 ZUC_LOG_ERR("Unsupported operation chain order parameter");
115 /* Only ZUC EEA3 supported */
116 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
119 if (cipher_xform->cipher.iv.length != ZUC_IV_KEY_LENGTH) {
120 ZUC_LOG_ERR("Wrong IV length");
123 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
126 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
131 /* Only ZUC EIA3 supported */
132 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
135 if (auth_xform->auth.digest_length != ZUC_DIGEST_LENGTH) {
136 ZUC_LOG_ERR("Wrong digest length");
140 sess->auth_op = auth_xform->auth.op;
142 if (auth_xform->auth.iv.length != ZUC_IV_KEY_LENGTH) {
143 ZUC_LOG_ERR("Wrong IV length");
146 sess->auth_iv_offset = auth_xform->auth.iv.offset;
149 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
159 /** Get ZUC session. */
160 static struct zuc_session *
161 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
163 struct zuc_session *sess;
165 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
166 if (unlikely(op->sym->session->dev_type !=
167 RTE_CRYPTODEV_ZUC_PMD))
170 sess = (struct zuc_session *)op->sym->session->_private;
172 struct rte_cryptodev_session *c_sess = NULL;
174 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
177 sess = (struct zuc_session *)c_sess->_private;
179 if (unlikely(zuc_set_session_parameters(sess,
180 op->sym->xform) != 0))
187 /** Encrypt/decrypt mbufs with same cipher key. */
189 process_zuc_cipher_op(struct rte_crypto_op **ops,
190 struct zuc_session *session,
194 uint8_t processed_ops = 0;
195 uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
196 uint8_t *iv[ZUC_MAX_BURST];
197 uint32_t num_bytes[ZUC_MAX_BURST];
198 uint8_t *cipher_keys[ZUC_MAX_BURST];
200 for (i = 0; i < num_ops; i++) {
201 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
202 || ((ops[i]->sym->cipher.data.offset
204 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
205 ZUC_LOG_ERR("Data Length or offset");
209 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
210 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
211 (ops[i]->sym->m_dst != NULL &&
212 !rte_pktmbuf_is_contiguous(
213 ops[i]->sym->m_dst))) {
214 ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
215 "op (%p) provides noncontiguous mbuf as "
216 "source/destination buffer.\n", ops[i]);
217 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
222 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
223 (ops[i]->sym->cipher.data.offset >> 3);
224 dst[i] = ops[i]->sym->m_dst ?
225 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
226 (ops[i]->sym->cipher.data.offset >> 3) :
227 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
228 (ops[i]->sym->cipher.data.offset >> 3);
229 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
230 session->cipher_iv_offset);
231 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
233 cipher_keys[i] = session->pKey_cipher;
238 sso_zuc_eea3_n_buffer(cipher_keys, iv, src, dst,
239 num_bytes, processed_ops);
241 return processed_ops;
244 /** Generate/verify hash from mbufs with same hash key. */
246 process_zuc_hash_op(struct rte_crypto_op **ops,
247 struct zuc_session *session,
251 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 ZUC_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 = (uint32_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
276 sso_zuc_eia3_1_buffer(session->pKey_hash,
278 length_in_bits, dst);
280 if (memcmp(dst, ops[i]->sym->auth.digest.data,
281 ZUC_DIGEST_LENGTH) != 0)
282 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
284 /* Trim area used for digest from mbuf. */
285 rte_pktmbuf_trim(ops[i]->sym->m_src,
288 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
290 sso_zuc_eia3_1_buffer(session->pKey_hash,
292 length_in_bits, dst);
297 return processed_ops;
300 /** Process a batch of crypto ops which shares the same session. */
302 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
303 struct zuc_qp *qp, uint8_t num_ops,
304 uint16_t *accumulated_enqueued_ops)
307 unsigned enqueued_ops, processed_ops;
309 switch (session->op) {
310 case ZUC_OP_ONLY_CIPHER:
311 processed_ops = process_zuc_cipher_op(ops,
314 case ZUC_OP_ONLY_AUTH:
315 processed_ops = process_zuc_hash_op(ops, session,
318 case ZUC_OP_CIPHER_AUTH:
319 processed_ops = process_zuc_cipher_op(ops, session,
321 process_zuc_hash_op(ops, session, processed_ops);
323 case ZUC_OP_AUTH_CIPHER:
324 processed_ops = process_zuc_hash_op(ops, session,
326 process_zuc_cipher_op(ops, session, processed_ops);
329 /* Operation not supported. */
333 for (i = 0; i < num_ops; i++) {
335 * If there was no error/authentication failure,
336 * change status to successful.
338 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
339 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
340 /* Free session if a session-less crypto op. */
341 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
342 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
343 ops[i]->sym->session = NULL;
347 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
348 (void **)ops, processed_ops, NULL);
349 qp->qp_stats.enqueued_count += enqueued_ops;
350 *accumulated_enqueued_ops += enqueued_ops;
356 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
359 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
360 struct rte_crypto_op *curr_c_op;
362 struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
363 struct zuc_qp *qp = queue_pair;
365 uint8_t burst_size = 0;
366 uint16_t enqueued_ops = 0;
367 uint8_t processed_ops;
369 for (i = 0; i < nb_ops; i++) {
372 /* Set status as enqueued (not processed yet) by default. */
373 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
375 curr_sess = zuc_get_session(qp, curr_c_op);
376 if (unlikely(curr_sess == NULL ||
377 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
379 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
383 /* Batch ops that share the same session. */
384 if (prev_sess == NULL) {
385 prev_sess = curr_sess;
386 c_ops[burst_size++] = curr_c_op;
387 } else if (curr_sess == prev_sess) {
388 c_ops[burst_size++] = curr_c_op;
390 * When there are enough ops to process in a batch,
391 * process them, and start a new batch.
393 if (burst_size == ZUC_MAX_BURST) {
394 processed_ops = process_ops(c_ops, prev_sess,
395 qp, burst_size, &enqueued_ops);
396 if (processed_ops < burst_size) {
406 * Different session, process the ops
407 * of the previous session.
409 processed_ops = process_ops(c_ops, prev_sess,
410 qp, burst_size, &enqueued_ops);
411 if (processed_ops < burst_size) {
417 prev_sess = curr_sess;
419 c_ops[burst_size++] = curr_c_op;
423 if (burst_size != 0) {
424 /* Process the crypto ops of the last session. */
425 processed_ops = process_ops(c_ops, prev_sess,
426 qp, burst_size, &enqueued_ops);
429 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
434 zuc_pmd_dequeue_burst(void *queue_pair,
435 struct rte_crypto_op **c_ops, uint16_t nb_ops)
437 struct zuc_qp *qp = queue_pair;
439 unsigned nb_dequeued;
441 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
442 (void **)c_ops, nb_ops, NULL);
443 qp->qp_stats.dequeued_count += nb_dequeued;
448 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
451 cryptodev_zuc_create(const char *name,
452 struct rte_vdev_device *vdev,
453 struct rte_crypto_vdev_init_params *init_params)
455 struct rte_cryptodev *dev;
456 struct zuc_private *internals;
457 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
459 if (init_params->name[0] == '\0')
460 snprintf(init_params->name, sizeof(init_params->name),
463 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
464 sizeof(struct zuc_private), init_params->socket_id,
467 ZUC_LOG_ERR("failed to create cryptodev vdev");
471 dev->dev_type = RTE_CRYPTODEV_ZUC_PMD;
472 dev->dev_ops = rte_zuc_pmd_ops;
474 /* Register RX/TX burst functions for data path. */
475 dev->dequeue_burst = zuc_pmd_dequeue_burst;
476 dev->enqueue_burst = zuc_pmd_enqueue_burst;
478 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
479 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
482 internals = dev->data->dev_private;
484 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
485 internals->max_nb_sessions = init_params->max_nb_sessions;
489 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
492 cryptodev_zuc_remove(vdev);
497 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
499 struct rte_crypto_vdev_init_params init_params = {
500 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
501 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
506 const char *input_args;
508 name = rte_vdev_device_name(vdev);
511 input_args = rte_vdev_device_args(vdev);
513 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
515 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
516 init_params.socket_id);
517 if (init_params.name[0] != '\0')
518 RTE_LOG(INFO, PMD, " User defined name = %s\n",
520 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
521 init_params.max_nb_queue_pairs);
522 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
523 init_params.max_nb_sessions);
525 return cryptodev_zuc_create(name, vdev, &init_params);
529 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
533 name = rte_vdev_device_name(vdev);
537 RTE_LOG(INFO, PMD, "Closing ZUC crypto device %s"
538 " on numa socket %u\n",
539 name, rte_socket_id());
544 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
545 .probe = cryptodev_zuc_probe,
546 .remove = cryptodev_zuc_remove
549 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
550 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
551 "max_nb_queue_pairs=<int> "
552 "max_nb_sessions=<int> "