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/types.h>
31 #include <sys/socket.h>
36 #define MY_NAME "nbd-tester-client"
39 static gchar errstr[1024];
40 const static int errstr_len=1024;
44 CONNECTION_TYPE_CONNECT,
45 CONNECTION_TYPE_INIT_PASSWD,
46 CONNECTION_TYPE_CLISERV,
51 CONNECTION_CLOSE_PROPERLY,
52 CONNECTION_CLOSE_FAST,
55 inline int read_all(int f, void *buf, size_t len) {
60 if((res=read(f, buf, len)) <=0) {
61 snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
71 int setup_connection(gchar *hostname, int port, CONNECTION_TYPE ctype) {
74 struct sockaddr_in addr;
79 if(ctype<CONNECTION_TYPE_CONNECT)
81 if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
82 strncpy(errstr, strerror(errno), errstr_len);
86 if(!(host=gethostbyname(hostname))) {
87 strncpy(errstr, strerror(errno), errstr_len);
90 addr.sin_family=AF_INET;
91 addr.sin_port=htons(port);
92 addr.sin_addr.s_addr=*((int *) host->h_addr);
93 if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) {
94 strncpy(errstr, strerror(errno), errstr_len);
97 if(ctype<CONNECTION_TYPE_INIT_PASSWD)
99 if(read_all(sock, buf, strlen(INIT_PASSWD))<0) {
100 snprintf(errstr, errstr_len, "Could not read INIT_PASSWD: %s",
105 snprintf(errstr, errstr_len, "Server closed connection");
108 if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
109 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
112 if(ctype<CONNECTION_TYPE_CLISERV)
114 if(read_all(sock, &tmp64, sizeof(tmp64))<0) {
115 snprintf(errstr, errstr_len, "Could not read cliserv_magic: %s",
120 if(tmp64 != cliserv_magic) {
121 strncpy(errstr, "cliserv_magic does not match", errstr_len);
124 if(ctype<CONNECTION_TYPE_FULL)
126 /* The information we get now contains information on sizes. If
127 * we're here, that means we want a 'working' connection, but
128 * we're not interested in the sizes. So, read them but throw
129 * the values away. We need to read the size of the device (a
130 * 64bit integer) plus the reserved fields (128 bytes; should
133 read_all(sock, buf, sizeof(tmp64)+128);
143 int close_connection(int sock, CLOSE_TYPE type) {
144 struct nbd_request req;
148 case CONNECTION_CLOSE_PROPERLY:
149 req.magic=htonl(NBD_REQUEST_MAGIC);
150 req.type=htonl(NBD_CMD_DISC);
151 memcpy(&(req.handle), &(counter), sizeof(counter));
155 if(write(sock, &req, sizeof(req))<0) {
156 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
159 case CONNECTION_CLOSE_FAST:
161 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
166 g_critical("Your compiler is on crack!"); /* or I am buggy */
172 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
173 struct nbd_reply rep;
177 read_all(sock, &rep, sizeof(rep));
178 rep.magic=ntohl(rep.magic);
179 rep.error=ntohl(rep.error);
180 if(rep.magic!=NBD_REPLY_MAGIC) {
181 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);
186 snprintf(errstr, errstr_len, "Received error from server: %ld (0x%lX). Handle is %lld (0x%llX).", rep.error, *((u64*)rep.handle), *((u64*)rep.handle));
190 read_all(sock, &buf, datasize);
196 int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char close_sock) {
199 struct nbd_request req;
206 signed int do_write=TRUE;
209 if((sock=setup_connection(hostname, port, CONNECTION_TYPE_CLISERV))<0) {
210 g_warning("Could not open socket: %s", errstr);
215 /* Assume the file is at least 4k in size. Not much of a test
216 * this way, but, well. */
220 if((tmp=read_all(sock, &size, sizeof(u64)))<0) {
222 snprintf(errstr, errstr_len, "Could not read from socket: %s", strerror(errno));
227 snprintf(errstr, errstr_len, "Server closed connection unexpectedly when trying to read size of device in throughput test");
230 read_all(sock,&buf,128);
232 req.magic=htonl(NBD_REQUEST_MAGIC);
233 req.type=htonl(NBD_CMD_READ);
235 for(i=0;i+1024<=size;i+=1024) {
237 *((u64*)req.handle)=i;
239 write(sock, &req, sizeof(req));
240 printf("Requests(+): %d\n", ++requests);
247 select(sock+1, &set, NULL, NULL, &tv);
248 if(FD_ISSET(sock, &set)) {
249 /* Okay, there's something ready for
251 if(read_packet_check_header(sock, 1024, i)<0) {
255 printf("Requests(-): %d\n", --requests);
257 } while FD_ISSET(sock, &set);
258 /* Now wait until we can write again or until a second have
259 * passed, whichever comes first*/
264 do_write=select(sock+1,NULL,&set,NULL,&tv);
265 if(!do_write) printf("Select finished\n");
267 snprintf(errstr, errstr_len, "select: %s", strerror(errno));
272 /* Now empty the read buffer */
278 select(sock+1, &set, NULL, NULL, &tv);
279 if(FD_ISSET(sock, &set)) {
280 /* Okay, there's something ready for
282 read_packet_check_header(sock, 1024, i);
283 printf("Requests(-): %d\n", --requests);
289 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
295 int main(int argc, char**argv) {
302 g_message("Not enough arguments");
303 g_message("Usage: %s <hostname> <port>", argv[0]);
307 hostname=g_strdup(argv[1]);
308 p=(strtol(argv[2], NULL, 0));
309 if(p==LONG_MIN||p==LONG_MAX) {
310 g_critical("Could not parse port number: %s", strerror(errno));
315 if(throughput_test(hostname, port, sock, FALSE, TRUE)<0) {
316 g_warning("Could not run throughput test: %s", errstr);