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 gchar * transactionlog = "nbd-tester-client.tr";
53 CONNECTION_TYPE_CONNECT,
54 CONNECTION_TYPE_INIT_PASSWD,
55 CONNECTION_TYPE_CLISERV,
60 CONNECTION_CLOSE_PROPERLY,
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;
288 static inline int read_all(int f, void *buf, size_t len) {
293 if((res=read(f, buf, len)) <=0) {
296 snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
306 static inline int write_all(int f, void *buf, size_t len) {
311 if((res=write(f, buf, len)) <=0) {
314 snprintf(errstr, errstr_len, "Write failed: %s", strerror(errno));
324 #define READ_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; }
325 #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; }
327 #define WRITE_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; }
328 #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; }
330 int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype, int* serverflags) {
332 struct hostent *host;
333 struct sockaddr_in addr;
335 uint64_t mymagic = (name ? opts_magic : cliserv_magic);
340 if(ctype<CONNECTION_TYPE_CONNECT)
342 if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
343 strncpy(errstr, strerror(errno), errstr_len);
347 if(!(host=gethostbyname(hostname))) {
348 strncpy(errstr, strerror(errno), errstr_len);
351 addr.sin_family=AF_INET;
352 addr.sin_port=htons(port);
353 addr.sin_addr.s_addr=*((int *) host->h_addr);
354 if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) {
355 strncpy(errstr, strerror(errno), errstr_len);
358 if(ctype<CONNECTION_TYPE_INIT_PASSWD)
360 READ_ALL_ERRCHK(sock, buf, strlen(INIT_PASSWD), err_open, "Could not read INIT_PASSWD: %s", strerror(errno));
362 snprintf(errstr, errstr_len, "Server closed connection");
365 if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
366 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
369 if(ctype<CONNECTION_TYPE_CLISERV)
371 READ_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not read cliserv_magic: %s", strerror(errno));
373 if(tmp64 != mymagic) {
374 strncpy(errstr, "mymagic does not match", errstr_len);
377 if(ctype<CONNECTION_TYPE_FULL)
380 READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
382 READ_ALL_ERRCHK(sock, buf, 128, err_open, "Could not read data: %s", strerror(errno));
386 READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read reserved field: %s", strerror(errno));
388 WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write reserved field: %s", strerror(errno));
390 tmp64 = htonll(opts_magic);
391 WRITE_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not write magic: %s", strerror(errno));
393 tmp32 = htonl(NBD_OPT_EXPORT_NAME);
394 WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write option: %s", strerror(errno));
395 tmp32 = htonl((uint32_t)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));
398 READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %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));
414 int close_connection(int sock, CLOSE_TYPE type) {
415 struct nbd_request req;
419 case CONNECTION_CLOSE_PROPERLY:
420 req.magic=htonl(NBD_REQUEST_MAGIC);
421 req.type=htonl(NBD_CMD_DISC);
422 memcpy(&(req.handle), &(counter), sizeof(counter));
426 if(write(sock, &req, sizeof(req))<0) {
427 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
430 case CONNECTION_CLOSE_FAST:
432 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
437 g_critical("Your compiler is on crack!"); /* or I am buggy */
443 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
444 struct nbd_reply rep;
448 READ_ALL_ERR_RT(sock, &rep, sizeof(rep), end, -1, "Could not read reply header: %s", strerror(errno));
449 rep.magic=ntohl(rep.magic);
450 rep.error=ntohl(rep.error);
451 if(rep.magic!=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);
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));
462 READ_ALL_ERR_RT(sock, &buf, datasize, end, -1, "Could not read data: %s", strerror(errno));
468 int oversize_test(gchar* hostname, int port, char* name, int sock,
469 char sock_is_open, char close_sock, int testflags) {
471 struct nbd_request req;
472 struct nbd_reply rep;
475 pid_t G_GNUC_UNUSED mypid = getpid();
476 char buf[((1024*1024)+sizeof(struct nbd_request)/2)<<1];
479 /* This should work */
481 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
482 g_warning("Could not open socket: %s", errstr);
487 req.magic=htonl(NBD_REQUEST_MAGIC);
488 req.type=htonl(NBD_CMD_READ);
489 req.len=htonl(1024*1024);
490 memcpy(&(req.handle),&i,sizeof(i));
492 WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
493 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
494 READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
495 READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
497 snprintf(errstr, errstr_len, "Received unexpected error: %d", rep.error);
503 /* This probably should not work */
504 i++; req.from=htonll(i);
505 req.len = htonl(ntohl(req.len) + sizeof(struct nbd_request) / 2);
506 WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
507 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
508 READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
509 READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
511 printf("Received expected error\n");
517 /* ... unless this works, too */
518 i++; req.from=htonll(i);
519 req.len = htonl(ntohl(req.len) << 1);
520 WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
521 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
522 READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
523 READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
529 if((rep.error && !got_err) || (!rep.error && got_err)) {
530 printf("Received unexpected error\n");
537 int throughput_test(gchar* hostname, int port, char* name, int sock,
538 char sock_is_open, char close_sock, int testflags) {
541 struct nbd_request req;
545 struct timeval start;
549 char speedchar[2] = { '\0', '\0' };
552 signed int do_write=TRUE;
553 pid_t mypid = getpid();
556 if (!(testflags & TEST_WRITE))
557 testflags &= ~TEST_FLUSH;
559 memset (writebuf, 'X', 1024);
562 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
563 g_warning("Could not open socket: %s", errstr);
568 if ((testflags & TEST_FLUSH) && ((serverflags & (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))
569 != (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))) {
570 snprintf(errstr, errstr_len, "Server did not supply flush capability flags");
574 req.magic=htonl(NBD_REQUEST_MAGIC);
576 if(gettimeofday(&start, NULL)<0) {
578 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
581 for(i=0;i+1024<=size;i+=1024) {
583 int sendfua = (testflags & TEST_FLUSH) && (((i>>10) & 15) == 3);
584 int sendflush = (testflags & TEST_FLUSH) && (((i>>10) & 15) == 11);
585 req.type=htonl((testflags & TEST_WRITE)?NBD_CMD_WRITE:NBD_CMD_READ);
587 req.type = htonl(NBD_CMD_WRITE | NBD_CMD_FLAG_FUA);
588 memcpy(&(req.handle),&i,sizeof(i));
590 if (write_all(sock, &req, sizeof(req)) <0) {
594 if (testflags & TEST_WRITE) {
595 if (write_all(sock, writebuf, 1024) <0) {
600 printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
602 long long int j = i ^ (1LL<<63);
603 req.type = htonl(NBD_CMD_FLUSH);
604 memcpy(&(req.handle),&j,sizeof(j));
606 if (write_all(sock, &req, sizeof(req)) <0) {
610 printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
618 select(sock+1, &set, NULL, NULL, &tv);
619 if(FD_ISSET(sock, &set)) {
620 /* Okay, there's something ready for
622 if(read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i)<0) {
626 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
628 } while FD_ISSET(sock, &set);
629 /* Now wait until we can write again or until a second have
630 * passed, whichever comes first*/
635 do_write=select(sock+1,NULL,&set,NULL,&tv);
636 if(!do_write) printf("Select finished\n");
638 snprintf(errstr, errstr_len, "select: %s", strerror(errno));
643 /* Now empty the read buffer */
649 select(sock+1, &set, NULL, NULL, &tv);
650 if(FD_ISSET(sock, &set)) {
651 /* Okay, there's something ready for
653 read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i);
654 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
657 if(gettimeofday(&stop, NULL)<0) {
659 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
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);
1175 int main(int argc, char**argv) {
1181 bool want_port = TRUE;
1184 testfunc test = throughput_test;
1187 g_message("%d: Not enough arguments", (int)getpid());
1188 g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
1189 g_message("%d: Or: %s <hostname> -N <exportname>", (int)getpid(), argv[0]);
1193 while((c=getopt(argc, argv, "-N:t:owfi"))>=0) {
1198 hostname=g_strdup(optarg);
1203 p=(strtol(argv[2], NULL, 0));
1204 if(p==LONG_MIN||p==LONG_MAX) {
1205 g_critical("Could not parse port number: %s", strerror(errno));
1212 name=g_strdup(optarg);
1217 transactionlog=g_strdup(optarg);
1223 testflags|=TEST_WRITE;
1226 testflags|=TEST_FLUSH;
1229 test=integrity_test;
1234 if(test(hostname, (int)p, name, sock, FALSE, TRUE, testflags)<0) {
1235 g_warning("Could not run test: %s", errstr);