ocf-bench.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * A loadable module that benchmarks the OCF crypto speed from kernel space.
  3. *
  4. * Copyright (C) 2004-2010 David McCullough <david_mccullough@mcafee.com>
  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. #include <linux/version.h>
  32. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
  33. #include <linux/config.h>
  34. #endif
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/list.h>
  38. #include <linux/slab.h>
  39. #include <linux/wait.h>
  40. #include <linux/sched.h>
  41. #include <linux/spinlock.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 = 40;
  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 = 1488;
  78. module_param(request_size, int, 0);
  79. MODULE_PARM_DESC(request_size, "size of each request");
  80. /*
  81. * OCF batching of requests
  82. */
  83. static int request_batch = 1;
  84. module_param(request_batch, int, 0);
  85. MODULE_PARM_DESC(request_batch, "enable OCF request batching");
  86. /*
  87. * OCF immediate callback on completion
  88. */
  89. static int request_cbimm = 1;
  90. module_param(request_cbimm, int, 0);
  91. MODULE_PARM_DESC(request_cbimm, "enable OCF immediate callback on completion");
  92. /*
  93. * a structure for each request
  94. */
  95. typedef struct {
  96. struct work_struct work;
  97. #ifdef BENCH_IXP_ACCESS_LIB
  98. IX_MBUF mbuf;
  99. #endif
  100. unsigned char *buffer;
  101. } request_t;
  102. static request_t *requests;
  103. static spinlock_t ocfbench_counter_lock;
  104. static int outstanding;
  105. static int total;
  106. /*************************************************************************/
  107. /*
  108. * OCF benchmark routines
  109. */
  110. static uint64_t ocf_cryptoid;
  111. static unsigned long jstart, jstop;
  112. static int ocf_init(void);
  113. static int ocf_cb(struct cryptop *crp);
  114. static void ocf_request(void *arg);
  115. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  116. static void ocf_request_wq(struct work_struct *work);
  117. #endif
  118. static int
  119. ocf_init(void)
  120. {
  121. int error;
  122. struct cryptoini crie, cria;
  123. struct cryptodesc crda, crde;
  124. memset(&crie, 0, sizeof(crie));
  125. memset(&cria, 0, sizeof(cria));
  126. memset(&crde, 0, sizeof(crde));
  127. memset(&crda, 0, sizeof(crda));
  128. cria.cri_alg = CRYPTO_SHA1_HMAC;
  129. cria.cri_klen = 20 * 8;
  130. cria.cri_key = "0123456789abcdefghij";
  131. //crie.cri_alg = CRYPTO_3DES_CBC;
  132. crie.cri_alg = CRYPTO_AES_CBC;
  133. crie.cri_klen = 24 * 8;
  134. crie.cri_key = "0123456789abcdefghijklmn";
  135. crie.cri_next = &cria;
  136. error = crypto_newsession(&ocf_cryptoid, &crie,
  137. CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
  138. if (error) {
  139. printk("crypto_newsession failed %d\n", error);
  140. return -1;
  141. }
  142. return 0;
  143. }
  144. static int
  145. ocf_cb(struct cryptop *crp)
  146. {
  147. request_t *r = (request_t *) crp->crp_opaque;
  148. unsigned long flags;
  149. if (crp->crp_etype)
  150. printk("Error in OCF processing: %d\n", crp->crp_etype);
  151. crypto_freereq(crp);
  152. crp = NULL;
  153. /* do all requests but take at least 1 second */
  154. spin_lock_irqsave(&ocfbench_counter_lock, flags);
  155. total++;
  156. if (total > request_num && jstart + HZ < jiffies) {
  157. outstanding--;
  158. spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
  159. return 0;
  160. }
  161. spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
  162. schedule_work(&r->work);
  163. return 0;
  164. }
  165. static void
  166. ocf_request(void *arg)
  167. {
  168. request_t *r = arg;
  169. struct cryptop *crp = crypto_getreq(2);
  170. struct cryptodesc *crde, *crda;
  171. unsigned long flags;
  172. if (!crp) {
  173. spin_lock_irqsave(&ocfbench_counter_lock, flags);
  174. outstanding--;
  175. spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
  176. return;
  177. }
  178. crde = crp->crp_desc;
  179. crda = crde->crd_next;
  180. crda->crd_skip = 0;
  181. crda->crd_flags = 0;
  182. crda->crd_len = request_size;
  183. crda->crd_inject = request_size;
  184. crda->crd_alg = CRYPTO_SHA1_HMAC;
  185. crda->crd_key = "0123456789abcdefghij";
  186. crda->crd_klen = 20 * 8;
  187. crde->crd_skip = 0;
  188. crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_ENCRYPT;
  189. crde->crd_len = request_size;
  190. crde->crd_inject = request_size;
  191. //crde->crd_alg = CRYPTO_3DES_CBC;
  192. crde->crd_alg = CRYPTO_AES_CBC;
  193. crde->crd_key = "0123456789abcdefghijklmn";
  194. crde->crd_klen = 24 * 8;
  195. crp->crp_ilen = request_size + 64;
  196. crp->crp_flags = 0;
  197. if (request_batch)
  198. crp->crp_flags |= CRYPTO_F_BATCH;
  199. if (request_cbimm)
  200. crp->crp_flags |= CRYPTO_F_CBIMM;
  201. crp->crp_buf = (caddr_t) r->buffer;
  202. crp->crp_callback = ocf_cb;
  203. crp->crp_sid = ocf_cryptoid;
  204. crp->crp_opaque = (caddr_t) r;
  205. crypto_dispatch(crp);
  206. }
  207. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  208. static void
  209. ocf_request_wq(struct work_struct *work)
  210. {
  211. request_t *r = container_of(work, request_t, work);
  212. ocf_request(r);
  213. }
  214. #endif
  215. static void
  216. ocf_done(void)
  217. {
  218. crypto_freesession(ocf_cryptoid);
  219. }
  220. /*************************************************************************/
  221. #ifdef BENCH_IXP_ACCESS_LIB
  222. /*************************************************************************/
  223. /*
  224. * CryptoAcc benchmark routines
  225. */
  226. static IxCryptoAccCtx ixp_ctx;
  227. static UINT32 ixp_ctx_id;
  228. static IX_MBUF ixp_pri;
  229. static IX_MBUF ixp_sec;
  230. static int ixp_registered = 0;
  231. static void ixp_register_cb(UINT32 ctx_id, IX_MBUF *bufp,
  232. IxCryptoAccStatus status);
  233. static void ixp_perform_cb(UINT32 ctx_id, IX_MBUF *sbufp, IX_MBUF *dbufp,
  234. IxCryptoAccStatus status);
  235. static void ixp_request(void *arg);
  236. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  237. static void ixp_request_wq(struct work_struct *work);
  238. #endif
  239. static int
  240. ixp_init(void)
  241. {
  242. IxCryptoAccStatus status;
  243. ixp_ctx.cipherCtx.cipherAlgo = IX_CRYPTO_ACC_CIPHER_3DES;
  244. ixp_ctx.cipherCtx.cipherMode = IX_CRYPTO_ACC_MODE_CBC;
  245. ixp_ctx.cipherCtx.cipherKeyLen = 24;
  246. ixp_ctx.cipherCtx.cipherBlockLen = IX_CRYPTO_ACC_DES_BLOCK_64;
  247. ixp_ctx.cipherCtx.cipherInitialVectorLen = IX_CRYPTO_ACC_DES_IV_64;
  248. memcpy(ixp_ctx.cipherCtx.key.cipherKey, "0123456789abcdefghijklmn", 24);
  249. ixp_ctx.authCtx.authAlgo = IX_CRYPTO_ACC_AUTH_SHA1;
  250. ixp_ctx.authCtx.authDigestLen = 12;
  251. ixp_ctx.authCtx.aadLen = 0;
  252. ixp_ctx.authCtx.authKeyLen = 20;
  253. memcpy(ixp_ctx.authCtx.key.authKey, "0123456789abcdefghij", 20);
  254. ixp_ctx.useDifferentSrcAndDestMbufs = 0;
  255. ixp_ctx.operation = IX_CRYPTO_ACC_OP_ENCRYPT_AUTH ;
  256. IX_MBUF_MLEN(&ixp_pri) = IX_MBUF_PKT_LEN(&ixp_pri) = 128;
  257. IX_MBUF_MDATA(&ixp_pri) = (unsigned char *) kmalloc(128, SLAB_ATOMIC);
  258. IX_MBUF_MLEN(&ixp_sec) = IX_MBUF_PKT_LEN(&ixp_sec) = 128;
  259. IX_MBUF_MDATA(&ixp_sec) = (unsigned char *) kmalloc(128, SLAB_ATOMIC);
  260. status = ixCryptoAccCtxRegister(&ixp_ctx, &ixp_pri, &ixp_sec,
  261. ixp_register_cb, ixp_perform_cb, &ixp_ctx_id);
  262. if (IX_CRYPTO_ACC_STATUS_SUCCESS == status) {
  263. while (!ixp_registered)
  264. schedule();
  265. return ixp_registered < 0 ? -1 : 0;
  266. }
  267. printk("ixp: ixCryptoAccCtxRegister failed %d\n", status);
  268. return -1;
  269. }
  270. static void
  271. ixp_register_cb(UINT32 ctx_id, IX_MBUF *bufp, IxCryptoAccStatus status)
  272. {
  273. if (bufp) {
  274. IX_MBUF_MLEN(bufp) = IX_MBUF_PKT_LEN(bufp) = 0;
  275. kfree(IX_MBUF_MDATA(bufp));
  276. IX_MBUF_MDATA(bufp) = NULL;
  277. }
  278. if (IX_CRYPTO_ACC_STATUS_WAIT == status)
  279. return;
  280. if (IX_CRYPTO_ACC_STATUS_SUCCESS == status)
  281. ixp_registered = 1;
  282. else
  283. ixp_registered = -1;
  284. }
  285. static void
  286. ixp_perform_cb(
  287. UINT32 ctx_id,
  288. IX_MBUF *sbufp,
  289. IX_MBUF *dbufp,
  290. IxCryptoAccStatus status)
  291. {
  292. request_t *r = NULL;
  293. unsigned long flags;
  294. /* do all requests but take at least 1 second */
  295. spin_lock_irqsave(&ocfbench_counter_lock, flags);
  296. total++;
  297. if (total > request_num && jstart + HZ < jiffies) {
  298. outstanding--;
  299. spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
  300. return;
  301. }
  302. if (!sbufp || !(r = IX_MBUF_PRIV(sbufp))) {
  303. printk("crappo %p %p\n", sbufp, r);
  304. outstanding--;
  305. spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
  306. return;
  307. }
  308. spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
  309. schedule_work(&r->work);
  310. }
  311. static void
  312. ixp_request(void *arg)
  313. {
  314. request_t *r = arg;
  315. IxCryptoAccStatus status;
  316. unsigned long flags;
  317. memset(&r->mbuf, 0, sizeof(r->mbuf));
  318. IX_MBUF_MLEN(&r->mbuf) = IX_MBUF_PKT_LEN(&r->mbuf) = request_size + 64;
  319. IX_MBUF_MDATA(&r->mbuf) = r->buffer;
  320. IX_MBUF_PRIV(&r->mbuf) = r;
  321. status = ixCryptoAccAuthCryptPerform(ixp_ctx_id, &r->mbuf, NULL,
  322. 0, request_size, 0, request_size, request_size, r->buffer);
  323. if (IX_CRYPTO_ACC_STATUS_SUCCESS != status) {
  324. printk("status1 = %d\n", status);
  325. spin_lock_irqsave(&ocfbench_counter_lock, flags);
  326. outstanding--;
  327. spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
  328. return;
  329. }
  330. return;
  331. }
  332. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  333. static void
  334. ixp_request_wq(struct work_struct *work)
  335. {
  336. request_t *r = container_of(work, request_t, work);
  337. ixp_request(r);
  338. }
  339. #endif
  340. static void
  341. ixp_done(void)
  342. {
  343. /* we should free the session here but I am lazy :-) */
  344. }
  345. /*************************************************************************/
  346. #endif /* BENCH_IXP_ACCESS_LIB */
  347. /*************************************************************************/
  348. int
  349. ocfbench_init(void)
  350. {
  351. int i;
  352. unsigned long mbps;
  353. unsigned long flags;
  354. printk("Crypto Speed tests\n");
  355. requests = kmalloc(sizeof(request_t) * request_q_len, GFP_KERNEL);
  356. if (!requests) {
  357. printk("malloc failed\n");
  358. return -EINVAL;
  359. }
  360. for (i = 0; i < request_q_len; i++) {
  361. /* +64 for return data */
  362. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  363. INIT_WORK(&requests[i].work, ocf_request_wq);
  364. #else
  365. INIT_WORK(&requests[i].work, ocf_request, &requests[i]);
  366. #endif
  367. requests[i].buffer = kmalloc(request_size + 128, GFP_DMA);
  368. if (!requests[i].buffer) {
  369. printk("malloc failed\n");
  370. return -EINVAL;
  371. }
  372. memset(requests[i].buffer, '0' + i, request_size + 128);
  373. }
  374. /*
  375. * OCF benchmark
  376. */
  377. printk("OCF: testing ...\n");
  378. if (ocf_init() == -1)
  379. return -EINVAL;
  380. spin_lock_init(&ocfbench_counter_lock);
  381. total = outstanding = 0;
  382. jstart = jiffies;
  383. for (i = 0; i < request_q_len; i++) {
  384. spin_lock_irqsave(&ocfbench_counter_lock, flags);
  385. outstanding++;
  386. spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
  387. ocf_request(&requests[i]);
  388. }
  389. while (outstanding > 0)
  390. schedule();
  391. jstop = jiffies;
  392. mbps = 0;
  393. if (jstop > jstart) {
  394. mbps = (unsigned long) total * (unsigned long) request_size * 8;
  395. mbps /= ((jstop - jstart) * 1000) / HZ;
  396. }
  397. printk("OCF: %d requests of %d bytes in %d jiffies (%d.%03d Mbps)\n",
  398. total, request_size, (int)(jstop - jstart),
  399. ((int)mbps) / 1000, ((int)mbps) % 1000);
  400. ocf_done();
  401. #ifdef BENCH_IXP_ACCESS_LIB
  402. /*
  403. * IXP benchmark
  404. */
  405. printk("IXP: testing ...\n");
  406. ixp_init();
  407. total = outstanding = 0;
  408. jstart = jiffies;
  409. for (i = 0; i < request_q_len; i++) {
  410. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  411. INIT_WORK(&requests[i].work, ixp_request_wq);
  412. #else
  413. INIT_WORK(&requests[i].work, ixp_request, &requests[i]);
  414. #endif
  415. spin_lock_irqsave(&ocfbench_counter_lock, flags);
  416. outstanding++;
  417. spin_unlock_irqrestore(&ocfbench_counter_lock, flags);
  418. ixp_request(&requests[i]);
  419. }
  420. while (outstanding > 0)
  421. schedule();
  422. jstop = jiffies;
  423. mbps = 0;
  424. if (jstop > jstart) {
  425. mbps = (unsigned long) total * (unsigned long) request_size * 8;
  426. mbps /= ((jstop - jstart) * 1000) / HZ;
  427. }
  428. printk("IXP: %d requests of %d bytes in %d jiffies (%d.%03d Mbps)\n",
  429. total, request_size, jstop - jstart,
  430. ((int)mbps) / 1000, ((int)mbps) % 1000);
  431. ixp_done();
  432. #endif /* BENCH_IXP_ACCESS_LIB */
  433. for (i = 0; i < request_q_len; i++)
  434. kfree(requests[i].buffer);
  435. kfree(requests);
  436. return -EINVAL; /* always fail to load so it can be re-run quickly ;-) */
  437. }
  438. static void __exit ocfbench_exit(void)
  439. {
  440. }
  441. module_init(ocfbench_init);
  442. module_exit(ocfbench_exit);
  443. MODULE_LICENSE("Dual BSD/GPL");
  444. MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
  445. MODULE_DESCRIPTION("Benchmark various in-kernel crypto speeds");