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
28 #include <sys/types.h>
29 #include <sys/socket.h>
34 #define MY_NAME "nbd-tester-client"
37 #include <netinet/in.h>
40 static gchar errstr[1024];
41 const static int errstr_len=1024;
47 CONNECTION_TYPE_CONNECT,
48 CONNECTION_TYPE_INIT_PASSWD,
49 CONNECTION_TYPE_CLISERV,
54 CONNECTION_CLOSE_PROPERLY,
55 CONNECTION_CLOSE_FAST,
58 inline int read_all(int f, void *buf, size_t len) {
63 if((res=read(f, buf, len)) <=0) {
64 snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
74 int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype) {
77 struct sockaddr_in addr;
79 uint64_t mymagic = (name ? opts_magic : cliserv_magic);
84 if(ctype<CONNECTION_TYPE_CONNECT)
86 if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
87 strncpy(errstr, strerror(errno), errstr_len);
91 if(!(host=gethostbyname(hostname))) {
92 strncpy(errstr, strerror(errno), errstr_len);
95 addr.sin_family=AF_INET;
96 addr.sin_port=htons(port);
97 addr.sin_addr.s_addr=*((int *) host->h_addr);
98 if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) {
99 strncpy(errstr, strerror(errno), errstr_len);
102 if(ctype<CONNECTION_TYPE_INIT_PASSWD)
104 if(read_all(sock, buf, strlen(INIT_PASSWD))<0) {
105 snprintf(errstr, errstr_len, "Could not read INIT_PASSWD: %s",
110 snprintf(errstr, errstr_len, "Server closed connection");
113 if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
114 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
117 if(ctype<CONNECTION_TYPE_CLISERV)
119 if(read_all(sock, &tmp64, sizeof(tmp64))<0) {
120 snprintf(errstr, errstr_len, "Could not read cliserv_magic: %s",
125 if(tmp64 != mymagic) {
126 strncpy(errstr, "mymagic does not match", errstr_len);
129 if(ctype<CONNECTION_TYPE_FULL)
132 read_all(sock, &size, sizeof(size));
134 read_all(sock, buf, 128);
138 read_all(sock, buf, sizeof(uint16_t));
140 write(sock, &tmp32, sizeof(tmp32));
142 tmp64 = htonll(opts_magic);
143 write(sock, &tmp64, sizeof(tmp64));
145 tmp32 = htonl(NBD_OPT_EXPORT_NAME);
146 write(sock, &tmp32, sizeof(tmp32));
147 tmp32 = htonl((uint32_t)strlen(name));
148 write(sock, &tmp32, sizeof(tmp32));
149 write(sock, name, strlen(name));
150 read_all(sock, &size, sizeof(size));
152 read_all(sock, buf, sizeof(uint16_t)+124);
162 int close_connection(int sock, CLOSE_TYPE type) {
163 struct nbd_request req;
167 case CONNECTION_CLOSE_PROPERLY:
168 req.magic=htonl(NBD_REQUEST_MAGIC);
169 req.type=htonl(NBD_CMD_DISC);
170 memcpy(&(req.handle), &(counter), sizeof(counter));
174 if(write(sock, &req, sizeof(req))<0) {
175 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
178 case CONNECTION_CLOSE_FAST:
180 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
185 g_critical("Your compiler is on crack!"); /* or I am buggy */
191 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
192 struct nbd_reply rep;
196 read_all(sock, &rep, sizeof(rep));
197 rep.magic=ntohl(rep.magic);
198 rep.error=ntohl(rep.error);
199 if(rep.magic!=NBD_REPLY_MAGIC) {
200 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);
205 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));
209 read_all(sock, &buf, datasize);
215 int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_is_open, char close_sock) {
218 struct nbd_request req;
222 struct timeval start;
226 char speedchar[2] = { '\0', '\0' };
229 signed int do_write=TRUE;
230 pid_t mypid = getpid();
234 if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
235 g_warning("Could not open socket: %s", errstr);
240 req.magic=htonl(NBD_REQUEST_MAGIC);
241 req.type=htonl(NBD_CMD_READ);
243 if(gettimeofday(&start, NULL)<0) {
245 snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
248 for(i=0;i+1024<=size;i+=1024) {
250 memcpy(&(req.handle),&i,sizeof(i));
252 write(sock, &req, sizeof(req));
253 printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
260 select(sock+1, &set, NULL, NULL, &tv);
261 if(FD_ISSET(sock, &set)) {
262 /* Okay, there's something ready for
264 if(read_packet_check_header(sock, 1024, i)<0) {
268 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
270 } while FD_ISSET(sock, &set);
271 /* Now wait until we can write again or until a second have
272 * passed, whichever comes first*/
277 do_write=select(sock+1,NULL,&set,NULL,&tv);
278 if(!do_write) printf("Select finished\n");
280 snprintf(errstr, errstr_len, "select: %s", strerror(errno));
285 /* Now empty the read buffer */
291 select(sock+1, &set, NULL, NULL, &tv);
292 if(FD_ISSET(sock, &set)) {
293 /* Okay, there's something ready for
295 read_packet_check_header(sock, 1024, i);
296 printf("%d: Requests(-): %d\n", (int)mypid, --requests);
299 if(gettimeofday(&stop, NULL)<0) {
301 snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
304 timespan=(float)(stop.tv_sec-start.tv_sec+(stop.tv_usec-start.tv_usec))/(float)1000000;
305 speed=(int)(size/timespan);
318 g_message("%d: Throughput test complete. Took %.3f seconds to complete, %d%sB/s", (int)getpid(), timespan,speed,speedchar);
322 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
328 int main(int argc, char**argv) {
336 g_message("%d: Not enough arguments", (int)getpid());
337 g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
338 g_message("%d: Or: %s <hostname> -N <exportname>", (int)getpid(), argv[0]);
342 hostname=g_strdup(argv[1]);
343 if(!strncmp(argv[2], "-N", 2)) {
344 name = g_strdup(argv[3]);
347 p=(strtol(argv[2], NULL, 0));
348 if(p==LONG_MIN||p==LONG_MAX) {
349 g_critical("Could not parse port number: %s", strerror(errno));
355 if(throughput_test(hostname, port, name, sock, FALSE, TRUE)<0) {
356 g_warning("Could not run throughput test: %s", errstr);