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