Extend test suite and fix named exports
authorWouter Verhelst <w@uter.be>
Mon, 9 Aug 2010 17:52:00 +0000 (13:52 -0400)
committerWouter Verhelst <w@uter.be>
Mon, 9 Aug 2010 17:52:00 +0000 (13:52 -0400)
Makefile.am
nbd-server.c
nbd-tester-client.c
simple_test

index fafbfac..fed359c 100644 (file)
@@ -1,7 +1,7 @@
 bin_PROGRAMS = nbd-server
 EXTRA_PROGRAMS = nbd-client knbd-client
 TESTS_ENVIRONMENT=$(srcdir)/simple_test
-TESTS = cmd cfg1 cfgmulti
+TESTS = cmd cfg1 cfgmulti cfgnew
 check_PROGRAMS = nbd-tester-client
 knbd_client_SOURCES = nbd-client.c cliserv.h
 nbd_client_SOURCES = nbd-client.c cliserv.h
@@ -28,3 +28,4 @@ dist-hook:
 cmd:
 cfg1:
 cfgmulti:
+cfgnew:
index ea446db..844e416 100644 (file)
@@ -1232,14 +1232,17 @@ CLIENT* negotiate(int net, CLIENT *client, GArray* servers) {
 
        memset(zeros, '\0', sizeof(zeros));
        if(!client || !client->modern) {
+               /* common */
                if (write(net, INIT_PASSWD, 8) < 0) {
                        err_nonfatal("Negotiation failed: %m");
                        if(client)
                                exit(EXIT_FAILURE);
                }
-               if(client && client->modern) {
+               if(!client || client->modern) {
+                       /* modern */
                        magic = htonll(opts_magic);
                } else {
+                       /* oldstyle */
                        magic = htonll(cliserv_magic);
                }
                if (write(net, &magic, sizeof(magic)) < 0) {
@@ -1249,6 +1252,7 @@ CLIENT* negotiate(int net, CLIENT *client, GArray* servers) {
                }
        }
        if(!client) {
+               /* modern */
                uint32_t reserved;
                uint32_t opt;
                uint32_t namelen;
@@ -1274,7 +1278,7 @@ CLIENT* negotiate(int net, CLIENT *client, GArray* servers) {
                read(net, &namelen, sizeof(namelen));
                namelen = ntohl(namelen);
                name = malloc(namelen+1);
-               name[namelen+1]=0;
+               name[namelen]=0;
                read(net, name, namelen);
                for(i=0; i<servers->len; i++) {
                        SERVER* serve = &(g_array_index(servers, SERVER, i));
@@ -1287,23 +1291,28 @@ CLIENT* negotiate(int net, CLIENT *client, GArray* servers) {
                                return client;
                        }
                }
+               return NULL;
        }
+       /* common */
        size_host = htonll((u64)(client->exportsize));
        if (write(net, &size_host, 8) < 0)
                err("Negotiation failed: %m");
        if (client->server->flags & F_READONLY)
                flags |= NBD_FLAG_READ_ONLY;
        if (!client->modern) {
+               /* oldstyle */
                flags = htonl(flags);
                if (write(client->net, &flags, 4) < 0)
                        err("Negotiation failed: %m");
        } else {
+               /* modern */
                smallflags = (uint16_t)(flags & ~((uint16_t)0));
                smallflags = htons(smallflags);
                if (write(client->net, &smallflags, sizeof(smallflags)) < 0) {
                        err("Negotiation failed: %m");
                }
        }
+       /* common */
        if (write(client->net, zeros, 124) < 0)
                err("Negotiation failed: %m");
        return NULL;
@@ -1707,6 +1716,7 @@ int serveloop(GArray* servers) {
                                if(!client) {
                                        err_nonfatal("negotiation failed");
                                        close(net);
+                                       net=0;
                                }
                        }
                        for(i=0;i<servers->len && !net;i++) {
index 8572f29..3169ab1 100644 (file)
@@ -40,6 +40,8 @@
 static gchar errstr[1024];
 const static int errstr_len=1024;
 
+static uint64_t size;
+
 typedef enum {
        CONNECTION_TYPE_NONE,
        CONNECTION_TYPE_CONNECT,
@@ -69,12 +71,14 @@ inline int read_all(int f, void *buf, size_t len) {
        return retval;
 }
 
-int setup_connection(gchar *hostname, int port, CONNECTION_TYPE ctype) {
+int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype) {
        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(ctype<CONNECTION_TYPE_CONNECT)
@@ -118,20 +122,34 @@ int setup_connection(gchar *hostname, int port, CONNECTION_TYPE ctype) {
                goto err_open;
        }
        tmp64=ntohll(tmp64);
-       if(tmp64 != cliserv_magic) {
-               strncpy(errstr, "cliserv_magic does not match", errstr_len);
+       if(tmp64 != mymagic) {
+               strncpy(errstr, "mymagic does not match", errstr_len);
                goto err_open;
        }
        if(ctype<CONNECTION_TYPE_FULL)
                goto end;
-       /* The information we get now contains information on sizes. If
-        * we're here, that means we want a 'working' connection, but
-        * we're not interested in the sizes. So, read them but throw
-        * the values away. We need to read the size of the device (a
-        * 64bit integer) plus the reserved fields (128 bytes; should
-        * all be zeroes).
-        */
-       read_all(sock, buf, sizeof(tmp64)+128);
+       if(!name) {
+               read_all(sock, &size, sizeof(size));
+               size=ntohll(size);
+               read_all(sock, buf, 128);
+               goto end;
+       }
+       /* flags */
+       read_all(sock, buf, sizeof(uint16_t));
+       /* reserved field */
+       write(sock, &tmp32, sizeof(tmp32));
+       /* magic */
+       tmp64 = htonll(opts_magic);
+       write(sock, &tmp64, sizeof(tmp64));
+       /* name */
+       tmp32 = htonl(NBD_OPT_EXPORT_NAME);
+       write(sock, &tmp32, sizeof(tmp32));
+       tmp32 = htonl((uint32_t)strlen(name));
+       write(sock, &tmp32, sizeof(tmp32));
+       write(sock, name, strlen(name));
+       read_all(sock, &size, sizeof(size));
+       size = ntohll(size);
+       read_all(sock, buf, sizeof(uint16_t)+124);
        goto end;
 err_open:
        close(sock);
@@ -194,11 +212,10 @@ end:
        return retval;
 }
 
-int throughput_test(gchar* hostname, int port, 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) {
        long long int i;
        char buf[1024];
        struct nbd_request req;
-       u64 size;
        int requests=0;
        fd_set set;
        struct timeval tv;
@@ -214,28 +231,12 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char
 
        size=0;
        if(!sock_is_open) {
-               if((sock=setup_connection(hostname, port, CONNECTION_TYPE_CLISERV))<0) {
+               if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
                        g_warning("Could not open socket: %s", errstr);
                        retval=-1;
                        goto err;
                }
-       } else {
-               /* Assume the file is at least 4k in size. Not much of a test
-                * this way, but, well. */
-               size=4096;
-       }
-       if((tmp=read_all(sock, &size, sizeof(u64)))<0) {
-               retval=-1;
-               snprintf(errstr, errstr_len, "Could not read from socket: %s", strerror(errno));
-               goto err_open;
        }
-       if(tmp==0) {
-               retval=-1;
-               snprintf(errstr, errstr_len, "Server closed connection unexpectedly when trying to read size of device in throughput test");
-               goto err;
-       }
-       read_all(sock,&buf,128);
-       size=ntohll(size);
        req.magic=htonl(NBD_REQUEST_MAGIC);
        req.type=htonl(NBD_CMD_READ);
        req.len=htonl(1024);
@@ -326,25 +327,32 @@ err:
 
 int main(int argc, char**argv) {
        gchar *hostname;
-       long int p;
+       long int p = 0;
        int port;
+       char* name = NULL;
        int sock=0;
 
        if(argc<3) {
                g_message("%d: Not enough arguments", (int)getpid());
                g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
+               g_message("%d: Or: %s <hostname> -N <exportname>", (int)getpid(), argv[0]);
                exit(EXIT_FAILURE);
        }
        logging();
        hostname=g_strdup(argv[1]);
-       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);
+       if(!strncmp(argv[2], "-N", 2)) {
+               name = g_strdup(argv[3]);
+               p = 10809;
+       } else {
+               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);
+               }
        }
        port=(int)p;
 
-       if(throughput_test(hostname, port, sock, FALSE, TRUE)<0) {
+       if(throughput_test(hostname, port, name, sock, FALSE, TRUE)<0) {
                g_warning("Could not run throughput test: %s", errstr);
                exit(EXIT_FAILURE);
        }
index 9bd3d07..d7a698c 100755 (executable)
@@ -3,6 +3,8 @@
 
 tmpnam=`mktemp`
 
+ulimit -c unlimited
+
 # Create a one-meg device
 dd if=/dev/zero of=$tmpnam bs=1024 count=1024
 
@@ -50,6 +52,7 @@ EOF
        exportname = $tmpnam
        port = 11114
        readonly = true
+       listenaddr = 127.0.0.1
 EOF
                ./nbd-server -C nbd-server.conf -p `pwd`/nbd-server.pid &
                PID=$!
@@ -65,7 +68,10 @@ EOF
                        else
                                kill $PID
                        fi
-                       rm -f $tmpnam nbd-server.conf
+                       if [ -z "$2" ]
+                       then
+                               rm -f $tmpnam nbd-server.conf
+                       fi
                        exit $retval
                fi
                ./nbd-tester-client localhost 11114
@@ -77,31 +83,12 @@ EOF
 [generic]
 [export1]
        exportname = $tmpnam
-       copyonwrite = true
-       listenaddr = 127.0.0.1
-[export2]
-       exportname = $tmpnam
-       readonly = true
 EOF
                ./nbd-server -C nbd-server.conf -p `pwd`/nbd-server.pid &
                PID=$!
                sleep 1
                ./nbd-tester-client localhost -N export1
                retval=$?
-               if [ $retval -ne 0 ]
-               then
-                       if [ -f nbd-server.pid ]
-                       then
-                               kill `cat nbd-server.pid`
-                               rm -f nbd-server.pid
-                       else
-                               kill $PID
-                       fi
-                       rm -f $tmpnam nbd-server.conf
-                       exit $retval
-               fi
-               ./nbd-tester-client localhost -N export2
-               retval=$?
        ;;
        *)
                echo "E: unknown test $1"
@@ -115,8 +102,11 @@ then
 else
        kill $PID
 fi
+if [ -z "$2" ]
+then
+       rm -f $tmpnam nbd-server.conf
+fi
 if [ $retval -ne 0 ]
 then
        exit $retval
 fi
-rm -f $tmpnam nbd-server.conf