56
61
CONNECTION_CLOSE_FAST,
67
struct nbd_request req;
68
struct reqcontext * next;
69
struct reqcontext * prev;
73
struct reqcontext * head;
74
struct reqcontext * tail;
94
void rclist_unlink(struct rclist * l, struct reqcontext * p) {
96
struct reqcontext * prev = p->prev;
97
struct reqcontext * next = p->next;
99
/* Fix link to previous */
116
/* Add a new list item to the tail */
117
void rclist_addtail(struct rclist * l, struct reqcontext * p) {
122
g_warning("addtail found list tail has a next pointer");
129
g_warning("addtail found no list tail but a list head");
138
void chunklist_unlink(struct chunklist * l, struct chunk * p) {
140
struct chunk * prev = p->prev;
141
struct chunk * next = p->next;
143
/* Fix link to previous */
160
/* Add a new list item to the tail */
161
void chunklist_addtail(struct chunklist * l, struct chunk * p) {
166
g_warning("addtail found list tail has a next pointer");
173
g_warning("addtail found no list tail but a list head");
182
/* Add some new bytes to a chunklist */
183
void addbuffer(struct chunklist * l, void * data, uint64_t len) {
185
uint64_t size = 64*1024;
186
struct chunk * pchunk;
190
/* First see if there is a current chunk, and if it has space */
191
if (l->tail && l->tail->space) {
192
uint64_t towrite = len;
193
if (towrite > l->tail->space)
194
towrite = l->tail->space;
195
memcpy(l->tail->writeptr, data, towrite);
196
l->tail->length += towrite;
197
l->tail->space -= towrite;
198
l->tail->writeptr += towrite;
204
/* We still need to write more, so prepare a new chunk */
205
if ((NULL == (buf = malloc(size))) || (NULL == (pchunk = calloc(1, sizeof(struct chunk))))) {
206
g_critical("Out of memory");
210
pchunk->buffer = buf;
211
pchunk->readptr = buf;
212
pchunk->writeptr = buf;
213
pchunk->space = size;
214
chunklist_addtail(l, pchunk);
220
/* returns 0 on success, -1 on failure */
221
int writebuffer(int fd, struct chunklist * l) {
223
struct chunk * pchunk = NULL;
233
if (!(pchunk->length) || !(pchunk->readptr)) {
234
chunklist_unlink(l, pchunk);
235
free(pchunk->buffer);
241
/* OK we have a chunk with some data in */
242
res = write(fd, pchunk->readptr, pchunk->length);
247
pchunk->length -= res;
248
pchunk->readptr += res;
249
if (!pchunk->length) {
250
chunklist_unlink(l, pchunk);
251
free(pchunk->buffer);
259
#define TEST_WRITE (1<<0)
260
#define TEST_FLUSH (1<<1)
262
int timeval_subtract (struct timeval *result, struct timeval *x,
264
if (x->tv_usec < y->tv_usec) {
265
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
266
y->tv_usec -= 1000000 * nsec;
270
if (x->tv_usec - y->tv_usec > 1000000) {
271
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
272
y->tv_usec += 1000000 * nsec;
276
result->tv_sec = x->tv_sec - y->tv_sec;
277
result->tv_usec = x->tv_usec - y->tv_usec;
279
return x->tv_sec < y->tv_sec;
282
double timeval_diff_to_double (struct timeval * x, struct timeval * y) {
284
timeval_subtract(&r, x, y);
285
return r.tv_sec * 1.0 + r.tv_usec/1000000.0;
59
288
static inline int read_all(int f, void *buf, size_t len) {
64
293
if((res=read(f, buf, len)) <=0) {
65
296
snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
134
READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read flags: %s", strerror(errno));
386
READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read reserved field: %s", strerror(errno));
135
387
/* reserved field */
136
write(sock, &tmp32, sizeof(tmp32));
388
WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write reserved field: %s", strerror(errno));
138
390
tmp64 = htonll(opts_magic);
139
write(sock, &tmp64, sizeof(tmp64));
391
WRITE_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not write magic: %s", strerror(errno));
141
393
tmp32 = htonl(NBD_OPT_EXPORT_NAME);
142
write(sock, &tmp32, sizeof(tmp32));
394
WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write option: %s", strerror(errno));
143
395
tmp32 = htonl((uint32_t)strlen(name));
144
write(sock, &tmp32, sizeof(tmp32));
145
write(sock, name, strlen(name));
396
WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write name length: %s", strerror(errno));
397
WRITE_ALL_ERRCHK(sock, name, strlen(name), err_open, "Could not write name:: %s", strerror(errno));
146
398
READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
147
399
size = ntohll(size);
148
READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t)+124, err_open, "Could not read reserved zeroes: %s", strerror(errno));
401
READ_ALL_ERRCHK(sock, &flags, sizeof(uint16_t), err_open, "Could not read flags: %s", strerror(errno));
402
flags = ntohs(flags);
403
*serverflags = flags;
404
READ_ALL_ERRCHK(sock, buf, 124, err_open, "Could not read reserved zeroes: %s", strerror(errno));
193
449
rep.magic=ntohl(rep.magic);
194
450
rep.error=ntohl(rep.error);
195
451
if(rep.magic!=NBD_REPLY_MAGIC) {
196
snprintf(errstr, errstr_len, "Received package with incorrect reply_magic. Index of sent packages is %lld (0x%llX), received handle is %lld (0x%llX). Received magic 0x%lX, expected 0x%lX", curhandle, curhandle, *((u64*)rep.handle), *((u64*)rep.handle), (long unsigned int)rep.magic, (long unsigned int)NBD_REPLY_MAGIC);
452
snprintf(errstr, errstr_len, "Received package with incorrect reply_magic. Index of sent packages is %lld (0x%llX), received handle is %lld (0x%llX). Received magic 0x%lX, expected 0x%lX", (long long int)curhandle, (long long unsigned int)curhandle, (long long int)*((u64*)rep.handle), (long long unsigned int)*((u64*)rep.handle), (long unsigned int)rep.magic, (long unsigned int)NBD_REPLY_MAGIC);
201
snprintf(errstr, errstr_len, "Received error from server: %ld (0x%lX). Handle is %lld (0x%llX).", (long int)rep.error, (long unsigned int)rep.error, (long long int)(*((u64*)rep.handle)), *((u64*)rep.handle));
457
snprintf(errstr, errstr_len, "Received error from server: %ld (0x%lX). Handle is %lld (0x%llX).", (long int)rep.error, (long unsigned int)rep.error, (long long int)(*((u64*)rep.handle)), (long long unsigned int)*((u64*)rep.handle));
205
READ_ALL_ERR_RT(sock, &buf, datasize, end, -1, "Could not read data: %s", strerror(errno));
462
READ_ALL_ERR_RT(sock, &buf, datasize, end, -1, "Could not read data: %s", strerror(errno));
211
int oversize_test(gchar* hostname, int port, char* name, int sock, char sock_is_open, char close_sock) {
468
int oversize_test(gchar* hostname, int port, char* name, int sock,
469
char sock_is_open, char close_sock, int testflags) {
213
471
struct nbd_request req;
214
472
struct nbd_reply rep;
217
pid_t mypid = getpid();
475
pid_t G_GNUC_UNUSED mypid = getpid();
218
476
char buf[((1024*1024)+sizeof(struct nbd_request)/2)<<1];
221
479
/* This should work */
222
480
if(!sock_is_open) {
223
if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
481
if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
224
482
g_warning("Could not open socket: %s", errstr);
365
659
snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
368
timespan=(float)(stop.tv_sec-start.tv_sec+(stop.tv_usec-start.tv_usec))/(float)1000000;
369
speed=(int)(size/timespan);
382
g_message("%d: Throughput test complete. Took %.3f seconds to complete, %d%siB/s", (int)getpid(), timespan,speed,speedchar);
386
close_connection(sock, CONNECTION_CLOSE_PROPERLY);
392
typedef int (*testfunc)(gchar*, int, char*, int, char, char);
662
timespan=timeval_diff_to_double(&stop, &start);
676
g_message("%d: Throughput %s test (%s flushes) complete. Took %.3f seconds to complete, %.3f%sib/s", (int)getpid(), (testflags & TEST_WRITE)?"write":"read", (testflags & TEST_FLUSH)?"with":"without", timespan, speed, speedchar);
680
close_connection(sock, CONNECTION_CLOSE_PROPERLY);
687
* fill 512 byte buffer 'buf' with a hashed selection of interesting data based
688
* only on handle and blknum. The first word is blknum, and the second handle, for ease
689
* of understanding. Things with handle 0 are blank.
691
static inline void makebuf(char *buf, uint64_t seq, uint64_t blknum) {
692
uint64_t x = ((uint64_t)blknum) ^ (seq << 32) ^ (seq >> 32);
693
uint64_t* p = (uint64_t*)buf;
699
for (i = 0; i<512/sizeof(uint64_t); i++) {
702
x+=0xFEEDA1ECDEADBEEFULL+i+(((uint64_t)i)<<56);
704
x = x ^ (x<<s) ^ (x>>(64-s)) ^ 0xAA55AA55AA55AA55ULL ^ seq;
708
static inline int checkbuf(char *buf, uint64_t seq, uint64_t blknum) {
710
makebuf(cmp, seq, blknum);
711
return memcmp(cmp, buf, 512)?-1:0;
714
static inline void dumpcommand(char * text, uint32_t command)
716
#ifdef DEBUG_COMMANDS
717
command=ntohl(command);
719
switch (command & NBD_CMD_MASK_COMMAND) {
721
ctext="NBD_CMD_READ";
724
ctext="NBD_CMD_WRITE";
727
ctext="NBD_CMD_DISC";
730
ctext="NBD_CMD_FLUSH";
736
printf("%s: %s [%s] (0x%08x)\n",
739
(command & NBD_CMD_FLAG_FUA)?"FUA":"NONE",
744
/* return an unused handle */
745
uint64_t getrandomhandle(GHashTable *phash) {
749
/* RAND_MAX may be as low as 2^15 */
750
for (i= 1 ; i<=5; i++)
751
handle ^= random() ^ (handle << 15);
752
} while (g_hash_table_lookup(phash, &handle));
756
int integrity_test(gchar* hostname, int port, char* name, int sock,
757
char sock_is_open, char close_sock, int testflags) {
758
struct nbd_reply rep;
762
struct timeval start;
766
char speedchar[2] = { '\0', '\0' };
769
pid_t G_GNUC_UNUSED mypid = getpid();
771
char *blkhashname=NULL;
772
uint32_t *blkhash = NULL;
775
uint64_t processed=0;
778
int readtransactionfile = 1;
779
struct rclist txqueue={NULL, NULL, 0};
780
struct rclist inflight={NULL, NULL, 0};
781
struct chunklist txbuf={NULL, NULL, 0};
783
GHashTable *handlehash = g_hash_table_new(g_int64_hash, g_int64_equal);
787
if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
788
g_warning("Could not open socket: %s", errstr);
794
if ((serverflags & (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))
795
!= (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))
796
g_warning("Server flags do not support FLUSH and FUA - these may error");
799
blkhashname=strdup("/tmp/blkarray-XXXXXX");
800
if (!blkhashname || (-1 == (blkhashfd = mkstemp(blkhashname)))) {
801
g_warning("Could not open temp file: %s", strerror(errno));
806
/* use tmpnam here to avoid further feature test nightmare */
807
if (-1 == (blkhashfd = open(blkhashname=strdup(tmpnam(NULL)),
809
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))) {
810
g_warning("Could not open temp file: %s", strerror(errno));
815
/* Ensure space freed if we die */
816
if (-1 == unlink(blkhashname)) {
817
g_warning("Could not unlink temp file: %s", strerror(errno));
822
if (-1 == lseek(blkhashfd, (off_t)((size>>9)<<2), SEEK_SET)) {
823
g_warning("Could not llseek temp file: %s", strerror(errno));
828
if (-1 == write(blkhashfd, "\0", 1)) {
829
g_warning("Could not write temp file: %s", strerror(errno));
834
if (NULL == (blkhash = mmap(NULL,
836
PROT_READ | PROT_WRITE,
840
g_warning("Could not mmap temp file: %s", strerror(errno));
845
if (-1 == (logfd = open(transactionlog, O_RDONLY)))
847
g_warning("Could open log file: %s", strerror(errno));
852
if(gettimeofday(&start, NULL)<0) {
854
snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
858
while (readtransactionfile || txqueue.numitems || txbuf.numitems || inflight.numitems) {
865
struct reqcontext * prc;
871
if (readtransactionfile)
872
FD_SET(logfd, &rset);
873
if (txqueue.numitems || txbuf.numitems)
875
if (inflight.numitems)
879
ret = select(1+((sock>logfd)?sock:logfd), &rset, &wset, NULL, &tv);
882
snprintf(errstr, errstr_len, "Timeout reading from socket");
885
g_warning("Could not mmap temp file: %s", errstr);
889
/* We know we've got at least one thing to do here then */
891
/* Get a command from the transaction log */
892
if (FD_ISSET(logfd, &rset)) {
894
/* Read a request or reply from the transaction file */
895
READ_ALL_ERRCHK(logfd,
899
"Could not read transaction log: %s",
901
magic = ntohl(magic);
903
case NBD_REQUEST_MAGIC:
904
if (NULL == (prc = calloc(1, sizeof(struct reqcontext)))) {
906
snprintf(errstr, errstr_len, "Could not allocate request");
909
READ_ALL_ERRCHK(logfd,
910
sizeof(magic)+(char *)&(prc->req),
911
sizeof(struct nbd_request)-sizeof(magic),
913
"Could not read transaction log: %s",
915
prc->req.magic = htonl(NBD_REQUEST_MAGIC);
916
memcpy(prc->orighandle, prc->req.handle, 8);
918
if ((ntohl(prc->req.type) & NBD_CMD_MASK_COMMAND) == NBD_CMD_DISC) {
919
/* no more to read; don't enqueue as no reply
920
* we will disconnect manually at the end
922
readtransactionfile = 0;
925
dumpcommand("Enqueuing command", prc->req.type);
926
rclist_addtail(&txqueue, prc);
930
case NBD_REPLY_MAGIC:
931
READ_ALL_ERRCHK(logfd,
932
sizeof(magic)+(char *)(&rep),
933
sizeof(struct nbd_reply)-sizeof(magic),
935
"Could not read transaction log: %s",
940
snprintf(errstr, errstr_len, "Transaction log file contained errored transaction");
944
/* We do not need to consume data on a read reply as there is
949
snprintf(errstr, errstr_len, "Could not measure start time: %08x", magic);
954
/* See if we have a write we can do */
955
if (FD_ISSET(sock, &wset))
957
if (!(txqueue.head) && !(txbuf.head))
958
g_warning("Socket write FD set but we shouldn't have been interested");
960
/* If there is no buffered data, generate some */
961
if (!(txbuf.head) && (NULL != (prc = txqueue.head)))
963
rclist_unlink(&txqueue, prc);
964
rclist_addtail(&inflight, prc);
966
if (ntohl(prc->req.magic) != NBD_REQUEST_MAGIC) {
968
g_warning("Asked to write a reply without a magic number");
972
dumpcommand("Sending command", prc->req.type);
973
command = ntohl(prc->req.type);
974
from = ntohll(prc->req.from);
975
len = ntohl(prc->req.len);
976
/* we rewrite the handle as they otherwise may not be unique */
977
*((uint64_t*)(prc->req.handle))=getrandomhandle(handlehash);
978
g_hash_table_insert(handlehash, prc->req.handle, prc);
979
addbuffer(&txbuf, &(prc->req), sizeof(struct nbd_request));
980
switch (command & NBD_CMD_MASK_COMMAND) {
984
uint64_t blknum = from>>9;
987
snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
988
(long long int) from, (long long int)size);
991
/* work out what we should be writing */
992
makebuf(dbuf, prc->seq, blknum);
993
addbuffer(&txbuf, dbuf, 512);
1006
snprintf(errstr, errstr_len, "Incomprehensible command: %08x", command);
1014
/* there should be some now */
1015
if (writebuffer(sock, &txbuf)<0) {
1017
snprintf(errstr, errstr_len, "Failed to write to socket buffer: %s", strerror(errno));
1023
/* See if there is a reply to be processed from the socket */
1024
if(FD_ISSET(sock, &rset)) {
1025
/* Okay, there's something ready for
1028
READ_ALL_ERRCHK(sock,
1030
sizeof(struct nbd_reply),
1032
"Could not read from server socket: %s",
1035
if (rep.magic != htonl(NBD_REPLY_MAGIC)) {
1037
snprintf(errstr, errstr_len, "Bad magic from server");
1043
snprintf(errstr, errstr_len, "Server errored a transaction");
1047
prc = g_hash_table_lookup(handlehash, rep.handle);
1050
snprintf(errstr, errstr_len, "Unrecognised handle in reply: 0x%llX", *(long long unsigned int*)(rep.handle));
1053
if (!g_hash_table_remove(handlehash, rep.handle)) {
1055
snprintf(errstr, errstr_len, "Could not remove handle from hash: 0x%llX", *(long long unsigned int*)(rep.handle));
1059
if (prc->req.magic != htonl(NBD_REQUEST_MAGIC)) {
1061
snprintf(errstr, errstr_len, "Bad magic in inflight data: %08x", prc->req.magic);
1065
dumpcommand("Processing reply to command", prc->req.type);
1066
command = ntohl(prc->req.type);
1067
from = ntohll(prc->req.from);
1068
len = ntohl(prc->req.len);
1070
switch (command & NBD_CMD_MASK_COMMAND) {
1073
uint64_t blknum = from>>9;
1076
snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
1077
(long long int) from, (long long int)size);
1080
READ_ALL_ERRCHK(sock,
1084
"Could not read data: %s",
1086
/* work out what we was written */
1087
if (checkbuf(dbuf, blkhash[blknum], blknum))
1090
snprintf(errstr, errstr_len, "Bad reply data: seq %08x", blkhash[blknum]);
1099
/* subsequent reads should get data with this seq*/
1101
uint64_t blknum = from>>9;
1102
blkhash[blknum]=(uint32_t)(prc->seq);
1112
rclist_unlink(&inflight, prc);
1113
prc->req.magic=0; /* so a duplicate reply is detected */
1117
if (!(printer++ % 10000) || !(readtransactionfile || txqueue.numitems || inflight.numitems) )
1118
printf("%d: Seq %08lld Queued: %08d Inflight: %08d Done: %08lld\n",
1120
(long long int) seq,
1123
(long long int) processed);
1127
if (gettimeofday(&stop, NULL)<0) {
1129
snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
1132
timespan=timeval_diff_to_double(&stop, &start);
1133
speed=xfer/timespan;
1146
g_message("%d: Integrity %s test complete. Took %.3f seconds to complete, %.3f%sib/s", (int)getpid(), (testflags & TEST_WRITE)?"write":"read", timespan, speed, speedchar);
1150
close_connection(sock, CONNECTION_CLOSE_PROPERLY);
1153
if (size && blkhash)
1154
munmap(blkhash, (size>>9)<<2);
1156
if (blkhashfd != -1)
1166
g_warning("%s",errstr);
1168
g_hash_table_destroy(handlehash);
1173
typedef int (*testfunc)(gchar*, int, char*, int, char, char, int);
394
1175
int main(int argc, char**argv) {
395
1176
gchar *hostname;