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) {
56 if((res=read(f, buf, len)) <=0) {
57 snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
65 int setup_connection(gchar *hostname, int port, CONNECTION_TYPE ctype) {
68 struct sockaddr_in addr;
73 if(ctype<CONNECTION_TYPE_CONNECT)
75 if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
76 strncpy(errstr, strerror(errno), errstr_len);
80 if(!(host=gethostbyname(hostname))) {
81 strncpy(errstr, strerror(errno), errstr_len);
84 addr.sin_family=AF_INET;
85 addr.sin_port=htons(port);
86 addr.sin_addr.s_addr=*((int *) host->h_addr);
87 if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) {
88 strncpy(errstr, strerror(errno), errstr_len);
91 if(ctype<CONNECTION_TYPE_INIT_PASSWD)
93 if(read_all(sock, buf, strlen(INIT_PASSWD))<0) {
94 snprintf(errstr, errstr_len, "Could not read INIT_PASSWD: %s",
99 snprintf(errstr, errstr_len, "Server closed connection");
102 if(strncmp(buf, INIT_PASSWD, strlen(INIT_PASSWD))) {
103 snprintf(errstr, errstr_len, "INIT_PASSWD does not match");
106 if(ctype<CONNECTION_TYPE_CLISERV)
108 if(read_all(sock, &tmp64, sizeof(tmp64))<0) {
109 snprintf(errstr, errstr_len, "Could not read cliserv_magic: %s",
114 if(tmp64 != cliserv_magic) {
115 strncpy(errstr, "cliserv_magic does not match", errstr_len);
118 if(ctype<CONNECTION_TYPE_FULL)
120 /* The information we get now contains information on sizes. If
121 * we're here, that means we want a 'working' connection, but
122 * we're not interested in the sizes. So, read them but throw
123 * the values away. We need to read the size of the device (a
124 * 64bit integer) plus the reserved fields (128 bytes; should
127 read_all(sock, buf, sizeof(tmp64)+128);
137 int close_connection(int sock, CLOSE_TYPE type) {
138 struct nbd_request req;
142 case CONNECTION_CLOSE_PROPERLY:
143 req.magic=htonl(NBD_REQUEST_MAGIC);
144 req.type=htonl(NBD_CMD_DISC);
145 memcpy(&(req.handle), &(counter), sizeof(counter));
149 if(write(sock, &req, sizeof(req))<0) {
150 snprintf(errstr, errstr_len, "Could not write to socket: %s", strerror(errno));
153 case CONNECTION_CLOSE_FAST:
155 snprintf(errstr, errstr_len, "Could not close socket: %s", strerror(errno));
160 g_critical("Your compiler is on crack!"); /* or I am buggy */
166 int read_packet_check_header(int sock, size_t datasize, long long int curhandle) {
167 struct nbd_reply rep;
171 read_all(sock, &rep, sizeof(rep));
172 rep.magic=ntohl(rep.magic);
173 rep.error=ntohl(rep.error);
174 if(rep.magic!=NBD_REPLY_MAGIC) {
175 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);
180 snprintf(errstr, errstr_len, "Received error from server: %ld (0x%lX). Handle is %lld (0x%llX).", rep.error, *((u64*)rep.handle), *((u64*)rep.handle));
184 read_all(sock, &buf, datasize);
190 int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char close_sock) {
193 struct nbd_request req;
200 signed int do_write=TRUE;
203 if((sock=setup_connection(hostname, port, CONNECTION_TYPE_CLISERV))<0) {
204 g_warning("Could not open socket: %s", errstr);
209 /* Assume the file is at least 4k in size. Not much of a test
210 * this way, but, well. */
214 if((tmp=read_all(sock, &size, sizeof(u64)))<0) {
216 snprintf(errstr, errstr_len, "Could not read from socket: %s", strerror(errno));
219 read_all(sock,&buf,128);
222 snprintf(errstr, errstr_len, "Server closed connection unexpectedly when trying to read size of device in throughput test");
226 req.magic=htonl(NBD_REQUEST_MAGIC);
227 req.type=htonl(NBD_CMD_READ);
229 for(i=0;i+1024<=size;i+=1024) {
231 *((u64*)req.handle)=i;
233 write(sock, &req, sizeof(req));
234 printf("Requests(+): %d\n", ++requests);
241 select(sock+1, &set, NULL, NULL, &tv);
242 if(FD_ISSET(sock, &set)) {
243 /* Okay, there's something ready for
245 read_packet_check_header(sock, 1024, i);
246 printf("Requests(-): %d\n", --requests);
248 } while FD_ISSET(sock, &set);
249 /* Now wait until we can write again or until a second have
250 * passed, whichever comes first*/
255 do_write=select(sock+1,NULL,&set,NULL,&tv);
256 if(!do_write) printf("Select finished\n");
258 snprintf(errstr, errstr_len, "select: %s", strerror(errno));
263 /* Now empty the read buffer */
269 select(sock+1, &set, NULL, NULL, &tv);
270 if(FD_ISSET(sock, &set)) {
271 /* Okay, there's something ready for
273 read_packet_check_header(sock, 1024, i);
274 printf("Requests(-): %d\n", --requests);
280 close_connection(sock, CONNECTION_CLOSE_PROPERLY);
286 int main(int argc, char**argv) {
293 g_message("Not enough arguments");
294 g_message("Usage: %s <hostname> <port>", argv[0]);
298 hostname=g_strdup(argv[1]);
299 p=(strtol(argv[2], NULL, 0));
300 if(p==LONG_MIN||p==LONG_MAX) {
301 g_critical("Could not parse port number: %s", strerror(errno));
306 if(throughput_test(hostname, port, sock, FALSE, TRUE)<0) {
307 g_warning("Could not run throughput test: %s", errstr);