Whamcloud - gitweb
LU-3570 libcfs: accelerate crc32c with pclmulqdq
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-crypto.c
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 only,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License version 2 for more details (a copy is included
13  * in the LICENSE file that accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License
16  * version 2 along with this program; If not, see http://www.gnu.org/licenses
17  *
18  * Please  visit http://www.xyratex.com/contact if you need additional
19  * information or have any questions.
20  *
21  * GPL HEADER END
22  */
23
24 /*
25  * Copyright 2012 Xyratex Technology Limited
26  *
27  * Copyright (c) 2012, Intel Corporation.
28  */
29
30 #include <linux/crypto.h>
31 #include <linux/scatterlist.h>
32 #include <libcfs/libcfs.h>
33 #include <libcfs/linux/linux-crypto.h>
34 /**
35  *  Array of  hash algorithm speed in MByte per second
36  */
37 static int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX];
38
39 static int cfs_crypto_hash_alloc(unsigned char alg_id,
40                                  const struct cfs_crypto_hash_type **type,
41                                  struct hash_desc *desc, unsigned char *key,
42                                  unsigned int key_len)
43 {
44         int     err = 0;
45
46         *type = cfs_crypto_hash_type(alg_id);
47
48         if (*type == NULL) {
49                 CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
50                       alg_id, CFS_HASH_ALG_MAX);
51                 return -EINVAL;
52         }
53         desc->tfm = crypto_alloc_hash((*type)->cht_name, 0, 0);
54
55         if (desc->tfm == NULL)
56                 return -EINVAL;
57
58         if (IS_ERR(desc->tfm)) {
59                 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
60                        (*type)->cht_name);
61                 return PTR_ERR(desc->tfm);
62         }
63
64         desc->flags = 0;
65
66         if (key != NULL) {
67                 err = crypto_hash_setkey(desc->tfm, key, key_len);
68         } else if ((*type)->cht_key != 0) {
69                 err = crypto_hash_setkey(desc->tfm,
70                                          (unsigned char *)&((*type)->cht_key),
71                                          (*type)->cht_size);
72         }
73
74         if (err != 0) {
75                 crypto_free_hash(desc->tfm);
76                 return err;
77         }
78
79         CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n",
80                (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_name,
81                (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_driver_name,
82                cfs_crypto_hash_speeds[alg_id]);
83
84         return crypto_hash_init(desc);
85 }
86
87 int cfs_crypto_hash_digest(unsigned char alg_id,
88                            const void *buf, unsigned int buf_len,
89                            unsigned char *key, unsigned int key_len,
90                            unsigned char *hash, unsigned int *hash_len)
91 {
92         struct scatterlist      sl;
93         struct hash_desc        hdesc;
94         int                     err;
95         const struct cfs_crypto_hash_type       *type;
96
97         if (buf == NULL || buf_len == 0 || hash_len == NULL)
98                 return -EINVAL;
99
100         err = cfs_crypto_hash_alloc(alg_id, &type, &hdesc, key, key_len);
101         if (err != 0)
102                 return err;
103
104         if (hash == NULL || *hash_len < type->cht_size) {
105                 *hash_len = type->cht_size;
106                 crypto_free_hash(hdesc.tfm);
107                 return -ENOSPC;
108         }
109         sg_init_one(&sl, (void *)buf, buf_len);
110
111         hdesc.flags = 0;
112         err = crypto_hash_digest(&hdesc, &sl, sl.length, hash);
113         crypto_free_hash(hdesc.tfm);
114
115         return err;
116 }
117 EXPORT_SYMBOL(cfs_crypto_hash_digest);
118
119 struct cfs_crypto_hash_desc *
120         cfs_crypto_hash_init(unsigned char alg_id,
121                              unsigned char *key, unsigned int key_len)
122 {
123
124         struct  hash_desc       *hdesc;
125         int                  err;
126         const struct cfs_crypto_hash_type       *type;
127
128         hdesc = kmalloc(sizeof(*hdesc), 0);
129         if (hdesc == NULL)
130                 return ERR_PTR(-ENOMEM);
131
132         err = cfs_crypto_hash_alloc(alg_id, &type, hdesc, key, key_len);
133
134         if (err) {
135                 kfree(hdesc);
136                 return ERR_PTR(err);
137         }
138         return (struct cfs_crypto_hash_desc *)hdesc;
139 }
140 EXPORT_SYMBOL(cfs_crypto_hash_init);
141
142 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
143                                 struct page *page, unsigned int offset,
144                                 unsigned int len)
145 {
146         struct scatterlist sl;
147
148         sg_init_table(&sl, 1);
149         sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
150
151         return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
152 }
153 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
154
155 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
156                            const void *buf, unsigned int buf_len)
157 {
158         struct scatterlist sl;
159
160         sg_init_one(&sl, (void *)buf, buf_len);
161
162         return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
163 }
164 EXPORT_SYMBOL(cfs_crypto_hash_update);
165
166 /*      If hash_len pointer is NULL - destroy descriptor. */
167 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
168                           unsigned char *hash, unsigned int *hash_len)
169 {
170         int     err;
171         int     size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
172
173         if (hash_len == NULL) {
174                 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
175                 kfree(hdesc);
176                 return 0;
177         }
178         if (hash == NULL || *hash_len < size) {
179                 *hash_len = size;
180                 return -ENOSPC;
181         }
182         err = crypto_hash_final((struct hash_desc *) hdesc, hash);
183
184         if (err < 0) {
185                 /* May be caller can fix error */
186                 return err;
187         }
188         crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
189         kfree(hdesc);
190         return err;
191 }
192 EXPORT_SYMBOL(cfs_crypto_hash_final);
193
194 static void cfs_crypto_performance_test(unsigned char alg_id,
195                                         const unsigned char *buf,
196                                         unsigned int buf_len)
197 {
198         unsigned long              start, end;
199         int                          bcount, err = 0;
200         int                          sec = 1; /* do test only 1 sec */
201         unsigned char              hash[64];
202         unsigned int                hash_len = 64;
203
204         for (start = jiffies, end = start + sec * HZ, bcount = 0;
205              time_before(jiffies, end); bcount++) {
206                 err = cfs_crypto_hash_digest(alg_id, buf, buf_len, NULL, 0,
207                                              hash, &hash_len);
208                 if (err)
209                         break;
210
211         }
212         end = jiffies;
213
214         if (err) {
215                 cfs_crypto_hash_speeds[alg_id] =  -1;
216                 CDEBUG(D_INFO, "Crypto hash algorithm %s, err = %d\n",
217                        cfs_crypto_hash_name(alg_id), err);
218         } else {
219                 unsigned long   tmp;
220                 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
221                        1000) / (1024 * 1024);
222                 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
223         }
224         CDEBUG(D_CONFIG, "Crypto hash algorithm %s speed = %d MB/s\n",
225                cfs_crypto_hash_name(alg_id), cfs_crypto_hash_speeds[alg_id]);
226 }
227
228 int cfs_crypto_hash_speed(unsigned char hash_alg)
229 {
230         if (hash_alg < CFS_HASH_ALG_MAX)
231                 return cfs_crypto_hash_speeds[hash_alg];
232         else
233                 return -1;
234 }
235 EXPORT_SYMBOL(cfs_crypto_hash_speed);
236
237 /**
238  * Do performance test for all hash algorithms.
239  */
240 static int cfs_crypto_test_hashes(void)
241 {
242         unsigned char      i;
243         unsigned char      *data;
244         unsigned int        j;
245         /* Data block size for testing hash. Maximum
246          * kmalloc size for 2.6.18 kernel is 128K */
247         unsigned int        data_len = 1 * 128 * 1024;
248
249         data = kmalloc(data_len, 0);
250         if (data == NULL) {
251                 CERROR("Failed to allocate mem\n");
252                 return -ENOMEM;
253         }
254
255         for (j = 0; j < data_len; j++)
256                 data[j] = j & 0xff;
257
258         for (i = 0; i < CFS_HASH_ALG_MAX; i++)
259                 cfs_crypto_performance_test(i, data, data_len);
260
261         kfree(data);
262         return 0;
263 }
264
265 static int adler32;
266
267 #ifdef HAVE_CRC32
268 static int crc32;
269 #endif
270 #ifdef HAVE_PCLMULQDQ
271 #ifdef NEED_CRC32_ACCEL
272 static int crc32pclmul;
273 #endif
274 #ifdef NEED_CRC32C_ACCEL
275 static int crc32c_pclmul;
276 #endif
277 #endif
278
279 int cfs_crypto_register(void)
280 {
281         request_module("crc32c");
282
283         adler32 = cfs_crypto_adler32_register();
284
285 #ifdef HAVE_CRC32
286         crc32 = cfs_crypto_crc32_register();
287 #endif
288 #ifdef HAVE_PCLMULQDQ
289 #ifdef NEED_CRC32_ACCEL
290         crc32pclmul = cfs_crypto_crc32_pclmul_register();
291 #endif
292 #ifdef NEED_CRC32C_ACCEL
293         crc32c_pclmul = cfs_crypto_crc32c_pclmul_register();
294 #endif
295 #endif
296         /* check all algorithms and do performance test */
297         cfs_crypto_test_hashes();
298         return 0;
299 }
300 void cfs_crypto_unregister(void)
301 {
302         if (adler32 == 0)
303                 cfs_crypto_adler32_unregister();
304
305 #ifdef HAVE_CRC32
306         if (crc32 == 0)
307                 cfs_crypto_crc32_unregister();
308 #endif
309 #ifdef HAVE_PCLMULQDQ
310 #ifdef NEED_CRC32_ACCEL
311         if (crc32pclmul == 0)
312                 cfs_crypto_crc32_pclmul_unregister();
313 #endif
314 #ifdef NEED_CRC32C_ACCEL
315         if (crc32c_pclmul == 0)
316                 cfs_crypto_crc32c_pclmul_unregister();
317 #endif
318 #endif
319         return;
320 }