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
49 * Global static parameter used to create a unique name for each SNOW 3G
52 static unsigned unique_name_id;
55 create_unique_device_name(char *name, size_t size)
62 ret = snprintf(name, size, "%s_%u", CRYPTODEV_NAME_SNOW3G_PMD,
69 /** Get xform chain order. */
70 static enum snow3g_operation
71 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
74 return SNOW3G_OP_NOT_SUPPORTED;
77 if (xform->next->next != NULL)
78 return SNOW3G_OP_NOT_SUPPORTED;
80 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
81 if (xform->next == NULL)
82 return SNOW3G_OP_ONLY_AUTH;
83 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
84 return SNOW3G_OP_AUTH_CIPHER;
86 return SNOW3G_OP_NOT_SUPPORTED;
89 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
90 if (xform->next == NULL)
91 return SNOW3G_OP_ONLY_CIPHER;
92 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
93 return SNOW3G_OP_CIPHER_AUTH;
95 return SNOW3G_OP_NOT_SUPPORTED;
98 return SNOW3G_OP_NOT_SUPPORTED;
102 /** Parse crypto xform chain and set private session parameters. */
104 snow3g_set_session_parameters(struct snow3g_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;
111 /* Select Crypto operation - hash then cipher / cipher then hash */
112 mode = snow3g_get_mode(xform);
115 case SNOW3G_OP_CIPHER_AUTH:
116 auth_xform = xform->next;
119 case SNOW3G_OP_ONLY_CIPHER:
120 cipher_xform = xform;
122 case SNOW3G_OP_AUTH_CIPHER:
123 cipher_xform = xform->next;
125 case SNOW3G_OP_ONLY_AUTH:
129 if (mode == SNOW3G_OP_NOT_SUPPORTED) {
130 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
135 /* Only SNOW 3G UEA2 supported */
136 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
139 sso_snow3g_init_key_sched(xform->cipher.key.data,
140 &sess->pKeySched_cipher);
144 /* Only SNOW 3G UIA2 supported */
145 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
147 sess->auth_op = auth_xform->auth.op;
149 sso_snow3g_init_key_sched(xform->auth.key.data,
150 &sess->pKeySched_hash);
159 /** Get SNOW 3G session. */
160 static struct snow3g_session *
161 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
163 struct snow3g_session *sess;
165 if (op->sym->type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
166 if (unlikely(op->sym->session->type !=
167 RTE_CRYPTODEV_SNOW3G_PMD))
170 sess = (struct snow3g_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 snow3g_session *)c_sess->_private;
179 if (unlikely(snow3g_set_session_parameters(sess,
180 op->sym->xform) != 0))
187 /** Encrypt/decrypt mbufs with same cipher key. */
189 process_snow3g_cipher_op(struct rte_crypto_op **ops,
190 struct snow3g_session *session,
194 uint8_t processed_ops = 0;
195 uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
196 uint8_t *IV[SNOW3G_MAX_BURST];
197 uint32_t num_bytes[SNOW3G_MAX_BURST];
199 for (i = 0; i < num_ops; i++) {
201 if (ops[i]->sym->cipher.iv.length != 16) {
202 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
203 SNOW3G_LOG_ERR("iv");
207 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
208 || ((ops[i]->sym->cipher.data.offset
210 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
211 SNOW3G_LOG_ERR("Data Length or offset");
215 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
216 (ops[i]->sym->cipher.data.offset >> 3);
217 dst[i] = ops[i]->sym->m_dst ?
218 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
219 (ops[i]->sym->cipher.data.offset >> 3) :
220 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
221 (ops[i]->sym->cipher.data.offset >> 3);
222 IV[i] = ops[i]->sym->cipher.iv.data;
223 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
228 sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, IV, src, dst,
229 num_bytes, processed_ops);
231 return processed_ops;
234 /** Generate/verify hash from mbufs with same hash key. */
236 process_snow3g_hash_op(struct rte_crypto_op **ops,
237 struct snow3g_session *session,
241 uint8_t processed_ops = 0;
243 uint32_t length_in_bits;
245 for (i = 0; i < num_ops; i++) {
246 if (ops[i]->sym->auth.aad.length != 16) {
247 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
248 SNOW3G_LOG_ERR("aad");
252 if (ops[i]->sym->auth.digest.length != 4) {
253 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
254 SNOW3G_LOG_ERR("digest");
258 if (((ops[i]->sym->auth.data.length % BYTE_LEN) != 0)
259 || ((ops[i]->sym->auth.data.offset
261 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
262 SNOW3G_LOG_ERR("Data Length or offset");
266 length_in_bits = ops[i]->sym->auth.data.length;
268 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
269 (ops[i]->sym->auth.data.offset >> 3);
271 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
272 dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
273 ops[i]->sym->auth.digest.length);
275 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
276 ops[i]->sym->auth.aad.data, src,
277 length_in_bits, dst);
279 if (memcmp(dst, ops[i]->sym->auth.digest.data,
280 ops[i]->sym->auth.digest.length) != 0)
281 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
283 /* Trim area used for digest from mbuf. */
284 rte_pktmbuf_trim(ops[i]->sym->m_src,
285 ops[i]->sym->auth.digest.length);
287 dst = ops[i]->sym->auth.digest.data;
289 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
290 ops[i]->sym->auth.aad.data, src,
291 length_in_bits, dst);
296 return processed_ops;
299 /** Process a batch of crypto ops which shares the same session. */
301 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
302 struct snow3g_qp *qp, uint8_t num_ops)
305 unsigned processed_ops;
307 switch (session->op) {
308 case SNOW3G_OP_ONLY_CIPHER:
309 processed_ops = process_snow3g_cipher_op(ops,
312 case SNOW3G_OP_ONLY_AUTH:
313 processed_ops = process_snow3g_hash_op(ops, session,
316 case SNOW3G_OP_CIPHER_AUTH:
317 processed_ops = process_snow3g_cipher_op(ops, session,
319 process_snow3g_hash_op(ops, session, processed_ops);
321 case SNOW3G_OP_AUTH_CIPHER:
322 processed_ops = process_snow3g_hash_op(ops, session,
324 process_snow3g_cipher_op(ops, session, processed_ops);
327 /* Operation not supported. */
331 for (i = 0; i < num_ops; i++) {
333 * If there was no error/authentication failure,
334 * change status to successful.
336 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
337 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
338 /* Free session if a session-less crypto op. */
339 if (ops[i]->sym->type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
340 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
341 ops[i]->sym->session = NULL;
345 return processed_ops;
349 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
352 struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
353 struct rte_crypto_op *curr_c_op;
355 struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
356 struct snow3g_qp *qp = queue_pair;
358 uint8_t burst_size = 0;
359 uint16_t enqueued_ops = 0;
360 uint8_t processed_ops;
362 for (i = 0; i < nb_ops; i++) {
365 /* Set status as enqueued (not processed yet) by default. */
366 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
368 curr_sess = snow3g_get_session(qp, curr_c_op);
369 if (unlikely(curr_sess == NULL ||
370 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
372 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
373 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
377 /* Batch ops that share the same session. */
378 if (prev_sess == NULL) {
379 prev_sess = curr_sess;
380 c_ops[burst_size++] = curr_c_op;
381 } else if (curr_sess == prev_sess) {
382 c_ops[burst_size++] = curr_c_op;
384 * When there are enough ops to process in a batch,
385 * process them, and start a new batch.
387 if (burst_size == SNOW3G_MAX_BURST) {
388 processed_ops = process_ops(c_ops,
389 prev_sess, qp, burst_size);
390 n = rte_ring_enqueue_burst(qp->processed_ops,
393 qp->qp_stats.enqueued_count += n;
395 if (n < burst_size) {
396 qp->qp_stats.enqueue_err_count +=
397 nb_ops - enqueued_ops;
406 * Different session, process the ops
407 * of the previous session.
409 processed_ops = process_ops(c_ops,
410 prev_sess, qp, burst_size);
411 n = rte_ring_enqueue_burst(qp->processed_ops,
414 qp->qp_stats.enqueued_count += n;
416 if (n < burst_size) {
417 qp->qp_stats.enqueue_err_count +=
418 nb_ops - enqueued_ops;
423 prev_sess = curr_sess;
424 c_ops[burst_size++] = curr_c_op;
428 if (burst_size != 0) {
429 /* Process the crypto ops of the last session. */
430 processed_ops = process_ops(c_ops,
431 prev_sess, qp, burst_size);
432 n = rte_ring_enqueue_burst(qp->processed_ops,
435 qp->qp_stats.enqueued_count += n;
437 if (n < burst_size) {
438 qp->qp_stats.enqueue_err_count +=
439 nb_ops - enqueued_ops;
448 snow3g_pmd_dequeue_burst(void *queue_pair,
449 struct rte_crypto_op **c_ops, uint16_t nb_ops)
451 struct snow3g_qp *qp = queue_pair;
453 unsigned nb_dequeued;
455 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
456 (void **)c_ops, nb_ops);
457 qp->qp_stats.dequeued_count += nb_dequeued;
462 static int cryptodev_snow3g_uninit(const char *name);
465 cryptodev_snow3g_create(const char *name,
466 struct rte_crypto_vdev_init_params *init_params)
468 struct rte_cryptodev *dev;
469 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
470 struct snow3g_private *internals;
472 /* Create a unique device name. */
473 if (create_unique_device_name(crypto_dev_name,
474 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
475 SNOW3G_LOG_ERR("failed to create unique cryptodev name");
479 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
480 sizeof(struct snow3g_private), init_params->socket_id);
482 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
486 dev->dev_type = RTE_CRYPTODEV_SNOW3G_PMD;
487 dev->dev_ops = rte_snow3g_pmd_ops;
489 /* Register RX/TX burst functions for data path. */
490 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
491 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
493 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
494 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING;
496 internals = dev->data->dev_private;
498 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
499 internals->max_nb_sessions = init_params->max_nb_sessions;
503 SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed", name);
505 cryptodev_snow3g_uninit(crypto_dev_name);
510 cryptodev_snow3g_init(const char *name,
511 const char *input_args)
513 struct rte_crypto_vdev_init_params init_params = {
514 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
515 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
519 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
521 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
522 init_params.socket_id);
523 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
524 init_params.max_nb_queue_pairs);
525 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
526 init_params.max_nb_sessions);
528 return cryptodev_snow3g_create(name, &init_params);
532 cryptodev_snow3g_uninit(const char *name)
537 RTE_LOG(INFO, PMD, "Closing SNOW3G crypto device %s"
538 " on numa socket %u\n",
539 name, rte_socket_id());
544 static struct rte_driver cryptodev_snow3g_pmd_drv = {
545 .name = CRYPTODEV_NAME_SNOW3G_PMD,
547 .init = cryptodev_snow3g_init,
548 .uninit = cryptodev_snow3g_uninit
551 PMD_REGISTER_DRIVER(cryptodev_snow3g_pmd_drv);