2 * IO verification helpers
12 #include "crc/crc64.h"
13 #include "crc/crc32.h"
14 #include "crc/crc16.h"
16 #include "crc/sha256.h"
17 #include "crc/sha512.h"
19 static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
25 r = os_random_long(&td->verify_state);
28 * lrand48_r seems to be broken and only fill the bottom
29 * 32-bits, even on 64-bit archs with 64-bit longs
42 static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
44 switch (td->o.verify_pattern_bytes) {
46 fill_random_bytes(td, p, len);
49 memset(p, td->o.verify_pattern, len);
54 unsigned int pattern = td->o.verify_pattern;
56 unsigned char c1, c2, c3, c4;
72 if (td->o.verify_pattern_bytes == 2 || i == len)
75 if (td->o.verify_pattern_bytes == 3 || i == len)
84 static void memswp(void* buf1, void* buf2, unsigned int len)
86 struct verify_header swap;
88 memcpy(&swap, buf1, len);
89 memcpy(buf1, buf2, len);
90 memcpy(buf2, &swap, len);
93 static void hexdump(void *buffer, int len)
95 unsigned char *p = buffer;
98 for (i = 0; i < len; i++)
99 log_info("%02x", p[i]);
104 * Prepare for seperation of verify_header and checksum header
106 static inline unsigned int __hdr_size(int verify_type)
110 switch (verify_type) {
116 len = sizeof(struct vhdr_md5);
119 len = sizeof(struct vhdr_crc64);
122 len = sizeof(struct vhdr_crc32);
125 len = sizeof(struct vhdr_crc16);
128 len = sizeof(struct vhdr_crc7);
131 len = sizeof(struct vhdr_sha256);
134 len = sizeof(struct vhdr_sha512);
137 len = sizeof(struct vhdr_meta);
140 log_err("fio: unknown verify header!\n");
144 return len + sizeof(struct verify_header);
147 static inline unsigned int hdr_size(struct verify_header *hdr)
149 return __hdr_size(hdr->verify_type);
152 static void *hdr_priv(struct verify_header *hdr)
156 return priv + sizeof(struct verify_header);
160 * Return data area 'header_num'
162 static inline void *io_u_verify_off(struct verify_header *hdr,
164 unsigned char header_num)
166 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
169 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
170 struct io_u *io_u, unsigned int header_num)
172 struct vhdr_meta *vh = hdr_priv(hdr);
174 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
175 log_err("meta: verify failed at %llu/%u\n",
176 io_u->offset + header_num * hdr->len,
184 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
185 unsigned int header_num)
187 void *p = io_u_verify_off(hdr, io_u, header_num);
188 struct vhdr_sha512 *vh = hdr_priv(hdr);
190 struct sha512_ctx sha512_ctx = {
194 sha512_init(&sha512_ctx);
195 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
197 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
198 log_err("sha512: verify failed at %llu/%u\n",
199 io_u->offset + header_num * hdr->len,
201 hexdump(vh->sha512, sizeof(vh->sha512));
202 hexdump(sha512_ctx.buf, sizeof(sha512));
209 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
210 unsigned int header_num)
212 void *p = io_u_verify_off(hdr, io_u, header_num);
213 struct vhdr_sha256 *vh = hdr_priv(hdr);
215 struct sha256_ctx sha256_ctx = {
219 sha256_init(&sha256_ctx);
220 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
222 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
223 log_err("sha256: verify failed at %llu/%u\n",
224 io_u->offset + header_num * hdr->len,
226 hexdump(vh->sha256, sizeof(vh->sha256));
227 hexdump(sha256_ctx.buf, sizeof(sha256));
234 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
235 unsigned char header_num)
237 void *p = io_u_verify_off(hdr, io_u, header_num);
238 struct vhdr_crc7 *vh = hdr_priv(hdr);
241 c = crc7(p, hdr->len - hdr_size(hdr));
244 log_err("crc7: verify failed at %llu/%u\n",
245 io_u->offset + header_num * hdr->len,
247 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
254 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
255 unsigned int header_num)
257 void *p = io_u_verify_off(hdr, io_u, header_num);
258 struct vhdr_crc16 *vh = hdr_priv(hdr);
261 c = crc16(p, hdr->len - hdr_size(hdr));
263 if (c != vh->crc16) {
264 log_err("crc16: verify failed at %llu/%u\n",
265 io_u->offset + header_num * hdr->len,
267 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
274 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
275 unsigned int header_num)
277 void *p = io_u_verify_off(hdr, io_u, header_num);
278 struct vhdr_crc64 *vh = hdr_priv(hdr);
279 unsigned long long c;
281 c = crc64(p, hdr->len - hdr_size(hdr));
283 if (c != vh->crc64) {
284 log_err("crc64: verify failed at %llu/%u\n",
285 io_u->offset + header_num * hdr->len,
287 log_err("crc64: wanted %llx, got %llx\n", (unsigned long long) vh->crc64, c);
294 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
295 unsigned int header_num)
297 void *p = io_u_verify_off(hdr, io_u, header_num);
298 struct vhdr_crc32 *vh = hdr_priv(hdr);
301 c = crc32(p, hdr->len - hdr_size(hdr));
303 if (c != vh->crc32) {
304 log_err("crc32: verify failed at %llu/%u\n",
305 io_u->offset + header_num * hdr->len,
307 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
314 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
315 unsigned int header_num)
317 void *p = io_u_verify_off(hdr, io_u, header_num);
318 struct vhdr_md5 *vh = hdr_priv(hdr);
319 uint32_t hash[MD5_HASH_WORDS];
320 struct md5_ctx md5_ctx = {
325 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
327 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
328 log_err("md5: verify failed at %llu/%u\n",
329 io_u->offset + header_num * hdr->len,
331 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
332 hexdump(md5_ctx.hash, sizeof(hash));
339 int verify_io_u(struct thread_data *td, struct io_u *io_u)
341 struct verify_header *hdr;
342 unsigned int hdr_inc, hdr_num = 0;
346 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
349 hdr_inc = io_u->buflen;
350 if (td->o.verify_interval)
351 hdr_inc = td->o.verify_interval;
353 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
354 if (td->o.verify_offset)
355 memswp(p, p + td->o.verify_offset, __hdr_size(td->o.verify));
359 if (hdr->fio_magic != FIO_HDR_MAGIC) {
360 log_err("Bad verify header %x\n", hdr->fio_magic);
364 switch (hdr->verify_type) {
366 ret = verify_io_u_md5(hdr, io_u, hdr_num);
369 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
372 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
375 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
378 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
381 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
384 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
387 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
390 log_err("Bad verify type %u\n", hdr->verify_type);
399 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
400 struct io_u *io_u, unsigned int header_num)
402 struct vhdr_meta *vh = hdr_priv(hdr);
404 vh->thread = td->thread_number;
406 vh->time_sec = io_u->start_time.tv_sec;
407 vh->time_usec = io_u->start_time.tv_usec;
409 vh->numberio = td->io_issues[DDIR_WRITE];
411 vh->offset = io_u->offset + header_num * td->o.verify_interval;
414 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
416 struct vhdr_sha512 *vh = hdr_priv(hdr);
417 struct sha512_ctx sha512_ctx = {
421 sha512_init(&sha512_ctx);
422 sha512_update(&sha512_ctx, p, len);
425 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
427 struct vhdr_sha256 *vh = hdr_priv(hdr);
428 struct sha256_ctx sha256_ctx = {
432 sha256_init(&sha256_ctx);
433 sha256_update(&sha256_ctx, p, len);
436 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
438 struct vhdr_crc7 *vh = hdr_priv(hdr);
440 vh->crc7 = crc7(p, len);
443 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
445 struct vhdr_crc16 *vh = hdr_priv(hdr);
447 vh->crc16 = crc16(p, len);
450 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
452 struct vhdr_crc32 *vh = hdr_priv(hdr);
454 vh->crc32 = crc32(p, len);
457 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
459 struct vhdr_crc64 *vh = hdr_priv(hdr);
461 vh->crc64 = crc64(p, len);
464 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
466 struct vhdr_md5 *vh = hdr_priv(hdr);
467 struct md5_ctx md5_ctx = {
468 .hash = (uint32_t *) vh->md5_digest,
472 md5_update(&md5_ctx, p, len);
476 * fill body of io_u->buf with random data and add a header with the
477 * crc32 or md5 sum of that data.
479 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
481 struct verify_header *hdr;
482 void *p = io_u->buf, *data;
483 unsigned int hdr_inc, data_len, header_num = 0;
485 if (td->o.verify == VERIFY_NULL)
488 fill_pattern(td, p, io_u->buflen);
490 hdr_inc = io_u->buflen;
491 if (td->o.verify_interval)
492 hdr_inc = td->o.verify_interval;
494 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
497 hdr->fio_magic = FIO_HDR_MAGIC;
498 hdr->verify_type = td->o.verify;
500 data_len = hdr_inc - hdr_size(hdr);
502 data = p + hdr_size(hdr);
503 switch (td->o.verify) {
505 fill_md5(hdr, data, data_len);
508 fill_crc64(hdr, data, data_len);
511 fill_crc32(hdr, data, data_len);
514 fill_crc16(hdr, data, data_len);
517 fill_crc7(hdr, data, data_len);
520 fill_sha256(hdr, data, data_len);
523 fill_sha512(hdr, data, data_len);
526 fill_meta(hdr, td, io_u, header_num);
529 log_err("fio: bad verify type: %d\n", td->o.verify);
532 if (td->o.verify_offset)
533 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
538 int get_next_verify(struct thread_data *td, struct io_u *io_u)
540 struct io_piece *ipo = NULL;
543 * this io_u is from a requeue, we already filled the offsets
548 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
549 struct rb_node *n = rb_first(&td->io_hist_tree);
551 ipo = rb_entry(n, struct io_piece, rb_node);
552 rb_erase(n, &td->io_hist_tree);
553 } else if (!list_empty(&td->io_hist_list)) {
554 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
555 list_del(&ipo->list);
559 io_u->offset = ipo->offset;
560 io_u->buflen = ipo->len;
561 io_u->file = ipo->file;
563 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
564 int r = td_io_open_file(td, io_u->file);
571 assert(io_u->file->flags & FIO_FILE_OPEN);
572 io_u->ddir = DDIR_READ;
573 io_u->xfer_buf = io_u->buf;
574 io_u->xfer_buflen = io_u->buflen;