Release 2.9.23
[nbd.git] / nbd-tester-client.c
1 /*
2  * Test client to test the NBD server. Doesn't do anything useful, except
3  * checking that the server does, actually, work.
4  *
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.
8  *
9  * Copyright(c) 2006  Wouter Verhelst
10  *
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.
14  *
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.
19  *
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
23  */
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>
40
41 #define MY_NAME "nbd-tester-client"
42 #include "cliserv.h"
43
44 static gchar errstr[1024];
45 const static int errstr_len=1024;
46
47 static uint64_t size;
48
49 static int looseordering = 0;
50
51 static gchar * transactionlog = "nbd-tester-client.tr";
52
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;
60
61 typedef enum {
62         CONNECTION_CLOSE_PROPERLY,
63         CONNECTION_CLOSE_FAST,
64 } CLOSE_TYPE;
65
66 struct reqcontext {
67         uint64_t seq;
68         char orighandle[8];
69         struct nbd_request req;
70         struct reqcontext * next;
71         struct reqcontext * prev;
72 };
73
74 struct rclist {
75         struct reqcontext * head;
76         struct reqcontext * tail;
77         int numitems;
78 };
79
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;
88 };
89
90 struct chunklist {
91         struct chunk * head;
92         struct chunk * tail;
93         int numitems;
94 };
95
96 struct blkitem {
97         uint32_t seq;
98         int32_t inflightr;
99         int32_t inflightw;
100 };
101
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;
106                 
107                 /* Fix link to previous */
108                 if (prev)
109                         prev->next = next;
110                 else
111                         l->head = next;
112                 
113                 if (next)
114                         next->prev = prev;
115                 else
116                         l->tail = prev;
117
118                 p->prev = NULL;
119                 p->next = NULL;
120                 l->numitems--;
121         }                                                       
122 }                                                                       
123
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;
142         }
143         l->numitems++;
144 }
145
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;
150                 
151                 /* Fix link to previous */
152                 if (prev)
153                         prev->next = next;
154                 else
155                         l->head = next;
156                 
157                 if (next)
158                         next->prev = prev;
159                 else
160                         l->tail = prev;
161
162                 p->prev = NULL;
163                 p->next = NULL;
164                 l->numitems--;
165         }                                                       
166 }                                                                       
167
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;
186         }
187         l->numitems++;
188 }
189
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;
195
196         while (len>0)
197         {
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;
209                 }
210
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);
216                         }
217
218                         pchunk->buffer = buf;
219                         pchunk->readptr = buf;
220                         pchunk->writeptr = buf;
221                         pchunk->space = size;
222                         chunklist_addtail(l, pchunk);
223                 }
224         }
225
226 }
227
228 /* returns 0 on success, -1 on failure */
229 int writebuffer(int fd, struct chunklist * l) {
230
231         struct chunk * pchunk = NULL;
232         int res;
233         if (!l)
234                 return 0;
235
236         while (!pchunk)
237         {
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;
246                 }
247         }
248         
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);
261         }
262         return 0;
263 }
264
265
266
267 #define TEST_WRITE (1<<0)
268 #define TEST_FLUSH (1<<1)
269
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;
276         }
277         
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;
282         }
283         
284         result->tv_sec = x->tv_sec - y->tv_sec;
285         result->tv_usec = x->tv_usec - y->tv_usec;
286         
287         return x->tv_sec < y->tv_sec;
288 }
289
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;
294 }
295
296 static inline int read_all(int f, void *buf, size_t len) {
297         ssize_t res;
298         size_t retval=0;
299
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;
306                 }
307                 len-=res;
308                 buf+=res;
309                 retval+=res;
310         }
311         return retval;
312 }
313
314 static inline int write_all(int f, void *buf, size_t len) {
315         ssize_t res;
316         size_t retval=0;
317
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;
324                 }
325                 len-=res;
326                 buf+=res;
327                 retval+=res;
328         }
329         return retval;
330 }
331
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; }
334
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; }
337
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;
346
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;
353         }
354         setmysockopt(sock);
355         if(!(host=gethostbyname(hostname))) {
356                 strncpy(errstr, strerror(errno), errstr_len);
357                 goto err_open;
358         }
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;
365         }
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;
372         }
373         if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
374                 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
375                 goto err_open;
376         }
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;
384         }
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;
392         }
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;
420 }
421
422 int close_connection(int sock, CLOSE_TYPE type) {
423         struct nbd_request req;
424         u64 counter=0;
425
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;
437                         }
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;
442                         }
443                         break;
444                 default:
445                         g_critical("Your compiler is on crack!"); /* or I am buggy */
446                         return -1;
447         }
448         return 0;
449 }
450
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];
455
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;
463         }
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;
468         }
469         if (datasize)
470                 READ_ALL_ERR_RT(sock, &buf, datasize, end, -1, "Could not read data: %s", strerror(errno));
471
472 end:
473         return retval;
474 }
475
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;
486
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;
493                 }
494         }
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");
510         }
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;
524         }
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");
536         }
537         if((rep.error && !got_err) || (!rep.error && got_err)) {
538                 printf("Received unexpected error\n");
539                 retval=-1;
540         }
541   err:
542         return retval;
543 }
544
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();
562
563
564         if (!(testflags & TEST_WRITE))
565                 testflags &= ~TEST_FLUSH;
566
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;
574                 }
575         }
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;
581         }
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;
588         }
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;
601                         }
602                         if (testflags & TEST_WRITE) {
603                                 if (write_all(sock, writebuf, 1024) <0) {
604                                         retval=-1;
605                                         goto err_open;
606                                 }
607                         }
608                         printf("%d: Requests(+): %d\n", (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;
617                                 }
618                                 printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
619                         }
620                 }
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;
633                                 }
634                                 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
635                         }
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;
649                 }
650         }
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\n", (int)mypid, --requests);
663                 }
664         } while (requests);
665         if(gettimeofday(&stop, NULL)<0) {
666                 retval=-1;
667                 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
668                 goto err_open;
669         }
670         timespan=timeval_diff_to_double(&stop, &start);
671         speed=size/timespan;
672         if(speed>1024) {
673                 speed=speed/1024.0;
674                 speedchar[0]='K';
675         }
676         if(speed>1024) {
677                 speed=speed/1024.0;
678                 speedchar[0]='M';
679         }
680         if(speed>1024) {
681                 speed=speed/1024.0;
682                 speedchar[0]='G';
683         }
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);
685
686 err_open:
687         if(close_sock) {
688                 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
689         }
690 err:
691         return retval;
692 }
693
694 /*
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.
698  */
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;
702         int i;
703         if (!seq) {
704                 bzero(buf, 512);
705                 return;
706         }
707         for (i = 0; i<512/sizeof(uint64_t); i++) {
708                 int s;
709                 *(p++) = x;
710                 x+=0xFEEDA1ECDEADBEEFULL+i+(((uint64_t)i)<<56);
711                 s = x & 63;
712                 x = x ^ (x<<s) ^ (x>>(64-s)) ^ 0xAA55AA55AA55AA55ULL ^ seq;
713         }
714 }
715                 
716 static inline int checkbuf(char *buf, uint64_t seq, uint64_t blknum) {
717         char cmp[512];
718         makebuf(cmp, seq, blknum);
719         return memcmp(cmp, buf, 512)?-1:0;
720 }
721
722 static inline void dumpcommand(char * text, uint32_t command)
723 {
724 #ifdef DEBUG_COMMANDS
725         command=ntohl(command);
726         char * ctext;
727         switch (command & NBD_CMD_MASK_COMMAND) {
728         case NBD_CMD_READ:
729                 ctext="NBD_CMD_READ";
730                 break;
731         case NBD_CMD_WRITE:
732                 ctext="NBD_CMD_WRITE";
733                 break;
734         case NBD_CMD_DISC:
735                 ctext="NBD_CMD_DISC";
736                 break;
737         case NBD_CMD_FLUSH:
738                 ctext="NBD_CMD_FLUSH";
739                 break;
740         default:
741                 ctext="UNKNOWN";
742                 break;
743         }
744         printf("%s: %s [%s] (0x%08x)\n",
745                text,
746                ctext,
747                (command & NBD_CMD_FLAG_FUA)?"FUA":"NONE",
748                command);
749 #endif
750 }
751
752 /* return an unused handle */
753 uint64_t getrandomhandle(GHashTable *phash) {
754         uint64_t handle = 0;
755         int i;
756         do {
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));
761         return handle;
762 }
763
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;
767         fd_set rset;
768         fd_set wset;
769         struct timeval tv;
770         struct timeval start;
771         struct timeval stop;
772         double timespan;
773         double speed;
774         char speedchar[2] = { '\0', '\0' };
775         int retval=0;
776         int serverflags = 0;
777         pid_t G_GNUC_UNUSED mypid = getpid();
778         int blkhashfd = -1;
779         char *blkhashname=NULL;
780         struct blkitem *blkhash = NULL;
781         int logfd=-1;
782         uint64_t seq=1;
783         uint64_t processed=0;
784         uint64_t printer=0;
785         uint64_t xfer=0;
786         int readtransactionfile = 1;
787         int blocked = 0;
788         struct rclist txqueue={NULL, NULL, 0};
789         struct rclist inflight={NULL, NULL, 0};
790         struct chunklist txbuf={NULL, NULL, 0};
791
792         GHashTable *handlehash = g_hash_table_new(g_int64_hash, g_int64_equal);
793
794         size=0;
795         if(!sock_is_open) {
796                 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
797                         g_warning("Could not open socket: %s", errstr);
798                         retval=-1;
799                         goto err;
800                 }
801         }
802
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");
806
807 #ifdef HAVE_MKSTEMP
808         blkhashname=strdup("/tmp/blkarray-XXXXXX");
809         if (!blkhashname || (-1 == (blkhashfd = mkstemp(blkhashname)))) {
810                 g_warning("Could not open temp file: %s", strerror(errno));
811                 retval=-1;
812                 goto err;
813         }
814 #else
815         /* use tmpnam here to avoid further feature test nightmare */
816         if (-1 == (blkhashfd = open(blkhashname=strdup(tmpnam(NULL)),
817                                     O_CREAT | O_RDWR,
818                                     S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))) {
819                 g_warning("Could not open temp file: %s", strerror(errno));
820                 retval=-1;
821                 goto err;
822         }
823 #endif
824         /* Ensure space freed if we die */
825         if (-1 == unlink(blkhashname)) {
826                 g_warning("Could not unlink temp file: %s", strerror(errno));
827                 retval=-1;
828                 goto err;
829         }
830
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));
833                 retval=-1;
834                 goto err;
835         }
836
837         if (-1 == write(blkhashfd, "\0", 1)) {
838                 g_warning("Could not write temp file: %s", strerror(errno));
839                 retval=-1;
840                 goto err;
841         }
842
843         if (NULL == (blkhash = mmap(NULL,
844                                     (size>>9)*sizeof(struct blkitem),
845                                     PROT_READ | PROT_WRITE,
846                                     MAP_SHARED,
847                                     blkhashfd,
848                                     0))) {
849                 g_warning("Could not mmap temp file: %s", strerror(errno));
850                 retval=-1;
851                 goto err;
852         }
853
854         if (-1 == (logfd = open(transactionlog, O_RDONLY)))
855         {
856                 g_warning("Could open log file: %s", strerror(errno));
857                 retval=-1;
858                 goto err;
859         }
860                 
861         if(gettimeofday(&start, NULL)<0) {
862                 retval=-1;
863                 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
864                 goto err_open;
865         }
866
867         while (readtransactionfile || txqueue.numitems || txbuf.numitems || inflight.numitems) {
868                 int ret;
869
870                 uint32_t magic;
871                 uint32_t command;
872                 uint64_t from;
873                 uint32_t len;
874                 struct reqcontext * prc;
875
876                 *errstr=0;
877
878                 FD_ZERO(&wset);
879                 FD_ZERO(&rset);
880                 if (readtransactionfile)
881                         FD_SET(logfd, &rset);
882                 if ((!blocked && txqueue.numitems) || txbuf.numitems)
883                         FD_SET(sock, &wset);
884                 if (inflight.numitems)
885                         FD_SET(sock, &rset);
886                 tv.tv_sec=5;
887                 tv.tv_usec=0;
888                 ret = select(1+((sock>logfd)?sock:logfd), &rset, &wset, NULL, &tv);
889                 if (ret == 0) {
890                         retval=-1;
891                         snprintf(errstr, errstr_len, "Timeout reading from socket");
892                         goto err_open;
893                 } else if (ret<0) {
894                         g_warning("Could not mmap temp file: %s", errstr);
895                         retval=-1;
896                         goto err;
897                 }
898                 /* We know we've got at least one thing to do here then */
899
900                 /* Get a command from the transaction log */
901                 if (FD_ISSET(logfd, &rset)) {
902                         
903                         /* Read a request or reply from the transaction file */
904                         READ_ALL_ERRCHK(logfd,
905                                         &magic,
906                                         sizeof(magic),
907                                         err_open,
908                                         "Could not read transaction log: %s",
909                                         strerror(errno));
910                         magic = ntohl(magic);
911                         switch (magic) {
912                         case NBD_REQUEST_MAGIC:
913                                 if (NULL == (prc = calloc(1, sizeof(struct reqcontext)))) {
914                                         retval=-1;
915                                         snprintf(errstr, errstr_len, "Could not allocate request");
916                                         goto err_open;
917                                 }
918                                 READ_ALL_ERRCHK(logfd,
919                                                 sizeof(magic)+(char *)&(prc->req),
920                                                 sizeof(struct nbd_request)-sizeof(magic),
921                                                 err_open,
922                                                 "Could not read transaction log: %s",
923                                                 strerror(errno));
924                                 prc->req.magic = htonl(NBD_REQUEST_MAGIC);
925                                 memcpy(prc->orighandle, prc->req.handle, 8);
926                                 prc->seq=seq++;
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
930                                          */
931                                         readtransactionfile = 0;
932                                         free (prc);
933                                 } else {
934                                         dumpcommand("Enqueuing command", prc->req.type);
935                                         rclist_addtail(&txqueue, prc);
936                                 }
937                                 prc = NULL;
938                                 break;
939                         case NBD_REPLY_MAGIC:
940                                 READ_ALL_ERRCHK(logfd,
941                                                 sizeof(magic)+(char *)(&rep),
942                                                 sizeof(struct nbd_reply)-sizeof(magic),
943                                                 err_open,
944                                                 "Could not read transaction log: %s",
945                                                 strerror(errno));
946
947                                 if (rep.error) {
948                                         retval=-1;
949                                         snprintf(errstr, errstr_len, "Transaction log file contained errored transaction");
950                                         goto err_open;
951                                 }
952                                         
953                                 /* We do not need to consume data on a read reply as there is
954                                  * none in the log */
955                                 break;
956                         default:
957                                 retval=-1;
958                                 snprintf(errstr, errstr_len, "Could not measure start time: %08x", magic);
959                                 goto err_open;
960                         }
961                 }
962
963                 /* See if we have a write we can do */
964                 if (FD_ISSET(sock, &wset))
965                 {
966                         if ((!(txqueue.head) && !(txbuf.head)) || blocked)
967                                 g_warning("Socket write FD set but we shouldn't have been interested");
968
969                         /* If there is no buffered data, generate some */
970                         if (!blocked && !(txbuf.head) && (NULL != (prc = txqueue.head)))
971                         {
972                                 if (ntohl(prc->req.magic) != NBD_REQUEST_MAGIC) {
973                                         retval=-1;
974                                         g_warning("Asked to write a request without a magic number");
975                                         goto err_open;
976                                 }
977                                         
978                                 command = ntohl(prc->req.type);
979                                 from = ntohll(prc->req.from);
980                                 len = ntohl(prc->req.len);
981
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
986                                  */
987                                 
988                                 if (!looseordering)
989                                 {
990                                         uint64_t cfrom;
991                                         uint32_t clen;
992                                         cfrom = from;
993                                         clen = len;
994                                         while (clen > 0) {
995                                                 uint64_t blknum = cfrom>>9;
996                                                 if (cfrom>=size) {
997                                                         snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
998                                                                  (long long int) cfrom, (long long int)size);
999                                                         goto err_open;
1000                                                 }
1001                                                 if (blkhash[blknum].inflightw ||
1002                                                     (blkhash[blknum].inflightr &&
1003                                                      ((command & NBD_CMD_MASK_COMMAND)==NBD_CMD_WRITE))) {
1004                                                         blocked=1;
1005                                                         break;
1006                                                 }
1007                                                 cfrom += 512;
1008                                                 clen -= 512;
1009                                         }
1010                                 }
1011
1012                                 if (blocked)
1013                                         goto skipdequeue;
1014
1015                                 rclist_unlink(&txqueue, prc);
1016                                 rclist_addtail(&inflight, prc);
1017                                 
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) {
1024                                 case NBD_CMD_WRITE:
1025                                         xfer+=len;
1026                                         while (len > 0) {
1027                                                 uint64_t blknum = from>>9;
1028                                                 char dbuf[512];
1029                                                 if (from>=size) {
1030                                                         snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
1031                                                                  (long long int) from, (long long int)size);
1032                                                         goto err_open;
1033                                                 }
1034                                                 (blkhash[blknum].inflightw)++;
1035                                                 /* work out what we should be writing */
1036                                                 makebuf(dbuf, prc->seq, blknum);
1037                                                 addbuffer(&txbuf, dbuf, 512);
1038                                                 from += 512;
1039                                                 len -= 512;
1040                                         }
1041                                         break;
1042                                 case NBD_CMD_READ:
1043                                         xfer+=len;
1044                                         while (len > 0) {
1045                                                 uint64_t blknum = from>>9;
1046                                                 if (from>=size) {
1047                                                         snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
1048                                                                  (long long int) from, (long long int)size);
1049                                                         goto err_open;
1050                                                 }
1051                                                 (blkhash[blknum].inflightr)++;
1052                                                 from += 512;
1053                                                 len -= 512;
1054                                         }
1055                                         break;
1056                                 case NBD_CMD_DISC:
1057                                 case NBD_CMD_FLUSH:
1058                                         break;
1059                                 default:
1060                                         retval=-1;
1061                                         snprintf(errstr, errstr_len, "Incomprehensible command: %08x", command);
1062                                         goto err_open;
1063                                         break;
1064                                 }
1065                                 
1066                                 prc = NULL;
1067                         }
1068                 skipdequeue:
1069
1070                         /* there should be some now */
1071                         if (writebuffer(sock, &txbuf)<0) {
1072                                 retval=-1;
1073                                 snprintf(errstr, errstr_len, "Failed to write to socket buffer: %s", strerror(errno));
1074                                 goto err_open;
1075                         }
1076                         
1077                 }
1078
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
1082                          * reading here */
1083                         
1084                         READ_ALL_ERRCHK(sock,
1085                                         &rep,
1086                                         sizeof(struct nbd_reply),
1087                                         err_open,
1088                                         "Could not read from server socket: %s",
1089                                         strerror(errno));
1090                         
1091                         if (rep.magic != htonl(NBD_REPLY_MAGIC)) {
1092                                 retval=-1;
1093                                 snprintf(errstr, errstr_len, "Bad magic from server");
1094                                 goto err_open;
1095                         }
1096                         
1097                         if (rep.error) {
1098                                 retval=-1;
1099                                 snprintf(errstr, errstr_len, "Server errored a transaction");
1100                                 goto err_open;
1101                         }
1102                                 
1103                         prc = g_hash_table_lookup(handlehash, rep.handle);
1104                         if (!prc) {
1105                                 retval=-1;
1106                                 snprintf(errstr, errstr_len, "Unrecognised handle in reply: 0x%llX", *(long long unsigned int*)(rep.handle));
1107                                 goto err_open;
1108                         }
1109                         if (!g_hash_table_remove(handlehash, rep.handle)) {
1110                                 retval=-1;
1111                                 snprintf(errstr, errstr_len, "Could not remove handle from hash: 0x%llX", *(long long unsigned int*)(rep.handle));
1112                                 goto err_open;
1113                         }
1114
1115                         if (prc->req.magic != htonl(NBD_REQUEST_MAGIC)) {
1116                                 retval=-1;
1117                                 snprintf(errstr, errstr_len, "Bad magic in inflight data: %08x", prc->req.magic);
1118                                 goto err_open;
1119                         }
1120                         
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);
1125                         
1126                         switch (command & NBD_CMD_MASK_COMMAND) {
1127                         case NBD_CMD_READ:
1128                                 while (len > 0) {
1129                                         uint64_t blknum = from>>9;
1130                                         char dbuf[512];
1131                                         if (from>=size) {
1132                                                 snprintf(errstr, errstr_len, "offset %llx beyond size %llx",
1133                                                          (long long int) from, (long long int)size);
1134                                                 goto err_open;
1135                                         }
1136                                         READ_ALL_ERRCHK(sock,
1137                                                         dbuf,
1138                                                         512,
1139                                                         err_open,
1140                                                         "Could not read data: %s",
1141                                                         strerror(errno));
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);
1145                                                 goto err_open;
1146                                         }
1147                                         /* work out what we was written */
1148                                         if (checkbuf(dbuf, blkhash[blknum].seq, blknum)) {
1149                                                 retval=-1;
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]
1155                                                          );
1156                                                 goto err_open;
1157                                                 
1158                                         }
1159                                         from += 512;
1160                                         len -= 512;
1161                                 }
1162                                 break;
1163                         case NBD_CMD_WRITE:
1164                                 /* subsequent reads should get data with this seq*/
1165                                 while (len > 0) {
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);
1170                                                 goto err_open;
1171                                         }
1172                                         blkhash[blknum].seq=(uint32_t)(prc->seq);
1173                                         from += 512;
1174                                         len -= 512;
1175                                 }
1176                                 break;
1177                         default:
1178                                 break;
1179                         }
1180                         blocked = 0;
1181                         processed++;
1182                         rclist_unlink(&inflight, prc);
1183                         prc->req.magic=0; /* so a duplicate reply is detected */
1184                         free(prc);
1185                 }
1186
1187                 if (!(printer++ % 10000) || !(readtransactionfile || txqueue.numitems || inflight.numitems) )
1188                         printf("%d: Seq %08lld Queued: %08d Inflight: %08d Done: %08lld\n",
1189                                (int)mypid,
1190                                (long long int) seq,
1191                                txqueue.numitems,
1192                                inflight.numitems,
1193                                (long long int) processed);
1194
1195         }
1196
1197         if (gettimeofday(&stop, NULL)<0) {
1198                 retval=-1;
1199                 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
1200                 goto err_open;
1201         }
1202         timespan=timeval_diff_to_double(&stop, &start);
1203         speed=xfer/timespan;
1204         if(speed>1024) {
1205                 speed=speed/1024.0;
1206                 speedchar[0]='K';
1207         }
1208         if(speed>1024) {
1209                 speed=speed/1024.0;
1210                 speedchar[0]='M';
1211         }
1212         if(speed>1024) {
1213                 speed=speed/1024.0;
1214                 speedchar[0]='G';
1215         }
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);
1217
1218 err_open:
1219         if(close_sock) {
1220                 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
1221         }
1222 err:
1223         if (size && blkhash)
1224                 munmap(blkhash, (size>>9)*sizeof(struct blkitem));
1225
1226         if (blkhashfd != -1)
1227                 close (blkhashfd);
1228
1229         if (logfd != -1)
1230                 close (logfd);
1231
1232         if (blkhashname)
1233                 free(blkhashname);
1234
1235         if (*errstr)
1236                 g_warning("%s",errstr);
1237
1238         g_hash_table_destroy(handlehash);
1239
1240         return retval;
1241 }
1242
1243 typedef int (*testfunc)(gchar*, int, char*, int, char, char, int);
1244
1245 int main(int argc, char**argv) {
1246         gchar *hostname;
1247         long int p = 0;
1248         char* name = NULL;
1249         int sock=0;
1250         int c;
1251         bool want_port = TRUE;
1252         int nonopt=0;
1253         int testflags=0;
1254         testfunc test = throughput_test;
1255
1256         /* Ignore SIGPIPE as we want to pick up the error from write() */
1257         signal (SIGPIPE, SIG_IGN);
1258
1259         if(argc<3) {
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]);
1263                 exit(EXIT_FAILURE);
1264         }
1265         logging();
1266         while((c=getopt(argc, argv, "-N:t:owfil"))>=0) {
1267                 switch(c) {
1268                         case 1:
1269                                 switch(nonopt) {
1270                                         case 0:
1271                                                 hostname=g_strdup(optarg);
1272                                                 nonopt++;
1273                                                 break;
1274                                         case 1:
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));
1278                                                         exit(EXIT_FAILURE);
1279                                                 }
1280                                                 break;
1281                                 }
1282                                 break;
1283                         case 'N':
1284                                 name=g_strdup(optarg);
1285                                 if(!p) {
1286                                         p = 10809;
1287                                 }
1288                                 want_port = false;
1289                                 break;
1290                         case 't':
1291                                 transactionlog=g_strdup(optarg);
1292                                 break;
1293                         case 'o':
1294                                 test=oversize_test;
1295                                 break;
1296                         case 'l':
1297                                 looseordering=1;
1298                                 break;
1299                         case 'w':
1300                                 testflags|=TEST_WRITE;
1301                                 break;
1302                         case 'f':
1303                                 testflags|=TEST_FLUSH;
1304                                 break;
1305                         case 'i':
1306                                 test=integrity_test;
1307                                 break;
1308                 }
1309         }
1310
1311         if(test(hostname, (int)p, name, sock, FALSE, TRUE, testflags)<0) {
1312                 g_warning("Could not run test: %s", errstr);
1313                 exit(EXIT_FAILURE);
1314         }
1315
1316         return 0;
1317 }