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>
41 #include <rte_kvargs.h>
43 #include "rte_snow3g_pmd_private.h"
45 #define SNOW3G_MAX_BURST 8
48 * Global static parameter used to create a unique name for each SNOW 3G
51 static unsigned unique_name_id;
54 create_unique_device_name(char *name, size_t size)
61 ret = snprintf(name, size, "%s_%u", CRYPTODEV_NAME_SNOW3G_PMD,
68 /** Get xform chain order. */
69 static enum snow3g_operation
70 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
73 return SNOW3G_OP_NOT_SUPPORTED;
76 if (xform->next->next != NULL)
77 return SNOW3G_OP_NOT_SUPPORTED;
79 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
80 if (xform->next == NULL)
81 return SNOW3G_OP_ONLY_AUTH;
82 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
83 return SNOW3G_OP_AUTH_CIPHER;
85 return SNOW3G_OP_NOT_SUPPORTED;
88 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
89 if (xform->next == NULL)
90 return SNOW3G_OP_ONLY_CIPHER;
91 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
92 return SNOW3G_OP_CIPHER_AUTH;
94 return SNOW3G_OP_NOT_SUPPORTED;
97 return SNOW3G_OP_NOT_SUPPORTED;
101 /** Parse crypto xform chain and set private session parameters. */
103 snow3g_set_session_parameters(struct snow3g_session *sess,
104 const struct rte_crypto_sym_xform *xform)
106 const struct rte_crypto_sym_xform *auth_xform = NULL;
107 const struct rte_crypto_sym_xform *cipher_xform = NULL;
110 /* Select Crypto operation - hash then cipher / cipher then hash */
111 mode = snow3g_get_mode(xform);
114 case SNOW3G_OP_CIPHER_AUTH:
115 auth_xform = xform->next;
118 case SNOW3G_OP_ONLY_CIPHER:
119 cipher_xform = xform;
121 case SNOW3G_OP_AUTH_CIPHER:
122 cipher_xform = xform->next;
124 case SNOW3G_OP_ONLY_AUTH:
128 if (mode == SNOW3G_OP_NOT_SUPPORTED) {
129 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
134 /* Only SNOW 3G UEA2 supported */
135 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
138 sso_snow3g_init_key_sched(xform->cipher.key.data,
139 &sess->pKeySched_cipher);
143 /* Only SNOW 3G UIA2 supported */
144 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
146 sess->auth_op = auth_xform->auth.op;
148 sso_snow3g_init_key_sched(xform->auth.key.data,
149 &sess->pKeySched_hash);
158 /** Get SNOW 3G session. */
159 static struct snow3g_session *
160 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
162 struct snow3g_session *sess;
164 if (op->sym->type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
165 if (unlikely(op->sym->session->type !=
166 RTE_CRYPTODEV_SNOW3G_PMD))
169 sess = (struct snow3g_session *)op->sym->session->_private;
171 struct rte_cryptodev_session *c_sess = NULL;
173 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
176 sess = (struct snow3g_session *)c_sess->_private;
178 if (unlikely(snow3g_set_session_parameters(sess,
179 op->sym->xform) != 0))
186 /** Encrypt/decrypt mbufs with same cipher key. */
188 process_snow3g_cipher_op(struct rte_crypto_op **ops,
189 struct snow3g_session *session,
193 uint8_t processed_ops = 0;
194 uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
195 uint8_t *IV[SNOW3G_MAX_BURST];
196 uint32_t num_bytes[SNOW3G_MAX_BURST];
198 for (i = 0; i < num_ops; i++) {
200 if (ops[i]->sym->cipher.iv.length != 16) {
201 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
202 SNOW3G_LOG_ERR("iv");
206 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
207 ops[i]->sym->cipher.data.offset;
208 dst[i] = ops[i]->sym->m_dst ?
209 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
210 ops[i]->sym->cipher.data.offset :
211 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
212 ops[i]->sym->cipher.data.offset;
213 IV[i] = ops[i]->sym->cipher.iv.data;
214 num_bytes[i] = ops[i]->sym->cipher.data.length;
219 sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, IV, src, dst,
220 num_bytes, processed_ops);
222 return processed_ops;
225 /** Generate/verify hash from mbufs with same hash key. */
227 process_snow3g_hash_op(struct rte_crypto_op **ops,
228 struct snow3g_session *session,
232 uint8_t processed_ops = 0;
234 uint32_t length_in_bits;
236 for (i = 0; i < num_ops; i++) {
237 if (ops[i]->sym->auth.aad.length != 16) {
238 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
239 SNOW3G_LOG_ERR("aad");
243 if (ops[i]->sym->auth.digest.length != 4) {
244 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
245 SNOW3G_LOG_ERR("digest");
249 length_in_bits = ops[i]->sym->auth.data.length * 8;
251 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
252 ops[i]->sym->auth.data.offset;
254 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
255 dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
256 ops[i]->sym->auth.digest.length);
258 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
259 ops[i]->sym->auth.aad.data, src,
260 length_in_bits, dst);
262 if (memcmp(dst, ops[i]->sym->auth.digest.data,
263 ops[i]->sym->auth.digest.length) != 0)
264 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
266 /* Trim area used for digest from mbuf. */
267 rte_pktmbuf_trim(ops[i]->sym->m_src,
268 ops[i]->sym->auth.digest.length);
270 dst = ops[i]->sym->auth.digest.data;
272 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
273 ops[i]->sym->auth.aad.data, src,
274 length_in_bits, dst);
279 return processed_ops;
282 /** Process a batch of crypto ops which shares the same session. */
284 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
285 struct snow3g_qp *qp, uint8_t num_ops)
288 unsigned processed_ops;
290 switch (session->op) {
291 case SNOW3G_OP_ONLY_CIPHER:
292 processed_ops = process_snow3g_cipher_op(ops,
295 case SNOW3G_OP_ONLY_AUTH:
296 processed_ops = process_snow3g_hash_op(ops, session,
299 case SNOW3G_OP_CIPHER_AUTH:
300 processed_ops = process_snow3g_cipher_op(ops, session,
302 process_snow3g_hash_op(ops, session, processed_ops);
304 case SNOW3G_OP_AUTH_CIPHER:
305 processed_ops = process_snow3g_hash_op(ops, session,
307 process_snow3g_cipher_op(ops, session, processed_ops);
310 /* Operation not supported. */
314 for (i = 0; i < num_ops; i++) {
316 * If there was no error/authentication failure,
317 * change status to successful.
319 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
320 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
321 /* Free session if a session-less crypto op. */
322 if (ops[i]->sym->type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
323 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
324 ops[i]->sym->session = NULL;
328 return processed_ops;
332 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
335 struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
336 struct rte_crypto_op *curr_c_op;
338 struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
339 struct snow3g_qp *qp = queue_pair;
341 uint8_t burst_size = 0;
342 uint16_t enqueued_ops = 0;
343 uint8_t processed_ops;
345 for (i = 0; i < nb_ops; i++) {
348 /* Set status as enqueued (not processed yet) by default. */
349 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
351 curr_sess = snow3g_get_session(qp, curr_c_op);
352 if (unlikely(curr_sess == NULL ||
353 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
355 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
356 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
360 /* Batch ops that share the same session. */
361 if (prev_sess == NULL) {
362 prev_sess = curr_sess;
363 c_ops[burst_size++] = curr_c_op;
364 } else if (curr_sess == prev_sess) {
365 c_ops[burst_size++] = curr_c_op;
367 * When there are enough ops to process in a batch,
368 * process them, and start a new batch.
370 if (burst_size == SNOW3G_MAX_BURST) {
371 processed_ops = process_ops(c_ops,
372 prev_sess, qp, burst_size);
373 n = rte_ring_enqueue_burst(qp->processed_ops,
376 qp->qp_stats.enqueued_count += n;
378 if (n < burst_size) {
379 qp->qp_stats.enqueue_err_count +=
380 nb_ops - enqueued_ops;
389 * Different session, process the ops
390 * of the previous session.
392 processed_ops = process_ops(c_ops,
393 prev_sess, qp, burst_size);
394 n = rte_ring_enqueue_burst(qp->processed_ops,
397 qp->qp_stats.enqueued_count += n;
399 if (n < burst_size) {
400 qp->qp_stats.enqueue_err_count +=
401 nb_ops - enqueued_ops;
406 prev_sess = curr_sess;
407 c_ops[burst_size++] = curr_c_op;
411 if (burst_size != 0) {
412 /* Process the crypto ops of the last session. */
413 processed_ops = process_ops(c_ops,
414 prev_sess, qp, burst_size);
415 n = rte_ring_enqueue_burst(qp->processed_ops,
418 qp->qp_stats.enqueued_count += n;
420 if (n < burst_size) {
421 qp->qp_stats.enqueue_err_count +=
422 nb_ops - enqueued_ops;
431 snow3g_pmd_dequeue_burst(void *queue_pair,
432 struct rte_crypto_op **c_ops, uint16_t nb_ops)
434 struct snow3g_qp *qp = queue_pair;
436 unsigned nb_dequeued;
438 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
439 (void **)c_ops, nb_ops);
440 qp->qp_stats.dequeued_count += nb_dequeued;
445 static int cryptodev_snow3g_uninit(const char *name);
448 cryptodev_snow3g_create(const char *name,
449 struct rte_crypto_vdev_init_params *init_params)
451 struct rte_cryptodev *dev;
452 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
453 struct snow3g_private *internals;
455 /* Create a unique device name. */
456 if (create_unique_device_name(crypto_dev_name,
457 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
458 SNOW3G_LOG_ERR("failed to create unique cryptodev name");
462 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
463 sizeof(struct snow3g_private), init_params->socket_id);
465 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
469 dev->dev_type = RTE_CRYPTODEV_SNOW3G_PMD;
470 dev->dev_ops = rte_snow3g_pmd_ops;
472 /* Register RX/TX burst functions for data path. */
473 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
474 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
476 internals = dev->data->dev_private;
478 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
479 internals->max_nb_sessions = init_params->max_nb_sessions;
483 SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed", name);
485 cryptodev_snow3g_uninit(crypto_dev_name);
490 cryptodev_snow3g_init(const char *name,
491 const char *input_args)
493 struct rte_crypto_vdev_init_params init_params = {
494 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
495 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
499 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
501 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
502 init_params.socket_id);
503 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
504 init_params.max_nb_queue_pairs);
505 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
506 init_params.max_nb_sessions);
508 return cryptodev_snow3g_create(name, &init_params);
512 cryptodev_snow3g_uninit(const char *name)
517 RTE_LOG(INFO, PMD, "Closing SNOW3G crypto device %s"
518 " on numa socket %u\n",
519 name, rte_socket_id());
524 static struct rte_driver cryptodev_snow3g_pmd_drv = {
525 .name = CRYPTODEV_NAME_SNOW3G_PMD,
527 .init = cryptodev_snow3g_init,
528 .uninit = cryptodev_snow3g_uninit
531 PMD_REGISTER_DRIVER(cryptodev_snow3g_pmd_drv);