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