Fix md5 verify
[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
11static void fill_random_bytes(struct thread_data *td,
12 void *p, unsigned int len)
13{
14 unsigned int todo;
15 int r;
16
17 while (len) {
18 r = os_random_long(&td->verify_state);
19
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
35void 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
43static void hexdump(void *buffer, int len)
44{
45 unsigned char *p = buffer;
46 int i;
47
48 for (i = 0; i < len; i++)
49 log_info("%02x", p[i]);
50 log_info("\n");
51}
52
53/*
54 * Return data area 'header_num'
55 */
56static inline void *io_u_verify_off(struct verify_header *hdr,
57 struct io_u *io_u,
58 unsigned char header_num)
59{
60 return io_u->buf + sizeof(*hdr) + header_num * hdr->len;
61}
62
63static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
64 unsigned char header_num)
65{
66 void *p = io_u_verify_off(hdr, io_u, header_num);
67 unsigned char c;
68
69 c = crc7(p, hdr->len - sizeof(*hdr));
70
71 if (c != hdr->crc7) {
72 log_err("crc7: verify failed at %llu/%u\n",
73 io_u->offset + header_num * hdr->len,
74 hdr->len);
75 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
76 return 1;
77 }
78
79 return 0;
80}
81
82static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
83 unsigned int header_num)
84{
85 void *p = io_u_verify_off(hdr, io_u, header_num);
86 unsigned short c;
87
88 c = crc16(p, hdr->len - sizeof(*hdr));
89
90 if (c != hdr->crc16) {
91 log_err("crc16: verify failed at %llu/%u\n",
92 io_u->offset + header_num * hdr->len,
93 hdr->len);
94 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
95 return 1;
96 }
97
98 return 0;
99}
100
101static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
102 unsigned int header_num)
103{
104 void *p = io_u_verify_off(hdr, io_u, header_num);
105 unsigned long long c;
106
107 c = crc64(p, hdr->len - sizeof(*hdr));
108
109 if (c != hdr->crc64) {
110 log_err("crc64: verify failed at %llu/%u\n",
111 io_u->offset + header_num * hdr->len,
112 hdr->len);
113 log_err("crc64: wanted %llx, got %llx\n", hdr->crc64, c);
114 return 1;
115 }
116
117 return 0;
118}
119
120static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
121 unsigned int header_num)
122{
123 void *p = io_u_verify_off(hdr, io_u, header_num);
124 unsigned long c;
125
126 c = crc32(p, hdr->len - sizeof(*hdr));
127
128 if (c != hdr->crc32) {
129 log_err("crc32: verify failed at %llu/%u\n",
130 io_u->offset + header_num * hdr->len,
131 hdr->len);
132 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
133 return 1;
134 }
135
136 return 0;
137}
138
139static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
140 unsigned int header_num)
141{
142 void *p = io_u_verify_off(hdr, io_u, header_num);
143 uint32_t hash[MD5_HASH_WORDS];
144 struct md5_ctx md5_ctx = {
145 .hash = hash,
146 };
147
148 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
149
150 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) {
151 log_err("md5: verify failed at %llu/%u\n",
152 io_u->offset + header_num * hdr->len,
153 hdr->len);
154 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
155 hexdump(md5_ctx.hash, sizeof(hash));
156 return 1;
157 }
158
159 return 0;
160}
161
162int verify_io_u(struct thread_data *td, struct io_u *io_u)
163{
164 struct verify_header *hdr;
165 unsigned int hdr_inc, hdr_num = 0;
166 void *p;
167 int ret;
168
169 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
170 return 0;
171
172 hdr_inc = io_u->buflen;
173 if (td->o.header_interval)
174 hdr_inc = td->o.header_interval;
175
176 for (p = io_u->buf; p < io_u->buf + io_u->buflen; p += hdr_inc) {
177 if (td->o.header_offset)
178 memswp(p, p + td->o.header_offset, sizeof(*hdr));
179
180 hdr = p;
181
182 if (hdr->fio_magic != FIO_HDR_MAGIC) {
183 log_err("Bad verify header %x\n", hdr->fio_magic);
184 return EIO;
185 }
186
187 switch (hdr->verify_type) {
188 case VERIFY_MD5:
189 ret = verify_io_u_md5(hdr, io_u, hdr_num);
190 break;
191 case VERIFY_CRC64:
192 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
193 break;
194 case VERIFY_CRC32:
195 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
196 break;
197 case VERIFY_CRC16:
198 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
199 break;
200 case VERIFY_CRC7:
201 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
202 break;
203 default:
204 log_err("Bad verify type %u\n", hdr->verify_type);
205 ret = 1;
206 }
207 hdr_num++;
208 }
209
210 return 0;
211}
212
213static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
214{
215 hdr->crc7 = crc7(p, len);
216}
217
218static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
219{
220 hdr->crc16 = crc16(p, len);
221}
222
223static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
224{
225 hdr->crc32 = crc32(p, len);
226}
227
228static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
229{
230 hdr->crc64 = crc64(p, len);
231}
232
233static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
234{
235 struct md5_ctx md5_ctx = {
236 .hash = (uint32_t *) hdr->md5_digest,
237 };
238
239 md5_update(&md5_ctx, p, len);
240}
241
242/*
243 * fill body of io_u->buf with random data and add a header with the
244 * crc32 or md5 sum of that data.
245 */
246void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
247{
248 struct verify_header *hdr;
249 void *p = io_u->buf, *data;
250 unsigned int hdr_inc, data_len;
251
252 if (td->o.verify == VERIFY_NULL)
253 return;
254
255 fill_random_bytes(td, p, io_u->buflen);
256
257 hdr_inc = io_u->buflen;
258 if (td->o.header_interval)
259 hdr_inc = td->o.header_interval;
260 data_len = hdr_inc - sizeof(*hdr);
261
262 for (;p < io_u->buf + io_u->buflen; p += hdr_inc) {
263 hdr = p;
264
265 hdr->fio_magic = FIO_HDR_MAGIC;
266 hdr->verify_type = td->o.verify;
267 hdr->len = hdr_inc;
268
269 data = p + sizeof(*hdr);
270 switch (td->o.verify) {
271 case VERIFY_MD5:
272 fill_md5(hdr, data, data_len);
273 break;
274 case VERIFY_CRC64:
275 fill_crc64(hdr, data, data_len);
276 break;
277 case VERIFY_CRC32:
278 fill_crc32(hdr, data, data_len);
279 break;
280 case VERIFY_CRC16:
281 fill_crc16(hdr, data, data_len);
282 break;
283 case VERIFY_CRC7:
284 fill_crc7(hdr, data, data_len);
285 break;
286 default:
287 log_err("fio: bad verify type: %d\n", td->o.verify);
288 assert(0);
289 }
290 if (td->o.header_offset)
291 memswp(p, p + td->o.header_offset, sizeof(*hdr));
292 }
293}
294
295int get_next_verify(struct thread_data *td, struct io_u *io_u)
296{
297 struct io_piece *ipo = NULL;
298
299 /*
300 * this io_u is from a requeue, we already filled the offsets
301 */
302 if (io_u->file)
303 return 0;
304
305 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
306 struct rb_node *n = rb_first(&td->io_hist_tree);
307
308 ipo = rb_entry(n, struct io_piece, rb_node);
309 rb_erase(n, &td->io_hist_tree);
310 } else if (!list_empty(&td->io_hist_list)) {
311 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
312 list_del(&ipo->list);
313 }
314
315 if (ipo) {
316 io_u->offset = ipo->offset;
317 io_u->buflen = ipo->len;
318 io_u->file = ipo->file;
319
320 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
321 int r = td_io_open_file(td, io_u->file);
322
323 if (r)
324 return 1;
325 }
326
327 get_file(ipo->file);
328 assert(io_u->file->flags & FIO_FILE_OPEN);
329 io_u->ddir = DDIR_READ;
330 io_u->xfer_buf = io_u->buf;
331 io_u->xfer_buflen = io_u->buflen;
332 free(ipo);
333 return 0;
334 }
335
336 return 1;
337}