fix documentation of NBD_CMD_FLUSH
[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 <syslog.h>
32 #include <unistd.h>
33 #include "config.h"
34 #include "lfs.h"
35 #define MY_NAME "nbd-tester-client"
36 #include "cliserv.h"
37
38 #include <netinet/in.h>
39 #include <glib.h>
40
41 static gchar errstr[1024];
42 const static int errstr_len=1024;
43
44 static uint64_t size;
45
46 typedef enum {
47         CONNECTION_TYPE_NONE,
48         CONNECTION_TYPE_CONNECT,
49         CONNECTION_TYPE_INIT_PASSWD,
50         CONNECTION_TYPE_CLISERV,
51         CONNECTION_TYPE_FULL,
52 } CONNECTION_TYPE;
53
54 typedef enum {
55         CONNECTION_CLOSE_PROPERLY,
56         CONNECTION_CLOSE_FAST,
57 } CLOSE_TYPE;
58
59 #define TEST_WRITE (1<<0)
60 #define TEST_FLUSH (1<<1)
61
62 int timeval_subtract (struct timeval *result, struct timeval *x,
63                       struct timeval *y) {
64         if (x->tv_usec < y->tv_usec) {
65                 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
66                 y->tv_usec -= 1000000 * nsec;
67                 y->tv_sec += nsec;
68         }
69         
70         if (x->tv_usec - y->tv_usec > 1000000) {
71                 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
72                 y->tv_usec += 1000000 * nsec;
73                 y->tv_sec -= nsec;
74         }
75         
76         result->tv_sec = x->tv_sec - y->tv_sec;
77         result->tv_usec = x->tv_usec - y->tv_usec;
78         
79         return x->tv_sec < y->tv_sec;
80 }
81
82 double timeval_diff_to_double (struct timeval * x, struct timeval * y) {
83         struct timeval r;
84         timeval_subtract(&r, x, y);
85         return r.tv_sec * 1.0 + r.tv_usec/1000000.0;
86 }
87
88 static inline int read_all(int f, void *buf, size_t len) {
89         ssize_t res;
90         size_t retval=0;
91
92         while(len>0) {
93                 if((res=read(f, buf, len)) <=0) {
94                         snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
95                         return -1;
96                 }
97                 len-=res;
98                 buf+=res;
99                 retval+=res;
100         }
101         return retval;
102 }
103
104 static inline int write_all(int f, void *buf, size_t len) {
105         ssize_t res;
106         size_t retval=0;
107
108         while(len>0) {
109                 if((res=write(f, buf, len)) <=0) {
110                         snprintf(errstr, errstr_len, "Write failed: %s", strerror(errno));
111                         return -1;
112                 }
113                 len-=res;
114                 buf+=res;
115                 retval+=res;
116         }
117         return retval;
118 }
119
120 #define READ_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; }
121 #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; }
122
123 #define WRITE_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; }
124 #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; }
125
126 int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype, int* serverflags) {
127         int sock;
128         struct hostent *host;
129         struct sockaddr_in addr;
130         char buf[256];
131         uint64_t mymagic = (name ? opts_magic : cliserv_magic);
132         u64 tmp64;
133         uint32_t tmp32 = 0;
134
135         sock=0;
136         if(ctype<CONNECTION_TYPE_CONNECT)
137                 goto end;
138         if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
139                 strncpy(errstr, strerror(errno), errstr_len);
140                 goto err;
141         }
142         setmysockopt(sock);
143         if(!(host=gethostbyname(hostname))) {
144                 strncpy(errstr, strerror(errno), errstr_len);
145                 goto err_open;
146         }
147         addr.sin_family=AF_INET;
148         addr.sin_port=htons(port);
149         addr.sin_addr.s_addr=*((int *) host->h_addr);
150         if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) {
151                 strncpy(errstr, strerror(errno), errstr_len);
152                 goto err_open;
153         }
154         if(ctype<CONNECTION_TYPE_INIT_PASSWD)
155                 goto end;
156         READ_ALL_ERRCHK(sock, buf, strlen(INIT_PASSWD), err_open, "Could not read INIT_PASSWD: %s", strerror(errno));
157         if(strlen(buf)==0) {
158                 snprintf(errstr, errstr_len, "Server closed connection");
159                 goto err_open;
160         }
161         if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
162                 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
163                 goto err_open;
164         }
165         if(ctype<CONNECTION_TYPE_CLISERV)
166                 goto end;
167         READ_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not read cliserv_magic: %s", strerror(errno));
168         tmp64=ntohll(tmp64);
169         if(tmp64 != mymagic) {
170                 strncpy(errstr, "mymagic does not match", errstr_len);
171                 goto err_open;
172         }
173         if(ctype<CONNECTION_TYPE_FULL)
174                 goto end;
175         if(!name) {
176                 READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
177                 size=ntohll(size);
178                 READ_ALL_ERRCHK(sock, buf, 128, err_open, "Could not read data: %s", strerror(errno));
179                 goto end;
180         }
181         /* flags */
182         READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read reserved field: %s", strerror(errno));
183         /* reserved field */
184         WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write reserved field: %s", strerror(errno));
185         /* magic */
186         tmp64 = htonll(opts_magic);
187         WRITE_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not write magic: %s", strerror(errno));
188         /* name */
189         tmp32 = htonl(NBD_OPT_EXPORT_NAME);
190         WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write option: %s", strerror(errno));
191         tmp32 = htonl((uint32_t)strlen(name));
192         WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write name length: %s", strerror(errno));
193         WRITE_ALL_ERRCHK(sock, name, strlen(name), err_open, "Could not write name:: %s", strerror(errno));
194         READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
195         size = ntohll(size);
196         uint16_t flags;
197         READ_ALL_ERRCHK(sock, &flags, sizeof(uint16_t), err_open, "Could not read flags: %s", strerror(errno));
198         flags = ntohs(flags);
199         *serverflags = flags;
200         g_warning("Server flags are: %08x", flags);
201         READ_ALL_ERRCHK(sock, buf, 124, err_open, "Could not read reserved zeroes: %s", strerror(errno));
202         goto end;
203 err_open:
204         close(sock);
205 err:
206         sock=-1;
207 end:
208         return sock;
209 }
210
211 int close_connection(int sock, CLOSE_TYPE type) {
212         struct nbd_request req;
213         u64 counter=0;
214
215         switch(type) {
216                 case CONNECTION_CLOSE_PROPERLY:
217                         req.magic=htonl(NBD_REQUEST_MAGIC);
218                         req.type=htonl(NBD_CMD_DISC);
219                         memcpy(&(req.handle), &(counter), sizeof(counter));
220                         counter++;
221                         req.from=0;
222                         req.len=0;
223                         if(write(sock, &req, sizeof(req))<0) {
224                                 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
225                                 return -1;
226                         }
227                 case CONNECTION_CLOSE_FAST:
228                         if(close(sock)<0) {
229                                 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
230                                 return -1;
231                         }
232                         break;
233                 default:
234                         g_critical("Your compiler is on crack!"); /* or I am buggy */
235                         return -1;
236         }
237         return 0;
238 }
239
240 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
241         struct nbd_reply rep;
242         int retval=0;
243         char buf[datasize];
244
245         READ_ALL_ERR_RT(sock, &rep, sizeof(rep), end, -1, "Could not read reply header: %s", strerror(errno));
246         rep.magic=ntohl(rep.magic);
247         rep.error=ntohl(rep.error);
248         if(rep.magic!=NBD_REPLY_MAGIC) {
249                 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);
250                 retval=-1;
251                 goto end;
252         }
253         if(rep.error) {
254                 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));
255                 retval=-1;
256                 goto end;
257         }
258         if (datasize)
259                 READ_ALL_ERR_RT(sock, &buf, datasize, end, -1, "Could not read data: %s", strerror(errno));
260
261 end:
262         return retval;
263 }
264
265 int oversize_test(gchar* hostname, int port, char* name, int sock,
266                   char sock_is_open, char close_sock, int testflags) {
267         int retval=0;
268         struct nbd_request req;
269         struct nbd_reply rep;
270         int request=0;
271         int i=0;
272         int serverflags = 0;
273         pid_t mypid = getpid();
274         char buf[((1024*1024)+sizeof(struct nbd_request)/2)<<1];
275         bool got_err;
276
277         /* This should work */
278         if(!sock_is_open) {
279                 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
280                         g_warning("Could not open socket: %s", errstr);
281                         retval=-1;
282                         goto err;
283                 }
284         }
285         req.magic=htonl(NBD_REQUEST_MAGIC);
286         req.type=htonl(NBD_CMD_READ);
287         req.len=htonl(1024*1024);
288         memcpy(&(req.handle),&i,sizeof(i));
289         req.from=htonll(i);
290         WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
291         printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
292         READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
293         READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
294         if(rep.error) {
295                 snprintf(errstr, errstr_len, "Received unexpected error: %d", rep.error);
296                 retval=-1;
297                 goto err;
298         } else {
299                 printf("OK\n");
300         }
301         /* This probably should not work */
302         i++; req.from=htonll(i);
303         req.len = htonl(ntohl(req.len) + sizeof(struct nbd_request) / 2);
304         WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
305         printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
306         READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
307         READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
308         if(rep.error) {
309                 printf("Received expected error\n");
310                 got_err=true;
311         } else {
312                 printf("OK\n");
313                 got_err=false;
314         }
315         /* ... unless this works, too */
316         i++; req.from=htonll(i);
317         req.len = htonl(ntohl(req.len) << 1);
318         WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
319         printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
320         READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
321         READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
322         if(rep.error) {
323                 printf("error\n");
324         } else {
325                 printf("OK\n");
326         }
327         if((rep.error && !got_err) || (!rep.error && got_err)) {
328                 printf("Received unexpected error\n");
329                 retval=-1;
330         }
331   err:
332         return retval;
333 }
334
335 int throughput_test(gchar* hostname, int port, char* name, int sock,
336                     char sock_is_open, char close_sock, int testflags) {
337         long long int i;
338         char buf[1024];
339         char writebuf[1024];
340         struct nbd_request req;
341         int requests=0;
342         fd_set set;
343         struct timeval tv;
344         struct timeval start;
345         struct timeval stop;
346         double timespan;
347         double speed;
348         char speedchar[2] = { '\0', '\0' };
349         int retval=0;
350         int serverflags = 0;
351         size_t tmp;
352         signed int do_write=TRUE;
353         pid_t mypid = getpid();
354
355
356         if (!(testflags & TEST_WRITE))
357                 testflags &= ~TEST_FLUSH;
358
359         memset (writebuf, 'X', 1024);
360         size=0;
361         if(!sock_is_open) {
362                 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
363                         g_warning("Could not open socket: %s", errstr);
364                         retval=-1;
365                         goto err;
366                 }
367         }
368         if ((testflags & TEST_FLUSH) && ((serverflags & (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))
369                                          != (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))) {
370                 snprintf(errstr, errstr_len, "Server did not supply flush capability flags");
371                 retval = -1;
372                 goto err_open;
373         }
374         req.magic=htonl(NBD_REQUEST_MAGIC);
375         req.len=htonl(1024);
376         if(gettimeofday(&start, NULL)<0) {
377                 retval=-1;
378                 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
379                 goto err_open;
380         }
381         for(i=0;i+1024<=size;i+=1024) {
382                 if(do_write) {
383                         int sendfua = (testflags & TEST_FLUSH) && (((i>>10) & 15) == 3);
384                         int sendflush = (testflags & TEST_FLUSH) && (((i>>10) & 15) == 11);
385                         req.type=htonl((testflags & TEST_WRITE)?NBD_CMD_WRITE:NBD_CMD_READ);
386                         if (sendfua)
387                                 req.type = htonl(NBD_CMD_WRITE | NBD_CMD_FLAG_FUA);
388                         memcpy(&(req.handle),&i,sizeof(i));
389                         req.from=htonll(i);
390                         if (write_all(sock, &req, sizeof(req)) <0) {
391                                 retval=-1;
392                                 goto err_open;
393                         }
394                         if (testflags & TEST_WRITE) {
395                                 if (write_all(sock, writebuf, 1024) <0) {
396                                         retval=-1;
397                                         goto err_open;
398                                 }
399                         }
400                         printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
401                         if (sendflush) {
402                                 long long int j = i ^ (1LL<<63);
403                                 req.type = htonl(NBD_CMD_FLUSH);
404                                 memcpy(&(req.handle),&j,sizeof(j));
405                                 req.from=0;
406                                 if (write_all(sock, &req, sizeof(req)) <0) {
407                                         retval=-1;
408                                         goto err_open;
409                                 }
410                                 printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
411                         }
412                 }
413                 do {
414                         FD_ZERO(&set);
415                         FD_SET(sock, &set);
416                         tv.tv_sec=0;
417                         tv.tv_usec=0;
418                         select(sock+1, &set, NULL, NULL, &tv);
419                         if(FD_ISSET(sock, &set)) {
420                                 /* Okay, there's something ready for
421                                  * reading here */
422                                 if(read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i)<0) {
423                                         retval=-1;
424                                         goto err_open;
425                                 }
426                                 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
427                         }
428                 } while FD_ISSET(sock, &set);
429                 /* Now wait until we can write again or until a second have
430                  * passed, whichever comes first*/
431                 FD_ZERO(&set);
432                 FD_SET(sock, &set);
433                 tv.tv_sec=1;
434                 tv.tv_usec=0;
435                 do_write=select(sock+1,NULL,&set,NULL,&tv);
436                 if(!do_write) printf("Select finished\n");
437                 if(do_write<0) {
438                         snprintf(errstr, errstr_len, "select: %s", strerror(errno));
439                         retval=-1;
440                         goto err_open;
441                 }
442         }
443         /* Now empty the read buffer */
444         do {
445                 FD_ZERO(&set);
446                 FD_SET(sock, &set);
447                 tv.tv_sec=0;
448                 tv.tv_usec=0;
449                 select(sock+1, &set, NULL, NULL, &tv);
450                 if(FD_ISSET(sock, &set)) {
451                         /* Okay, there's something ready for
452                          * reading here */
453                         read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i);
454                         printf("%d: Requests(-): %d\n", (int)mypid, --requests);
455                 }
456         } while (requests);
457         if(gettimeofday(&stop, NULL)<0) {
458                 retval=-1;
459                 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
460                 goto err_open;
461         }
462         timespan=timeval_diff_to_double(&stop, &start);
463         speed=size/timespan;
464         if(speed>1024) {
465                 speed=speed/1024.0;
466                 speedchar[0]='K';
467         }
468         if(speed>1024) {
469                 speed=speed/1024.0;
470                 speedchar[0]='M';
471         }
472         if(speed>1024) {
473                 speed=speed/1024.0;
474                 speedchar[0]='G';
475         }
476         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);
477
478 err_open:
479         if(close_sock) {
480                 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
481         }
482 err:
483         return retval;
484 }
485
486 typedef int (*testfunc)(gchar*, int, char*, int, char, char, int);
487
488 int main(int argc, char**argv) {
489         gchar *hostname;
490         long int p = 0;
491         char* name = NULL;
492         int sock=0;
493         int c;
494         bool want_port = TRUE;
495         int nonopt=0;
496         int testflags=0;
497         testfunc test = throughput_test;
498
499         if(argc<3) {
500                 g_message("%d: Not enough arguments", (int)getpid());
501                 g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
502                 g_message("%d: Or: %s <hostname> -N <exportname>", (int)getpid(), argv[0]);
503                 exit(EXIT_FAILURE);
504         }
505         logging();
506         while((c=getopt(argc, argv, "-N:owf"))>=0) {
507                 switch(c) {
508                         case 1:
509                                 switch(nonopt) {
510                                         case 0:
511                                                 hostname=g_strdup(optarg);
512                                                 nonopt++;
513                                                 break;
514                                         case 1:
515                                                 if(want_port)
516                                                 p=(strtol(argv[2], NULL, 0));
517                                                 if(p==LONG_MIN||p==LONG_MAX) {
518                                                         g_critical("Could not parse port number: %s", strerror(errno));
519                                                         exit(EXIT_FAILURE);
520                                                 }
521                                                 break;
522                                 }
523                                 break;
524                         case 'N':
525                                 name=g_strdup(optarg);
526                                 p = 10809;
527                                 want_port = false;
528                                 break;
529                         case 'o':
530                                 test=oversize_test;
531                                 break;
532                         case 'w':
533                                 testflags|=TEST_WRITE;
534                                 break;
535                         case 'f':
536                                 testflags|=TEST_FLUSH;
537                                 break;
538                 }
539         }
540
541         if(test(hostname, (int)p, name, sock, FALSE, TRUE, testflags)<0) {
542                 g_warning("Could not run test: %s", errstr);
543                 exit(EXIT_FAILURE);
544         }
545
546         return 0;
547 }