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