Only show maxval in --cmdhelp if it is given
[fio.git] / verify.c
... / ...
CommitLineData
1/*
2 * IO verification helpers
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
7#include <assert.h>
8
9#include "fio.h"
10#include "verify.h"
11
12#include "crc/md5.h"
13#include "crc/crc64.h"
14#include "crc/crc32.h"
15#include "crc/crc32c.h"
16#include "crc/crc16.h"
17#include "crc/crc7.h"
18#include "crc/sha256.h"
19#include "crc/sha512.h"
20
21static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
22{
23 unsigned int todo;
24 int r;
25
26 while (len) {
27 r = os_random_long(&td->verify_state);
28
29 /*
30 * lrand48_r seems to be broken and only fill the bottom
31 * 32-bits, even on 64-bit archs with 64-bit longs
32 */
33 todo = sizeof(r);
34 if (todo > len)
35 todo = len;
36
37 memcpy(p, &r, todo);
38
39 len -= todo;
40 p += todo;
41 }
42}
43
44static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
45{
46 switch (td->o.verify_pattern_bytes) {
47 case 0:
48 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
49 fill_random_bytes(td, p, len);
50 break;
51 case 1:
52 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
53 memset(p, td->o.verify_pattern, len);
54 break;
55 case 2:
56 case 3:
57 case 4: {
58 unsigned int pattern = td->o.verify_pattern;
59 unsigned int i = 0;
60 unsigned char c1, c2, c3, c4;
61 unsigned char *b = p;
62
63 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
64 td->o.verify_pattern_bytes, len);
65
66 c1 = pattern & 0xff;
67 pattern >>= 8;
68 c2 = pattern & 0xff;
69 pattern >>= 8;
70 c3 = pattern & 0xff;
71 pattern >>= 8;
72 c4 = pattern & 0xff;
73
74 while (i < len) {
75 b[i++] = c1;
76 if (i == len)
77 break;
78 b[i++] = c2;
79 if (td->o.verify_pattern_bytes == 2 || i == len)
80 continue;
81 b[i++] = c3;
82 if (td->o.verify_pattern_bytes == 3 || i == len)
83 continue;
84 b[i++] = c4;
85 }
86 break;
87 }
88 }
89}
90
91static void memswp(void *buf1, void *buf2, unsigned int len)
92{
93 char swap[200];
94
95 assert(len <= sizeof(swap));
96
97 memcpy(&swap, buf1, len);
98 memcpy(buf1, buf2, len);
99 memcpy(buf2, &swap, len);
100}
101
102static void hexdump(void *buffer, int len)
103{
104 unsigned char *p = buffer;
105 int i;
106
107 for (i = 0; i < len; i++)
108 log_info("%02x", p[i]);
109 log_info("\n");
110}
111
112/*
113 * Prepare for seperation of verify_header and checksum header
114 */
115static inline unsigned int __hdr_size(int verify_type)
116{
117 unsigned int len = len;
118
119 switch (verify_type) {
120 case VERIFY_NONE:
121 case VERIFY_NULL:
122 len = 0;
123 break;
124 case VERIFY_MD5:
125 len = sizeof(struct vhdr_md5);
126 break;
127 case VERIFY_CRC64:
128 len = sizeof(struct vhdr_crc64);
129 break;
130 case VERIFY_CRC32C:
131 case VERIFY_CRC32:
132 case VERIFY_CRC32C_INTEL:
133 len = sizeof(struct vhdr_crc32);
134 break;
135 case VERIFY_CRC16:
136 len = sizeof(struct vhdr_crc16);
137 break;
138 case VERIFY_CRC7:
139 len = sizeof(struct vhdr_crc7);
140 break;
141 case VERIFY_SHA256:
142 len = sizeof(struct vhdr_sha256);
143 break;
144 case VERIFY_SHA512:
145 len = sizeof(struct vhdr_sha512);
146 break;
147 case VERIFY_META:
148 len = sizeof(struct vhdr_meta);
149 break;
150 default:
151 log_err("fio: unknown verify header!\n");
152 assert(0);
153 }
154
155 return len + sizeof(struct verify_header);
156}
157
158static inline unsigned int hdr_size(struct verify_header *hdr)
159{
160 return __hdr_size(hdr->verify_type);
161}
162
163static void *hdr_priv(struct verify_header *hdr)
164{
165 void *priv = hdr;
166
167 return priv + sizeof(struct verify_header);
168}
169
170/*
171 * Return data area 'header_num'
172 */
173static inline void *io_u_verify_off(struct verify_header *hdr,
174 struct io_u *io_u, unsigned char header_num)
175{
176 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
177}
178
179static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
180 struct io_u *io_u, unsigned int header_num)
181{
182 struct vhdr_meta *vh = hdr_priv(hdr);
183
184 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
185
186 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
187 log_err("meta: verify failed at %llu/%u\n",
188 io_u->offset + header_num * hdr->len, hdr->len);
189 return EILSEQ;
190 }
191
192 return 0;
193}
194
195static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
196 unsigned int header_num)
197{
198 void *p = io_u_verify_off(hdr, io_u, header_num);
199 struct vhdr_sha512 *vh = hdr_priv(hdr);
200 uint8_t sha512[128];
201 struct sha512_ctx sha512_ctx = {
202 .buf = sha512,
203 };
204
205 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
206
207 sha512_init(&sha512_ctx);
208 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
209
210 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
211 log_err("sha512: verify failed at %llu/%u\n",
212 io_u->offset + header_num * hdr->len, hdr->len);
213 hexdump(vh->sha512, sizeof(vh->sha512));
214 hexdump(sha512_ctx.buf, sizeof(sha512));
215 return EILSEQ;
216 }
217
218 return 0;
219}
220
221static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
222 unsigned int header_num)
223{
224 void *p = io_u_verify_off(hdr, io_u, header_num);
225 struct vhdr_sha256 *vh = hdr_priv(hdr);
226 uint8_t sha256[128];
227 struct sha256_ctx sha256_ctx = {
228 .buf = sha256,
229 };
230
231 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
232
233 sha256_init(&sha256_ctx);
234 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
235
236 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
237 log_err("sha256: verify failed at %llu/%u\n",
238 io_u->offset + header_num * hdr->len, hdr->len);
239 hexdump(vh->sha256, sizeof(vh->sha256));
240 hexdump(sha256_ctx.buf, sizeof(sha256));
241 return EILSEQ;
242 }
243
244 return 0;
245}
246
247static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
248 unsigned char header_num)
249{
250 void *p = io_u_verify_off(hdr, io_u, header_num);
251 struct vhdr_crc7 *vh = hdr_priv(hdr);
252 unsigned char c;
253
254 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
255
256 c = crc7(p, hdr->len - hdr_size(hdr));
257
258 if (c != vh->crc7) {
259 log_err("crc7: verify failed at %llu/%u\n",
260 io_u->offset + header_num * hdr->len, hdr->len);
261 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
262 return EILSEQ;
263 }
264
265 return 0;
266}
267
268static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
269 unsigned int header_num)
270{
271 void *p = io_u_verify_off(hdr, io_u, header_num);
272 struct vhdr_crc16 *vh = hdr_priv(hdr);
273 unsigned short c;
274
275 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
276
277 c = crc16(p, hdr->len - hdr_size(hdr));
278
279 if (c != vh->crc16) {
280 log_err("crc16: verify failed at %llu/%u\n",
281 io_u->offset + header_num * hdr->len, hdr->len);
282 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
283 return EILSEQ;
284 }
285
286 return 0;
287}
288
289static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
290 unsigned int header_num)
291{
292 void *p = io_u_verify_off(hdr, io_u, header_num);
293 struct vhdr_crc64 *vh = hdr_priv(hdr);
294 unsigned long long c;
295
296 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
297
298 c = crc64(p, hdr->len - hdr_size(hdr));
299
300 if (c != vh->crc64) {
301 log_err("crc64: verify failed at %llu/%u\n",
302 io_u->offset + header_num * hdr->len,
303 hdr->len);
304 log_err("crc64: wanted %llx, got %llx\n",
305 (unsigned long long) vh->crc64, c);
306 return EILSEQ;
307 }
308
309 return 0;
310}
311
312static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
313 unsigned int header_num)
314{
315 void *p = io_u_verify_off(hdr, io_u, header_num);
316 struct vhdr_crc32 *vh = hdr_priv(hdr);
317 uint32_t c;
318
319 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
320
321 c = crc32(p, hdr->len - hdr_size(hdr));
322
323 if (c != vh->crc32) {
324 log_err("crc32: verify failed at %llu/%u\n",
325 io_u->offset + header_num * hdr->len, hdr->len);
326 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
327 return EILSEQ;
328 }
329
330 return 0;
331}
332
333static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
334 unsigned int header_num)
335{
336 void *p = io_u_verify_off(hdr, io_u, header_num);
337 struct vhdr_crc32 *vh = hdr_priv(hdr);
338 uint32_t c;
339
340 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
341
342 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
343 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
344 else
345 c = crc32c(p, hdr->len - hdr_size(hdr));
346
347 if (c != vh->crc32) {
348 log_err("crc32c: verify failed at %llu/%u\n",
349 io_u->offset + header_num * hdr->len, hdr->len);
350 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
351 return EILSEQ;
352 }
353
354 return 0;
355}
356
357static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
358 unsigned int header_num)
359{
360 void *p = io_u_verify_off(hdr, io_u, header_num);
361 struct vhdr_md5 *vh = hdr_priv(hdr);
362 uint32_t hash[MD5_HASH_WORDS];
363 struct md5_ctx md5_ctx = {
364 .hash = hash,
365 };
366
367 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
368
369 md5_init(&md5_ctx);
370 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
371
372 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
373 log_err("md5: verify failed at %llu/%u\n",
374 io_u->offset + header_num * hdr->len, hdr->len);
375 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
376 hexdump(md5_ctx.hash, sizeof(hash));
377 return EILSEQ;
378 }
379
380 return 0;
381}
382
383static unsigned int hweight8(unsigned int w)
384{
385 unsigned int res = w - ((w >> 1) & 0x55);
386
387 res = (res & 0x33) + ((res >> 2) & 0x33);
388 return (res + (res >> 4)) & 0x0F;
389}
390
391int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
392 char *buf, unsigned int len, unsigned int mod)
393{
394 unsigned int i;
395 char split_pattern[4];
396
397 for (i = 0; i < 4; i++) {
398 split_pattern[i] = pattern & 0xff;
399 pattern >>= 8;
400 }
401
402 for (i = 0; i < len; i++) {
403 if (buf[i] != split_pattern[mod]) {
404 unsigned int bits;
405
406 bits = hweight8(buf[i] ^ split_pattern[mod]);
407 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
408 buf[i], split_pattern[mod], bits);
409 log_err("fio: bad pattern block offset %u\n", i);
410 return EILSEQ;
411 }
412 mod++;
413 if (mod == pattern_size)
414 mod = 0;
415 }
416
417 return 0;
418}
419
420int verify_io_u(struct thread_data *td, struct io_u *io_u)
421{
422 struct verify_header *hdr;
423 unsigned int hdr_size, hdr_inc, hdr_num = 0;
424 void *p;
425 int ret;
426
427 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
428 return 0;
429
430 hdr_inc = io_u->buflen;
431 if (td->o.verify_interval)
432 hdr_inc = td->o.verify_interval;
433
434 ret = 0;
435 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
436 p += hdr_inc, hdr_num++) {
437 if (ret && td->o.verify_fatal) {
438 td->terminate = 1;
439 break;
440 }
441 hdr_size = __hdr_size(td->o.verify);
442 if (td->o.verify_offset)
443 memswp(p, p + td->o.verify_offset, hdr_size);
444 hdr = p;
445
446 if (hdr->fio_magic != FIO_HDR_MAGIC) {
447 log_err("Bad verify header %x\n", hdr->fio_magic);
448 return EILSEQ;
449 }
450
451 if (td->o.verify_pattern_bytes) {
452 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
453 io_u, hdr->len);
454 ret = verify_io_u_pattern(td->o.verify_pattern,
455 td->o.verify_pattern_bytes,
456 p + hdr_size,
457 hdr_inc - hdr_size,
458 hdr_size % 4);
459 if (ret)
460 log_err("fio: verify failed at %llu/%u\n",
461 io_u->offset + hdr_num * hdr->len,
462 hdr->len);
463 continue;
464 }
465
466 switch (hdr->verify_type) {
467 case VERIFY_MD5:
468 ret = verify_io_u_md5(hdr, io_u, hdr_num);
469 break;
470 case VERIFY_CRC64:
471 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
472 break;
473 case VERIFY_CRC32C:
474 case VERIFY_CRC32C_INTEL:
475 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
476 break;
477 case VERIFY_CRC32:
478 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
479 break;
480 case VERIFY_CRC16:
481 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
482 break;
483 case VERIFY_CRC7:
484 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
485 break;
486 case VERIFY_SHA256:
487 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
488 break;
489 case VERIFY_SHA512:
490 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
491 break;
492 case VERIFY_META:
493 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
494 break;
495 default:
496 log_err("Bad verify type %u\n", hdr->verify_type);
497 ret = EINVAL;
498 }
499 }
500
501 return ret;
502}
503
504static void fill_meta(struct verify_header *hdr, struct thread_data *td,
505 struct io_u *io_u, unsigned int header_num)
506{
507 struct vhdr_meta *vh = hdr_priv(hdr);
508
509 vh->thread = td->thread_number;
510
511 vh->time_sec = io_u->start_time.tv_sec;
512 vh->time_usec = io_u->start_time.tv_usec;
513
514 vh->numberio = td->io_issues[DDIR_WRITE];
515
516 vh->offset = io_u->offset + header_num * td->o.verify_interval;
517}
518
519static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
520{
521 struct vhdr_sha512 *vh = hdr_priv(hdr);
522 struct sha512_ctx sha512_ctx = {
523 .buf = vh->sha512,
524 };
525
526 sha512_init(&sha512_ctx);
527 sha512_update(&sha512_ctx, p, len);
528}
529
530static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
531{
532 struct vhdr_sha256 *vh = hdr_priv(hdr);
533 struct sha256_ctx sha256_ctx = {
534 .buf = vh->sha256,
535 };
536
537 sha256_init(&sha256_ctx);
538 sha256_update(&sha256_ctx, p, len);
539}
540
541static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
542{
543 struct vhdr_crc7 *vh = hdr_priv(hdr);
544
545 vh->crc7 = crc7(p, len);
546}
547
548static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
549{
550 struct vhdr_crc16 *vh = hdr_priv(hdr);
551
552 vh->crc16 = crc16(p, len);
553}
554
555static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
556{
557 struct vhdr_crc32 *vh = hdr_priv(hdr);
558
559 vh->crc32 = crc32(p, len);
560}
561
562static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
563{
564 struct vhdr_crc32 *vh = hdr_priv(hdr);
565
566 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
567 vh->crc32 = crc32c_intel(p, len);
568 else
569 vh->crc32 = crc32c(p, len);
570}
571
572static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
573{
574 struct vhdr_crc64 *vh = hdr_priv(hdr);
575
576 vh->crc64 = crc64(p, len);
577}
578
579static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
580{
581 struct vhdr_md5 *vh = hdr_priv(hdr);
582 struct md5_ctx md5_ctx = {
583 .hash = (uint32_t *) vh->md5_digest,
584 };
585
586 md5_init(&md5_ctx);
587 md5_update(&md5_ctx, p, len);
588}
589
590/*
591 * fill body of io_u->buf with random data and add a header with the
592 * crc32 or md5 sum of that data.
593 */
594void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
595{
596 struct verify_header *hdr;
597 void *p = io_u->buf, *data;
598 unsigned int hdr_inc, data_len, header_num = 0;
599
600 if (td->o.verify == VERIFY_NULL)
601 return;
602
603 fill_pattern(td, p, io_u->buflen);
604
605 hdr_inc = io_u->buflen;
606 if (td->o.verify_interval)
607 hdr_inc = td->o.verify_interval;
608
609 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
610 hdr = p;
611
612 hdr->fio_magic = FIO_HDR_MAGIC;
613 hdr->verify_type = td->o.verify;
614 hdr->len = hdr_inc;
615 data_len = hdr_inc - hdr_size(hdr);
616
617 data = p + hdr_size(hdr);
618 switch (td->o.verify) {
619 case VERIFY_MD5:
620 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
621 io_u, hdr->len);
622 fill_md5(hdr, data, data_len);
623 break;
624 case VERIFY_CRC64:
625 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
626 io_u, hdr->len);
627 fill_crc64(hdr, data, data_len);
628 break;
629 case VERIFY_CRC32C:
630 case VERIFY_CRC32C_INTEL:
631 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
632 io_u, hdr->len);
633 fill_crc32c(hdr, data, data_len);
634 break;
635 case VERIFY_CRC32:
636 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
637 io_u, hdr->len);
638 fill_crc32(hdr, data, data_len);
639 break;
640 case VERIFY_CRC16:
641 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
642 io_u, hdr->len);
643 fill_crc16(hdr, data, data_len);
644 break;
645 case VERIFY_CRC7:
646 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
647 io_u, hdr->len);
648 fill_crc7(hdr, data, data_len);
649 break;
650 case VERIFY_SHA256:
651 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
652 io_u, hdr->len);
653 fill_sha256(hdr, data, data_len);
654 break;
655 case VERIFY_SHA512:
656 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
657 io_u, hdr->len);
658 fill_sha512(hdr, data, data_len);
659 break;
660 case VERIFY_META:
661 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
662 io_u, hdr->len);
663 fill_meta(hdr, td, io_u, header_num);
664 break;
665 default:
666 log_err("fio: bad verify type: %d\n", td->o.verify);
667 assert(0);
668 }
669 if (td->o.verify_offset)
670 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
671 header_num++;
672 }
673}
674
675int get_next_verify(struct thread_data *td, struct io_u *io_u)
676{
677 struct io_piece *ipo = NULL;
678
679 /*
680 * this io_u is from a requeue, we already filled the offsets
681 */
682 if (io_u->file)
683 return 0;
684
685 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
686 struct rb_node *n = rb_first(&td->io_hist_tree);
687
688 ipo = rb_entry(n, struct io_piece, rb_node);
689 rb_erase(n, &td->io_hist_tree);
690 } else if (!flist_empty(&td->io_hist_list)) {
691 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
692 flist_del(&ipo->list);
693 }
694
695 if (ipo) {
696 io_u->offset = ipo->offset;
697 io_u->buflen = ipo->len;
698 io_u->file = ipo->file;
699
700 if (!fio_file_open(io_u->file)) {
701 int r = td_io_open_file(td, io_u->file);
702
703 if (r) {
704 dprint(FD_VERIFY, "failed file %s open\n",
705 io_u->file->file_name);
706 return 1;
707 }
708 }
709
710 get_file(ipo->file);
711 assert(fio_file_open(io_u->file));
712 io_u->ddir = DDIR_READ;
713 io_u->xfer_buf = io_u->buf;
714 io_u->xfer_buflen = io_u->buflen;
715 free(ipo);
716 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
717 return 0;
718 }
719
720 dprint(FD_VERIFY, "get_next_verify: empty\n");
721 return 1;
722}