Merge branch 'master' of git://nbd.git.sourceforge.net/gitroot/nbd/nbd
authorAlex Bligh <alex@alex.org.uk>
Tue, 17 May 2011 10:14:35 +0000 (11:14 +0100)
committerAlex Bligh <alex@alex.org.uk>
Tue, 17 May 2011 10:14:35 +0000 (11:14 +0100)
Makefile.am
nbd-tester-client.c
simple_test

index 20edd8c..f2da3e6 100644 (file)
@@ -3,7 +3,7 @@ bin_PROGRAMS = nbd-server
 sbin_PROGRAMS = @NBD_CLIENT_NAME@
 EXTRA_PROGRAMS = nbd-client knbd-client
 TESTS_ENVIRONMENT=$(srcdir)/simple_test
-TESTS = cmd cfg1 cfgmulti cfgnew cfgsize
+TESTS = cmd cfg1 cfgmulti cfgnew cfgsize write
 check_PROGRAMS = nbd-tester-client
 knbd_client_SOURCES = nbd-client.c cliserv.h
 nbd_client_SOURCES = nbd-client.c cliserv.h
@@ -21,3 +21,4 @@ cfg1:
 cfgmulti:
 cfgnew:
 cfgsize:
+write:
index d52b687..7c1cb75 100644 (file)
@@ -56,6 +56,32 @@ typedef enum {
        CONNECTION_CLOSE_FAST,
 } CLOSE_TYPE;
 
+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;
@@ -72,9 +98,28 @@ static inline int read_all(int f, void *buf, size_t len) {
        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 sock;
        struct hostent *host;
@@ -131,21 +176,24 @@ int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE cty
                goto end;
        }
        /* flags */
-       READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read flags: %s", strerror(errno));
+       READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read reserved field: %s", strerror(errno));
        /* reserved field */
-       write(sock, &tmp32, sizeof(tmp32));
+       WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write reserved field: %s", strerror(errno));
        /* magic */
        tmp64 = htonll(opts_magic);
-       write(sock, &tmp64, sizeof(tmp64));
+       WRITE_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not write magic: %s", strerror(errno));
        /* name */
        tmp32 = htonl(NBD_OPT_EXPORT_NAME);
-       write(sock, &tmp32, sizeof(tmp32));
+       WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write option: %s", strerror(errno));
        tmp32 = htonl((uint32_t)strlen(name));
-       write(sock, &tmp32, sizeof(tmp32));
-       write(sock, name, strlen(name));
+       WRITE_ALL_ERRCHK(sock, &tmp32, sizeof(tmp32), err_open, "Could not write name length: %s", strerror(errno));
+       WRITE_ALL_ERRCHK(sock, name, strlen(name), err_open, "Could not write name:: %s", strerror(errno));
        READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
        size = ntohll(size);
-       READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t)+124, err_open, "Could not read reserved zeroes: %s", strerror(errno));
+       uint16_t flags;
+       READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read flags: %s", strerror(errno));
+       flags = ntohs(flags);
+       READ_ALL_ERRCHK(sock, buf, 124, err_open, "Could not read reserved zeroes: %s", strerror(errno));
        goto end;
 err_open:
        close(sock);
@@ -193,22 +241,24 @@ int read_packet_check_header(int sock, size_t datasize, long long int curhandle)
        rep.magic=ntohl(rep.magic);
        rep.error=ntohl(rep.error);
        if(rep.magic!=NBD_REPLY_MAGIC) {
-               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);
+               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", (long long int)curhandle, (long long unsigned int)curhandle, (long long int)*((u64*)rep.handle), (long long unsigned int)*((u64*)rep.handle), (long unsigned int)rep.magic, (long unsigned int)NBD_REPLY_MAGIC);
                retval=-1;
                goto end;
        }
        if(rep.error) {
-               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));
+               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)), (long long unsigned int)*((u64*)rep.handle));
                retval=-1;
                goto end;
        }
