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 static 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 static inline int write_all(int f, void *buf, size_t len) {
80 if((res=write(f, buf, len)) <=0) {
81 snprintf(errstr, errstr_len, "Write failed: %s", strerror(errno));
91 #define READ_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; }
92 #define READ_ALL_ERR_RT(f, buf, len, whereto, rval, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); retval = rval; goto whereto; }
94 #define WRITE_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; }
95 #define WRITE_ALL_ERR_RT(f, buf, len, whereto, rval, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); retval = rval; goto whereto; }
97 int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype) {
100 struct sockaddr_in addr;
102 uint64_t mymagic = (name ? opts_magic : cliserv_magic);
107 if(ctype<CONNECTION_TYPE_CONNECT)
109 if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
110 strncpy(errstr, strerror(errno), errstr_len);
114 if(!(host=gethostbyname(hostname))) {
115 strncpy(errstr, strerror(errno), errstr_len);
118 addr.sin_family=AF_INET;
119 addr.sin_port=htons(port);
120 addr.sin_addr.s_addr=*((int *) host->h_addr);
121 if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) {
122 strncpy(errstr, strerror(errno), errstr_len);
125 if(ctype<CONNECTION_TYPE_INIT_PASSWD)
127 READ_ALL_ERRCHK(sock, buf, strlen(INIT_PASSWD), err_open, "Could not read INIT_PASSWD: %s", strerror(errno));
129 snprintf(errstr, errstr_len, "Server closed connection");
132 if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
133 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
136 if(ctype<CONNECTION_TYPE_CLISERV)
138 READ_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not read cliserv_magic: %s", strerror(errno));
140 if(tmp64 != mymagic) {
141 strncpy(errstr, "mymagic does not match", errstr_len);
144 if(ctype<CONNECTION_TYPE_FULL)
147 READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
149 READ_ALL_ERRCHK(sock, buf, 128, err_open, "Could not read data: %s", strerror(errno));
153 READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read reserved field: %s", strerror(errno));
155 WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write reserved field: %s", strerror(errno));
157 tmp64 = htonll(opts_magic);
158 WRITE_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not write magic: %s", strerror(errno));
160 tmp32 = htonl(NBD_OPT_EXPORT_NAME);
161 WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write option: %s", strerror(errno));
162 tmp32 = htonl((uint32_t)strlen(name));
163 WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write name length: %s", strerror(errno));
164 WRITE_ALL_ERRCHK(sock, name, strlen(name), err_open, "Could not write name:: %s", strerror(errno));
165 READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
168 READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read flags: %s", strerror(errno));
169 flags = ntohs(flags);
170 READ_ALL_ERRCHK(sock, buf, 124, err_open, "Could not read reserved zeroes: %s", strerror(errno));
180 int close_connection(int sock, CLOSE_TYPE type) {
181 struct nbd_request req;
185 case CONNECTION_CLOSE_PROPERLY:
186 req.magic=htonl(NBD_REQUEST_MAGIC);
187 req.type=htonl(NBD_CMD_DISC);
188 memcpy(&(req.handle), &(counter), sizeof(counter));
192 if(write(sock, &req, sizeof(req))<0) {
193 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
196 case CONNECTION_CLOSE_FAST:
198 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
203 g_critical("Your compiler is on crack!"); /* or I am buggy */
209 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
210 struct nbd_reply rep;
214 READ_ALL_ERR_RT(sock, &rep, sizeof(rep), end, -1, "Could not read reply header: %s", strerror(errno));
215 rep.magic=ntohl(rep.magic);
216 rep.error=ntohl(rep.error);
217 if(rep.magic!=NBD_REPLY_MAGIC) {
218 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", (long long int)curhandle, (long long unsigned int)curhandle, (long long int)*((u64*)rep.handle), (long long unsigned int)*((u64*)rep.handle), (long unsigned int)rep.magic, (long unsigned int)NBD_REPLY_MAGIC);
223 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)), (long long unsigned int)*((u64*)rep.handle));
228 READ_ALL_ERR_RT(sock, &buf, datasize, end, -1, "Could not read data: %s", strerror(errno));
234 int oversize_test(gchar* hostname, int port, char* name, int sock,
235 char sock_is_open, char close_sock, int write) {
237 struct nbd_request req;
238 struct nbd_reply rep;
241 pid_t mypid = getpid();
242 char buf[((1024*1024)+sizeof(struct nbd_request)/2)<<1];
245 /* This should work */
247 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
248 g_warning("Could not open socket: %s", errstr);
253 req.magic=htonl(NBD_REQUEST_MAGIC);
254 req.type=htonl(NBD_CMD_READ);
255 req.len=htonl(1024*1024);
256 memcpy(&(req.handle),&i,sizeof(i));
258 WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
259 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
260 READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
261 READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
263 snprintf(errstr, errstr_len, "Received unexpected error: %d", rep.error);
269 /* This probably should not work */
270 i++; req.from=htonll(i);
271 req.len = htonl(ntohl(req.len) + sizeof(struct nbd_request) / 2);
272 WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
273 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
274 READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
275 READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
277 printf("Received expected error\n");
283 /* ... unless this works, too */
284 i++; req.from=htonll(i);
285 req.len = htonl(ntohl(req.len) << 1);
286 WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
287 printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
288 READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
289 READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
295 if((rep.error && !got_err) || (!rep.error && got_err)) {
296 printf("Received unexpected error\n");
303 int throughput_test(gchar* hostname, int port, char* name, int sock,
304 char sock_is_open, char close_sock, int write) {
308 struct nbd_request req;
312 struct timeval start;
316 char speedchar[2] = { '\0', '\0' };
319 signed int do_write=TRUE;
320 pid_t mypid = getpid();
322 memset (writebuf, 'X', sizeof(1024));
325 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
326 g_warning("Could not open socket: %s", errstr);
331 req.magic=htonl(NBD_REQUEST_MAGIC);
332 req.type=htonl(write?NBD_CMD_WRITE:NBD_CMD_READ);
334 if(gettimeofday(&start, NULL)<0) {
336 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
339 for(i=0;i+1024<=size;i+=1024) {
341 memcpy(&(req.handle),&i,sizeof(i));
343 if (write_all(sock, &req, sizeof(req)) <0) {
348 if (write_all(sock, writebuf, 1024) <0) {
353 printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
360 select(sock+1, &set, NULL, NULL, &tv);
361 if(FD_ISSET(sock, &set)) {
362 /* Okay, there's something ready for
364 if(read_packet_check_header(sock, write?0:1024, i)<0) {
368 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
370 } while FD_ISSET(sock, &set);
371 /* Now wait until we can write again or until a second have
372 * passed, whichever comes first*/
377 do_write=select(sock+1,NULL,&set,NULL,&tv);
378 if(!do_write) printf("Select finished\n");
380 snprintf(errstr, errstr_len, "select: %s", strerror(errno));
385 /* Now empty the read buffer */
391 select(sock+1, &set, NULL, NULL, &tv);
392 if(FD_ISSET(sock, &set)) {
393 /* Okay, there's something ready for
395 read_packet_check_header(sock, write?0:1024, i);
396 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
399 if(gettimeofday(&stop, NULL)<0) {
401 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
404 timespan=(float)(stop.tv_sec-start.tv_sec+(stop.tv_usec-start.tv_usec))/(float)1000000;
405 speed=(int)(size/timespan);
418 g_message("%d: Throughput %s test complete. Took %.3f seconds to complete, %d%siB/s", (int)getpid(), write?"write":"read", timespan, speed, speedchar);
422 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
428 typedef int (*testfunc)(gchar*, int, char*, int, char, char, int);
430 int main(int argc, char**argv) {
436 bool want_port = TRUE;
439 testfunc test = throughput_test;
442 g_message("%d: Not enough arguments", (int)getpid());
443 g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
444 g_message("%d: Or: %s <hostname> -N <exportname>", (int)getpid(), argv[0]);
448 while((c=getopt(argc, argv, "-N:ow"))>=0) {
453 hostname=g_strdup(optarg);
458 p=(strtol(argv[2], NULL, 0));
459 if(p==LONG_MIN||p==LONG_MAX) {
460 g_critical("Could not parse port number: %s", strerror(errno));
467 name=g_strdup(optarg);
480 if(test(hostname, (int)p, name, sock, FALSE, TRUE, write)<0) {
481 g_warning("Could not run test: %s", errstr);