2 * Test client to test the NBD server. Doesn't do anything useful, except
3 * checking that the server does, actually, work.
5 * Note that the only 'real' test is to check the client against a kernel. If
6 * it works here but does not work in the kernel, then that's most likely a bug
7 * in this program and/or in nbd-server.
9 * Copyright(c) 2006 Wouter Verhelst
11 * This program is Free Software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation, in version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * You should have received a copy of the GNU General Public License along with
21 * this program; if not, write to the Free Software Foundation, Inc., 51
22 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
30 #include <sys/socket.h>
38 #include <netinet/in.h>
41 #define MY_NAME "nbd-tester-client"
44 static gchar errstr[1024];
45 const static int errstr_len=1024;
49 static int looseordering = 0;
51 static gchar * transactionlog = "nbd-tester-client.tr";
55 CONNECTION_TYPE_CONNECT,
56 CONNECTION_TYPE_INIT_PASSWD,
57 CONNECTION_TYPE_CLISERV,
62 CONNECTION_CLOSE_PROPERLY,
63 CONNECTION_CLOSE_FAST,
69 struct nbd_request req;
70 struct reqcontext * next;
71 struct reqcontext * prev;
75 struct reqcontext * head;
76 struct reqcontext * tail;
102 void rclist_unlink(struct rclist * l, struct reqcontext * p) {
104 struct reqcontext * prev = p->prev;
105 struct reqcontext * next = p->next;
107 /* Fix link to previous */
124 /* Add a new list item to the tail */
125 void rclist_addtail(struct rclist * l, struct reqcontext * p) {
130 g_warning("addtail found list tail has a next pointer");
137 g_warning("addtail found no list tail but a list head");
146 void chunklist_unlink(struct chunklist * l, struct chunk * p) {
148 struct chunk * prev = p->prev;
149 struct chunk * next = p->next;
151 /* Fix link to previous */
168 /* Add a new list item to the tail */
169 void chunklist_addtail(struct chunklist * l, struct chunk * p) {
174 g_warning("addtail found list tail has a next pointer");
181 g_warning("addtail found no list tail but a list head");
190 /* Add some new bytes to a chunklist */
191 void addbuffer(struct chunklist * l, void * data, uint64_t len) {
193 uint64_t size = 64*1024;
194 struct chunk * pchunk;
198 /* First see if there is a current chunk, and if it has space */
199 if (l->tail && l->tail->space) {
200 uint64_t towrite = len;
201 if (towrite > l->tail->space)
202 towrite = l->tail->space;
203 memcpy(l->tail->writeptr, data, towrite);
204 l->tail->length += towrite;
205 l->tail->space -= towrite;
206 l->tail->writeptr += towrite;
212 /* We still need to write more, so prepare a new chunk */
213 if ((NULL == (buf = malloc(size))) || (NULL == (pchunk = calloc(1, sizeof(struct chunk))))) {
214 g_critical("Out of memory");
218 pchunk->buffer = buf;
219 pchunk->readptr = buf;
220 pchunk->writeptr = buf;
221 pchunk->space = size;
222 chunklist_addtail(l, pchunk);
228 /* returns 0 on success, -1 on failure */
229 int writebuffer(int fd, struct chunklist * l) {
231 struct chunk * pchunk = NULL;
241 if (!(pchunk->length) || !(pchunk->readptr)) {
242 chunklist_unlink(l, pchunk);
243 free(pchunk->buffer);
249 /* OK we have a chunk with some data in */
250 res = write(fd, pchunk->readptr, pchunk->length);
255 pchunk->length -= res;
256 pchunk->readptr += res;
257 if (!pchunk->length) {
258 chunklist_unlink(l, pchunk);
259 free(pchunk->buffer);
267 #define TEST_WRITE (1<<0)
268 #define TEST_FLUSH (1<<1)
270 int timeval_subtract (struct timeval *result, struct timeval *x,
272 if (x->tv_usec < y->tv_usec) {
273 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
274 y->tv_usec -= 1000000 * nsec;
278 if (x->tv_usec - y->tv_usec > 1000000) {
279 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
280 y->tv_usec += 1000000 * nsec;
284 result->tv_sec = x->tv_sec - y->tv_sec;
285 result->tv_usec = x->tv_usec - y->tv_usec;
287 return x->tv_sec < y->tv_sec;
290 double timeval_diff_to_double (struct timeval * x, struct timeval * y) {
292 timeval_subtract(&r, x, y);
293 return r.tv_sec * 1.0 + r.tv_usec/1000000.0;
296 static inline int read_all(int f, void *buf, size_t len) {
301 if((res=read(f, buf, len)) <=0) {
304 snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
314 static inline int write_all(int f, void *buf, size_t len) {
319 if((res=write(f, buf, len)) <=0) {
322 snprintf(errstr, errstr_len, "Write failed: %s", strerror(errno));
332 #define READ_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; }
333 #define READ_ALL_ERR_RT(f, buf, len, whereto, rval, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); retval = rval; goto whereto; }
335 #define WRITE_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; }
336 #define WRITE_ALL_ERR_RT(f, buf, len, whereto, rval, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); retval = rval; goto whereto; }
338 int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype, int* serverflags) {
340 struct hostent *host;
341 struct sockaddr_in addr;
343 uint64_t mymagic = (name ? opts_magic : cliserv_magic);
348 if(ctype<CONNECTION_TYPE_CONNECT)
350 if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
351 strncpy(errstr, strerror(errno), errstr_len);
355 if(!(host=gethostbyname(hostname))) {
356 strncpy(errstr, strerror(errno), errstr_len);
359 addr.sin_family=AF_INET;
360 addr.sin_port=htons(port);
361 addr.sin_addr.s_addr=*((int *) host->h_addr);
362 if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) {
363 strncpy(errstr, strerror(errno), errstr_len);
366 if(ctype<CONNECTION_TYPE_INIT_PASSWD)
368 READ_ALL_ERRCHK(sock, buf, strlen(INIT_PASSWD), err_open, "Could not read INIT_PASSWD: %s", strerror(errno));
370 snprintf(errstr, errstr_len, "Server closed connection");
373 if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
374 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
377 if(ctype<CONNECTION_TYPE_CLISERV)
379 READ_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not read cliserv_magic: %s", strerror(errno));
381 if(tmp64 != mymagic) {
382 strncpy(errstr, "mymagic does not match", errstr_len);
385 if(ctype<CONNECTION_TYPE_FULL)
388 READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
390 READ_ALL_ERRCHK(sock, buf, 128, err_open, "Could not read data: %s", strerror(errno));
394 READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read reserved field: %s", strerror(errno));
396 WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write reserved field: %s", strerror(errno));
398 tmp64 = htonll(opts_magic);
399 WRITE_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not write magic: %s", strerror(errno));
401 tmp32 = htonl(NBD_OPT_EXPORT_NAME);
402 WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write option: %s", strerror(errno));
403 tmp32 = htonl((uint32_t)strlen(name));
404 WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write name length: %s", strerror(errno));
405 WRITE_ALL_ERRCHK(sock, name, strlen(name), err_open, "Could not write name:: %s", strerror(errno));
406 READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
409 READ_ALL_ERRCHK(sock, &flags, sizeof(uint16_t), err_open, "Could not read flags: %s", strerror(errno));
410 flags = ntohs(flags);
411 *serverflags = flags;
412 READ_ALL_ERRCHK(sock, buf, 124, err_open, "Could not read reserved zeroes: %s", strerror(errno));
422 int close_connection(int sock, CLOSE_TYPE type) {
423 struct nbd_request req;
427 case CONNECTION_CLOSE_PROPERLY:
428 req.magic=htonl(NBD_REQUEST_MAGIC);
429 req.type=htonl(NBD_CMD_DISC);
430 memcpy(&(req.handle), &(counter), sizeof(counter));
434 if(write(sock, &req, sizeof(req))<0) {
435 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
438 case CONNECTION_CLOSE_FAST:
440 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
445 g_critical("Your compiler is on crack!"); /* or I am buggy */
451 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
452 struct nbd_reply rep;
456 READ_ALL_ERR_RT(sock, &rep, sizeof(rep), end, -1, "Could not read reply header: %s", strerror(errno));
457 rep.magic=ntohl(rep.magic);
458 rep.error=ntohl(rep.error);
459 if(rep.magic!=NBD_REPLY_MAGIC) {
460 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);
465 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));
470 READ_ALL_ERR_RT(sock, &buf, datasize, end, -1, "Could not read data: %s", strerror(errno));
476 int oversize_test(gchar* hostname, int port, char* name, int sock,
477 char sock_is_open, char close_sock, int testflags) {
479 struct nbd_request req;
480 struct nbd_reply rep;
483 pid_t G_GNUC_UNUSED mypid = getpid();
484 char buf[((1024*1024)+sizeof(struct nbd_request)/2)<<1];
487 /* This should work */
489 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
490 g_warning("Could not open socket: %s", errstr);
495 req.magic=htonl(NBD_REQUEST_MAGIC);
496 req.type=htonl(NBD_CMD_READ);
497 req.len=htonl(1024*1024);
498 memcpy(&(req.handle),&i,sizeof(i));
500 WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
501 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
502 READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
503 READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
505 snprintf(errstr, errstr_len, "Received unexpected error: %d", rep.error);
511 /* This probably should not work */
512 i++; req.from=htonll(i);
513 req.len = htonl(ntohl(req.len) + sizeof(struct nbd_request) / 2);
514 WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
515 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
516 READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
517 READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
519 printf("Received expected error\n");
525 /* ... unless this works, too */
526 i++; req.from=htonll(i);
527 req.len = htonl(ntohl(req.len) << 1);
528 WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
529 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
530 READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
531 READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
537 if((rep.error && !got_err) || (!rep.error && got_err)) {
538 printf("Received unexpected error\n");
545 int throughput_test(gchar* hostname, int port, char* name, int sock,
546 char sock_is_open, char close_sock, int testflags) {
549 struct nbd_request req;
553 struct timeval start;
557 char speedchar[2] = { '\0', '\0' };
560 signed int do_write=TRUE;
561 pid_t mypid = getpid();
564 if (!(testflags & TEST_WRITE))
565 testflags &= ~TEST_FLUSH;
567 memset (writebuf, 'X', 1024);
570 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
571 g_warning("Could not open socket: %s", errstr);
576 if ((testflags & TEST_FLUSH) && ((serverflags & (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))
577 != (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))) {
578 snprintf(errstr, errstr_len, "Server did not supply flush capability flags");
582 req.magic=htonl(NBD_REQUEST_MAGIC);
584 if(gettimeofday(&start, NULL)<0) {
586 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
589 for(i=0;i+1024<=size;i+=1024) {
591 int sendfua = (testflags & TEST_FLUSH) && (((i>>10) & 15) == 3);
592 int sendflush = (testflags & TEST_FLUSH) && (((i>>10) & 15) == 11);
593 req.type=htonl((testflags & TEST_WRITE)?NBD_CMD_WRITE:NBD_CMD_READ);
595 req.type = htonl(NBD_CMD_WRITE | NBD_CMD_FLAG_FUA);
596 memcpy(&(req.handle),&i,sizeof(i));
598 if (write_all(sock, &req, sizeof(req)) <0) {
602 if (testflags & TEST_WRITE) {
603 if (write_all(sock, writebuf, 1024) <0) {
608 printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
610 long long int j = i ^ (1LL<<63);
611 req.type = htonl(NBD_CMD_FLUSH);
612 memcpy(&(req.handle),&j,sizeof(j));
614 if (write_all(sock, &req, sizeof(req)) <0) {
618 printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
626 select(sock+1, &set, NULL, NULL, &tv);
627 if(FD_ISSET(sock, &set)) {
628 /* Okay, there's something ready for
630 if(read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i)<0) {
634 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
636 } while FD_ISSET(sock, &set);
637 /* Now wait until we can write again or until a second have
638 * passed, whichever comes first*/
643 do_write=select(sock+1,NULL,&set,NULL,&tv);
644 if(!do_write) printf("Select finished\n");
646 snprintf(errstr, errstr_len, "select: %s", strerror(errno));
651 /* Now empty the read buffer */
657 select(sock+1, &set, NULL, NULL, &tv);
658 if(FD_ISSET(sock, &set)) {
659 /* Okay, there's something ready for
661 read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i);
662 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
665 if(gettimeofday(&stop, NULL)<0) {
667 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
670 timespan=timeval_diff_to_double(&stop, &start);
684 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);
688 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
695 * fill 512 byte buffer 'buf' with a hashed selection of interesting data based
696 * only on handle and blknum. The first word is blknum, and the second handle, for ease
697 * of understanding. Things with handle 0 are blank.
699 static inline void makebuf(char *buf, uint64_t seq, uint64_t blknum) {
700 uint64_t x = ((uint64_t)blknum) ^ (seq << 32) ^ (seq >> 32);
701 uint64_t* p = (uint64_t*)buf;
707 for (i = 0; i<512/sizeof(uint64_t); i++) {
710 x+=0xFEEDA1ECDEADBEEFULL+i+(((uint64_t)i)<<56);
712 x = x ^ (x<<s) ^ (x>>(64-s)) ^ 0xAA55AA55AA55AA55ULL ^ seq;
716 static inline int checkbuf(char *buf, uint64_t seq, uint64_t blknum) {
718 makebuf(cmp, seq, blknum);
719 return memcmp(cmp, buf, 512)?-1:0;
722 static inline void dumpcommand(char * text, uint32_t command)
724 #ifdef DEBUG_COMMANDS
725 command=ntohl(command);
727 switch (command & NBD_CMD_MASK_COMMAND) {
729 ctext="NBD_CMD_READ";
732 ctext="NBD_CMD_WRITE";
735 ctext="NBD_CMD_DISC";
738 ctext="NBD_CMD_FLUSH";
744 printf("%s: %s [%s] (0x%08x)\n",
747 (command & NBD_CMD_FLAG_FUA)?"FUA":"NONE",
752 /* return an unused handle */
753 uint64_t getrandomhandle(GHashTable *phash) {
757 /* RAND_MAX may be as low as 2^15 */
758 for (i= 1 ; i<=5; i++)
759 handle ^= random() ^ (handle << 15);
760 } while (g_hash_table_lookup(phash, &handle));
764 int integrity_test(gchar* hostname, int port, char* name, int sock,
765 char sock_is_open, char close_sock, int testflags) {
766 struct nbd_reply rep;
770 struct timeval start;
774 char speedchar[2] = { '\0', '\0' };
777 pid_t G_GNUC_UNUSED mypid = getpid();
779 char *blkhashname=NULL;
780 struct blkitem *blkhash = NULL;
783 uint64_t processed=0;
786 int readtransactionfile = 1;
788 struct rclist txqueue={NULL, NULL, 0};
789 struct rclist inflight={NULL, NULL, 0};
790 struct chunklist txbuf={NULL, NULL, 0};
792 GHashTable *handlehash = g_hash_table_new(g_int64_hash, g_int64_equal);
796 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
797 g_warning("Could not open socket: %s", errstr);
803 if ((serverflags & (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))
804 != (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))
805 g_warning("Server flags do not support FLUSH and FUA - these may error");
808 blkhashname=strdup("/tmp/blkarray-XXXXXX");
809 if (!blkhashname || (-1 == (blkhashfd = mkstemp(blkhashname)))) {
810 g_warning("Could not open temp file: %s", strerror(errno));
815 /* use tmpnam here to avoid further feature test nightmare */
816 if (-1 == (blkhashfd = open(blkhashname=strdup(tmpnam(NULL)),
818 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))) {
819 g_warning("Could not open temp file: %s", strerror(errno));
824 /* Ensure space freed if we die */
825 if (-1 == unlink(blkhashname)) {
826 g_warning("Could not unlink temp file: %s", strerror(errno));
831 if (-1 == lseek(blkhashfd, (off_t)((size>>9)*sizeof(struct blkitem)), SEEK_SET)) {
832 g_warning("Could not llseek temp file: %s", strerror(errno));
837 if (-1 == write(blkhashfd, "\0", 1)) {
838 g_warning("Could not write temp file: %s", strerror(errno));
843 if (NULL == (blkhash = mmap(NULL,
844 (size>>9)*sizeof(struct blkitem),
845 PROT_READ | PROT_WRITE,
849 g_warning("Could not mmap temp file: %s", strerror(errno));
854 if (-1 == (logfd = open(transactionlog, O_RDONLY)))
856 g_warning("Could open log file: %s", strerror(errno));
861 if(gettimeofday(&start, NULL)<0) {
863 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
867 while (readtransactionfile || txqueue.numitems || txbuf.numitems || inflight.numitems) {
874 struct reqcontext * prc;
880 if (readtransactionfile)
881 FD_SET(logfd, &rset);
882 if ((!blocked && txqueue.numitems) || txbuf.numitems)
884 if (inflight.numitems)
888 ret = select(1+((sock>logfd)?sock:logfd), &rset, &wset, NULL, &tv);
891 snprintf(errstr, errstr_len, "Timeout reading from socket");
894 g_warning("Could not mmap temp file: %s", errstr);
898 /* We know we've got at least one thing to do here then */
900 /* Get a command from the transaction log */
901 if (FD_ISSET(logfd, &rset)) {
903 /* Read a request or reply from the transaction file */
904 READ_ALL_ERRCHK(logfd,
908 "Could not read transaction log: %s",
910 magic = ntohl(magic);
912 case NBD_REQUEST_MAGIC:
913 if (NULL == (prc = calloc(1, sizeof(struct reqcontext)))) {
915 snprintf(errstr, errstr_len, "Could not allocate request");
918 READ_ALL_ERRCHK(logfd,
919 sizeof(magic)+(char *)&(prc->req),
920 sizeof(struct nbd_request)-sizeof(magic),
922 "Could not read transaction log: %s",
924 prc->req.magic = htonl(NBD_REQUEST_MAGIC);
925 memcpy(prc->orighandle, prc->req.handle, 8);
927 if ((ntohl(prc->req.type) & NBD_CMD_MASK_COMMAND) == NBD_CMD_DISC) {
928 /* no more to read; don't enqueue as no reply
929 * we will disconnect manually at the end
931 readtransactionfile = 0;
934 dumpcommand("Enqueuing command", prc->req.type);
935 rclist_addtail(&txqueue, prc);
939 case NBD_REPLY_MAGIC:
940 READ_ALL_ERRCHK(logfd,
941 sizeof(magic)+(char *)(&rep),
942 sizeof(struct nbd_reply)-sizeof(magic),
944 "Could not read transaction log: %s",
949 snprintf(errstr, errstr_len, "Transaction log file contained errored transaction");
953 /* We do not need to consume data on a read reply as there is
958 snprintf(errstr, errstr_len, "Could not measure start time: %08x", magic);
963 /* See if we have a write we can do */
964 if (FD_ISSET(sock, &wset))
966 if ((!(txqueue.head) && !(txbuf.head)) || blocked)
967 g_warning("Socket write FD set but we shouldn't have been interested");
969 /* If there is no buffered data, generate some */
970 if (!blocked && !(txbuf.head) && (NULL != (prc = txqueue.head)))
972 if (ntohl(prc->req.magic) != NBD_REQUEST_MAGIC) {
974 g_warning("Asked to write a request without a magic number");
978 command = ntohl(prc->req.type);
979 from = ntohll(prc->req.from);
980 len = ntohl(prc->req.len);
982 /* First check whether we can touch this command at all. If this
983 * command is a read, and there is an inflight write, OR if this
984 * command is a write, and there is an inflight read or write, then
985 * we need to leave the command alone and signal that we are blocked
995 uint64_t blknum = cfrom>>9;
997 snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
998 (long long int) cfrom, (long long int)size);
1001 if (blkhash[blknum].inflightw ||
1002 (blkhash[blknum].inflightr &&
1003 ((command & NBD_CMD_MASK_COMMAND)==NBD_CMD_WRITE))) {
1015 rclist_unlink(&txqueue, prc);
1016 rclist_addtail(&inflight, prc);
1018 dumpcommand("Sending command", prc->req.type);
1019 /* we rewrite the handle as they otherwise may not be unique */
1020 *((uint64_t*)(prc->req.handle))=getrandomhandle(handlehash);
1021 g_hash_table_insert(handlehash, prc->req.handle, prc);
1022 addbuffer(&txbuf, &(prc->req), sizeof(struct nbd_request));
1023 switch (command & NBD_CMD_MASK_COMMAND) {
1027 uint64_t blknum = from>>9;
1030 snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
1031 (long long int) from, (long long int)size);
1034 (blkhash[blknum].inflightw)++;
1035 /* work out what we should be writing */
1036 makebuf(dbuf, prc->seq, blknum);
1037 addbuffer(&txbuf, dbuf, 512);
1045 uint64_t blknum = from>>9;
1047 snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
1048 (long long int) from, (long long int)size);
1051 (blkhash[blknum].inflightr)++;
1061 snprintf(errstr, errstr_len, "Incomprehensible command: %08x", command);
1070 /* there should be some now */
1071 if (writebuffer(sock, &txbuf)<0) {
1073 snprintf(errstr, errstr_len, "Failed to write to socket buffer: %s", strerror(errno));
1079 /* See if there is a reply to be processed from the socket */
1080 if(FD_ISSET(sock, &rset)) {
1081 /* Okay, there's something ready for
1084 READ_ALL_ERRCHK(sock,
1086 sizeof(struct nbd_reply),
1088 "Could not read from server socket: %s",
1091 if (rep.magic != htonl(NBD_REPLY_MAGIC)) {
1093 snprintf(errstr, errstr_len, "Bad magic from server");
1099 snprintf(errstr, errstr_len, "Server errored a transaction");
1103 prc = g_hash_table_lookup(handlehash, rep.handle);
1106 snprintf(errstr, errstr_len, "Unrecognised handle in reply: 0x%llX", *(long long unsigned int*)(rep.handle));
1109 if (!g_hash_table_remove(handlehash, rep.handle)) {
1111 snprintf(errstr, errstr_len, "Could not remove handle from hash: 0x%llX", *(long long unsigned int*)(rep.handle));
1115 if (prc->req.magic != htonl(NBD_REQUEST_MAGIC)) {
1117 snprintf(errstr, errstr_len, "Bad magic in inflight data: %08x", prc->req.magic);
1121 dumpcommand("Processing reply to command", prc->req.type);
1122 command = ntohl(prc->req.type);
1123 from = ntohll(prc->req.from);
1124 len = ntohl(prc->req.len);
1126 switch (command & NBD_CMD_MASK_COMMAND) {
1129 uint64_t blknum = from>>9;
1132 snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
1133 (long long int) from, (long long int)size);
1136 READ_ALL_ERRCHK(sock,
1140 "Could not read data: %s",
1142 if (--(blkhash[blknum].inflightr) <0 ) {
1143 snprintf(errstr, errstr_len, "Received a read reply for offset %llx when not in flight",
1144 (long long int) from);
1147 /* work out what we was written */
1148 if (checkbuf(dbuf, blkhash[blknum].seq, blknum)) {
1150 snprintf(errstr, errstr_len, "Bad reply data: I wanted blk %08x, seq %08x but I got (at a guess) blk %08x, seq %08x",
1151 (unsigned int) blknum,
1152 blkhash[blknum].seq,
1153 ((uint32_t *)(dbuf))[0],
1154 ((uint32_t *)(dbuf))[1]
1164 /* subsequent reads should get data with this seq*/
1166 uint64_t blknum = from>>9;
1167 if (--(blkhash[blknum].inflightw) <0 ) {
1168 snprintf(errstr, errstr_len, "Received a write reply for offset %llx when not in flight",
1169 (long long int) from);
1172 blkhash[blknum].seq=(uint32_t)(prc->seq);
1182 rclist_unlink(&inflight, prc);
1183 prc->req.magic=0; /* so a duplicate reply is detected */
1187 if (!(printer++ % 10000) || !(readtransactionfile || txqueue.numitems || inflight.numitems) )
1188 printf("%d: Seq %08lld Queued: %08d Inflight: %08d Done: %08lld\n",
1190 (long long int) seq,
1193 (long long int) processed);
1197 if (gettimeofday(&stop, NULL)<0) {
1199 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
1202 timespan=timeval_diff_to_double(&stop, &start);
1203 speed=xfer/timespan;
1216 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);
1220 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
1223 if (size && blkhash)
1224 munmap(blkhash, (size>>9)*sizeof(struct blkitem));
1226 if (blkhashfd != -1)
1236 g_warning("%s",errstr);
1238 g_hash_table_destroy(handlehash);
1243 typedef int (*testfunc)(gchar*, int, char*, int, char, char, int);
1245 int main(int argc, char**argv) {
1251 bool want_port = TRUE;
1254 testfunc test = throughput_test;
1256 /* Ignore SIGPIPE as we want to pick up the error from write() */
1257 signal (SIGPIPE, SIG_IGN);
1260 g_message("%d: Not enough arguments", (int)getpid());
1261 g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
1262 g_message("%d: Or: %s <hostname> -N <exportname> [<port>]", (int)getpid(), argv[0]);
1266 while((c=getopt(argc, argv, "-N:t:owfil"))>=0) {
1271 hostname=g_strdup(optarg);
1275 p=(strtol(argv[2], NULL, 0));
1276 if(p==LONG_MIN||p==LONG_MAX) {
1277 g_critical("Could not parse port number: %s", strerror(errno));
1284 name=g_strdup(optarg);
1291 transactionlog=g_strdup(optarg);
1300 testflags|=TEST_WRITE;
1303 testflags|=TEST_FLUSH;
1306 test=integrity_test;
1311 if(test(hostname, (int)p, name, sock, FALSE, TRUE, testflags)<0) {
1312 g_warning("Could not run test: %s", errstr);