2 * A loadable module that benchmarks the OCF crypto speed from kernel space.
4 * Copyright (C) 2004-2010 David McCullough <david_mccullough@mcafee.com>
8 * The free distribution and use of this software in both source and binary
9 * form is allowed (with or without changes) provided that:
11 * 1. distributions of this source code include the above copyright
12 * notice, this list of conditions and the following disclaimer;
14 * 2. distributions in binary form include the above copyright
15 * notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other associated materials;
18 * 3. the copyright holder's name is not used to endorse products
19 * built using this software without specific written permission.
21 * ALTERNATIVELY, provided that this notice is retained in full, this product
22 * may be distributed under the terms of the GNU General Public License (GPL),
23 * in which case the provisions of the GPL apply INSTEAD OF those given above.
27 * This software is provided 'as is' with no explicit or implied warranties
28 * in respect of its properties, including, but not limited to, correctness
29 * and/or fitness for purpose.
33 #include <linux/version.h>
34 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
35 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/list.h>
40 #include <linux/slab.h>
41 #include <linux/wait.h>
42 #include <linux/sched.h>
43 #include <linux/spinlock.h>
44 #include <linux/interrupt.h>
45 #include <cryptodev.h>
47 #ifdef I_HAVE_AN_XSCALE_WITH_INTEL_SDK
48 #define BENCH_IXP_ACCESS_LIB 1
50 #ifdef BENCH_IXP_ACCESS_LIB
52 #include <IxOsBuffMgt.h>
54 #include <IxCryptoAcc.h>
56 #include <IxOsServices.h>
57 #include <IxOsCacheMMU.h>
61 * support for access lib version 1.4
64 #define IX_MBUF_PRIV(x) ((x)->priv)
68 * the number of simultaneously active requests
70 static int request_q_len = 40;
71 module_param(request_q_len, int, 0);
72 MODULE_PARM_DESC(request_q_len, "Number of outstanding requests");
75 * how many requests we want to have processed
77 static int request_num = 1024;
78 module_param(request_num, int, 0);
79 MODULE_PARM_DESC(request_num, "run for at least this many requests");
82 * the size of each request
84 static int request_size = 1488;
85 module_param(request_size, int, 0);
86 MODULE_PARM_DESC(request_size, "size of each request");
89 * OCF batching of requests
91 static int request_batch = 1;
92 module_param(request_batch, int, 0);
93 MODULE_PARM_DESC(request_batch, "enable OCF request batching");
96 * OCF immediate callback on completion
98 static int request_cbimm = 1;
99 module_param(request_cbimm, int, 0);
100 MODULE_PARM_DESC(request_cbimm, "enable OCF immediate callback on completion");
103 * a structure for each request
106 struct work_struct work;
107 #ifdef BENCH_IXP_ACCESS_LIB
110 unsigned char *buffer;
113 static request_t *requests;
115 static spinlock_t ocfbench_counter_lock;
116 static int outstanding;
119 /*************************************************************************/
121 * OCF benchmark routines
124 static uint64_t ocf_cryptoid;
125 static unsigned long jstart, jstop;
127 static int ocf_init(void);
128 static int ocf_cb(struct cryptop *crp);
129 static void ocf_request(void *arg);
130 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
131 static void ocf_request_wq(struct work_struct *work);
138 struct cryptoini crie, cria;
139 struct cryptodesc crda, crde;
141 memset(&crie, 0, sizeof(crie));
142 memset(&cria, 0, sizeof(cria));
143 memset(&crde, 0, sizeof(crde));
144 memset(&crda, 0, sizeof(crda));
146 cria.cri_alg = CRYPTO_SHA1_HMAC;
147 cria.cri_klen = 20 * 8;
148 cria.cri_key = "0123456789abcdefghij";
150 //crie.cri_alg = CRYPTO_3DES_CBC;
151 crie.cri_alg = CRYPTO_AES_CBC;
152 crie.cri_klen = 24 * 8;
153 crie.cri_key = "0123456789abcdefghijklmn";
155 crie.cri_next = &cria;
157 error = crypto_newsession(&ocf_cryptoid, &crie,
158 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
160 printk("crypto_newsession failed %d\n", error);
167 ocf_cb(struct cryptop *crp)
169 request_t *r = (request_t *) crp->crp_opaque;
173 printk("Error in OCF processing: %d\n", crp->crp_etype);
177 /* do all requests but take at least 1 second */
178 spin_lock_irqsave(&ocfbench_counter_lock, flags);
180 if (total > request_num && jstart + HZ < jiffies) {
182 spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
185 spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
187 schedule_work(&r->work);
193 ocf_request(void *arg)
196 struct cryptop *crp = crypto_getreq(2);
197 struct cryptodesc *crde, *crda;
201 spin_lock_irqsave(&ocfbench_counter_lock, flags);
203 spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
207 crde = crp->crp_desc;
208 crda = crde->crd_next;
212 crda->crd_len = request_size;
213 crda->crd_inject = request_size;
214 crda->crd_alg = CRYPTO_SHA1_HMAC;
215 crda->crd_key = "0123456789abcdefghij";
216 crda->crd_klen = 20 * 8;
219 crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_ENCRYPT;
220 crde->crd_len = request_size;
221 crde->crd_inject = request_size;
222 //crde->crd_alg = CRYPTO_3DES_CBC;
223 crde->crd_alg = CRYPTO_AES_CBC;
224 crde->crd_key = "0123456789abcdefghijklmn";
225 crde->crd_klen = 24 * 8;
227 crp->crp_ilen = request_size + 64;
230 crp->crp_flags |= CRYPTO_F_BATCH;
232 crp->crp_flags |= CRYPTO_F_CBIMM;
233 crp->crp_buf = (caddr_t) r->buffer;
234 crp->crp_callback = ocf_cb;
235 crp->crp_sid = ocf_cryptoid;
236 crp->crp_opaque = (caddr_t) r;
237 crypto_dispatch(crp);
240 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
242 ocf_request_wq(struct work_struct *work)
244 request_t *r = container_of(work, request_t, work);
252 crypto_freesession(ocf_cryptoid);
255 /*************************************************************************/
256 #ifdef BENCH_IXP_ACCESS_LIB
257 /*************************************************************************/
259 * CryptoAcc benchmark routines
262 static IxCryptoAccCtx ixp_ctx;
263 static UINT32 ixp_ctx_id;
264 static IX_MBUF ixp_pri;
265 static IX_MBUF ixp_sec;
266 static int ixp_registered = 0;
268 static void ixp_register_cb(UINT32 ctx_id, IX_MBUF *bufp,
269 IxCryptoAccStatus status);
270 static void ixp_perform_cb(UINT32 ctx_id, IX_MBUF *sbufp, IX_MBUF *dbufp,
271 IxCryptoAccStatus status);
272 static void ixp_request(void *arg);
273 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
274 static void ixp_request_wq(struct work_struct *work);
280 IxCryptoAccStatus status;
282 ixp_ctx.cipherCtx.cipherAlgo = IX_CRYPTO_ACC_CIPHER_3DES;
283 ixp_ctx.cipherCtx.cipherMode = IX_CRYPTO_ACC_MODE_CBC;
284 ixp_ctx.cipherCtx.cipherKeyLen = 24;
285 ixp_ctx.cipherCtx.cipherBlockLen = IX_CRYPTO_ACC_DES_BLOCK_64;
286 ixp_ctx.cipherCtx.cipherInitialVectorLen = IX_CRYPTO_ACC_DES_IV_64;
287 memcpy(ixp_ctx.cipherCtx.key.cipherKey, "0123456789abcdefghijklmn", 24);
289 ixp_ctx.authCtx.authAlgo = IX_CRYPTO_ACC_AUTH_SHA1;
290 ixp_ctx.authCtx.authDigestLen = 12;
291 ixp_ctx.authCtx.aadLen = 0;
292 ixp_ctx.authCtx.authKeyLen = 20;
293 memcpy(ixp_ctx.authCtx.key.authKey, "0123456789abcdefghij", 20);
295 ixp_ctx.useDifferentSrcAndDestMbufs = 0;
296 ixp_ctx.operation = IX_CRYPTO_ACC_OP_ENCRYPT_AUTH ;
298 IX_MBUF_MLEN(&ixp_pri) = IX_MBUF_PKT_LEN(&ixp_pri) = 128;
299 IX_MBUF_MDATA(&ixp_pri) = (unsigned char *) kmalloc(128, SLAB_ATOMIC);
300 IX_MBUF_MLEN(&ixp_sec) = IX_MBUF_PKT_LEN(&ixp_sec) = 128;
301 IX_MBUF_MDATA(&ixp_sec) = (unsigned char *) kmalloc(128, SLAB_ATOMIC);
303 status = ixCryptoAccCtxRegister(&ixp_ctx, &ixp_pri, &ixp_sec,
304 ixp_register_cb, ixp_perform_cb, &ixp_ctx_id);
306 if (IX_CRYPTO_ACC_STATUS_SUCCESS == status) {
307 while (!ixp_registered)
309 return ixp_registered < 0 ? -1 : 0;
312 printk("ixp: ixCryptoAccCtxRegister failed %d\n", status);
317 ixp_register_cb(UINT32 ctx_id, IX_MBUF *bufp, IxCryptoAccStatus status)
320 IX_MBUF_MLEN(bufp) = IX_MBUF_PKT_LEN(bufp) = 0;
321 kfree(IX_MBUF_MDATA(bufp));
322 IX_MBUF_MDATA(bufp) = NULL;
325 if (IX_CRYPTO_ACC_STATUS_WAIT == status)
327 if (IX_CRYPTO_ACC_STATUS_SUCCESS == status)
338 IxCryptoAccStatus status)
343 /* do all requests but take at least 1 second */
344 spin_lock_irqsave(&ocfbench_counter_lock, flags);
346 if (total > request_num && jstart + HZ < jiffies) {
348 spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
352 if (!sbufp || !(r = IX_MBUF_PRIV(sbufp))) {
353 printk("crappo %p %p\n", sbufp, r);
355 spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
358 spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
360 schedule_work(&r->work);
364 ixp_request(void *arg)
367 IxCryptoAccStatus status;
370 memset(&r->mbuf, 0, sizeof(r->mbuf));
371 IX_MBUF_MLEN(&r->mbuf) = IX_MBUF_PKT_LEN(&r->mbuf) = request_size + 64;
372 IX_MBUF_MDATA(&r->mbuf) = r->buffer;
373 IX_MBUF_PRIV(&r->mbuf) = r;
374 status = ixCryptoAccAuthCryptPerform(ixp_ctx_id, &r->mbuf, NULL,
375 0, request_size, 0, request_size, request_size, r->buffer);
376 if (IX_CRYPTO_ACC_STATUS_SUCCESS != status) {
377 printk("status1 = %d\n", status);
378 spin_lock_irqsave(&ocfbench_counter_lock, flags);
380 spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
386 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
388 ixp_request_wq(struct work_struct *work)
390 request_t *r = container_of(work, request_t, work);
398 /* we should free the session here but I am lazy :-) */
401 /*************************************************************************/
402 #endif /* BENCH_IXP_ACCESS_LIB */
403 /*************************************************************************/
412 printk("Crypto Speed tests\n");
414 requests = kmalloc(sizeof(request_t) * request_q_len, GFP_KERNEL);
416 printk("malloc failed\n");
420 for (i = 0; i < request_q_len; i++) {
421 /* +64 for return data */
422 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
423 INIT_WORK(&requests[i].work, ocf_request_wq);
425 INIT_WORK(&requests[i].work, ocf_request, &requests[i]);
427 requests[i].buffer = kmalloc(request_size + 128, GFP_DMA);
428 if (!requests[i].buffer) {
429 printk("malloc failed\n");
432 memset(requests[i].buffer, '0' + i, request_size + 128);
438 printk("OCF: testing ...\n");
439 if (ocf_init() == -1)
442 spin_lock_init(&ocfbench_counter_lock);
443 total = outstanding = 0;
445 for (i = 0; i < request_q_len; i++) {
446 spin_lock_irqsave(&ocfbench_counter_lock, flags);
448 spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
449 ocf_request(&requests[i]);
451 while (outstanding > 0)
456 if (jstop > jstart) {
457 mbps = (unsigned long) total * (unsigned long) request_size * 8;
458 mbps /= ((jstop - jstart) * 1000) / HZ;
460 printk("OCF: %d requests of %d bytes in %d jiffies (%d.%03d Mbps)\n",
461 total, request_size, (int)(jstop - jstart),
462 ((int)mbps) / 1000, ((int)mbps) % 1000);
465 #ifdef BENCH_IXP_ACCESS_LIB
469 printk("IXP: testing ...\n");
471 total = outstanding = 0;
473 for (i = 0; i < request_q_len; i++) {
474 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
475 INIT_WORK(&requests[i].work, ixp_request_wq);
477 INIT_WORK(&requests[i].work, ixp_request, &requests[i]);
479 spin_lock_irqsave(&ocfbench_counter_lock, flags);
481 spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
482 ixp_request(&requests[i]);
484 while (outstanding > 0)
489 if (jstop > jstart) {
490 mbps = (unsigned long) total * (unsigned long) request_size * 8;
491 mbps /= ((jstop - jstart) * 1000) / HZ;
493 printk("IXP: %d requests of %d bytes in %d jiffies (%d.%03d Mbps)\n",
494 total, request_size, jstop - jstart,
495 ((int)mbps) / 1000, ((int)mbps) % 1000);
497 #endif /* BENCH_IXP_ACCESS_LIB */
499 for (i = 0; i < request_q_len; i++)
500 kfree(requests[i].buffer);
502 return -EINVAL; /* always fail to load so it can be re-run quickly ;-) */
505 static void __exit ocfbench_exit(void)
509 module_init(ocfbench_init);
510 module_exit(ocfbench_exit);
512 MODULE_LICENSE("Dual BSD/GPL");
513 MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
514 MODULE_DESCRIPTION("Benchmark various in-kernel crypto speeds");