2 * IO verification helpers
13 #include "crc/crc64.h"
14 #include "crc/crc32.h"
15 #include "crc/crc32c.h"
16 #include "crc/crc16.h"
18 #include "crc/sha256.h"
19 #include "crc/sha512.h"
21 static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
27 r = os_random_long(&td->verify_state);
30 * lrand48_r seems to be broken and only fill the bottom
31 * 32-bits, even on 64-bit archs with 64-bit longs
44 static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
46 switch (td->o.verify_pattern_bytes) {
48 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
49 fill_random_bytes(td, p, len);
52 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
53 memset(p, td->o.verify_pattern, len);
58 unsigned int pattern = td->o.verify_pattern;
60 unsigned char c1, c2, c3, c4;
63 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
64 td->o.verify_pattern_bytes, len);
79 if (td->o.verify_pattern_bytes == 2 || i == len)
82 if (td->o.verify_pattern_bytes == 3 || i == len)
91 static void memswp(void *buf1, void *buf2, unsigned int len)
95 assert(len <= sizeof(swap));
97 memcpy(&swap, buf1, len);
98 memcpy(buf1, buf2, len);
99 memcpy(buf2, &swap, len);
102 static void hexdump(void *buffer, int len)
104 unsigned char *p = buffer;
107 for (i = 0; i < len; i++)
108 log_info("%02x", p[i]);
113 * Prepare for seperation of verify_header and checksum header
115 static inline unsigned int __hdr_size(int verify_type)
117 unsigned int len = len;
119 switch (verify_type) {
125 len = sizeof(struct vhdr_md5);
128 len = sizeof(struct vhdr_crc64);
132 case VERIFY_CRC32C_INTEL:
133 len = sizeof(struct vhdr_crc32);
136 len = sizeof(struct vhdr_crc16);
139 len = sizeof(struct vhdr_crc7);
142 len = sizeof(struct vhdr_sha256);
145 len = sizeof(struct vhdr_sha512);
148 len = sizeof(struct vhdr_meta);
151 log_err("fio: unknown verify header!\n");
155 return len + sizeof(struct verify_header);
158 static inline unsigned int hdr_size(struct verify_header *hdr)
160 return __hdr_size(hdr->verify_type);
163 static void *hdr_priv(struct verify_header *hdr)
167 return priv + sizeof(struct verify_header);
171 * Return data area 'header_num'
173 static inline void *io_u_verify_off(struct verify_header *hdr,
174 struct io_u *io_u, unsigned char header_num)
176 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
179 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
180 struct io_u *io_u, unsigned int header_num)
182 struct vhdr_meta *vh = hdr_priv(hdr);
184 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
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);
195 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
196 unsigned int header_num)
198 void *p = io_u_verify_off(hdr, io_u, header_num);
199 struct vhdr_sha512 *vh = hdr_priv(hdr);
201 struct sha512_ctx sha512_ctx = {
205 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
207 sha512_init(&sha512_ctx);
208 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
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));
221 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
222 unsigned int header_num)
224 void *p = io_u_verify_off(hdr, io_u, header_num);
225 struct vhdr_sha256 *vh = hdr_priv(hdr);
227 struct sha256_ctx sha256_ctx = {
231 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
233 sha256_init(&sha256_ctx);
234 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
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));
247 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
248 unsigned char header_num)
250 void *p = io_u_verify_off(hdr, io_u, header_num);
251 struct vhdr_crc7 *vh = hdr_priv(hdr);
254 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
256 c = crc7(p, hdr->len - hdr_size(hdr));
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);
268 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
269 unsigned int header_num)
271 void *p = io_u_verify_off(hdr, io_u, header_num);
272 struct vhdr_crc16 *vh = hdr_priv(hdr);
275 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
277 c = crc16(p, hdr->len - hdr_size(hdr));
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);
289 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
290 unsigned int header_num)
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;
296 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
298 c = crc64(p, hdr->len - hdr_size(hdr));
300 if (c != vh->crc64) {
301 log_err("crc64: verify failed at %llu/%u\n",
302 io_u->offset + header_num * hdr->len,
304 log_err("crc64: wanted %llx, got %llx\n",
305 (unsigned long long) vh->crc64, c);
312 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
313 unsigned int header_num)
315 void *p = io_u_verify_off(hdr, io_u, header_num);
316 struct vhdr_crc32 *vh = hdr_priv(hdr);
319 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
321 c = crc32(p, hdr->len - hdr_size(hdr));
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);
333 static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
334 unsigned int header_num)
336 void *p = io_u_verify_off(hdr, io_u, header_num);
337 struct vhdr_crc32 *vh = hdr_priv(hdr);
340 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
342 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
343 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
345 c = crc32c(p, hdr->len - hdr_size(hdr));
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);
357 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
358 unsigned int header_num)
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 = {
367 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
370 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
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));
383 static unsigned int hweight8(unsigned int w)
385 unsigned int res = w - ((w >> 1) & 0x55);
387 res = (res & 0x33) + ((res >> 2) & 0x33);
388 return (res + (res >> 4)) & 0x0F;
391 int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
392 char *buf, unsigned int len, unsigned int mod)
395 char split_pattern[4];
397 for (i = 0; i < 4; i++) {
398 split_pattern[i] = pattern & 0xff;
402 for (i = 0; i < len; i++) {
403 if (buf[i] != split_pattern[mod]) {
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);
413 if (mod == pattern_size)
420 int verify_io_u(struct thread_data *td, struct io_u *io_u)
422 struct verify_header *hdr;
423 unsigned int hdr_size, hdr_inc, hdr_num = 0;
427 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
430 hdr_inc = io_u->buflen;
431 if (td->o.verify_interval)
432 hdr_inc = td->o.verify_interval;
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) {
441 hdr_size = __hdr_size(td->o.verify);
442 if (td->o.verify_offset)
443 memswp(p, p + td->o.verify_offset, hdr_size);
446 if (hdr->fio_magic != FIO_HDR_MAGIC) {
447 log_err("Bad verify header %x\n", hdr->fio_magic);
451 if (td->o.verify_pattern_bytes) {
452 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
454 ret = verify_io_u_pattern(td->o.verify_pattern,
455 td->o.verify_pattern_bytes,
460 log_err("fio: verify failed at %llu/%u\n",
461 io_u->offset + hdr_num * hdr->len,
466 switch (hdr->verify_type) {
468 ret = verify_io_u_md5(hdr, io_u, hdr_num);
471 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
474 case VERIFY_CRC32C_INTEL:
475 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
478 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
481 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
484 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
487 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
490 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
493 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
496 log_err("Bad verify type %u\n", hdr->verify_type);
504 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
505 struct io_u *io_u, unsigned int header_num)
507 struct vhdr_meta *vh = hdr_priv(hdr);
509 vh->thread = td->thread_number;
511 vh->time_sec = io_u->start_time.tv_sec;
512 vh->time_usec = io_u->start_time.tv_usec;
514 vh->numberio = td->io_issues[DDIR_WRITE];
516 vh->offset = io_u->offset + header_num * td->o.verify_interval;
519 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
521 struct vhdr_sha512 *vh = hdr_priv(hdr);
522 struct sha512_ctx sha512_ctx = {
526 sha512_init(&sha512_ctx);
527 sha512_update(&sha512_ctx, p, len);
530 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
532 struct vhdr_sha256 *vh = hdr_priv(hdr);
533 struct sha256_ctx sha256_ctx = {
537 sha256_init(&sha256_ctx);
538 sha256_update(&sha256_ctx, p, len);
541 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
543 struct vhdr_crc7 *vh = hdr_priv(hdr);
545 vh->crc7 = crc7(p, len);
548 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
550 struct vhdr_crc16 *vh = hdr_priv(hdr);
552 vh->crc16 = crc16(p, len);
555 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
557 struct vhdr_crc32 *vh = hdr_priv(hdr);
559 vh->crc32 = crc32(p, len);
562 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
564 struct vhdr_crc32 *vh = hdr_priv(hdr);
566 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
567 vh->crc32 = crc32c_intel(p, len);
569 vh->crc32 = crc32c(p, len);
572 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
574 struct vhdr_crc64 *vh = hdr_priv(hdr);
576 vh->crc64 = crc64(p, len);
579 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
581 struct vhdr_md5 *vh = hdr_priv(hdr);
582 struct md5_ctx md5_ctx = {
583 .hash = (uint32_t *) vh->md5_digest,
587 md5_update(&md5_ctx, p, len);
591 * fill body of io_u->buf with random data and add a header with the
592 * crc32 or md5 sum of that data.
594 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
596 struct verify_header *hdr;
597 void *p = io_u->buf, *data;
598 unsigned int hdr_inc, data_len, header_num = 0;
600 if (td->o.verify == VERIFY_NULL)
603 fill_pattern(td, p, io_u->buflen);
605 hdr_inc = io_u->buflen;
606 if (td->o.verify_interval)
607 hdr_inc = td->o.verify_interval;
609 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
612 hdr->fio_magic = FIO_HDR_MAGIC;
613 hdr->verify_type = td->o.verify;
615 data_len = hdr_inc - hdr_size(hdr);
617 data = p + hdr_size(hdr);
618 switch (td->o.verify) {
620 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
622 fill_md5(hdr, data, data_len);
625 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
627 fill_crc64(hdr, data, data_len);
630 case VERIFY_CRC32C_INTEL:
631 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
633 fill_crc32c(hdr, data, data_len);
636 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
638 fill_crc32(hdr, data, data_len);
641 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
643 fill_crc16(hdr, data, data_len);
646 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
648 fill_crc7(hdr, data, data_len);
651 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
653 fill_sha256(hdr, data, data_len);
656 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
658 fill_sha512(hdr, data, data_len);
661 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
663 fill_meta(hdr, td, io_u, header_num);
666 log_err("fio: bad verify type: %d\n", td->o.verify);
669 if (td->o.verify_offset)
670 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
675 int get_next_verify(struct thread_data *td, struct io_u *io_u)
677 struct io_piece *ipo = NULL;
680 * this io_u is from a requeue, we already filled the offsets
685 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
686 struct rb_node *n = rb_first(&td->io_hist_tree);
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);
696 io_u->offset = ipo->offset;
697 io_u->buflen = ipo->len;
698 io_u->file = ipo->file;
700 if (!fio_file_open(io_u->file)) {
701 int r = td_io_open_file(td, io_u->file);
704 dprint(FD_VERIFY, "failed file %s open\n",
705 io_u->file->file_name);
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;
716 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
720 dprint(FD_VERIFY, "get_next_verify: empty\n");