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