4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2018, DataDirect Networks Storage.
26 * General data integrity functions
28 #include <linux/blkdev.h>
29 #include <linux/crc-t10dif.h>
30 #include <asm-generic/checksum.h>
31 #include <obd_class.h>
32 #include <obd_cksum.h>
34 __u16 obd_dif_crc_fn(void *data, unsigned int len)
36 return cpu_to_be16(crc_t10dif(data, len));
38 EXPORT_SYMBOL(obd_dif_crc_fn);
40 __u16 obd_dif_ip_fn(void *data, unsigned int len)
42 return ip_compute_csum(data, len);
44 EXPORT_SYMBOL(obd_dif_ip_fn);
46 int obd_page_dif_generate_buffer(const char *obd_name, struct page *page,
47 __u32 offset, __u32 length,
48 __u16 *guard_start, int guard_number,
49 int *used_number, int sector_size,
54 __u16 *guard_buf = guard_start;
55 unsigned int data_size;
58 data_buf = kmap(page) + offset;
59 for (i = 0; i < length; i += sector_size) {
60 if (used >= guard_number) {
61 CERROR("%s: unexpected used guard number of DIF %u/%u, "
62 "data length %u, sector size %u: rc = %d\n",
63 obd_name, used, guard_number, length,
67 data_size = length - i;
68 if (data_size > sector_size)
69 data_size = sector_size;
70 *guard_buf = fn(data_buf, data_size);
72 data_buf += data_size;
80 EXPORT_SYMBOL(obd_page_dif_generate_buffer);
82 static int __obd_t10_performance_test(const char *obd_name,
83 enum cksum_types cksum_type,
84 struct page *data_page,
87 unsigned char cfs_alg = cksum_obd2cfs(OBD_CKSUM_T10_TOP);
88 struct cfs_crypto_hash_desc *hdesc;
89 obd_dif_csum_fn *fn = NULL;
91 unsigned char *buffer;
103 obd_t10_cksum2dif(cksum_type, &fn, §or_size);
107 __page = alloc_page(GFP_KERNEL);
111 hdesc = cfs_crypto_hash_init(cfs_alg, NULL, 0);
114 CERROR("%s: unable to initialize checksum hash %s: rc = %d\n",
115 obd_name, cfs_crypto_hash_name(cfs_alg), rc);
119 buffer = kmap(__page);
120 guard_start = (__u16 *)buffer;
121 guard_number = PAGE_SIZE / sizeof(*guard_start);
122 for (i = 0; i < repeat_number; i++) {
124 * The left guard number should be able to hold checksums of a
127 rc = obd_page_dif_generate_buffer(obd_name, data_page, 0,
129 guard_start + used_number,
130 guard_number - used_number,
131 &used, sector_size, fn);
136 if (used_number == guard_number) {
137 cfs_crypto_hash_update_page(hdesc, __page, 0,
138 used_number * sizeof(*guard_start));
146 if (used_number != 0)
147 cfs_crypto_hash_update_page(hdesc, __page, 0,
148 used_number * sizeof(*guard_start));
150 bufsize = sizeof(cksum);
152 rc2 = cfs_crypto_hash_final(hdesc, (unsigned char *)&cksum, &bufsize);
161 * Array of T10PI checksum algorithm speed in MByte per second
163 static int obd_t10_cksum_speeds[OBD_T10_CKSUM_MAX];
165 static enum obd_t10_cksum_type
166 obd_t10_cksum2type(enum cksum_types cksum_type)
168 switch (cksum_type) {
169 case OBD_CKSUM_T10IP512:
170 return OBD_T10_CKSUM_IP512;
171 case OBD_CKSUM_T10IP4K:
172 return OBD_T10_CKSUM_IP4K;
173 case OBD_CKSUM_T10CRC512:
174 return OBD_T10_CKSUM_CRC512;
175 case OBD_CKSUM_T10CRC4K:
176 return OBD_T10_CKSUM_CRC4K;
178 return OBD_T10_CKSUM_UNKNOWN;
182 static const char *obd_t10_cksum_name(enum obd_t10_cksum_type index)
186 /* Need to skip "crc32", "adler", "crc32c", "reserved" */
187 return cksum_name[3 + index];
191 * Compute the speed of specified T10PI checksum type
193 * Run a speed test on the given T10PI checksum on buffer using a 1MB buffer
194 * size. This is a reasonable buffer size for Lustre RPCs, even if the actual
195 * RPC size is larger or smaller.
197 * The speed is stored internally in the obd_t10_cksum_speeds[] array, and
198 * is available through the obd_t10_cksum_speed() function.
200 * This function needs to stay the same as cfs_crypto_performance_test() so
201 * that the speeds are comparable. And this function should reflect the real
202 * cost of the checksum calculation.
204 * \param[in] obd_name name of the OBD device
205 * \param[in] cksum_type checksum type (OBD_CKSUM_T10*)
207 static void obd_t10_performance_test(const char *obd_name,
208 enum cksum_types cksum_type)
210 enum obd_t10_cksum_type index = obd_t10_cksum2type(cksum_type);
211 const int buf_len = max(PAGE_SIZE, 1048576UL);
212 unsigned long bcount;
219 page = alloc_page(GFP_KERNEL);
226 memset(buf, 0xAD, PAGE_SIZE);
229 for (start = jiffies, end = start + msecs_to_jiffies(MSEC_PER_SEC / 4),
230 bcount = 0; time_before(jiffies, end) && rc == 0; bcount++) {
231 rc = __obd_t10_performance_test(obd_name, cksum_type, page,
232 buf_len / PAGE_SIZE);
240 obd_t10_cksum_speeds[index] = rc;
241 CDEBUG(D_INFO, "%s: T10 checksum algorithm %s test error: "
242 "rc = %d\n", obd_name, obd_t10_cksum_name(index), rc);
246 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
247 1000) / (1024 * 1024);
248 obd_t10_cksum_speeds[index] = (int)tmp;
249 CDEBUG(D_CONFIG, "%s: T10 checksum algorithm %s speed = %d "
250 "MB/s\n", obd_name, obd_t10_cksum_name(index),
251 obd_t10_cksum_speeds[index]);
255 int obd_t10_cksum_speed(const char *obd_name,
256 enum cksum_types cksum_type)
258 enum obd_t10_cksum_type index = obd_t10_cksum2type(cksum_type);
260 if (unlikely(obd_t10_cksum_speeds[index] == 0)) {
261 static DEFINE_MUTEX(obd_t10_cksum_speed_mutex);
263 mutex_lock(&obd_t10_cksum_speed_mutex);
264 if (obd_t10_cksum_speeds[index] == 0)
265 obd_t10_performance_test(obd_name, cksum_type);
266 mutex_unlock(&obd_t10_cksum_speed_mutex);
269 return obd_t10_cksum_speeds[index];
271 EXPORT_SYMBOL(obd_t10_cksum_speed);