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