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,33))
35 #include <generated/autoconf.h>
37 #include <linux/autoconf.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/list.h>
42 #include <linux/slab.h>
43 #include <linux/wait.h>
44 #include <linux/sched.h>
45 #include <linux/spinlock.h>
46 #include <linux/version.h>
47 #include <linux/interrupt.h>
48 #include <cryptodev.h>
50 #ifdef I_HAVE_AN_XSCALE_WITH_INTEL_SDK
51 #define BENCH_IXP_ACCESS_LIB 1
53 #ifdef BENCH_IXP_ACCESS_LIB
55 #include <IxOsBuffMgt.h>
57 #include <IxCryptoAcc.h>
59 #include <IxOsServices.h>
60 #include <IxOsCacheMMU.h>
64 * support for access lib version 1.4
67 #define IX_MBUF_PRIV(x) ((x)->priv)
71 * the number of simultaneously active requests
73 static int request_q_len
= 20;
74 module_param(request_q_len
, int, 0);
75 MODULE_PARM_DESC(request_q_len
, "Number of outstanding requests");
77 * how many requests we want to have processed
79 static int request_num
= 1024;
80 module_param(request_num
, int, 0);
81 MODULE_PARM_DESC(request_num
, "run for at least this many requests");
83 * the size of each request
85 static int request_size
= 1500;
86 module_param(request_size
, int, 0);
87 MODULE_PARM_DESC(request_size
, "size of each request");
90 * a structure for each request
93 struct work_struct work
;
94 #ifdef BENCH_IXP_ACCESS_LIB
97 unsigned char *buffer
;
100 static request_t
*requests
;
102 static int outstanding
;
105 /*************************************************************************/
107 * OCF benchmark routines
110 static uint64_t ocf_cryptoid
;
111 static int ocf_init(void);
112 static int ocf_cb(struct cryptop
*crp
);
113 static void ocf_request(void *arg
);
114 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
115 static void ocf_request_wq(struct work_struct
*work
);
122 struct cryptoini crie
, cria
;
123 struct cryptodesc crda
, crde
;
125 memset(&crie
, 0, sizeof(crie
));
126 memset(&cria
, 0, sizeof(cria
));
127 memset(&crde
, 0, sizeof(crde
));
128 memset(&crda
, 0, sizeof(crda
));
130 cria
.cri_alg
= CRYPTO_SHA1_HMAC
;
131 cria
.cri_klen
= 20 * 8;
132 cria
.cri_key
= "0123456789abcdefghij";
134 crie
.cri_alg
= CRYPTO_3DES_CBC
;
135 crie
.cri_klen
= 24 * 8;
136 crie
.cri_key
= "0123456789abcdefghijklmn";
138 crie
.cri_next
= &cria
;
140 error
= crypto_newsession(&ocf_cryptoid
, &crie
, 0);
142 printk("crypto_newsession failed %d\n", error
);
149 ocf_cb(struct cryptop
*crp
)
151 request_t
*r
= (request_t
*) crp
->crp_opaque
;
154 printk("Error in OCF processing: %d\n", crp
->crp_etype
);
159 if (total
> request_num
) {
164 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
165 INIT_WORK(&r
->work
, ocf_request_wq
);
167 INIT_WORK(&r
->work
, ocf_request
, r
);
169 schedule_work(&r
->work
);
175 ocf_request(void *arg
)
178 struct cryptop
*crp
= crypto_getreq(2);
179 struct cryptodesc
*crde
, *crda
;
186 crde
= crp
->crp_desc
;
187 crda
= crde
->crd_next
;
191 crda
->crd_len
= request_size
;
192 crda
->crd_inject
= request_size
;
193 crda
->crd_alg
= CRYPTO_SHA1_HMAC
;
194 crda
->crd_key
= "0123456789abcdefghij";
195 crda
->crd_klen
= 20 * 8;
198 crde
->crd_flags
= CRD_F_IV_EXPLICIT
| CRD_F_ENCRYPT
;
199 crde
->crd_len
= request_size
;
200 crde
->crd_inject
= request_size
;
201 crde
->crd_alg
= CRYPTO_3DES_CBC
;
202 crde
->crd_key
= "0123456789abcdefghijklmn";
203 crde
->crd_klen
= 24 * 8;
205 crp
->crp_ilen
= request_size
+ 64;
206 crp
->crp_flags
= CRYPTO_F_CBIMM
;
207 crp
->crp_buf
= (caddr_t
) r
->buffer
;
208 crp
->crp_callback
= ocf_cb
;
209 crp
->crp_sid
= ocf_cryptoid
;
210 crp
->crp_opaque
= (caddr_t
) r
;
211 crypto_dispatch(crp
);
214 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
216 ocf_request_wq(struct work_struct
*work
)
218 request_t
*r
= container_of(work
, request_t
, work
);
223 /*************************************************************************/
224 #ifdef BENCH_IXP_ACCESS_LIB
225 /*************************************************************************/
227 * CryptoAcc benchmark routines
230 static IxCryptoAccCtx ixp_ctx
;
231 static UINT32 ixp_ctx_id
;
232 static IX_MBUF ixp_pri
;
233 static IX_MBUF ixp_sec
;
234 static int ixp_registered
= 0;
236 static void ixp_register_cb(UINT32 ctx_id
, IX_MBUF
*bufp
,
237 IxCryptoAccStatus status
);
238 static void ixp_perform_cb(UINT32 ctx_id
, IX_MBUF
*sbufp
, IX_MBUF
*dbufp
,
239 IxCryptoAccStatus status
);
240 static void ixp_request(void *arg
);
241 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
242 static void ixp_request_wq(struct work_struct
*work
);
248 IxCryptoAccStatus status
;
250 ixp_ctx
.cipherCtx
.cipherAlgo
= IX_CRYPTO_ACC_CIPHER_3DES
;
251 ixp_ctx
.cipherCtx
.cipherMode
= IX_CRYPTO_ACC_MODE_CBC
;
252 ixp_ctx
.cipherCtx
.cipherKeyLen
= 24;
253 ixp_ctx
.cipherCtx
.cipherBlockLen
= IX_CRYPTO_ACC_DES_BLOCK_64
;
254 ixp_ctx
.cipherCtx
.cipherInitialVectorLen
= IX_CRYPTO_ACC_DES_IV_64
;
255 memcpy(ixp_ctx
.cipherCtx
.key
.cipherKey
, "0123456789abcdefghijklmn", 24);
257 ixp_ctx
.authCtx
.authAlgo
= IX_CRYPTO_ACC_AUTH_SHA1
;
258 ixp_ctx
.authCtx
.authDigestLen
= 12;
259 ixp_ctx
.authCtx
.aadLen
= 0;
260 ixp_ctx
.authCtx
.authKeyLen
= 20;
261 memcpy(ixp_ctx
.authCtx
.key
.authKey
, "0123456789abcdefghij", 20);
263 ixp_ctx
.useDifferentSrcAndDestMbufs
= 0;
264 ixp_ctx
.operation
= IX_CRYPTO_ACC_OP_ENCRYPT_AUTH
;
266 IX_MBUF_MLEN(&ixp_pri
) = IX_MBUF_PKT_LEN(&ixp_pri
) = 128;
267 IX_MBUF_MDATA(&ixp_pri
) = (unsigned char *) kmalloc(128, SLAB_ATOMIC
);
268 IX_MBUF_MLEN(&ixp_sec
) = IX_MBUF_PKT_LEN(&ixp_sec
) = 128;
269 IX_MBUF_MDATA(&ixp_sec
) = (unsigned char *) kmalloc(128, SLAB_ATOMIC
);
271 status
= ixCryptoAccCtxRegister(&ixp_ctx
, &ixp_pri
, &ixp_sec
,
272 ixp_register_cb
, ixp_perform_cb
, &ixp_ctx_id
);
274 if (IX_CRYPTO_ACC_STATUS_SUCCESS
== status
) {
275 while (!ixp_registered
)
277 return ixp_registered
< 0 ? -1 : 0;
280 printk("ixp: ixCryptoAccCtxRegister failed %d\n", status
);
285 ixp_register_cb(UINT32 ctx_id
, IX_MBUF
*bufp
, IxCryptoAccStatus status
)
288 IX_MBUF_MLEN(bufp
) = IX_MBUF_PKT_LEN(bufp
) = 0;
289 kfree(IX_MBUF_MDATA(bufp
));
290 IX_MBUF_MDATA(bufp
) = NULL
;
293 if (IX_CRYPTO_ACC_STATUS_WAIT
== status
)
295 if (IX_CRYPTO_ACC_STATUS_SUCCESS
== status
)
306 IxCryptoAccStatus status
)
311 if (total
> request_num
) {
316 if (!sbufp
|| !(r
= IX_MBUF_PRIV(sbufp
))) {
317 printk("crappo %p %p\n", sbufp
, r
);
322 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
323 INIT_WORK(&r
->work
, ixp_request_wq
);
325 INIT_WORK(&r
->work
, ixp_request
, r
);
327 schedule_work(&r
->work
);
331 ixp_request(void *arg
)
334 IxCryptoAccStatus status
;
336 memset(&r
->mbuf
, 0, sizeof(r
->mbuf
));
337 IX_MBUF_MLEN(&r
->mbuf
) = IX_MBUF_PKT_LEN(&r
->mbuf
) = request_size
+ 64;
338 IX_MBUF_MDATA(&r
->mbuf
) = r
->buffer
;
339 IX_MBUF_PRIV(&r
->mbuf
) = r
;
340 status
= ixCryptoAccAuthCryptPerform(ixp_ctx_id
, &r
->mbuf
, NULL
,
341 0, request_size
, 0, request_size
, request_size
, r
->buffer
);
342 if (IX_CRYPTO_ACC_STATUS_SUCCESS
!= status
) {
343 printk("status1 = %d\n", status
);
350 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
352 ixp_request_wq(struct work_struct
*work
)
354 request_t
*r
= container_of(work
, request_t
, work
);
359 /*************************************************************************/
360 #endif /* BENCH_IXP_ACCESS_LIB */
361 /*************************************************************************/
366 int i
, jstart
, jstop
;
368 printk("Crypto Speed tests\n");
370 requests
= kmalloc(sizeof(request_t
) * request_q_len
, GFP_KERNEL
);
372 printk("malloc failed\n");
376 for (i
= 0; i
< request_q_len
; i
++) {
377 /* +64 for return data */
378 requests
[i
].buffer
= kmalloc(request_size
+ 128, GFP_DMA
);
379 if (!requests
[i
].buffer
) {
380 printk("malloc failed\n");
383 memset(requests
[i
].buffer
, '0' + i
, request_size
+ 128);
389 printk("OCF: testing ...\n");
391 total
= outstanding
= 0;
393 for (i
= 0; i
< request_q_len
; i
++) {
395 ocf_request(&requests
[i
]);
397 while (outstanding
> 0)
401 printk("OCF: %d requests of %d bytes in %d jiffies\n", total
, request_size
,
404 #ifdef BENCH_IXP_ACCESS_LIB
408 printk("IXP: testing ...\n");
410 total
= outstanding
= 0;
412 for (i
= 0; i
< request_q_len
; i
++) {
414 ixp_request(&requests
[i
]);
416 while (outstanding
> 0)
420 printk("IXP: %d requests of %d bytes in %d jiffies\n", total
, request_size
,
422 #endif /* BENCH_IXP_ACCESS_LIB */
424 for (i
= 0; i
< request_q_len
; i
++)
425 kfree(requests
[i
].buffer
);
427 return -EINVAL
; /* always fail to load so it can be re-run quickly ;-) */
430 static void __exit
ocfbench_exit(void)
434 module_init(ocfbench_init
);
435 module_exit(ocfbench_exit
);
437 MODULE_LICENSE("BSD");
438 MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
439 MODULE_DESCRIPTION("Benchmark various in-kernel crypto speeds");