ocf-bench.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * A loadable module that benchmarks the OCF crypto speed from kernel space.
  3. *
  4. * Copyright (C) 2004-2010 David McCullough <[email protected]>
  5. *
  6. * LICENSE TERMS
  7. *
  8. * The free distribution and use of this software in both source and binary
  9. * form is allowed (with or without changes) provided that:
  10. *
  11. * 1. distributions of this source code include the above copyright
  12. * notice, this list of conditions and the following disclaimer;
  13. *
  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;
  17. *
  18. * 3. the copyright holder's name is not used to endorse products
  19. * built using this software without specific written permission.
  20. *
  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.
  24. *
  25. * DISCLAIMER
  26. *
  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.
  30. */
  31. #ifndef AUTOCONF_INCLUDED
  32. #include <linux/config.h>
  33. #endif
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/list.h>
  37. #include <linux/slab.h>
  38. #include <linux/wait.h>
  39. #include <linux/sched.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/version.h>
  42. #include <linux/interrupt.h>
  43. #include <cryptodev.h>
  44. #ifdef I_HAVE_AN_XSCALE_WITH_INTEL_SDK
  45. #define BENCH_IXP_ACCESS_LIB 1
  46. #endif
  47. #ifdef BENCH_IXP_ACCESS_LIB
  48. #include <IxTypes.h>
  49. #include <IxOsBuffMgt.h>
  50. #include <IxNpeDl.h>
  51. #include <IxCryptoAcc.h>
  52. #include <IxQMgr.h>
  53. #include <IxOsServices.h>
  54. #include <IxOsCacheMMU.h>
  55. #endif
  56. /*
  57. * support for access lib version 1.4
  58. */
  59. #ifndef IX_MBUF_PRIV
  60. #define IX_MBUF_PRIV(x) ((x)->priv)
  61. #endif
  62. /*
  63. * the number of simultaneously active requests
  64. */
  65. static int request_q_len = 20;
  66. module_param(request_q_len, int, 0);
  67. MODULE_PARM_DESC(request_q_len, "Number of outstanding requests");
  68. /*
  69. * how many requests we want to have processed
  70. */
  71. static int request_num = 1024;
  72. module_param(request_num, int, 0);
  73. MODULE_PARM_DESC(request_num, "run for at least this many requests");
  74. /*
  75. * the size of each request
  76. */
  77. static int request_size = 1500;
  78. module_param(request_size, int, 0);
  79. MODULE_PARM_DESC(request_size, "size of each request");
  80. /*
  81. * a structure for each request
  82. */
  83. typedef struct {
  84. struct work_struct work;
  85. #ifdef BENCH_IXP_ACCESS_LIB
  86. IX_MBUF mbuf;
  87. #endif
  88. unsigned char *buffer;
  89. } request_t;
  90. static request_t *requests;
  91. static int outstanding;
  92. static int total;
  93. /*************************************************************************/
  94. /*
  95. * OCF benchmark routines
  96. */
  97. static uint64_t ocf_cryptoid;
  98. static int ocf_init(void);
  99. static int ocf_cb(struct cryptop *crp);
  100. static void ocf_request(void *arg);
  101. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  102. static void ocf_request_wq(struct work_struct *work);
  103. #endif
  104. static int
  105. ocf_init(void)
  106. {
  107. int error;
  108. struct cryptoini crie, cria;
  109. struct cryptodesc crda, crde;
  110. memset(&crie, 0, sizeof(crie));
  111. memset(&cria, 0, sizeof(cria));
  112. memset(&crde, 0, sizeof(crde));
  113. memset(&crda, 0, sizeof(crda));
  114. cria.cri_alg = CRYPTO_SHA1_HMAC;
  115. cria.cri_klen = 20 * 8;
  116. cria.cri_key = "0123456789abcdefghij";
  117. crie.cri_alg = CRYPTO_3DES_CBC;
  118. crie.cri_klen = 24 * 8;
  119. crie.cri_key = "0123456789abcdefghijklmn";
  120. crie.cri_next = &cria;
  121. error = crypto_newsession(&ocf_cryptoid, &crie, 0);
  122. if (error) {
  123. printk("crypto_newsession failed %d\n", error);
  124. return -1;
  125. }
  126. return 0;
  127. }
  128. static int
  129. ocf_cb(struct cryptop *crp)
  130. {
  131. request_t *r = (request_t *) crp->crp_opaque;
  132. if (crp->crp_etype)
  133. printk("Error in OCF processing: %d\n", crp->crp_etype);
  134. total++;
  135. crypto_freereq(crp);
  136. crp = NULL;
  137. if (total > request_num) {
  138. outstanding--;
  139. return 0;
  140. }
  141. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  142. INIT_WORK(&r->work, ocf_request_wq);
  143. #else
  144. INIT_WORK(&r->work, ocf_request, r);
  145. #endif
  146. schedule_work(&r->work);
  147. return 0;
  148. }
  149. static void
  150. ocf_request(void *arg)
  151. {
  152. request_t *r = arg;
  153. struct cryptop *crp = crypto_getreq(2);
  154. struct cryptodesc *crde, *crda;
  155. if (!crp) {
  156. outstanding--;
  157. return;
  158. }
  159. crde = crp->crp_desc;
  160. crda = crde->crd_next;
  161. crda->crd_skip = 0;
  162. crda->crd_flags = 0;
  163. crda->crd_len = request_size;
  164. crda->crd_inject = request_size;
  165. crda->crd_alg = CRYPTO_SHA1_HMAC;
  166. crda->crd_key = "0123456789abcdefghij";
  167. crda->crd_klen = 20 * 8;
  168. crde->crd_skip = 0;
  169. crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_ENCRYPT;
  170. crde->crd_len = request_size;
  171. crde->crd_inject = request_size;
  172. crde->crd_alg = CRYPTO_3DES_CBC;
  173. crde->crd_key = "0123456789abcdefghijklmn";
  174. crde->crd_klen = 24 * 8;
  175. crp->crp_ilen = request_size + 64;
  176. crp->crp_flags = CRYPTO_F_CBIMM;
  177. crp->crp_buf = (caddr_t) r->buffer;
  178. crp->crp_callback = ocf_cb;
  179. crp->crp_sid = ocf_cryptoid;
  180. crp->crp_opaque = (caddr_t) r;
  181. crypto_dispatch(crp);
  182. }
  183. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  184. static void
  185. ocf_request_wq(struct work_struct *work)
  186. {
  187. request_t *r = container_of(work, request_t, work);
  188. ocf_request(r);
  189. }
  190. #endif
  191. /*************************************************************************/
  192. #ifdef BENCH_IXP_ACCESS_LIB
  193. /*************************************************************************/
  194. /*
  195. * CryptoAcc benchmark routines
  196. */
  197. static IxCryptoAccCtx ixp_ctx;
  198. static UINT32 ixp_ctx_id;
  199. static IX_MBUF ixp_pri;
  200. static IX_MBUF ixp_sec;
  201. static int ixp_registered = 0;
  202. static void ixp_register_cb(UINT32 ctx_id, IX_MBUF *bufp,
  203. IxCryptoAccStatus status);
  204. static void ixp_perform_cb(UINT32 ctx_id, IX_MBUF *sbufp, IX_MBUF *dbufp,
  205. IxCryptoAccStatus status);
  206. static void ixp_request(void *arg);
  207. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  208. static void ixp_request_wq(struct work_struct *work);
  209. #endif
  210. static int
  211. ixp_init(void)
  212. {
  213. IxCryptoAccStatus status;
  214. ixp_ctx.cipherCtx.cipherAlgo = IX_CRYPTO_ACC_CIPHER_3DES;
  215. ixp_ctx.cipherCtx.cipherMode = IX_CRYPTO_ACC_MODE_CBC;
  216. ixp_ctx.cipherCtx.cipherKeyLen = 24;
  217. ixp_ctx.cipherCtx.cipherBlockLen = IX_CRYPTO_ACC_DES_BLOCK_64;
  218. ixp_ctx.cipherCtx.cipherInitialVectorLen = IX_CRYPTO_ACC_DES_IV_64;
  219. memcpy(ixp_ctx.cipherCtx.key.cipherKey, "0123456789abcdefghijklmn", 24);
  220. ixp_ctx.authCtx.authAlgo = IX_CRYPTO_ACC_AUTH_SHA1;
  221. ixp_ctx.authCtx.authDigestLen = 12;
  222. ixp_ctx.authCtx.aadLen = 0;
  223. ixp_ctx.authCtx.authKeyLen = 20;
  224. memcpy(ixp_ctx.authCtx.key.authKey, "0123456789abcdefghij", 20);
  225. ixp_ctx.useDifferentSrcAndDestMbufs = 0;
  226. ixp_ctx.operation = IX_CRYPTO_ACC_OP_ENCRYPT_AUTH ;
  227. IX_MBUF_MLEN(&ixp_pri) = IX_MBUF_PKT_LEN(&ixp_pri) = 128;
  228. IX_MBUF_MDATA(&ixp_pri) = (unsigned char *) kmalloc(128, SLAB_ATOMIC);
  229. IX_MBUF_MLEN(&ixp_sec) = IX_MBUF_PKT_LEN(&ixp_sec) = 128;
  230. IX_MBUF_MDATA(&ixp_sec) = (unsigned char *) kmalloc(128, SLAB_ATOMIC);
  231. status = ixCryptoAccCtxRegister(&ixp_ctx, &ixp_pri, &ixp_sec,
  232. ixp_register_cb, ixp_perform_cb, &ixp_ctx_id);
  233. if (IX_CRYPTO_ACC_STATUS_SUCCESS == status) {
  234. while (!ixp_registered)
  235. schedule();
  236. return ixp_registered < 0 ? -1 : 0;
  237. }
  238. printk("ixp: ixCryptoAccCtxRegister failed %d\n", status);
  239. return -1;
  240. }
  241. static void
  242. ixp_register_cb(UINT32 ctx_id, IX_MBUF *bufp, IxCryptoAccStatus status)
  243. {
  244. if (bufp) {
  245. IX_MBUF_MLEN(bufp) = IX_MBUF_PKT_LEN(bufp) = 0;
  246. kfree(IX_MBUF_MDATA(bufp));
  247. IX_MBUF_MDATA(bufp) = NULL;
  248. }
  249. if (IX_CRYPTO_ACC_STATUS_WAIT == status)
  250. return;
  251. if (IX_CRYPTO_ACC_STATUS_SUCCESS == status)
  252. ixp_registered = 1;
  253. else
  254. ixp_registered = -1;
  255. }
  256. static void
  257. ixp_perform_cb(
  258. UINT32 ctx_id,
  259. IX_MBUF *sbufp,
  260. IX_MBUF *dbufp,
  261. IxCryptoAccStatus status)
  262. {
  263. request_t *r = NULL;
  264. total++;
  265. if (total > request_num) {
  266. outstanding--;
  267. return;
  268. }
  269. if (!sbufp || !(r = IX_MBUF_PRIV(sbufp))) {
  270. printk("crappo %p %p\n", sbufp, r);
  271. outstanding--;
  272. return;
  273. }
  274. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  275. INIT_WORK(&r->work, ixp_request_wq);
  276. #else
  277. INIT_WORK(&r->work, ixp_request, r);
  278. #endif
  279. schedule_work(&r->work);
  280. }
  281. static void
  282. ixp_request(void *arg)
  283. {
  284. request_t *r = arg;
  285. IxCryptoAccStatus status;
  286. memset(&r->mbuf, 0, sizeof(r->mbuf));
  287. IX_MBUF_MLEN(&r->mbuf) = IX_MBUF_PKT_LEN(&r->mbuf) = request_size + 64;
  288. IX_MBUF_MDATA(&r->mbuf) = r->buffer;
  289. IX_MBUF_PRIV(&r->mbuf) = r;
  290. status = ixCryptoAccAuthCryptPerform(ixp_ctx_id, &r->mbuf, NULL,
  291. 0, request_size, 0, request_size, request_size, r->buffer);
  292. if (IX_CRYPTO_ACC_STATUS_SUCCESS != status) {
  293. printk("status1 = %d\n", status);
  294. outstanding--;
  295. return;
  296. }
  297. return;
  298. }
  299. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  300. static void
  301. ixp_request_wq(struct work_struct *work)
  302. {
  303. request_t *r = container_of(work, request_t, work);
  304. ixp_request(r);
  305. }
  306. #endif
  307. /*************************************************************************/
  308. #endif /* BENCH_IXP_ACCESS_LIB */
  309. /*************************************************************************/
  310. int
  311. ocfbench_init(void)
  312. {
  313. int i, jstart, jstop;
  314. printk("Crypto Speed tests\n");
  315. requests = kmalloc(sizeof(request_t) * request_q_len, GFP_KERNEL);
  316. if (!requests) {
  317. printk("malloc failed\n");
  318. return -EINVAL;
  319. }
  320. for (i = 0; i < request_q_len; i++) {
  321. /* +64 for return data */
  322. requests[i].buffer = kmalloc(request_size + 128, GFP_DMA);
  323. if (!requests[i].buffer) {
  324. printk("malloc failed\n");
  325. return -EINVAL;
  326. }
  327. memset(requests[i].buffer, '0' + i, request_size + 128);
  328. }
  329. /*
  330. * OCF benchmark
  331. */
  332. printk("OCF: testing ...\n");
  333. ocf_init();
  334. total = outstanding = 0;
  335. jstart = jiffies;
  336. for (i = 0; i < request_q_len; i++) {
  337. outstanding++;
  338. ocf_request(&requests[i]);
  339. }
  340. while (outstanding > 0)
  341. schedule();
  342. jstop = jiffies;
  343. printk("OCF: %d requests of %d bytes in %d jiffies\n", total, request_size,
  344. jstop - jstart);
  345. #ifdef BENCH_IXP_ACCESS_LIB
  346. /*
  347. * IXP benchmark
  348. */
  349. printk("IXP: testing ...\n");
  350. ixp_init();
  351. total = outstanding = 0;
  352. jstart = jiffies;
  353. for (i = 0; i < request_q_len; i++) {
  354. outstanding++;
  355. ixp_request(&requests[i]);
  356. }
  357. while (outstanding > 0)
  358. schedule();
  359. jstop = jiffies;
  360. printk("IXP: %d requests of %d bytes in %d jiffies\n", total, request_size,
  361. jstop - jstart);
  362. #endif /* BENCH_IXP_ACCESS_LIB */
  363. for (i = 0; i < request_q_len; i++)
  364. kfree(requests[i].buffer);
  365. kfree(requests);
  366. return -EINVAL; /* always fail to load so it can be re-run quickly ;-) */
  367. }
  368. static void __exit ocfbench_exit(void)
  369. {
  370. }
  371. module_init(ocfbench_init);
  372. module_exit(ocfbench_exit);
  373. MODULE_LICENSE("BSD");
  374. MODULE_AUTHOR("David McCullough <[email protected]>");
  375. MODULE_DESCRIPTION("Benchmark various in-kernel crypto speeds");