/* * Test client to test the NBD server. Doesn't do anything useful, except * checking that the server does, actually, work. * * Note that the only 'real' test is to check the client against a kernel. If * it works here but does not work in the kernel, then that's most likely a bug * in this program and/or in nbd-server. * * Copyright(c) 2006 Wouter Verhelst * * This program is Free Software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, in version 2. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include #include #include #include #include "config.h" #include "lfs.h" #define MY_NAME "nbd-tester-client" #include "cliserv.h" #include #include static gchar errstr[1024]; const static int errstr_len=1024; static uint64_t size; typedef enum { CONNECTION_TYPE_NONE, CONNECTION_TYPE_CONNECT, CONNECTION_TYPE_INIT_PASSWD, CONNECTION_TYPE_CLISERV, CONNECTION_TYPE_FULL, } CONNECTION_TYPE; typedef enum { CONNECTION_CLOSE_PROPERLY, CONNECTION_CLOSE_FAST, } CLOSE_TYPE; #define TEST_WRITE (1<<0) #define TEST_FLUSH (1<<1) int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) { if (x->tv_usec < y->tv_usec) { int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; y->tv_usec -= 1000000 * nsec; y->tv_sec += nsec; } if (x->tv_usec - y->tv_usec > 1000000) { int nsec = (x->tv_usec - y->tv_usec) / 1000000; y->tv_usec += 1000000 * nsec; y->tv_sec -= nsec; } result->tv_sec = x->tv_sec - y->tv_sec; result->tv_usec = x->tv_usec - y->tv_usec; return x->tv_sec < y->tv_sec; } double timeval_diff_to_double (struct timeval * x, struct timeval * y) { struct timeval r; timeval_subtract(&r, x, y); return r.tv_sec * 1.0 + r.tv_usec/1000000.0; } static inline int read_all(int f, void *buf, size_t len) { ssize_t res; size_t retval=0; while(len>0) { if((res=read(f, buf, len)) <=0) { snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno)); return -1; } len-=res; buf+=res; retval+=res; } return retval; } static inline int write_all(int f, void *buf, size_t len) { ssize_t res; size_t retval=0; while(len>0) { if((res=write(f, buf, len)) <=0) { snprintf(errstr, errstr_len, "Write failed: %s", strerror(errno)); return -1; } len-=res; buf+=res; retval+=res; } return retval; } #define READ_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; } #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; } #define WRITE_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; } #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; } int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype, int* serverflags) { int sock; struct hostent *host; struct sockaddr_in addr; char buf[256]; uint64_t mymagic = (name ? opts_magic : cliserv_magic); u64 tmp64; uint32_t tmp32 = 0; sock=0; if(ctypeh_addr); if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) { strncpy(errstr, strerror(errno), errstr_len); goto err_open; } if(ctype>10) & 15) == 3); int sendflush = (testflags & TEST_FLUSH) && (((i>>10) & 15) == 11); req.type=htonl((testflags & TEST_WRITE)?NBD_CMD_WRITE:NBD_CMD_READ); if (sendfua) req.type = htonl(NBD_CMD_WRITE | NBD_CMD_FLAG_FUA); memcpy(&(req.handle),&i,sizeof(i)); req.from=htonll(i); if (write_all(sock, &req, sizeof(req)) <0) { retval=-1; goto err_open; } if (testflags & TEST_WRITE) { if (write_all(sock, writebuf, 1024) <0) { retval=-1; goto err_open; } } printf("%d: Requests(+): %d\n", (int)mypid, ++requests); if (sendflush) { long long int j = i ^ (1LL<<63); req.type = htonl(NBD_CMD_FLUSH); memcpy(&(req.handle),&j,sizeof(j)); req.from=0; if (write_all(sock, &req, sizeof(req)) <0) { retval=-1; goto err_open; } printf("%d: Requests(+): %d\n", (int)mypid, ++requests); } } do { FD_ZERO(&set); FD_SET(sock, &set); tv.tv_sec=0; tv.tv_usec=0; select(sock+1, &set, NULL, NULL, &tv); if(FD_ISSET(sock, &set)) { /* Okay, there's something ready for * reading here */ if(read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i)<0) { retval=-1; goto err_open; } printf("%d: Requests(-): %d\n", (int)mypid, --requests); } } while FD_ISSET(sock, &set); /* Now wait until we can write again or until a second have * passed, whichever comes first*/ FD_ZERO(&set); FD_SET(sock, &set); tv.tv_sec=1; tv.tv_usec=0; do_write=select(sock+1,NULL,&set,NULL,&tv); if(!do_write) printf("Select finished\n"); if(do_write<0) { snprintf(errstr, errstr_len, "select: %s", strerror(errno)); retval=-1; goto err_open; } } /* Now empty the read buffer */ do { FD_ZERO(&set); FD_SET(sock, &set); tv.tv_sec=0; tv.tv_usec=0; select(sock+1, &set, NULL, NULL, &tv); if(FD_ISSET(sock, &set)) { /* Okay, there's something ready for * reading here */ read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i); printf("%d: Requests(-): %d\n", (int)mypid, --requests); } } while (requests); if(gettimeofday(&stop, NULL)<0) { retval=-1; snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno)); goto err_open; } timespan=timeval_diff_to_double(&stop, &start); speed=size/timespan; if(speed>1024) { speed=speed/1024.0; speedchar[0]='K'; } if(speed>1024) { speed=speed/1024.0; speedchar[0]='M'; } if(speed>1024) { speed=speed/1024.0; speedchar[0]='G'; } g_message("%d: Throughput %s test (%s flushes) complete. Took %.3f seconds to complete, %.3f%sib/s", (int)getpid(), (testflags & TEST_WRITE)?"write":"read", (testflags & TEST_FLUSH)?"with":"without", timespan, speed, speedchar); err_open: if(close_sock) { close_connection(sock, CONNECTION_CLOSE_PROPERLY); } err: return retval; } typedef int (*testfunc)(gchar*, int, char*, int, char, char, int); int main(int argc, char**argv) { gchar *hostname; long int p = 0; char* name = NULL; int sock=0; int c; bool want_port = TRUE; int nonopt=0; int testflags=0; testfunc test = throughput_test; if(argc<3) { g_message("%d: Not enough arguments", (int)getpid()); g_message("%d: Usage: %s ", (int)getpid(), argv[0]); g_message("%d: Or: %s -N ", (int)getpid(), argv[0]); exit(EXIT_FAILURE); } logging(); while((c=getopt(argc, argv, "-N:owf"))>=0) { switch(c) { case 1: switch(nonopt) { case 0: hostname=g_strdup(optarg); nonopt++; break; case 1: if(want_port) p=(strtol(argv[2], NULL, 0)); if(p==LONG_MIN||p==LONG_MAX) { g_critical("Could not parse port number: %s", strerror(errno)); exit(EXIT_FAILURE); } break; } break; case 'N': name=g_strdup(optarg); p = 10809; want_port = false; break; case 'o': test=oversize_test; break; case 'w': testflags|=TEST_WRITE; break; case 'f': testflags|=TEST_FLUSH; break; } } if(test(hostname, (int)p, name, sock, FALSE, TRUE, testflags)<0) { g_warning("Could not run test: %s", errstr); exit(EXIT_FAILURE); } return 0; }