2 * Test client to test the NBD server. Doesn't do anything useful, except
3 * checking that the server does, actually, work.
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.
9 * Copyright(c) 2006 Wouter Verhelst
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.
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
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
29 #include <sys/types.h>
30 #include <sys/socket.h>
35 #define MY_NAME "nbd-tester-client"
38 #include <netinet/in.h>
41 static gchar errstr[1024];
42 const static int errstr_len=1024;
48 CONNECTION_TYPE_CONNECT,
49 CONNECTION_TYPE_INIT_PASSWD,
50 CONNECTION_TYPE_CLISERV,
55 CONNECTION_CLOSE_PROPERLY,
56 CONNECTION_CLOSE_FAST,
59 inline int read_all(int f, void *buf, size_t len) {
64 if((res=read(f, buf, len)) <=0) {
65 snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
75 int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype) {
78 struct sockaddr_in addr;
80 uint64_t mymagic = (name ? opts_magic : cliserv_magic);
85 if(ctype<CONNECTION_TYPE_CONNECT)
87 if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
88 strncpy(errstr, strerror(errno), errstr_len);
92 if(!(host=gethostbyname(hostname))) {
93 strncpy(errstr, strerror(errno), errstr_len);
96 addr.sin_family=AF_INET;
97 addr.sin_port=htons(port);
98 addr.sin_addr.s_addr=*((int *) host->h_addr);
99 if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) {
100 strncpy(errstr, strerror(errno), errstr_len);
103 if(ctype<CONNECTION_TYPE_INIT_PASSWD)
105 if(read_all(sock, buf, strlen(INIT_PASSWD))<0) {
106 snprintf(errstr, errstr_len, "Could not read INIT_PASSWD: %s",
111 snprintf(errstr, errstr_len, "Server closed connection");
114 if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
115 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
118 if(ctype<CONNECTION_TYPE_CLISERV)
120 if(read_all(sock, &tmp64, sizeof(tmp64))<0) {
121 snprintf(errstr, errstr_len, "Could not read cliserv_magic: %s",
126 if(tmp64 != mymagic) {
127 strncpy(errstr, "mymagic does not match", errstr_len);
130 if(ctype<CONNECTION_TYPE_FULL)
133 read_all(sock, &size, sizeof(size));
135 read_all(sock, buf, 128);
139 read_all(sock, buf, sizeof(uint16_t));
141 write(sock, &tmp32, sizeof(tmp32));
143 tmp64 = htonll(opts_magic);
144 write(sock, &tmp64, sizeof(tmp64));
146 tmp32 = htonl(NBD_OPT_EXPORT_NAME);
147 write(sock, &tmp32, sizeof(tmp32));
148 tmp32 = htonl((uint32_t)strlen(name));
149 write(sock, &tmp32, sizeof(tmp32));
150 write(sock, name, strlen(name));
151 read_all(sock, &size, sizeof(size));
153 read_all(sock, buf, sizeof(uint16_t)+124);
163 int close_connection(int sock, CLOSE_TYPE type) {
164 struct nbd_request req;
168 case CONNECTION_CLOSE_PROPERLY:
169 req.magic=htonl(NBD_REQUEST_MAGIC);
170 req.type=htonl(NBD_CMD_DISC);
171 memcpy(&(req.handle), &(counter), sizeof(counter));
175 if(write(sock, &req, sizeof(req))<0) {
176 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
179 case CONNECTION_CLOSE_FAST:
181 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
186 g_critical("Your compiler is on crack!"); /* or I am buggy */
192 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
193 struct nbd_reply rep;
197 read_all(sock, &rep, sizeof(rep));
198 rep.magic=ntohl(rep.magic);
199 rep.error=ntohl(rep.error);
200 if(rep.magic!=NBD_REPLY_MAGIC) {
201 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);
206 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));
210 read_all(sock, &buf, datasize);
216 int oversize_test(gchar* hostname, int port, char* name, int sock, char sock_is_open, char close_sock) {
218 struct nbd_request req;
219 struct nbd_reply rep;
222 pid_t mypid = getpid();
223 char buf[((1024*1024)+sizeof(struct nbd_request)/2)<<1];
226 /* This should work */
228 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
229 g_warning("Could not open socket: %s", errstr);
234 req.magic=htonl(NBD_REQUEST_MAGIC);
235 req.type=htonl(NBD_CMD_READ);
236 req.len=htonl(1024*1024);
237 memcpy(&(req.handle),&i,sizeof(i));
239 write(sock, &req, sizeof(req));
240 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
241 read_all(sock, &rep, sizeof(struct nbd_reply));
242 read_all(sock, &buf, ntohl(req.len));
244 printf("Received unexpected error\n");
250 /* This probably should not work */
251 i++; req.from=htonll(i);
252 req.len = htonl(ntohl(req.len) + sizeof(struct nbd_request) / 2);
253 write(sock, &req, sizeof(req));
254 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
255 read_all(sock, &rep, sizeof(struct nbd_reply));
256 read_all(sock, &buf, ntohl(req.len));
258 printf("Received expected error\n");
264 /* ... unless this works, too */
265 i++; req.from=htonll(i);
266 req.len = htonl(ntohl(req.len) << 1);
267 write(sock, &req, sizeof(req));
268 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
269 read_all(sock, &rep, sizeof(struct nbd_reply));
270 read_all(sock, &buf, ntohl(req.len));
276 if((rep.error && !got_err) || (!rep.error && got_err)) {
277 printf("Received unexpected error\n");
284 int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_is_open, char close_sock) {
287 struct nbd_request req;
291 struct timeval start;
295 char speedchar[2] = { '\0', '\0' };
298 signed int do_write=TRUE;
299 pid_t mypid = getpid();
303 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
304 g_warning("Could not open socket: %s", errstr);
309 req.magic=htonl(NBD_REQUEST_MAGIC);
310 req.type=htonl(NBD_CMD_READ);
312 if(gettimeofday(&start, NULL)<0) {
314 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
317 for(i=0;i+1024<=size;i+=1024) {
319 memcpy(&(req.handle),&i,sizeof(i));
321 write(sock, &req, sizeof(req));
322 printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
329 select(sock+1, &set, NULL, NULL, &tv);
330 if(FD_ISSET(sock, &set)) {
331 /* Okay, there's something ready for
333 if(read_packet_check_header(sock, 1024, i)<0) {
337 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
339 } while FD_ISSET(sock, &set);
340 /* Now wait until we can write again or until a second have
341 * passed, whichever comes first*/
346 do_write=select(sock+1,NULL,&set,NULL,&tv);
347 if(!do_write) printf("Select finished\n");
349 snprintf(errstr, errstr_len, "select: %s", strerror(errno));
354 /* Now empty the read buffer */
360 select(sock+1, &set, NULL, NULL, &tv);
361 if(FD_ISSET(sock, &set)) {
362 /* Okay, there's something ready for
364 read_packet_check_header(sock, 1024, i);
365 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
368 if(gettimeofday(&stop, NULL)<0) {
370 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
373 timespan=(float)(stop.tv_sec-start.tv_sec+(stop.tv_usec-start.tv_usec))/(float)1000000;
374 speed=(int)(size/timespan);
387 g_message("%d: Throughput test complete. Took %.3f seconds to complete, %d%siB/s", (int)getpid(), timespan,speed,speedchar);
391 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
397 typedef int (*testfunc)(gchar*, int, char*, int, char, char);
399 int main(int argc, char**argv) {
405 bool want_port = TRUE;
407 testfunc test = throughput_test;
410 g_message("%d: Not enough arguments", (int)getpid());
411 g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
412 g_message("%d: Or: %s <hostname> -N <exportname>", (int)getpid(), argv[0]);
416 while((c=getopt(argc, argv, "-N:o"))>=0) {
421 hostname=g_strdup(optarg);
426 p=(strtol(argv[2], NULL, 0));
427 if(p==LONG_MIN||p==LONG_MAX) {
428 g_critical("Could not parse port number: %s", strerror(errno));
435 name=g_strdup(optarg);
445 if(test(hostname, (int)p, name, sock, FALSE, TRUE)<0) {
446 g_warning("Could not run test: %s", errstr);