blob: 8bc34571076d403475e764a4ec41df6554234431 [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
11static void fill_random_bytes(struct thread_data *td,
12 unsigned char *p, unsigned int len)
13{
14 unsigned int todo;
Jens Axboe4c5946c2007-07-26 11:55:10 +020015 int r;
Jens Axboee29d1b72006-10-18 15:43:15 +020016
17 while (len) {
Jens Axboe4c5946c2007-07-26 11:55:10 +020018 r = os_random_long(&td->verify_state);
Jens Axboee29d1b72006-10-18 15:43:15 +020019
20 /*
21 * lrand48_r seems to be broken and only fill the bottom
22 * 32-bits, even on 64-bit archs with 64-bit longs
23 */
24 todo = sizeof(r);
25 if (todo > len)
26 todo = len;
27
28 memcpy(p, &r, todo);
29
30 len -= todo;
31 p += todo;
32 }
33}
34
Shawn Lewis546a9142007-07-28 21:11:37 +020035void memswp(void* buf1, void* buf2, unsigned int len)
36{
37 struct verify_header swap;
38 memcpy(&swap, buf1, len);
39 memcpy(buf1, buf2, len);
40 memcpy(buf2, &swap, len);
41}
42
Jens Axboee29d1b72006-10-18 15:43:15 +020043static void hexdump(void *buffer, int len)
44{
45 unsigned char *p = buffer;
46 int i;
47
48 for (i = 0; i < len; i++)
Jens Axboe6d861442007-03-15 09:22:23 +010049 log_info("%02x", p[i]);
50 log_info("\n");
Jens Axboee29d1b72006-10-18 15:43:15 +020051}
52
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020053static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
54 unsigned char header_num)
Jens Axboe1e154bd2007-07-27 09:52:40 +020055{
56 unsigned char *p = io_u->buf;
57 unsigned char c;
58
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020059 p += header_num * hdr->len + sizeof(*hdr);
Jens Axboe1e154bd2007-07-27 09:52:40 +020060 c = crc7(p, hdr->len - sizeof(*hdr));
61
62 if (c != hdr->crc7) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020063 log_err("crc7: verify failed at %llu/%u\n",
64 io_u->offset + header_num * hdr->len,
65 hdr->len);
Jens Axboe1e154bd2007-07-27 09:52:40 +020066 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
67 return 1;
68 }
69
70 return 0;
71}
72
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020073static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
74 unsigned int header_num)
Jens Axboe969f7ed2007-07-27 09:07:17 +020075{
76 unsigned char *p = io_u->buf;
77 unsigned short c;
78
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020079 p += header_num * hdr->len + sizeof(*hdr);
Jens Axboe969f7ed2007-07-27 09:07:17 +020080 c = crc16(p, hdr->len - sizeof(*hdr));
81
82 if (c != hdr->crc16) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020083 log_err("crc16: verify failed at %llu/%u\n",
84 io_u->offset + header_num * hdr->len,
85 hdr->len);
Jens Axboe1f24ea42007-07-27 09:53:14 +020086 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
Jens Axboe969f7ed2007-07-27 09:07:17 +020087 return 1;
88 }
89
90 return 0;
91}
92
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020093static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
94 unsigned int header_num)
Jens Axboed77a7af2007-07-27 15:35:06 +020095{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020096 unsigned char *p = io_u->buf;
Jens Axboed77a7af2007-07-27 15:35:06 +020097 unsigned long long c;
98
Shawn Lewis3f9f4e22007-07-28 21:10:37 +020099 p += header_num * hdr->len + sizeof(*hdr);
Jens Axboed77a7af2007-07-27 15:35:06 +0200100 c = crc64(p, hdr->len - sizeof(*hdr));
101
102 if (c != hdr->crc64) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200103 log_err("crc64: verify failed at %llu/%u\n",
104 io_u->offset + header_num * hdr->len,
105 hdr->len);
Jens Axboed77a7af2007-07-27 15:35:06 +0200106 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
107 return 1;
108 }
109
110 return 0;
111}
112
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200113static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
114 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200115{
Jens Axboe969f7ed2007-07-27 09:07:17 +0200116 unsigned char *p = io_u->buf;
Jens Axboee29d1b72006-10-18 15:43:15 +0200117 unsigned long c;
118
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200119 p += header_num * hdr->len + sizeof(*hdr);
Jens Axboee29d1b72006-10-18 15:43:15 +0200120 c = crc32(p, hdr->len - sizeof(*hdr));
121
122 if (c != hdr->crc32) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200123 log_err("crc32: verify failed at %llu/%u\n",
124 io_u->offset + header_num * hdr->len,
125 hdr->len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200126 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
127 return 1;
128 }
129
130 return 0;
131}
132
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200133static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
134 unsigned int header_num)
Jens Axboee29d1b72006-10-18 15:43:15 +0200135{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200136 unsigned char *p = io_u->buf;
Jens Axboe8c432322007-07-27 13:16:24 +0200137 uint32_t hash[MD5_HASH_WORDS];
138 struct md5_ctx md5_ctx = {
139 .hash = hash,
140 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200141
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200142 p += header_num * hdr->len + sizeof(*hdr);
Jens Axboee29d1b72006-10-18 15:43:15 +0200143 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
144
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200145 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
146 log_err("md5: verify failed at %llu/%u\n",
147 io_u->offset + header_num * hdr->len,
148 hdr->len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200149 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
Jens Axboe8c432322007-07-27 13:16:24 +0200150 hexdump(md5_ctx.hash, sizeof(hash));
Jens Axboee29d1b72006-10-18 15:43:15 +0200151 return 1;
152 }
153
154 return 0;
155}
156
Jens Axboe36690c92007-03-26 10:23:34 +0200157int verify_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboee29d1b72006-10-18 15:43:15 +0200158{
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200159 unsigned char *p = (unsigned char*) io_u->buf;
160 struct verify_header *hdr;
161 unsigned int hdr_inc, hdr_num = 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200162 int ret;
163
Shawn Lewis1dcc0492007-07-27 08:02:45 +0200164 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
Jens Axboe36690c92007-03-26 10:23:34 +0200165 return 0;
166
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200167 hdr_inc = io_u->buflen;
168 if (td->o.header_interval)
169 hdr_inc = td->o.header_interval;
Jens Axboee29d1b72006-10-18 15:43:15 +0200170
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200171 for (; p < (unsigned char*) io_u->buf + io_u->buflen; p += hdr_inc) {
Shawn Lewis546a9142007-07-28 21:11:37 +0200172 if (td->o.header_offset)
173 memswp(p, &p[td->o.header_offset], sizeof(*hdr));
174
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200175 hdr = (struct verify_header*) p;
Jens Axboee29d1b72006-10-18 15:43:15 +0200176
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200177 if (hdr->fio_magic != FIO_HDR_MAGIC) {
178 log_err("Bad verify header %x\n", hdr->fio_magic);
179 return EIO;
180 }
181
182 switch (hdr->verify_type) {
183 case VERIFY_MD5:
184 ret = verify_io_u_md5(hdr, io_u, hdr_num);
185 break;
186 case VERIFY_CRC64:
187 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
188 break;
189 case VERIFY_CRC32:
190 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
191 break;
192 case VERIFY_CRC16:
193 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
194 break;
195 case VERIFY_CRC7:
196 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
197 break;
198 default:
199 log_err("Bad verify type %u\n", hdr->verify_type);
200 ret = 1;
201 }
202 hdr_num++;
203 }
Jens Axboea7dfe862007-03-12 10:05:08 +0100204
205 return 0;
Jens Axboee29d1b72006-10-18 15:43:15 +0200206}
207
Jens Axboe1e154bd2007-07-27 09:52:40 +0200208static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
209{
210 hdr->crc7 = crc7(p, len);
211}
212
Jens Axboe969f7ed2007-07-27 09:07:17 +0200213static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
214{
215 hdr->crc16 = crc16(p, len);
216}
217
Jens Axboee29d1b72006-10-18 15:43:15 +0200218static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
219{
220 hdr->crc32 = crc32(p, len);
221}
222
Jens Axboed77a7af2007-07-27 15:35:06 +0200223static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
224{
225 hdr->crc64 = crc64(p, len);
226}
227
Jens Axboee29d1b72006-10-18 15:43:15 +0200228static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
229{
Jens Axboe8c432322007-07-27 13:16:24 +0200230 struct md5_ctx md5_ctx = {
231 .hash = (uint32_t *) hdr->md5_digest,
232 };
Jens Axboee29d1b72006-10-18 15:43:15 +0200233
Jens Axboee29d1b72006-10-18 15:43:15 +0200234 md5_update(&md5_ctx, p, len);
Jens Axboee29d1b72006-10-18 15:43:15 +0200235}
236
237/*
238 * fill body of io_u->buf with random data and add a header with the
239 * crc32 or md5 sum of that data.
240 */
241void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
242{
Jens Axboebaefa9b2007-07-27 12:57:25 +0200243 struct verify_header *hdr;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200244 unsigned char *p = io_u->buf, *data;
Shawn Lewis546a9142007-07-28 21:11:37 +0200245 unsigned int hdr_inc, data_len;
Jens Axboee29d1b72006-10-18 15:43:15 +0200246
Jens Axboe9cc3d152007-03-26 10:32:30 +0200247 if (td->o.verify == VERIFY_NULL)
248 return;
249
Shawn Lewis546a9142007-07-28 21:11:37 +0200250 fill_random_bytes(td, p, io_u->buflen);
Jens Axboebaefa9b2007-07-27 12:57:25 +0200251
Shawn Lewis546a9142007-07-28 21:11:37 +0200252 hdr_inc = io_u->buflen;
253 if (td->o.header_interval)
254 hdr_inc = td->o.header_interval;
255 data_len = hdr_inc - sizeof(*hdr);
256
257 for (;p < (unsigned char*) io_u->buf + io_u->buflen; p += hdr_inc) {
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200258 hdr = (struct verify_header*) p;
259
260 hdr->fio_magic = FIO_HDR_MAGIC;
261 hdr->verify_type = td->o.verify;
Shawn Lewis546a9142007-07-28 21:11:37 +0200262 hdr->len = hdr_inc;
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200263
264 data = p + sizeof(*hdr);
Shawn Lewis3f9f4e22007-07-28 21:10:37 +0200265 switch (td->o.verify) {
266 case VERIFY_MD5:
267 fill_md5(hdr, data, data_len);
268 break;
269 case VERIFY_CRC64:
270 fill_crc64(hdr, data, data_len);
271 break;
272 case VERIFY_CRC32:
273 fill_crc32(hdr, data, data_len);
274 break;
275 case VERIFY_CRC16:
276 fill_crc16(hdr, data, data_len);
277 break;
278 case VERIFY_CRC7:
279 fill_crc7(hdr, data, data_len);
280 break;
281 default:
282 log_err("fio: bad verify type: %d\n", td->o.verify);
283 assert(0);
284 }
Shawn Lewis546a9142007-07-28 21:11:37 +0200285 if (td->o.header_offset)
286 memswp(p, &p[td->o.header_offset], sizeof(*hdr));
Jens Axboee29d1b72006-10-18 15:43:15 +0200287 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200288}
289
290int get_next_verify(struct thread_data *td, struct io_u *io_u)
291{
Jens Axboe8de8f042007-03-27 10:36:12 +0200292 struct io_piece *ipo = NULL;
Jens Axboee29d1b72006-10-18 15:43:15 +0200293
Jens Axboed2d7fa52007-02-19 13:21:35 +0100294 /*
295 * this io_u is from a requeue, we already filled the offsets
296 */
297 if (io_u->file)
298 return 0;
299
Jens Axboe8de8f042007-03-27 10:36:12 +0200300 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
301 struct rb_node *n = rb_first(&td->io_hist_tree);
302
Jens Axboe4b878982007-03-26 09:32:22 +0200303 ipo = rb_entry(n, struct io_piece, rb_node);
Jens Axboe4b878982007-03-26 09:32:22 +0200304 rb_erase(n, &td->io_hist_tree);
Jens Axboe8de8f042007-03-27 10:36:12 +0200305 } else if (!list_empty(&td->io_hist_list)) {
306 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
307 list_del(&ipo->list);
308 }
Jens Axboee29d1b72006-10-18 15:43:15 +0200309
Jens Axboe8de8f042007-03-27 10:36:12 +0200310 if (ipo) {
Jens Axboee29d1b72006-10-18 15:43:15 +0200311 io_u->offset = ipo->offset;
312 io_u->buflen = ipo->len;
Jens Axboe36167d82007-02-18 05:41:31 +0100313 io_u->file = ipo->file;
Jens Axboe97af62c2007-05-22 11:12:13 +0200314
315 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
316 int r = td_io_open_file(td, io_u->file);
317
318 if (r)
319 return 1;
320 }
321
322 get_file(ipo->file);
323 assert(io_u->file->flags & FIO_FILE_OPEN);
Jens Axboee29d1b72006-10-18 15:43:15 +0200324 io_u->ddir = DDIR_READ;
Jens Axboe36167d82007-02-18 05:41:31 +0100325 io_u->xfer_buf = io_u->buf;
326 io_u->xfer_buflen = io_u->buflen;
Jens Axboee29d1b72006-10-18 15:43:15 +0200327 free(ipo);
328 return 0;
329 }
330
331 return 1;
332}