forked from libguestfs/libnbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strict-structured-reads.c
286 lines (257 loc) · 7.45 KB
/
strict-structured-reads.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* Example usage with qemu-nbd:
*
* sock=`mktemp -u`
* qemu-nbd -f $format -k $sock -r image
* ./strict-structured-reads $sock
*
* Example usage with nbdkit (but less useful while nbdkit can't send holes):
*
* nbdkit -U- sparse-random 1G --run './strict-structured-reads "$unixsocket"'
*
* This will perform read randomly over the image and check that all
* structured replies comply with the NBD spec (chunks may be out of
* order or interleaved, but no read succeeds unless chunks cover the
* entire region, with no overlapping or zero-length chunks).
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <libnbd.h>
/* A linked list of ranges still not seen. */
struct range {
uint64_t first;
uint64_t last;
struct range *next;
};
/* Per-read data. */
struct data {
uint64_t offset;
size_t count;
uint32_t flags;
size_t chunks;
struct range *remaining;
};
#define MAX_BUF (2 * 1024 * 1024)
static char buf[MAX_BUF];
/* Various statistics */
static int total_data_chunks;
static int64_t total_data_bytes;
static int total_hole_chunks;
static int64_t total_hole_bytes;
static int total_chunks;
static int total_df_reads;
static int total_reads;
static int64_t total_bytes;
static int total_success;
static int
read_chunk (void *opaque,
const void *bufv, size_t count, uint64_t offset,
unsigned status, int *error)
{
struct data *data = opaque;
struct range *r, **prev;
/* libnbd guarantees this: */
assert (offset >= data->offset);
assert (offset + count <= data->offset + data->count);
switch (status) {
case LIBNBD_READ_DATA:
total_data_chunks++;
total_data_bytes += count;
break;
case LIBNBD_READ_HOLE:
total_hole_chunks++;
total_hole_bytes += count;
break;
case LIBNBD_READ_ERROR:
assert (count == 0);
count = 1; /* Ensure no further chunks visit that offset */
break;
default:
goto error;
}
data->chunks++;
if (count == 0) {
fprintf (stderr, "buggy server: chunk must have non-zero size\n");
goto error;
}
/* Find element in remaining, or the server is in error */
for (prev = &data->remaining, r = *prev; r; prev = &r->next, r = r->next) {
if (offset >= r->first)
break;
}
if (r == NULL || offset + count > r->last) {
/* we fail to detect double errors reported at the same offset,
* but at least the read is already going to fail.
*/
if (status == LIBNBD_READ_ERROR)
return 0;
fprintf (stderr, "buggy server: chunk with overlapping range\n");
goto error;
}
/* Resize or split r to track new remaining bytes */
if (offset == r->first) {
if (offset + count == r->last) {
*prev = r->next;
free (r);
}
else
r->first += count;
}
else if (offset + count == r->last) {
r->last -= count;
}
else {
struct range *n = malloc (sizeof *n);
assert (n);
n->next = r->next;
r->next = n;
n->last = r->last;
r->last = offset - r->first;
n->first = offset + count;
}
return 0;
error:
*error = EPROTO;
return -1;
}
static int
read_verify (void *opaque, int *error)
{
int ret = 0;
struct data *data = opaque;
ret = -1;
total_reads++;
total_chunks += data->chunks;
if (*error)
goto cleanup;
assert (data->chunks > 0);
if (data->flags & LIBNBD_CMD_FLAG_DF) {
total_df_reads++;
if (data->chunks > 1) {
fprintf (stderr, "buggy server: too many chunks for DF flag\n");
*error = EPROTO;
goto cleanup;
}
}
if (data->remaining && !*error) {
fprintf (stderr, "buggy server: not enough chunks on success\n");
*error = EPROTO;
goto cleanup;
}
total_bytes += data->count;
total_success++;
ret = 0;
cleanup:
while (data->remaining) {
struct range *r = data->remaining;
data->remaining = r->next;
free (r);
}
return ret;
}
int
main (int argc, char *argv[])
{
struct nbd_handle *nbd;
size_t i;
int64_t exportsize;
int64_t maxsize = MAX_BUF;
srand (time (NULL));
if (argc != 2) {
fprintf (stderr, "%s socket|uri\n", argv[0]);
exit (EXIT_FAILURE);
}
nbd = nbd_create ();
if (nbd == NULL) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (strstr (argv[1], "://")) {
if (nbd_connect_uri (nbd, argv[1]) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
}
else if (nbd_connect_unix (nbd, argv[1]) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
exportsize = nbd_get_size (nbd);
if (exportsize == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (exportsize < 512) {
fprintf (stderr, "image is too small for useful testing\n");
exit (EXIT_FAILURE);
}
if (exportsize <= maxsize)
maxsize = exportsize - 1;
/* Queue up 1000 parallel reads. We are reusing the same buffer,
* which is not safe in real life, but okay here because we aren't
* validating contents, only server behavior.
*/
for (i = 0; i < 1000; ++i) {
uint32_t flags = 0;
struct data *d = malloc (sizeof *d);
struct range *r = malloc (sizeof *r);
uint64_t offset;
nbd_chunk_callback chunk_callback = { .callback = read_chunk,
.user_data = d };
nbd_completion_callback completion_callback = { .callback = read_verify,
.user_data = d,
.free = free };
assert (d && r);
offset = rand () % (exportsize - maxsize);
if (rand () & 1)
flags = LIBNBD_CMD_FLAG_DF;
*r = (struct range) { .first = offset, .last = offset + maxsize, };
*d = (struct data) { .offset = offset, .count = maxsize, .flags = flags,
.remaining = r, };
if (nbd_aio_pread_structured (nbd, buf, sizeof buf, offset, chunk_callback,
completion_callback, flags) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
}
while (nbd_aio_in_flight (nbd) > 0) {
int64_t cookie = nbd_aio_peek_command_completed (nbd);
if (cookie == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (cookie == 0) {
if (nbd_poll (nbd, -1) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
}
else
nbd_aio_command_completed (nbd, cookie);
}
if (nbd_shutdown (nbd, 0) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
printf ("traffic:\n");
printf (" bytes sent: %10" PRIu64 "\n", nbd_stats_bytes_sent (nbd));
printf (" bytes received: %10" PRIu64 "\n", nbd_stats_bytes_received (nbd));
printf (" chunks sent: %10" PRIu64 "\n", nbd_stats_chunks_sent (nbd));
printf (" chunks received: %10" PRIu64 "\n", nbd_stats_chunks_received (nbd));
printf ("totals:\n");
printf (" data chunks: %10d\n", total_data_chunks);
printf (" data bytes: %10" PRId64 "\n", total_data_bytes);
printf (" hole chunks: %10d\n", total_hole_chunks);
printf (" hole bytes: %10" PRId64 "\n", total_hole_bytes);
printf (" all chunks: %10d\n", total_chunks);
printf (" df reads: %10d\n", total_df_reads);
printf (" reads: %10d\n", total_reads);
printf (" bytes read: %10" PRId64 "\n", total_bytes);
printf (" compliant: %10d\n", total_success);
nbd_close (nbd);
exit (EXIT_SUCCESS);
}