-       READ_ALL_ERR_RT(sock, &buf, datasize, end, -1, "Could not read data: %s", strerror(errno));
+       if (datasize)
+               READ_ALL_ERR_RT(sock, &buf, datasize, end, -1, "Could not read data: %s", strerror(errno));
 
 end:
        return retval;
 }
 
-int oversize_test(gchar* hostname, int port, char* name, int sock, char sock_is_open, char close_sock) {
+int oversize_test(gchar* hostname, int port, char* name, int sock,
+                 char sock_is_open, char close_sock, int write) {
        int retval=0;
        struct nbd_request req;
        struct nbd_reply rep;
@@ -231,7 +281,7 @@ int oversize_test(gchar* hostname, int port, char* name, int sock, char sock_is_
        req.len=htonl(1024*1024);
        memcpy(&(req.handle),&i,sizeof(i));
        req.from=htonll(i);
-       write(sock, &req, sizeof(req));
+       WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
        printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
        READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
        READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
@@ -245,7 +295,7 @@ int oversize_test(gchar* hostname, int port, char* name, int sock, char sock_is_
        /* This probably should not work */
        i++; req.from=htonll(i);
        req.len = htonl(ntohl(req.len) + sizeof(struct nbd_request) / 2);
-       write(sock, &req, sizeof(req));
+       WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
        printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
        READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
        READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
@@ -259,7 +309,7 @@ int oversize_test(gchar* hostname, int port, char* name, int sock, char sock_is_
        /* ... unless this works, too */
        i++; req.from=htonll(i);
        req.len = htonl(ntohl(req.len) << 1);
-       write(sock, &req, sizeof(req));
+       WRITE_ALL_ERR_RT(sock, &req, sizeof(req), err, -1, "Could not write request: %s", strerror(errno));
        printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
        READ_ALL_ERR_RT(sock, &rep, sizeof(struct nbd_reply), err, -1, "Could not read reply header: %s", strerror(errno));
        READ_ALL_ERR_RT(sock, &buf, ntohl(req.len), err, -1, "Could not read data: %s", strerror(errno));
@@ -276,23 +326,26 @@ int oversize_test(gchar* hostname, int port, char* name, int sock, char sock_is_
        return retval;
 }
 
-int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_is_open, char close_sock) {
+int throughput_test(gchar* hostname, int port, char* name, int sock,
+                   char sock_is_open, char close_sock, int write) {
        long long int i;
        char buf[1024];
+       char writebuf[1024];
        struct nbd_request req;
        int requests=0;
        fd_set set;
        struct timeval tv;
        struct timeval start;
        struct timeval stop;
-       float timespan;
-       int speed;
+       double timespan;
+       double speed;
        char speedchar[2] = { '\0', '\0' };
        int retval=0;
        size_t tmp;
        signed int do_write=TRUE;
        pid_t mypid = getpid();
 
+       memset (writebuf, 'X', sizeof(1024));
        size=0;
        if(!sock_is_open) {
                if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
@@ -302,7 +355,7 @@ int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_i
                }
        }
        req.magic=htonl(NBD_REQUEST_MAGIC);
-       req.type=htonl(NBD_CMD_READ);
+       req.type=htonl(write?NBD_CMD_WRITE:NBD_CMD_READ);
        req.len=htonl(1024);
        if(gettimeofday(&start, NULL)<0) {
                retval=-1;
@@ -313,7 +366,16 @@ int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_i
                if(do_write) {
                        memcpy(&(req.handle),&i,sizeof(i));
                        req.from=htonll(i);
-                       write(sock, &req, sizeof(req));
+                       if (write_all(sock, &req, sizeof(req)) <0) {
+                               retval=-1;
+                               goto err_open;
+                       }
+                       if (write) {
+                               if (write_all(sock, writebuf, 1024) <0) {
+                                       retval=-1;
+                                       goto err_open;
+                               }
+                       }
                        printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
                }
                do {
@@ -325,7 +387,7 @@ int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_i
                        if(FD_ISSET(sock, &set)) {
                                /* Okay, there's something ready for
                                 * reading here */
-                               if(read_packet_check_header(sock, 1024, i)<0) {
+                               if(read_packet_check_header(sock, write?0:1024, i)<0) {
                                        retval=-1;
                                        goto err_open;
                                }
@@ -356,7 +418,7 @@ int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_i
                if(FD_ISSET(sock, &set)) {
                        /* Okay, there's something ready for
                         * reading here */
-                       read_packet_check_header(sock, 1024, i);
+                       read_packet_check_header(sock, write?0:1024, i);
                        printf("%d: Requests(-): %d\n", (int)mypid, --requests);
                }
        } while (requests);
@@ -365,21 +427,21 @@ int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_i
                snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
                goto err_open;
        }
-       timespan=(float)(stop.tv_sec-start.tv_sec+(stop.tv_usec-start.tv_usec))/(float)1000000;
-       speed=(int)(size/timespan);
+       timespan=timeval_diff_to_double(&stop, &start);
+       speed=size/timespan;
        if(speed>1024) {
-               speed>>=10;
+               speed=speed/1024.0;
                speedchar[0]='K';
        }
        if(speed>1024) {
-               speed>>=10;
+               speed=speed/1024.0;
                speedchar[0]='M';
        }
        if(speed>1024) {
-               speed>>=10;
+               speed=speed/1024.0;
                speedchar[0]='G';
        }
-       g_message("%d: Throughput test complete. Took %.3f seconds to complete, %d%siB/s", (int)getpid(), timespan,speed,speedchar);
+       g_message("%d: Throughput %s test complete. Took %.3f seconds to complete, %.3f%sib/s", (int)getpid(), write?"write":"read", timespan, speed, speedchar);
 
 err_open:
        if(close_sock) {
@@ -389,7 +451,7 @@ err:
        return retval;
 }
 
-typedef int (*testfunc)(gchar*, int, char*, int, char, char);
+typedef int (*testfunc)(gchar*, int, char*, int, char, char, int);
 
 int main(int argc, char**argv) {
        gchar *hostname;
@@ -399,6 +461,7 @@ int main(int argc, char**argv) {
        int c;
        bool want_port = TRUE;
        int nonopt=0;
+       int write=0;
        testfunc test = throughput_test;
 
        if(argc<3) {
@@ -408,7 +471,7 @@ int main(int argc, char**argv) {
                exit(EXIT_FAILURE);
        }
        logging();
-       while((c=getopt(argc, argv, "-N:o"))>=0) {
+       while((c=getopt(argc, argv, "-N:ow"))>=0) {
                switch(c) {
                        case 1:
                                switch(nonopt) {
@@ -434,10 +497,13 @@ int main(int argc, char**argv) {
                        case 'o':
                                test=oversize_test;
                                break;
+                       case 'w':
+                               write=1;
+                               break;
                }
        }
 
-       if(test(hostname, (int)p, name, sock, FALSE, TRUE)<0) {
+       if(test(hostname, (int)p, name, sock, FALSE, TRUE, write)<0) {
                g_warning("Could not run test: %s", errstr);
                exit(EXIT_FAILURE);
        }
index 9acb4e1..53efb07 100755 (executable)
@@ -6,7 +6,7 @@ tmpnam=`mktemp`
 ulimit -c unlimited
 
 # Create a one-meg device
-dd if=/dev/zero of=$tmpnam bs=1024 count=4096
+dd if=/dev/zero of=$tmpnam bs=1024 count=4096 >/dev/null 2>&1
 
 echo $1
 
@@ -99,6 +99,19 @@ EOF
                ./nbd-tester-client localhost -N export1
                retval=$?
        ;;
+       */write)
+               # Test new-style exports
+               cat >nbd-server.conf <<EOF
+[generic]
+[export1]
+       exportname = $tmpnam
+EOF
+               ./nbd-server -C nbd-server.conf -p `pwd`/nbd-server.pid &
+               PID=$!
+               sleep 1
+               ./nbd-tester-client localhost -N export1 -w
+               retval=$?
+       ;;
        *)
                echo "E: unknown test $1"
                exit 1