Release
[nbd.git] / nbd-tester-client.c
blob21013f444d15cb1df857dbbbbb2ee3a84be8a5f5
1 /*
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
18 * more details.
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
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <sys/mman.h>
33 #include <fcntl.h>
34 #include <syslog.h>
35 #include <unistd.h>
36 #include "config.h"
37 #include "lfs.h"
38 #include <netinet/in.h>
39 #include <glib.h>
41 #define MY_NAME "nbd-tester-client"
42 #include "cliserv.h"
44 static gchar errstr[1024];
45 const static int errstr_len=1024;
47 static uint64_t size;
49 static int looseordering = 0;
51 static gchar * transactionlog = "nbd-tester-client.tr";
53 typedef enum {
54 CONNECTION_TYPE_NONE,
55 CONNECTION_TYPE_CONNECT,
56 CONNECTION_TYPE_INIT_PASSWD,
57 CONNECTION_TYPE_CLISERV,
58 CONNECTION_TYPE_FULL,
59 } CONNECTION_TYPE;
61 typedef enum {
62 CONNECTION_CLOSE_PROPERLY,
63 CONNECTION_CLOSE_FAST,
64 } CLOSE_TYPE;
66 struct reqcontext {
67 uint64_t seq;
68 char orighandle[8];
69 struct nbd_request req;
70 struct reqcontext * next;
71 struct reqcontext * prev;
74 struct rclist {
75 struct reqcontext * head;
76 struct reqcontext * tail;
77 int numitems;
80 struct chunk {
81 char * buffer;
82 char * readptr;
83 char * writeptr;
84 uint64_t space;
85 uint64_t length;
86 struct chunk * next;
87 struct chunk * prev;
90 struct chunklist {
91 struct chunk * head;
92 struct chunk * tail;
93 int numitems;
96 struct blkitem {
97 uint32_t seq;
98 int32_t inflightr;
99 int32_t inflightw;
102 void rclist_unlink(struct rclist * l, struct reqcontext * p) {
103 if (p && l) {
104 struct reqcontext * prev = p->prev;
105 struct reqcontext * next = p->next;
107 /* Fix link to previous */
108 if (prev)
109 prev->next = next;
110 else
111 l->head = next;
113 if (next)
114 next->prev = prev;
115 else
116 l->tail = prev;
118 p->prev = NULL;
119 p->next = NULL;
120 l->numitems--;
124 /* Add a new list item to the tail */
125 void rclist_addtail(struct rclist * l, struct reqcontext * p) {
126 if (!p || !l)
127 return;
128 if (l->tail) {
129 if (l->tail->next)
130 g_warning("addtail found list tail has a next pointer");
131 l->tail->next = p;
132 p->next = NULL;
133 p->prev = l->tail;
134 l->tail = p;
135 } else {
136 if (l->head)
137 g_warning("addtail found no list tail but a list head");
138 l->head = p;
139 l->tail = p;
140 p->prev = NULL;
141 p->next = NULL;
143 l->numitems++;
146 void chunklist_unlink(struct chunklist * l, struct chunk * p) {
147 if (p && l) {
148 struct chunk * prev = p->prev;
149 struct chunk * next = p->next;
151 /* Fix link to previous */
152 if (prev)
153 prev->next = next;
154 else
155 l->head = next;
157 if (next)
158 next->prev = prev;
159 else
160 l->tail = prev;
162 p->prev = NULL;
163 p->next = NULL;
164 l->numitems--;
168 /* Add a new list item to the tail */
169 void chunklist_addtail(struct chunklist * l, struct chunk * p) {
170 if (!p || !l)
171 return;
172 if (l->tail) {
173 if (l->tail->next)
174 g_warning("addtail found list tail has a next pointer");
175 l->tail->next = p;
176 p->next = NULL;
177 p->prev = l->tail;
178 l->tail = p;
179 } else {
180 if (l->head)
181 g_warning("addtail found no list tail but a list head");
182 l->head = p;
183 l->tail = p;
184 p->prev = NULL;
185 p->next = NULL;
187 l->numitems++;
190 /* Add some new bytes to a chunklist */
191 void addbuffer(struct chunklist * l, void * data, uint64_t len) {
192 void * buf;
193 uint64_t size = 64*1024;
194 struct chunk * pchunk;
196 while (len>0)
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;
207 len -= towrite;
208 data += towrite;
211 if (len>0) {
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");
215 exit (1);
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;
232 int res;
233 if (!l)
234 return 0;
236 while (!pchunk)
238 pchunk = l->head;
239 if (!pchunk)
240 return 0;
241 if (!(pchunk->length) || !(pchunk->readptr)) {
242 chunklist_unlink(l, pchunk);
243 free(pchunk->buffer);
244 free(pchunk);
245 pchunk = NULL;
249 /* OK we have a chunk with some data in */
250 res = write(fd, pchunk->readptr, pchunk->length);
251 if (res==0)
252 errno = EAGAIN;
253 if (res<=0)
254 return -1;
255 pchunk->length -= res;
256 pchunk->readptr += res;
257 if (!pchunk->length) {
258 chunklist_unlink(l, pchunk);
259 free(pchunk->buffer);
260 free(pchunk);
262 return 0;
267 #define TEST_WRITE (1<<0)
268 #define TEST_FLUSH (1<<1)
270 int timeval_subtract (struct timeval *result, struct timeval *x,
271 struct timeval *y) {
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;
275 y->tv_sec += 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;
281 y->tv_sec -= 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) {
291 struct timeval r;
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) {
297 ssize_t res;
298 size_t retval=0;
300 while(len>0) {
301 if((res=read(f, buf, len)) <=0) {
302 if (!res)
303 errno=EAGAIN;
304 snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
305 return -1;
307 len-=res;
308 buf+=res;
309 retval+=res;
311 return retval;
314 static inline int write_all(int f, void *buf, size_t len) {
315 ssize_t res;
316 size_t retval=0;
318 while(len>0) {
319 if((res=write(f, buf, len)) <=0) {
320 if (!res)
321 errno=EAGAIN;
322 snprintf(errstr, errstr_len, "Write failed: %s", strerror(errno));
323 return -1;
325 len-=res;
326 buf+=res;
327 retval+=res;
329 return retval;
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) {
339 int sock;
340 struct hostent *host;
341 struct sockaddr_in addr;
342 char buf[256];
343 uint64_t mymagic = (name ? opts_magic : cliserv_magic);
344 u64 tmp64;
345 uint32_t tmp32 = 0;
347 sock=0;
348 if(ctype<CONNECTION_TYPE_CONNECT)
349 goto end;
350 if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
351 strncpy(errstr, strerror(errno), errstr_len);
352 goto err;
354 setmysockopt(sock);
355 if(!(host=gethostbyname(hostname))) {
356 strncpy(errstr, hstrerror(h_errno), errstr_len);
357 goto err_open;
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);
364 goto err_open;
366 if(ctype<CONNECTION_TYPE_INIT_PASSWD)
367 goto end;
368 READ_ALL_ERRCHK(sock, buf, strlen(INIT_PASSWD), err_open, "Could not read INIT_PASSWD: %s", strerror(errno));
369 if(strlen(buf)==0) {
370 snprintf(errstr, errstr_len, "Server closed connection");
371 goto err_open;
373 if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
374 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
375 goto err_open;
377 if(ctype<CONNECTION_TYPE_CLISERV)
378 goto end;
379 READ_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not read cliserv_magic: %s", strerror(errno));
380 tmp64=ntohll(tmp64);
381 if(tmp64 != mymagic) {
382 strncpy(errstr, "mymagic does not match", errstr_len);
383 goto err_open;
385 if(ctype<CONNECTION_TYPE_FULL)
386 goto end;
387 if(!name) {
388 READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
389 size=ntohll(size);
390 READ_ALL_ERRCHK(sock, buf, 128, err_open, "Could not read data: %s", strerror(errno));
391 goto end;
393 /* flags */
394 READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read reserved field: %s", strerror(errno));
395 /* reserved field */
396 WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write reserved field: %s", strerror(errno));
397 /* magic */
398 tmp64 = htonll(opts_magic);
399 WRITE_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not write magic: %s", strerror(errno));
400 /* name */
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));
407 size = ntohll(size);
408 uint16_t flags;
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));
413 goto end;
414 err_open:
415 close(sock);
416 err:
417 sock=-1;
418 end:
419 return sock;
422 int close_connection(int sock, CLOSE_TYPE type) {
423 struct nbd_request req;
424 u64 counter=0;
426 switch(type) {
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));
431 counter++;
432 req.from=0;
433 req.len=0;
434 if(write(sock, &req, sizeof(req))<0) {
435 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
436 return -1;
438 case CONNECTION_CLOSE_FAST:
439 if(close(sock)<0) {
440 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
441 return -1;
443 break;
444 default:
445 g_critical("Your compiler is on crack!"); /* or I am buggy */
446 return -1;
448 return 0;
451 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
452 struct nbd_reply rep;
453 int retval=0;
454 char buf[datasize];
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);
461 retval=-1;
462 goto end;
464 if(rep.error) {
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));
466 retval=-1;
467 goto end;
469 if (datasize)
470 READ_ALL_ERR_RT(sock, &buf, datasize, end, -1, "Could not read data: %s", strerror(errno));
472 end:
473 return retval;
476 int oversize_test(gchar* hostname, int port, char* name, int sock,
477 char sock_is_open, char close_sock, int testflags) {
478 int retval=0;
479 struct nbd_request req;
480 struct nbd_reply rep;
481 int i=0;
482 int serverflags = 0;
483 pid_t G_GNUC_UNUSED mypid = getpid();
484 char buf[((1024*1024)+sizeof(struct nbd_request)/2)<<1];
485 bool got_err;
487 /* This should work */
488 if(!sock_is_open) {
489 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
490 g_warning("Could not open socket: %s", errstr);
491 retval=-1;
492 goto err;
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));
499 req.from=htonll(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));
504 if(rep.error) {
505 snprintf(errstr, errstr_len, "Received unexpected error: %d", rep.error);
506 retval=-1;
507 goto err;
508 } else {
509 printf("OK\n");
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));
518 if(rep.error) {
519 printf("Received expected error\n");
520 got_err=true;
521 } else {
522 printf("OK\n");
523 got_err=false;
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));
532 if(rep.error) {
533 printf("error\n");
534 } else {
535 printf("OK\n");
537 if((rep.error && !got_err) || (!rep.error && got_err)) {
538 printf("Received unexpected error\n");
539 retval=-1;
541 err:
542 return retval;
545 int throughput_test(gchar* hostname, int port, char* name, int sock,
546 char sock_is_open, char close_sock, int testflags) {
547 long long int i;
548 char writebuf[1024];
549 struct nbd_request req;
550 int requests=0;
551 fd_set set;
552 struct timeval tv;
553 struct timeval start;
554 struct timeval stop;
555 double timespan;
556 double speed;
557 char speedchar[2] = { '\0', '\0' };
558 int retval=0;
559 int serverflags = 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);
568 size=0;
569 if(!sock_is_open) {
570 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
571 g_warning("Could not open socket: %s", errstr);
572 retval=-1;
573 goto err;
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");
579 retval = -1;
580 goto err_open;
582 req.magic=htonl(NBD_REQUEST_MAGIC);
583 req.len=htonl(1024);
584 if(gettimeofday(&start, NULL)<0) {
585 retval=-1;
586 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
587 goto err_open;
589 for(i=0;i+1024<=size;i+=1024) {
590 if(do_write) {
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);
594 if (sendfua)
595 req.type = htonl(NBD_CMD_WRITE | NBD_CMD_FLAG_FUA);
596 memcpy(&(req.handle),&i,sizeof(i));
597 req.from=htonll(i);
598 if (write_all(sock, &req, sizeof(req)) <0) {
599 retval=-1;
600 goto err_open;
602 if (testflags & TEST_WRITE) {
603 if (write_all(sock, writebuf, 1024) <0) {
604 retval=-1;
605 goto err_open;
608 printf("%d: Requests(+): %d\r", (int)mypid, ++requests);
609 if (sendflush) {
610 long long int j = i ^ (1LL<<63);
611 req.type = htonl(NBD_CMD_FLUSH);
612 memcpy(&(req.handle),&j,sizeof(j));
613 req.from=0;
614 if (write_all(sock, &req, sizeof(req)) <0) {
615 retval=-1;
616 goto err_open;
618 printf("%d: Requests(+): %d\r", (int)mypid, ++requests);
621 do {
622 FD_ZERO(&set);
623 FD_SET(sock, &set);
624 tv.tv_sec=0;
625 tv.tv_usec=0;
626 select(sock+1, &set, NULL, NULL, &tv);
627 if(FD_ISSET(sock, &set)) {
628 /* Okay, there's something ready for
629 * reading here */
630 if(read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i)<0) {
631 retval=-1;
632 goto err_open;
634 printf("%d: Requests(-): %d\r", (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*/
639 FD_ZERO(&set);
640 FD_SET(sock, &set);
641 tv.tv_sec=1;
642 tv.tv_usec=0;
643 do_write=select(sock+1,NULL,&set,NULL,&tv);
644 if(!do_write) printf("Select finished\n");
645 if(do_write<0) {
646 snprintf(errstr, errstr_len, "select: %s", strerror(errno));
647 retval=-1;
648 goto err_open;
651 /* Now empty the read buffer */
652 do {
653 FD_ZERO(&set);
654 FD_SET(sock, &set);
655 tv.tv_sec=0;
656 tv.tv_usec=0;
657 select(sock+1, &set, NULL, NULL, &tv);
658 if(FD_ISSET(sock, &set)) {
659 /* Okay, there's something ready for
660 * reading here */
661 read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i);
662 printf("%d: Requests(-): %d\r", (int)mypid, --requests);
664 } while (requests);
665 printf("\n");
666 if(gettimeofday(&stop, NULL)<0) {
667 retval=-1;
668 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
669 goto err_open;
671 timespan=timeval_diff_to_double(&stop, &start);
672 speed=size/timespan;
673 if(speed>1024) {
674 speed=speed/1024.0;
675 speedchar[0]='K';
677 if(speed>1024) {
678 speed=speed/1024.0;
679 speedchar[0]='M';
681 if(speed>1024) {
682 speed=speed/1024.0;
683 speedchar[0]='G';
685 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);
687 err_open:
688 if(close_sock) {
689 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
691 err:
692 return retval;
696 * fill 512 byte buffer 'buf' with a hashed selection of interesting data based
697 * only on handle and blknum. The first word is blknum, and the second handle, for ease
698 * of understanding. Things with handle 0 are blank.
700 static inline void makebuf(char *buf, uint64_t seq, uint64_t blknum) {
701 uint64_t x = ((uint64_t)blknum) ^ (seq << 32) ^ (seq >> 32);
702 uint64_t* p = (uint64_t*)buf;
703 int i;
704 if (!seq) {
705 bzero(buf, 512);
706 return;
708 for (i = 0; i<512/sizeof(uint64_t); i++) {
709 int s;
710 *(p++) = x;
711 x+=0xFEEDA1ECDEADBEEFULL+i+(((uint64_t)i)<<56);
712 s = x & 63;
713 x = x ^ (x<<s) ^ (x>>(64-s)) ^ 0xAA55AA55AA55AA55ULL ^ seq;
717 static inline int checkbuf(char *buf, uint64_t seq, uint64_t blknum) {
718 uint64_t cmp[64]; // 512/8 = 64
719 makebuf((char *)cmp, seq, blknum);
720 return memcmp(cmp, buf, 512)?-1:0;
723 static inline void dumpcommand(char * text, uint32_t command)
725 #ifdef DEBUG_COMMANDS
726 command=ntohl(command);
727 char * ctext;
728 switch (command & NBD_CMD_MASK_COMMAND) {
729 case NBD_CMD_READ:
730 ctext="NBD_CMD_READ";
731 break;
732 case NBD_CMD_WRITE:
733 ctext="NBD_CMD_WRITE";
734 break;
735 case NBD_CMD_DISC:
736 ctext="NBD_CMD_DISC";
737 break;
738 case NBD_CMD_FLUSH:
739 ctext="NBD_CMD_FLUSH";
740 break;
741 default:
742 ctext="UNKNOWN";
743 break;
745 printf("%s: %s [%s] (0x%08x)\n",
746 text,
747 ctext,
748 (command & NBD_CMD_FLAG_FUA)?"FUA":"NONE",
749 command);
750 #endif
753 /* return an unused handle */
754 uint64_t getrandomhandle(GHashTable *phash) {
755 uint64_t handle = 0;
756 int i;
757 do {
758 /* RAND_MAX may be as low as 2^15 */
759 for (i= 1 ; i<=5; i++)
760 handle ^= random() ^ (handle << 15);
761 } while (g_hash_table_lookup(phash, &handle));
762 return handle;
765 int integrity_test(gchar* hostname, int port, char* name, int sock,
766 char sock_is_open, char close_sock, int testflags) {
767 struct nbd_reply rep;
768 fd_set rset;
769 fd_set wset;
770 struct timeval tv;
771 struct timeval start;
772 struct timeval stop;
773 double timespan;
774 double speed;
775 char speedchar[2] = { '\0', '\0' };
776 int retval=0;
777 int serverflags = 0;
778 pid_t G_GNUC_UNUSED mypid = getpid();
779 int blkhashfd = -1;
780 char *blkhashname=NULL;
781 struct blkitem *blkhash = NULL;
782 int logfd=-1;
783 uint64_t seq=1;
784 uint64_t processed=0;
785 uint64_t printer=0;
786 uint64_t xfer=0;
787 int readtransactionfile = 1;
788 int blocked = 0;
789 struct rclist txqueue={NULL, NULL, 0};
790 struct rclist inflight={NULL, NULL, 0};
791 struct chunklist txbuf={NULL, NULL, 0};
793 GHashTable *handlehash = g_hash_table_new(g_int64_hash, g_int64_equal);
795 size=0;
796 if(!sock_is_open) {
797 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
798 g_warning("Could not open socket: %s", errstr);
799 retval=-1;
800 goto err;
804 if ((serverflags & (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))
805 != (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))
806 g_warning("Server flags do not support FLUSH and FUA - these may error");
808 #ifdef HAVE_MKSTEMP
809 blkhashname=strdup("/tmp/blkarray-XXXXXX");
810 if (!blkhashname || (-1 == (blkhashfd = mkstemp(blkhashname)))) {
811 g_warning("Could not open temp file: %s", strerror(errno));
812 retval=-1;
813 goto err;
815 #else
816 /* use tmpnam here to avoid further feature test nightmare */
817 if (-1 == (blkhashfd = open(blkhashname=strdup(tmpnam(NULL)),
818 O_CREAT | O_RDWR,
819 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))) {
820 g_warning("Could not open temp file: %s", strerror(errno));
821 retval=-1;
822 goto err;
824 #endif
825 /* Ensure space freed if we die */
826 if (-1 == unlink(blkhashname)) {
827 g_warning("Could not unlink temp file: %s", strerror(errno));
828 retval=-1;
829 goto err;
832 if (-1 == lseek(blkhashfd, (off_t)((size>>9)*sizeof(struct blkitem)), SEEK_SET)) {
833 g_warning("Could not llseek temp file: %s", strerror(errno));
834 retval=-1;
835 goto err;
838 if (-1 == write(blkhashfd, "\0", 1)) {
839 g_warning("Could not write temp file: %s", strerror(errno));
840 retval=-1;
841 goto err;
844 if (NULL == (blkhash = mmap(NULL,
845 (size>>9)*sizeof(struct blkitem),
846 PROT_READ | PROT_WRITE,
847 MAP_SHARED,
848 blkhashfd,
849 0))) {
850 g_warning("Could not mmap temp file: %s", strerror(errno));
851 retval=-1;
852 goto err;
855 if (-1 == (logfd = open(transactionlog, O_RDONLY)))
857 g_warning("Could open log file: %s", strerror(errno));
858 retval=-1;
859 goto err;
862 if(gettimeofday(&start, NULL)<0) {
863 retval=-1;
864 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
865 goto err_open;
868 while (readtransactionfile || txqueue.numitems || txbuf.numitems || inflight.numitems) {
869 int ret;
871 uint32_t magic;
872 uint32_t command;
873 uint64_t from;
874 uint32_t len;
875 struct reqcontext * prc;
877 *errstr=0;
879 FD_ZERO(&wset);
880 FD_ZERO(&rset);
881 if (readtransactionfile)
882 FD_SET(logfd, &rset);
883 if ((!blocked && txqueue.numitems) || txbuf.numitems)
884 FD_SET(sock, &wset);
885 if (inflight.numitems)
886 FD_SET(sock, &rset);
887 tv.tv_sec=5;
888 tv.tv_usec=0;
889 ret = select(1+((sock>logfd)?sock:logfd), &rset, &wset, NULL, &tv);
890 if (ret == 0) {
891 retval=-1;
892 snprintf(errstr, errstr_len, "Timeout reading from socket");
893 goto err_open;
894 } else if (ret<0) {
895 g_warning("Could not mmap temp file: %s", errstr);
896 retval=-1;
897 goto err;
899 /* We know we've got at least one thing to do here then */
901 /* Get a command from the transaction log */
902 if (FD_ISSET(logfd, &rset)) {
904 /* Read a request or reply from the transaction file */
905 READ_ALL_ERRCHK(logfd,
906 &magic,
907 sizeof(magic),
908 err_open,
909 "Could not read transaction log: %s",
910 strerror(errno));
911 magic = ntohl(magic);
912 switch (magic) {
913 case NBD_REQUEST_MAGIC:
914 if (NULL == (prc = calloc(1, sizeof(struct reqcontext)))) {
915 retval=-1;
916 snprintf(errstr, errstr_len, "Could not allocate request");
917 goto err_open;
919 READ_ALL_ERRCHK(logfd,
920 sizeof(magic)+(char *)&(prc->req),
921 sizeof(struct nbd_request)-sizeof(magic),
922 err_open,
923 "Could not read transaction log: %s",
924 strerror(errno));
925 prc->req.magic = htonl(NBD_REQUEST_MAGIC);
926 memcpy(prc->orighandle, prc->req.handle, 8);
927 prc->seq=seq++;
928 if ((ntohl(prc->req.type) & NBD_CMD_MASK_COMMAND) == NBD_CMD_DISC) {
929 /* no more to read; don't enqueue as no reply
930 * we will disconnect manually at the end
932 readtransactionfile = 0;
933 free (prc);
934 } else {
935 dumpcommand("Enqueuing command", prc->req.type);
936 rclist_addtail(&txqueue, prc);
938 prc = NULL;
939 break;
940 case NBD_REPLY_MAGIC:
941 READ_ALL_ERRCHK(logfd,
942 sizeof(magic)+(char *)(&rep),
943 sizeof(struct nbd_reply)-sizeof(magic),
944 err_open,
945 "Could not read transaction log: %s",
946 strerror(errno));
948 if (rep.error) {
949 retval=-1;
950 snprintf(errstr, errstr_len, "Transaction log file contained errored transaction");
951 goto err_open;
954 /* We do not need to consume data on a read reply as there is
955 * none in the log */
956 break;
957 default:
958 retval=-1;
959 snprintf(errstr, errstr_len, "Could not measure start time: %08x", magic);
960 goto err_open;
964 /* See if we have a write we can do */
965 if (FD_ISSET(sock, &wset))
967 if ((!(txqueue.head) && !(txbuf.head)) || blocked)
968 g_warning("Socket write FD set but we shouldn't have been interested");
970 /* If there is no buffered data, generate some */
971 if (!blocked && !(txbuf.head) && (NULL != (prc = txqueue.head)))
973 if (ntohl(prc->req.magic) != NBD_REQUEST_MAGIC) {
974 retval=-1;
975 g_warning("Asked to write a request without a magic number");
976 goto err_open;
979 command = ntohl(prc->req.type);
980 from = ntohll(prc->req.from);
981 len = ntohl(prc->req.len);
983 /* First check whether we can touch this command at all. If this
984 * command is a read, and there is an inflight write, OR if this
985 * command is a write, and there is an inflight read or write, then
986 * we need to leave the command alone and signal that we are blocked
989 if (!looseordering)
991 uint64_t cfrom;
992 uint32_t clen;
993 cfrom = from;
994 clen = len;
995 while (clen > 0) {
996 uint64_t blknum = cfrom>>9;
997 if (cfrom>=size) {
998 snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
999 (long long int) cfrom, (long long int)size);
1000 goto err_open;
1002 if (blkhash[blknum].inflightw ||
1003 (blkhash[blknum].inflightr &&
1004 ((command & NBD_CMD_MASK_COMMAND)==NBD_CMD_WRITE))) {
1005 blocked=1;
1006 break;
1008 cfrom += 512;
1009 clen -= 512;
1013 if (blocked)
1014 goto skipdequeue;
1016 rclist_unlink(&txqueue, prc);
1017 rclist_addtail(&inflight, prc);
1019 dumpcommand("Sending command", prc->req.type);
1020 /* we rewrite the handle as they otherwise may not be unique */
1021 *((uint64_t*)(prc->req.handle))=getrandomhandle(handlehash);
1022 g_hash_table_insert(handlehash, prc->req.handle, prc);
1023 addbuffer(&txbuf, &(prc->req), sizeof(struct nbd_request));
1024 switch (command & NBD_CMD_MASK_COMMAND) {
1025 case NBD_CMD_WRITE:
1026 xfer+=len;
1027 while (len > 0) {
1028 uint64_t blknum = from>>9;
1029 char dbuf[512];
1030 if (from>=size) {
1031 snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
1032 (long long int) from, (long long int)size);
1033 goto err_open;
1035 (blkhash[blknum].inflightw)++;
1036 /* work out what we should be writing */
1037 makebuf(dbuf, prc->seq, blknum);
1038 addbuffer(&txbuf, dbuf, 512);
1039 from += 512;
1040 len -= 512;
1042 break;
1043 case NBD_CMD_READ:
1044 xfer+=len;
1045 while (len > 0) {
1046 uint64_t blknum = from>>9;
1047 if (from>=size) {
1048 snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
1049 (long long int) from, (long long int)size);
1050 goto err_open;
1052 (blkhash[blknum].inflightr)++;
1053 from += 512;
1054 len -= 512;
1056 break;
1057 case NBD_CMD_DISC:
1058 case NBD_CMD_FLUSH:
1059 break;
1060 default:
1061 retval=-1;
1062 snprintf(errstr, errstr_len, "Incomprehensible command: %08x", command);
1063 goto err_open;
1064 break;
1067 prc = NULL;
1069 skipdequeue:
1071 /* there should be some now */
1072 if (writebuffer(sock, &txbuf)<0) {
1073 retval=-1;
1074 snprintf(errstr, errstr_len, "Failed to write to socket buffer: %s", strerror(errno));
1075 goto err_open;
1080 /* See if there is a reply to be processed from the socket */
1081 if(FD_ISSET(sock, &rset)) {
1082 /* Okay, there's something ready for
1083 * reading here */
1085 READ_ALL_ERRCHK(sock,
1086 &rep,
1087 sizeof(struct nbd_reply),
1088 err_open,
1089 "Could not read from server socket: %s",
1090 strerror(errno));
1092 if (rep.magic != htonl(NBD_REPLY_MAGIC)) {
1093 retval=-1;
1094 snprintf(errstr, errstr_len, "Bad magic from server");
1095 goto err_open;
1098 if (rep.error) {
1099 retval=-1;
1100 snprintf(errstr, errstr_len, "Server errored a transaction");
1101 goto err_open;
1104 uint64_t handle;
1105 memcpy(&handle,rep.handle,8);
1106 prc = g_hash_table_lookup(handlehash, &handle);
1107 if (!prc) {
1108 retval=-1;
1109 snprintf(errstr, errstr_len, "Unrecognised handle in reply: 0x%llX", *(long long unsigned int*)(rep.handle));
1110 goto err_open;
1112 if (!g_hash_table_remove(handlehash, &handle)) {
1113 retval=-1;
1114 snprintf(errstr, errstr_len, "Could not remove handle from hash: 0x%llX", *(long long unsigned int*)(rep.handle));
1115 goto err_open;
1118 if (prc->req.magic != htonl(NBD_REQUEST_MAGIC)) {
1119 retval=-1;
1120 snprintf(errstr, errstr_len, "Bad magic in inflight data: %08x", prc->req.magic);
1121 goto err_open;
1124 dumpcommand("Processing reply to command", prc->req.type);
1125 command = ntohl(prc->req.type);
1126 from = ntohll(prc->req.from);
1127 len = ntohl(prc->req.len);
1129 switch (command & NBD_CMD_MASK_COMMAND) {
1130 case NBD_CMD_READ:
1131 while (len > 0) {
1132 uint64_t blknum = from>>9;
1133 char dbuf[512];
1134 if (from>=size) {
1135 snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
1136 (long long int) from, (long long int)size);
1137 goto err_open;
1139 READ_ALL_ERRCHK(sock,
1140 dbuf,
1141 512,
1142 err_open,
1143 "Could not read data: %s",
1144 strerror(errno));
1145 if (--(blkhash[blknum].inflightr) <0 ) {
1146 snprintf(errstr, errstr_len, "Received a read reply for offset %llx when not in flight",
1147 (long long int) from);
1148 goto err_open;
1150 /* work out what we was written */
1151 if (checkbuf(dbuf, blkhash[blknum].seq, blknum)) {
1152 retval=-1;
1153 snprintf(errstr, errstr_len, "Bad reply data: I wanted blk %08x, seq %08x but I got (at a guess) blk %08x, seq %08x",
1154 (unsigned int) blknum,
1155 blkhash[blknum].seq,
1156 ((uint32_t *)(dbuf))[0],
1157 ((uint32_t *)(dbuf))[1]
1159 goto err_open;
1162 from += 512;
1163 len -= 512;
1165 break;
1166 case NBD_CMD_WRITE:
1167 /* subsequent reads should get data with this seq*/
1168 while (len > 0) {
1169 uint64_t blknum = from>>9;
1170 if (--(blkhash[blknum].inflightw) <0 ) {
1171 snprintf(errstr, errstr_len, "Received a write reply for offset %llx when not in flight",
1172 (long long int) from);
1173 goto err_open;
1175 blkhash[blknum].seq=(uint32_t)(prc->seq);
1176 from += 512;
1177 len -= 512;
1179 break;
1180 default:
1181 break;
1183 blocked = 0;
1184 processed++;
1185 rclist_unlink(&inflight, prc);
1186 prc->req.magic=0; /* so a duplicate reply is detected */
1187 free(prc);
1190 if (!(printer++ % 1000) || !(readtransactionfile || txqueue.numitems || inflight.numitems) )
1191 printf("%d: Seq %08lld Queued: %08d Inflight: %08d Done: %08lld\r",
1192 (int)mypid,
1193 (long long int) seq,
1194 txqueue.numitems,
1195 inflight.numitems,
1196 (long long int) processed);
1200 printf("\n");
1202 if (gettimeofday(&stop, NULL)<0) {
1203 retval=-1;
1204 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
1205 goto err_open;
1207 timespan=timeval_diff_to_double(&stop, &start);
1208 speed=xfer/timespan;
1209 if(speed>1024) {
1210 speed=speed/1024.0;
1211 speedchar[0]='K';
1213 if(speed>1024) {
1214 speed=speed/1024.0;
1215 speedchar[0]='M';
1217 if(speed>1024) {
1218 speed=speed/1024.0;
1219 speedchar[0]='G';
1221 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);
1223 err_open:
1224 if(close_sock) {
1225 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
1227 err:
1228 if (size && blkhash)
1229 munmap(blkhash, (size>>9)*sizeof(struct blkitem));
1231 if (blkhashfd != -1)
1232 close (blkhashfd);
1234 if (logfd != -1)
1235 close (logfd);
1237 if (blkhashname)
1238 free(blkhashname);
1240 if (*errstr)
1241 g_warning("%s",errstr);
1243 g_hash_table_destroy(handlehash);
1245 return retval;
1248 void handle_nonopt(char* opt, gchar** hostname, long int* p) {
1249 static int nonopt=0;
1251 switch(nonopt) {
1252 case 0:
1253 *hostname=g_strdup(opt);
1254 nonopt++;
1255 break;
1256 case 1:
1257 *p=(strtol(opt, NULL, 0));
1258 if(*p==LONG_MIN||*p==LONG_MAX) {
1259 g_critical("Could not parse port number: %s", strerror(errno));
1260 exit(EXIT_FAILURE);
1262 break;
1266 typedef int (*testfunc)(gchar*, int, char*, int, char, char, int);
1268 int main(int argc, char**argv) {
1269 gchar *hostname;
1270 long int p = 0;
1271 char* name = NULL;
1272 int sock=0;
1273 int c;
1274 int nonopt=0;
1275 int testflags=0;
1276 testfunc test = throughput_test;
1278 /* Ignore SIGPIPE as we want to pick up the error from write() */
1279 signal (SIGPIPE, SIG_IGN);
1281 if(argc<3) {
1282 g_message("%d: Not enough arguments", (int)getpid());
1283 g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
1284 g_message("%d: Or: %s <hostname> -N <exportname> [<port>]", (int)getpid(), argv[0]);
1285 exit(EXIT_FAILURE);
1287 logging();
1288 while((c=getopt(argc, argv, "-N:t:owfil"))>=0) {
1289 switch(c) {
1290 case 1:
1291 handle_nonopt(optarg, &hostname, &p);
1292 break;
1293 case 'N':
1294 name=g_strdup(optarg);
1295 if(!p) {
1296 p = 10809;
1298 break;
1299 case 't':
1300 transactionlog=g_strdup(optarg);
1301 break;
1302 case 'o':
1303 test=oversize_test;
1304 break;
1305 case 'l':
1306 looseordering=1;
1307 break;
1308 case 'w':
1309 testflags|=TEST_WRITE;
1310 break;
1311 case 'f':
1312 testflags|=TEST_FLUSH;
1313 break;
1314 case 'i':
1315 test=integrity_test;
1316 break;
1320 while(optind < argc) {
1321 handle_nonopt(argv[optind++], &hostname, &p);
1324 if(test(hostname, (int)p, name, sock, FALSE, TRUE, testflags)<0) {
1325 g_warning("Could not run test: %s", errstr);
1326 exit(EXIT_FAILURE);
1329 return 0;