blob: e261a01abc1b294f64fd4dea91e81dd5fdce4456 [file] [log] [blame]
Jens Axboee29d1b72006-10-18 15:43:15 +02001/*
2 * IO verification helpers
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
Jens Axboe97af62c2007-05-22 11:12:13 +02007#include <assert.h>
Jens Axboee29d1b72006-10-18 15:43:15 +02008
9#include "fio.h"
Jens Axboee29d1b72006-10-18 15:43:15 +020010
Jens Axboecd14cc12007-07-30 10:59:33 +020011#include "md5.h"
12#include "crc64.h"
13#include "crc32.h"
14#include "crc16.h"
15#include "crc7.h"
16#include "sha256.h"
17#include "sha512.h"
18
Jens Axboe90059d62007-07-30 09:33:12 +020019static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
Jens Axboee29d1b72006-10-18 15:43:15 +020020{
21 unsigned int todo;
Jens Axboe4c5946c2007-07-26 11:55:10 +020022 int r;
Jens Axboee29d1b72006-10-18 15:43:15 +020023
24 while (len) {
Jens Axboe4c5946c2007-07-26 11:55:10 +020025 r = os_random_long(&td->verify_state);
Jens Axboee29d1b72006-10-18 15:43:15 +020026
27 /*
28 * lrand48_r seems to be broken and only fill the bottom
29 * 32-bits, even on 64-bit archs with 64-bit longs
30 */
31 todo = sizeof(r);
32 if (todo > len)
33 todo = len;
34
35 memcpy(p, &r, todo);
36
37 len -= todo;
38 p += todo;
39 }
40}
41
Jens Axboe90059d62007-07-30 09:33:12 +020042static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
43{
44 switch (td->o.verify_pattern_bytes) {
45 case 0:
46 fill_random_bytes(td, p, len);
47 break;
48 case 1:
49 memset(p, td->o.verify_pattern, len);
50 break;
51 case 2:
52 case 3:
53 case 4: {
54 unsigned int pattern = td->o.verify_pattern;
55 unsigned int i = 0;
56 unsigned char c1, c2, c3, c4;
57 unsigned char *b = p;
58
59 c1 = pattern & 0xff;
60 pattern >>= 8;
61 c2 = pattern & 0xff;
62 pattern >>= 8;
63 c3 = pattern & 0xff;
64 pattern >>= 8;
65 c4 = pattern & 0xff;
66
67 while (i < len) {
68 b[i++] = c1;
69 if (i == len)
70 break;
71 b[i++] = c2;
72 if (td->o.verify_pattern_bytes == 2 || i == len)
73 continue;
74 b[i++] = c3;
75 if (td->o.verify_pattern_bytes == 3 || i == len)
76 continue;
77 b[i++] = c4;
78 }
79 break;
80 }
81 }
82}
83
84static void memswp(void* buf1, void* buf2, unsigned int len)
Shawn Lewis546a9142007-07-28 21:11:37 +020085{
86 struct verify_header swap;
Jens Axboe90059d62007-07-30 09:33:12 +020087
Shawn Lewis546a9142007-07-28 21:11:37 +020088 memcpy(&swap, buf1, len);
89 memcpy(buf1, buf2, len);
90 memcpy(buf2, &swap, len);
91}
92
Jens Axboee29d1b72006-10-18 15:43:15 +020093static void hexdump(void *buffer, int len)
94{
95 unsigned char *p = buffer;
96 int i;
97
98 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +010099 log_info("%02x", p[i]);
100 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +0200101}
102
Jens Axboed9f2caf2007-07-28 21:22:03 +0200103/*
Jens Axboe87677832007-07-30 12:08:14 +0200104 * Prepare for seperation of verify_header and checksum header
105 */
106static inline unsigned int __hdr_size(int fio_unused verify_type)
107{
108 return sizeof(struct verify_header);
109
110}
111
112static inline unsigned int hdr_size(struct verify_header *hdr)
113{
114 return sizeof(*hdr);
115}
116
117/*
Jens Axboed9f2caf2007-07-28 21:22:03 +0200118 * Return data area 'header_num'
119 */
120static inline void *io_u_verify_off(struct verify_header *hdr,
121 struct io_u *io_u,
122 unsigned char header_num)
123{
Jens Axboe87677832007-07-30 12:08:14 +0200124 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
Jens Axboed9f2caf2007-07-28 21:22:03 +0200125}
126
Jens Axboecd14cc12007-07-30 10:59:33 +0200127static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
128 unsigned int header_num)
129{
130 void *p = io_u_verify_off(hdr, io_u, header_num);
131 uint8_t sha512[128];
132 struct sha512_ctx sha512_ctx = {
133 .buf = sha512,
134 };
135
136 sha512_init(&sha512_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200137 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200138
139 if (memcmp(hdr->sha512, sha512_ctx.buf, sizeof(sha512))) {
140 log_err("sha512: verify failed at %llu/%u\n",
141 io_u->offset + header_num * hdr->len,
142 hdr->len);
143 hexdump(hdr->sha512, sizeof(hdr->sha512));
144 hexdump(sha512_ctx.buf, sizeof(sha512));
145 return 1;
146 }
147
148 return 0;
149}
150
151static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
152 unsigned int header_num)
153{
154 void *p = io_u_verify_off(hdr, io_u, header_num);
155 uint8_t sha256[128];
156 struct sha256_ctx sha256_ctx = {
157 .buf = sha256,
158 };
159
160 sha256_init(&sha256_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200161 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboecd14cc12007-07-30 10:59:33 +0200162
163 if (memcmp(hdr->sha256, sha256_ctx.buf, sizeof(sha256))) {
164 log_err("sha256: verify failed at %llu/%u\n",
165 io_u->offset + header_num * hdr->len,
166 hdr->len);
167 hexdump(hdr->sha256, sizeof(hdr->sha256));
168 hexdump(sha256_ctx.buf, sizeof(sha256));
169 return 1;
170 }
171
172 return 0;
173}
174
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200175static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
176 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +0200177{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200178 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200179 unsigned char c;
180
Jens Axboe87677832007-07-30 12:08:14 +0200181 c = crc7(p, hdr->len - hdr_size(hdr));
Jens Axboe1e154bd2007-07-27 09:52:40 +0200182
183 if (c != hdr->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200184 log_err("crc7: verify failed at %llu/%u\n",
185 io_u->offset + header_num * hdr->len,
186 hdr->len);
Jens Axboe1e154bd2007-07-27 09:52:40 +0200187 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
188 return 1;
189 }
190
191 return 0;
192}
193
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200194static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
195 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +0200196{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200197 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200198 unsigned short c;
199
Jens Axboe87677832007-07-30 12:08:14 +0200200 c = crc16(p, hdr->len - hdr_size(hdr));
Jens Axboe969f7ed2007-07-27 09:07:17 +0200201
202 if (c != hdr->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200203 log_err("crc16: verify failed at %llu/%u\n",
204 io_u->offset + header_num * hdr->len,
205 hdr->len);
Jens Axboe1f24ea42007-07-27 09:53:14 +0200206 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
Jens Axboe969f7ed2007-07-27 09:07:17 +0200207 return 1;
208 }
209
210 return 0;
211}
212
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200213static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
214 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +0200215{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200216 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboed77a7af2007-07-27 15:35:06 +0200217 unsigned long long c;
218
Jens Axboe87677832007-07-30 12:08:14 +0200219 c = crc64(p, hdr->len - hdr_size(hdr));
Jens Axboed77a7af2007-07-27 15:35:06 +0200220
221 if (c != hdr->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200222 log_err("crc64: verify failed at %llu/%u\n",
223 io_u->offset + header_num * hdr->len,
224 hdr->len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200225 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
226 return 1;
227 }
228
229 return 0;
230}
231
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200232static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
233 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200234{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200235 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboee29d1b72006-10-18 15:43:15 +0200236 unsigned long c;
237
Jens Axboe87677832007-07-30 12:08:14 +0200238 c = crc32(p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200239
240 if (c != hdr->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200241 log_err("crc32: verify failed at %llu/%u\n",
242 io_u->offset + header_num * hdr->len,
243 hdr->len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200244 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
245 return 1;
246 }
247
248 return 0;
249}
250
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200251static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
252 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200253{
Jens Axboed9f2caf2007-07-28 21:22:03 +0200254 void *p = io_u_verify_off(hdr, io_u, header_num);
Jens Axboe8c432322007-07-27 13:16:24 +0200255 uint32_t hash[MD5_HASH_WORDS];
256 struct md5_ctx md5_ctx = {
257 .hash = hash,
258 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200259
Jens Axboe61f821f2007-07-30 10:18:06 +0200260 md5_init(&md5_ctx);
Jens Axboe87677832007-07-30 12:08:14 +0200261 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200262
Jens Axboec9b3fdf2007-07-29 08:49:16 +0200263 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200264 log_err("md5: verify failed at %llu/%u\n",
265 io_u->offset + header_num * hdr->len,
266 hdr->len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200267 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200268 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboee29d1b72006-10-18 15:43:15 +0200269 return 1;
270 }
271
272 return 0;
273}
274
Jens Axboe36690c92007-03-26 10:23:34 +0200275int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200276{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200277 struct verify_header *hdr;
278 unsigned int hdr_inc, hdr_num = 0;
Jens Axboe95646102007-07-28 21:17:50 +0200279 void *p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200280 int ret;
281
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200282 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200283 return 0;
284
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200285 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200286 if (td->o.verify_interval)
287 hdr_inc = td->o.verify_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200288
Jens Axboe95646102007-07-28 21:17:50 +0200289 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
Jens Axboea59e1702007-07-30 08:53:27 +0200290 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200291 memswp(p, p + td->o.verify_offset, __hdr_size(hdr->verify_type));
Shawn Lewis546a9142007-07-28 21:11:37 +0200292
Jens Axboe95646102007-07-28 21:17:50 +0200293 hdr = p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200294
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200295 if (hdr->fio_magic != FIO_HDR_MAGIC) {
296 log_err("Bad verify header %x\n", hdr->fio_magic);
297 return EIO;
298 }
299
300 switch (hdr->verify_type) {
301 case VERIFY_MD5:
302 ret = verify_io_u_md5(hdr, io_u, hdr_num);
303 break;
304 case VERIFY_CRC64:
305 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
306 break;
307 case VERIFY_CRC32:
308 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
309 break;
310 case VERIFY_CRC16:
311 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
312 break;
313 case VERIFY_CRC7:
314 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
315 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200316 case VERIFY_SHA256:
317 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
318 break;
319 case VERIFY_SHA512:
320 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
321 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200322 default:
323 log_err("Bad verify type %u\n", hdr->verify_type);
324 ret = 1;
325 }
326 hdr_num++;
327 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100328
329 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200330}
331
Jens Axboecd14cc12007-07-30 10:59:33 +0200332static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
333{
334 struct sha512_ctx sha512_ctx = {
335 .buf = hdr->sha512,
336 };
337
338 sha512_init(&sha512_ctx);
339 sha512_update(&sha512_ctx, p, len);
340}
341
342static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
343{
344 struct sha256_ctx sha256_ctx = {
345 .buf = hdr->sha256,
346 };
347
348 sha256_init(&sha256_ctx);
349 sha256_update(&sha256_ctx, p, len);
350}
351
Jens Axboe1e154bd2007-07-27 09:52:40 +0200352static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
353{
354 hdr->crc7 = crc7(p, len);
355}
356
Jens Axboe969f7ed2007-07-27 09:07:17 +0200357static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
358{
359 hdr->crc16 = crc16(p, len);
360}
361
Jens Axboee29d1b72006-10-18 15:43:15 +0200362static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
363{
364 hdr->crc32 = crc32(p, len);
365}
366
Jens Axboed77a7af2007-07-27 15:35:06 +0200367static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
368{
369 hdr->crc64 = crc64(p, len);
370}
371
Jens Axboee29d1b72006-10-18 15:43:15 +0200372static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
373{
Jens Axboe8c432322007-07-27 13:16:24 +0200374 struct md5_ctx md5_ctx = {
375 .hash = (uint32_t *) hdr->md5_digest,
376 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200377
Jens Axboe61f821f2007-07-30 10:18:06 +0200378 md5_init(&md5_ctx);
Jens Axboee29d1b72006-10-18 15:43:15 +0200379 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200380}
381
382/*
383 * fill body of io_u->buf with random data and add a header with the
384 * crc32 or md5 sum of that data.
385 */
386void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
387{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200388 struct verify_header *hdr;
Jens Axboe95646102007-07-28 21:17:50 +0200389 void *p = io_u->buf, *data;
Shawn Lewis546a9142007-07-28 21:11:37 +0200390 unsigned int hdr_inc, data_len;
Jens Axboee29d1b72006-10-18 15:43:15 +0200391
Jens Axboe9cc3d152007-03-26 10:32:30 +0200392 if (td->o.verify == VERIFY_NULL)
393 return;
394
Jens Axboe90059d62007-07-30 09:33:12 +0200395 fill_pattern(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200396
Shawn Lewis546a9142007-07-28 21:11:37 +0200397 hdr_inc = io_u->buflen;
Jens Axboea59e1702007-07-30 08:53:27 +0200398 if (td->o.verify_interval)
399 hdr_inc = td->o.verify_interval;
Shawn Lewis546a9142007-07-28 21:11:37 +0200400
Jens Axboe95646102007-07-28 21:17:50 +0200401 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
402 hdr = p;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200403
404 hdr->fio_magic = FIO_HDR_MAGIC;
405 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200406 hdr->len = hdr_inc;
Jens Axboe87677832007-07-30 12:08:14 +0200407 data_len = hdr_inc - hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200408
Jens Axboe87677832007-07-30 12:08:14 +0200409 data = p + hdr_size(hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200410 switch (td->o.verify) {
411 case VERIFY_MD5:
412 fill_md5(hdr, data, data_len);
413 break;
414 case VERIFY_CRC64:
415 fill_crc64(hdr, data, data_len);
416 break;
417 case VERIFY_CRC32:
418 fill_crc32(hdr, data, data_len);
419 break;
420 case VERIFY_CRC16:
421 fill_crc16(hdr, data, data_len);
422 break;
423 case VERIFY_CRC7:
424 fill_crc7(hdr, data, data_len);
425 break;
Jens Axboecd14cc12007-07-30 10:59:33 +0200426 case VERIFY_SHA256:
427 fill_sha256(hdr, data, data_len);
428 break;
429 case VERIFY_SHA512:
430 fill_sha512(hdr, data, data_len);
431 break;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200432 default:
433 log_err("fio: bad verify type: %d\n", td->o.verify);
434 assert(0);
435 }
Jens Axboea59e1702007-07-30 08:53:27 +0200436 if (td->o.verify_offset)
Jens Axboe87677832007-07-30 12:08:14 +0200437 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200438 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200439}
440
441int get_next_verify(struct thread_data *td, struct io_u *io_u)
442{
Jens Axboe8de8f042007-03-27 10:36:12 +0200443 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200444
Jens Axboed2d7fa52007-02-19 13:21:35 +0100445 /*
446 * this io_u is from a requeue, we already filled the offsets
447 */
448 if (io_u->file)
449 return 0;
450
Jens Axboe8de8f042007-03-27 10:36:12 +0200451 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
452 struct rb_node *n = rb_first(&td->io_hist_tree);
453
Jens Axboe4b878982007-03-26 09:32:22 +0200454 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200455 rb_erase(n, &td->io_hist_tree);
Jens Axboe8de8f042007-03-27 10:36:12 +0200456 } else if (!list_empty(&td->io_hist_list)) {
457 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
458 list_del(&ipo->list);
459 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200460
Jens Axboe8de8f042007-03-27 10:36:12 +0200461 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200462 io_u->offset = ipo->offset;
463 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100464 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200465
466 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
467 int r = td_io_open_file(td, io_u->file);
468
469 if (r)
470 return 1;
471 }
472
473 get_file(ipo->file);
474 assert(io_u->file->flags & FIO_FILE_OPEN);
Jens Axboee29d1b72006-10-18 15:43:15 +0200475 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100476 io_u->xfer_buf = io_u->buf;
477 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200478 free(ipo);
479 return 0;
480 }
481
482 return 1;
483}