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>
34 #define MY_NAME "nbd-tester-client"
37 static gchar errstr[1024];
38 const static int errstr_len=1024;
42 CONNECTION_TYPE_CONNECT,
43 CONNECTION_TYPE_INIT_PASSWD,
44 CONNECTION_TYPE_CLISERV,
49 CONNECTION_CLOSE_PROPERLY,
50 CONNECTION_CLOSE_FAST,
53 inline int read_all(int f, void *buf, size_t len) {
58 if((res=read(f, buf, len)) <=0) {
59 snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
69 int setup_connection(gchar *hostname, int port, CONNECTION_TYPE ctype) {
72 struct sockaddr_in addr;
77 if(ctype<CONNECTION_TYPE_CONNECT)
79 if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
80 strncpy(errstr, strerror(errno), errstr_len);
84 if(!(host=gethostbyname(hostname))) {
85 strncpy(errstr, strerror(errno), errstr_len);
88 addr.sin_family=AF_INET;
89 addr.sin_port=htons(port);
90 addr.sin_addr.s_addr=*((int *) host->h_addr);
91 if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) {
92 strncpy(errstr, strerror(errno), errstr_len);
95 if(ctype<CONNECTION_TYPE_INIT_PASSWD)
97 if(read_all(sock, buf, strlen(INIT_PASSWD))<0) {
98 snprintf(errstr, errstr_len, "Could not read INIT_PASSWD: %s",
103 snprintf(errstr, errstr_len, "Server closed connection");
106 if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
107 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
110 if(ctype<CONNECTION_TYPE_CLISERV)
112 if(read_all(sock, &tmp64, sizeof(tmp64))<0) {
113 snprintf(errstr, errstr_len, "Could not read cliserv_magic: %s",
118 if(tmp64 != cliserv_magic) {
119 strncpy(errstr, "cliserv_magic does not match", errstr_len);
122 if(ctype<CONNECTION_TYPE_FULL)
124 /* The information we get now contains information on sizes. If
125 * we're here, that means we want a 'working' connection, but
126 * we're not interested in the sizes. So, read them but throw
127 * the values away. We need to read the size of the device (a
128 * 64bit integer) plus the reserved fields (128 bytes; should
131 read_all(sock, buf, sizeof(tmp64)+128);
141 int close_connection(int sock, CLOSE_TYPE type) {
142 struct nbd_request req;
146 case CONNECTION_CLOSE_PROPERLY:
147 req.magic=htonl(NBD_REQUEST_MAGIC);
148 req.type=htonl(NBD_CMD_DISC);
149 memcpy(&(req.handle), &(counter), sizeof(counter));
153 if(write(sock, &req, sizeof(req))<0) {
154 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
157 case CONNECTION_CLOSE_FAST:
159 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
164 g_critical("Your compiler is on crack!"); /* or I am buggy */
170 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
171 struct nbd_reply rep;
175 read_all(sock, &rep, sizeof(rep));
176 rep.magic=ntohl(rep.magic);
177 rep.error=ntohl(rep.error);
178 if(rep.magic!=NBD_REPLY_MAGIC) {
179 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);
184 snprintf(errstr, errstr_len, "Received error from server: %ld (0x%lX). Handle is %lld (0x%llX).", rep.error, *((u64*)rep.handle), *((u64*)rep.handle));
188 read_all(sock, &buf, datasize);
194 int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char close_sock) {
197 struct nbd_request req;
204 signed int do_write=TRUE;
207 if((sock=setup_connection(hostname, port, CONNECTION_TYPE_CLISERV))<0) {
208 g_warning("Could not open socket: %s", errstr);
213 /* Assume the file is at least 4k in size. Not much of a test
214 * this way, but, well. */
218 if((tmp=read_all(sock, &size, sizeof(u64)))<0) {
220 snprintf(errstr, errstr_len, "Could not read from socket: %s", strerror(errno));
225 snprintf(errstr, errstr_len, "Server closed connection unexpectedly when trying to read size of device in throughput test");
228 read_all(sock,&buf,128);
230 req.magic=htonl(NBD_REQUEST_MAGIC);
231 req.type=htonl(NBD_CMD_READ);
233 for(i=0;i+1024<=size;i+=1024) {
235 *((u64*)req.handle)=i;
237 write(sock, &req, sizeof(req));
238 printf("Requests(+): %d\n", ++requests);
245 select(sock+1, &set, NULL, NULL, &tv);
246 if(FD_ISSET(sock, &set)) {
247 /* Okay, there's something ready for
249 if(read_packet_check_header(sock, 1024, i)<0) {
253 printf("Requests(-): %d\n", --requests);
255 } while FD_ISSET(sock, &set);
256 /* Now wait until we can write again or until a second have
257 * passed, whichever comes first*/
262 do_write=select(sock+1,NULL,&set,NULL,&tv);
263 if(!do_write) printf("Select finished\n");
265 snprintf(errstr, errstr_len, "select: %s", strerror(errno));
270 /* Now empty the read buffer */
276 select(sock+1, &set, NULL, NULL, &tv);
277 if(FD_ISSET(sock, &set)) {
278 /* Okay, there's something ready for
280 read_packet_check_header(sock, 1024, i);
281 printf("Requests(-): %d\n", --requests);
287 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
293 int main(int argc, char**argv) {
300 g_message("Not enough arguments");
301 g_message("Usage: %s <hostname> <port>", argv[0]);
305 hostname=g_strdup(argv[1]);
306 p=(strtol(argv[2], NULL, 0));
307 if(p==LONG_MIN||p==LONG_MAX) {
308 g_critical("Could not parse port number: %s", strerror(errno));
313 if(throughput_test(hostname, port, sock, FALSE, TRUE)<0) {
314 g_warning("Could not run throughput test: %s", errstr);