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