8572f298593c12a8106565c26f3efc414d306667
[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 <string.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <syslog.h>
31 #include <unistd.h>
32 #include "config.h"
33 #include "lfs.h"
34 #define MY_NAME "nbd-tester-client"
35 #include "cliserv.h"
36
37 #include <netinet/in.h>
38 #include <glib.h>
39
40 static gchar errstr[1024];
41 const static int errstr_len=1024;
42
43 typedef enum {
44         CONNECTION_TYPE_NONE,
45         CONNECTION_TYPE_CONNECT,
46         CONNECTION_TYPE_INIT_PASSWD,
47         CONNECTION_TYPE_CLISERV,
48         CONNECTION_TYPE_FULL,
49 } CONNECTION_TYPE;
50
51 typedef enum {
52         CONNECTION_CLOSE_PROPERLY,
53         CONNECTION_CLOSE_FAST,
54 } CLOSE_TYPE;
55
56 inline int read_all(int f, void *buf, size_t len) {
57         ssize_t res;
58         size_t retval=0;
59
60         while(len>0) {
61                 if((res=read(f, buf, len)) <=0) {
62                         snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
63                         return -1;
64                 }
65                 len-=res;
66                 buf+=res;
67                 retval+=res;
68         }
69         return retval;
70 }
71
72 int setup_connection(gchar *hostname, int port, CONNECTION_TYPE ctype) {
73         int sock;
74         struct hostent *host;
75         struct sockaddr_in addr;
76         char buf[256];
77         u64 tmp64;
78
79         sock=0;
80         if(ctype<CONNECTION_TYPE_CONNECT)
81                 goto end;
82         if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
83                 strncpy(errstr, strerror(errno), errstr_len);
84                 goto err;
85         }
86         setmysockopt(sock);
87         if(!(host=gethostbyname(hostname))) {
88                 strncpy(errstr, strerror(errno), errstr_len);
89                 goto err_open;
90         }
91         addr.sin_family=AF_INET;
92         addr.sin_port=htons(port);
93         addr.sin_addr.s_addr=*((int *) host->h_addr);
94         if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) {
95                 strncpy(errstr, strerror(errno), errstr_len);
96                 goto err_open;
97         }
98         if(ctype<CONNECTION_TYPE_INIT_PASSWD)
99                 goto end;
100         if(read_all(sock, buf, strlen(INIT_PASSWD))<0) {
101                 snprintf(errstr, errstr_len, "Could not read INIT_PASSWD: %s",
102                                 strerror(errno));
103                 goto err_open;
104         }
105         if(strlen(buf)==0) {
106                 snprintf(errstr, errstr_len, "Server closed connection");
107                 goto err_open;
108         }
109         if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
110                 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
111                 goto err_open;
112         }
113         if(ctype<CONNECTION_TYPE_CLISERV)
114                 goto end;
115         if(read_all(sock, &tmp64, sizeof(tmp64))<0) {
116                 snprintf(errstr, errstr_len, "Could not read cliserv_magic: %s",
117                                 strerror(errno));
118                 goto err_open;
119         }
120         tmp64=ntohll(tmp64);
121         if(tmp64 != cliserv_magic) {
122                 strncpy(errstr, "cliserv_magic does not match", errstr_len);
123                 goto err_open;
124         }
125         if(ctype<CONNECTION_TYPE_FULL)
126                 goto end;
127         /* The information we get now contains information on sizes. If
128          * we're here, that means we want a 'working' connection, but
129          * we're not interested in the sizes. So, read them but throw
130          * the values away. We need to read the size of the device (a
131          * 64bit integer) plus the reserved fields (128 bytes; should
132          * all be zeroes).
133          */
134         read_all(sock, buf, sizeof(tmp64)+128);
135         goto end;
136 err_open:
137         close(sock);
138 err:
139         sock=-1;
140 end:
141         return sock;
142 }
143
144 int close_connection(int sock, CLOSE_TYPE type) {
145         struct nbd_request req;
146         u64 counter=0;
147
148         switch(type) {
149                 case CONNECTION_CLOSE_PROPERLY:
150                         req.magic=htonl(NBD_REQUEST_MAGIC);
151                         req.type=htonl(NBD_CMD_DISC);
152                         memcpy(&(req.handle), &(counter), sizeof(counter));
153                         counter++;
154                         req.from=0;
155                         req.len=0;
156                         if(write(sock, &req, sizeof(req))<0) {
157                                 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
158                                 return -1;
159                         }
160                 case CONNECTION_CLOSE_FAST:
161                         if(close(sock)<0) {
162                                 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
163                                 return -1;
164                         }
165                         break;
166                 default:
167                         g_critical("Your compiler is on crack!"); /* or I am buggy */
168                         return -1;
169         }
170         return 0;
171 }
172
173 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
174         struct nbd_reply rep;
175         int retval=0;
176         char buf[datasize];
177
178         read_all(sock, &rep, sizeof(rep));
179         rep.magic=ntohl(rep.magic);
180         rep.error=ntohl(rep.error);
181         if(rep.magic!=NBD_REPLY_MAGIC) {
182                 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", curhandle, curhandle, *((u64*)rep.handle), *((u64*)rep.handle), (long unsigned int)rep.magic, (long unsigned int)NBD_REPLY_MAGIC);
183                 retval=-1;
184                 goto end;
185         }
186         if(rep.error) {
187                 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)), *((u64*)rep.handle));
188                 retval=-1;
189                 goto end;
190         }
191         read_all(sock, &buf, datasize);
192
193 end:
194         return retval;
195 }
196
197 int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char close_sock) {
198         long long int i;
199         char buf[1024];
200         struct nbd_request req;
201         u64 size;
202         int requests=0;
203         fd_set set;
204         struct timeval tv;
205         struct timeval start;
206         struct timeval stop;
207         float timespan;
208         int speed;
209         char speedchar[2] = { '\0', '\0' };
210         int retval=0;
211         size_t tmp;
212         signed int do_write=TRUE;
213         pid_t mypid = getpid();
214
215         size=0;
216         if(!sock_is_open) {
217                 if((sock=setup_connection(hostname, port, CONNECTION_TYPE_CLISERV))<0) {
218                         g_warning("Could not open socket: %s", errstr);
219                         retval=-1;
220                         goto err;
221                 }
222         } else {
223                 /* Assume the file is at least 4k in size. Not much of a test
224                  * this way, but, well. */
225                 size=4096;
226         }
227         if((tmp=read_all(sock, &size, sizeof(u64)))<0) {
228                 retval=-1;
229                 snprintf(errstr, errstr_len, "Could not read from socket: %s", strerror(errno));
230                 goto err_open;
231         }
232         if(tmp==0) {
233                 retval=-1;
234                 snprintf(errstr, errstr_len, "Server closed connection unexpectedly when trying to read size of device in throughput test");
235                 goto err;
236         }
237         read_all(sock,&buf,128);
238         size=ntohll(size);
239         req.magic=htonl(NBD_REQUEST_MAGIC);
240         req.type=htonl(NBD_CMD_READ);
241         req.len=htonl(1024);
242         if(gettimeofday(&start, NULL)<0) {
243                 retval=-1;
244                 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
245                 goto err_open;
246         }
247         for(i=0;i+1024<=size;i+=1024) {
248                 if(do_write) {
249                         memcpy(&(req.handle),&i,sizeof(i));
250                         req.from=htonll(i);
251                         write(sock, &req, sizeof(req));
252                         printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
253                 }
254                 do {
255                         FD_ZERO(&set);
256                         FD_SET(sock, &set);
257                         tv.tv_sec=0;
258                         tv.tv_usec=0;
259                         select(sock+1, &set, NULL, NULL, &tv);
260                         if(FD_ISSET(sock, &set)) {
261                                 /* Okay, there's something ready for
262                                  * reading here */
263                                 if(read_packet_check_header(sock, 1024, i)<0) {
264                                         retval=-1;
265                                         goto err_open;
266                                 }
267                                 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
268                         }
269                 } while FD_ISSET(sock, &set);
270                 /* Now wait until we can write again or until a second have
271                  * passed, whichever comes first*/
272                 FD_ZERO(&set);
273                 FD_SET(sock, &set);
274                 tv.tv_sec=1;
275                 tv.tv_usec=0;
276                 do_write=select(sock+1,NULL,&set,NULL,&tv);
277                 if(!do_write) printf("Select finished\n");
278                 if(do_write<0) {
279                         snprintf(errstr, errstr_len, "select: %s", strerror(errno));
280                         retval=-1;
281                         goto err_open;
282                 }
283         }
284         /* Now empty the read buffer */
285         do {
286                 FD_ZERO(&set);
287                 FD_SET(sock, &set);
288                 tv.tv_sec=0;
289                 tv.tv_usec=0;
290                 select(sock+1, &set, NULL, NULL, &tv);
291                 if(FD_ISSET(sock, &set)) {
292                         /* Okay, there's something ready for
293                          * reading here */
294                         read_packet_check_header(sock, 1024, i);
295                         printf("%d: Requests(-): %d\n", (int)mypid, --requests);
296                 }
297         } while (requests);
298         if(gettimeofday(&stop, NULL)<0) {
299                 retval=-1;
300                 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
301                 goto err_open;
302         }
303         timespan=(float)(stop.tv_sec-start.tv_sec+(stop.tv_usec-start.tv_usec))/(float)1000000;
304         speed=(int)(size/timespan);
305         if(speed>1024) {
306                 speed>>=10;
307                 speedchar[0]='K';
308         }
309         if(speed>1024) {
310                 speed>>=10;
311                 speedchar[0]='M';
312         }
313         if(speed>1024) {
314                 speed>>=10;
315                 speedchar[0]='G';
316         }
317         g_message("%d: Throughput test complete. Took %.3f seconds to complete, %d%sB/s", (int)getpid(), timespan,speed,speedchar);
318
319 err_open:
320         if(close_sock) {
321                 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
322         }
323 err:
324         return retval;
325 }
326
327 int main(int argc, char**argv) {
328         gchar *hostname;
329         long int p;
330         int port;
331         int sock=0;
332
333         if(argc<3) {
334                 g_message("%d: Not enough arguments", (int)getpid());
335                 g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
336                 exit(EXIT_FAILURE);
337         }
338         logging();
339         hostname=g_strdup(argv[1]);
340         p=(strtol(argv[2], NULL, 0));
341         if(p==LONG_MIN||p==LONG_MAX) {
342                 g_critical("Could not parse port number: %s", strerror(errno));
343                 exit(EXIT_FAILURE);
344         }
345         port=(int)p;
346
347         if(throughput_test(hostname, port, sock, FALSE, TRUE)<0) {
348                 g_warning("Could not run throughput test: %s", errstr);
349                 exit(EXIT_FAILURE);
350         }
351
352         return 0;
353 }