Merge branch 'alex'
authorWouter Verhelst <w@uter.be>
Tue, 17 May 2011 18:28:58 +0000 (20:28 +0200)
committerWouter Verhelst <w@uter.be>
Tue, 17 May 2011 18:28:58 +0000 (20:28 +0200)
Makefile.am
cliserv.h
man/nbd-server.1.in.sgml
nbd-server.c
nbd-tester-client.c
nbd.h
simple_test

index 20edd8c..a99eb37 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 flush
 check_PROGRAMS = nbd-tester-client
 knbd_client_SOURCES = nbd-client.c cliserv.h
 nbd_client_SOURCES = nbd-client.c cliserv.h
@@ -21,3 +21,5 @@ cfg1:
 cfgmulti:
 cfgnew:
 cfgsize:
+write:
+flush:
index 51c8bd1..b32626f 100644 (file)
--- a/cliserv.h
+++ b/cliserv.h
@@ -132,10 +132,6 @@ u64 ntohll(u64 a) {
 #endif
 #define htonll ntohll
 
-/* Flags used between the client and server */
-#define NBD_FLAG_HAS_FLAGS     (1 << 0)        /* Flags are there */
-#define NBD_FLAG_READ_ONLY     (1 << 1)        /* Device is read-only */
-
 #define NBD_DEFAULT_PORT       "10809" /* Port on which named exports are
                                         * served */
 
index 2695086..047fcb6 100644 (file)
@@ -67,6 +67,7 @@ manpage.1: manpage.sgml
       <arg><option>-o <replaceable>section name</replaceable></option></arg>
       <arg><option>-C <replaceable>config file</replaceable></option></arg>
       <arg><option>-M <replaceable>max connections</replaceable></option></arg>
+      <arg><option>-d</option></arg>
     </cmdsynopsis>
   </refsynopsisdiv>
   <refsect1>
@@ -216,6 +217,12 @@ manpage.1: manpage.sgml
        </listitem>
       </varlistentry>
       <varlistentry>
+        <term><option>-d</option></term>
+       <listitem>
+         <para>Do not fork. Useful for debugging.</para>
+       </listitem>
+      </varlistentry>
+      <varlistentry>
        <term><option>host list filename</option></term>
        <listitem>
          <para>This argument should contain a list of IP-addresses
index 5c7cbc0..cd584f0 100644 (file)
@@ -116,6 +116,9 @@ gchar* rungroup=NULL;
 /** whether to export using the old negotiation protocol (port-based) */
 gboolean do_oldstyle=FALSE;
 
+/* Whether we should avoid forking */
+int dontfork = 0;
+
 /** Logging macros, now nothing goes to syslog unless you say ISSERVER */
 #ifdef ISSERVER
 #define msg2(a,b) syslog(a,b)
@@ -413,6 +416,7 @@ SERVER* cmdline(int argc, char *argv[]) {
                {"read-only", no_argument, NULL, 'r'},
                {"multi-file", no_argument, NULL, 'm'},
                {"copy-on-write", no_argument, NULL, 'c'},
+               {"dont-fork", no_argument, NULL, 'd'},
                {"authorize-file", required_argument, NULL, 'l'},
                {"config-file", required_argument, NULL, 'C'},
                {"pid-file", required_argument, NULL, 'p'},
@@ -434,7 +438,7 @@ SERVER* cmdline(int argc, char *argv[]) {
        serve=g_new0(SERVER, 1);
        serve->authname = g_strdup(default_authname);
        serve->virtstyle=VIRT_IPLIT;
-       while((c=getopt_long(argc, argv, "-C:cl:mo:rp:M:", long_options, &i))>=0) {
+       while((c=getopt_long(argc, argv, "-C:cdl:mo:rp:M:", long_options, &i))>=0) {
                switch (c) {
                case 1:
                        /* non-option argument */
@@ -503,6 +507,9 @@ SERVER* cmdline(int argc, char *argv[]) {
                case 'c': 
                        serve->flags |=F_COPYONWRITE;
                        break;
+               case 'd': 
+                       dontfork = 1;
+                       break;
                case 'C':
                        g_free(config_file_pos);
                        config_file_pos=g_strdup(optarg);
@@ -1787,31 +1794,33 @@ int serveloop(GArray* servers) {
                                }
                                msg2(LOG_INFO,"Authorized client") ;
                                pid=g_malloc(sizeof(pid_t));
-#ifndef NOFORK
-                               if ((*pid=fork())<0) {
-                                       msg3(LOG_INFO,"Could not fork (%s)",strerror(errno)) ;
-                                       close(net);
-                                       continue;
-                               }
-                               if (*pid>0) { /* parent */
-                                       close(net);
-                                       g_hash_table_insert(children, pid, pid);
-                                       continue;
-                               }
-                               /* child */
-                               g_hash_table_destroy(children);
-                               for(i=0;i<servers->len;i++) {
-                                       serve=&g_array_index(servers, SERVER, i);
-                                       close(serve->socket);
+
+                               if (!dontfork) {
+                                       if ((*pid=fork())<0) {
+                                               msg3(LOG_INFO,"Could not fork (%s)",strerror(errno)) ;
+                                               close(net);
+                                               continue;
+                                       }
+                                       if (*pid>0) { /* parent */
+                                               close(net);
+                                               g_hash_table_insert(children, pid, pid);
+                                               continue;
+                                       }
+                                       /* child */
+                                       g_hash_table_destroy(children);
+                                       for(i=0;i<servers->len;i++) {
+                                               serve=&g_array_index(servers, SERVER, i);
+                                               close(serve->socket);
+                                       }
+                                       /* FALSE does not free the
+                                          actual data. This is required,
+                                          because the client has a
+                                          direct reference into that
+                                          data, and otherwise we get a
+                                          segfault... */
+                                       g_array_free(servers, FALSE);
                                }
-                               /* FALSE does not free the
-                               actual data. This is required,
-                               because the client has a
-                               direct reference into that
-                               data, and otherwise we get a
-                               segfault... */
-                               g_array_free(servers, FALSE);
-#endif // NOFORK
+
                                msg2(LOG_INFO,"Starting to serve");
                                serveconnection(client);
                                exit(EXIT_SUCCESS);
@@ -1978,7 +1987,7 @@ void setup_servers(GArray* servers) {
  *     is only used to create a PID file of the form
  *     /var/run/nbd-server.&lt;port&gt;.pid; it's not modified in any way.
  **/
-#if !defined(NODAEMON) && !defined(NOFORK)
+#if !defined(NODAEMON)
 void daemonize(SERVER* serve) {
        FILE*pidf;
 
@@ -2007,7 +2016,7 @@ void daemonize(SERVER* serve) {
 }
 #else
 #define daemonize(serve)
-#endif /* !defined(NODAEMON) && !defined(NOFORK) */
+#endif /* !defined(NODAEMON) */
 
 /*
  * Everything beyond this point (in the file) is run in non-daemon mode.
@@ -2146,7 +2155,8 @@ int main(int argc, char *argv[]) {
                g_message("No configured exports; quitting.");
                exit(EXIT_FAILURE);
        }
-       daemonize(serve);
+       if (!dontfork)
+               daemonize(serve);
        setup_servers(servers);
        dousers();
        serveloop(servers);
index beb6d06..1b7ec52 100644 (file)
@@ -56,6 +56,35 @@ typedef enum {
        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;
@@ -94,7 +123,7 @@ static inline int write_all(int f, void *buf, size_t len) {
 #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 setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype, int* serverflags) {
        int sock;
        struct hostent *host;
        struct sockaddr_in addr;
@@ -165,8 +194,10 @@ int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE cty
        READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
        size = ntohll(size);
        uint16_t flags;
-       READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read flags: %s", strerror(errno));
+       READ_ALL_ERRCHK(sock, &flags, sizeof(uint16_t), err_open, "Could not read flags: %s", strerror(errno));
        flags = ntohs(flags);
+       *serverflags = flags;
+       g_warning("Server flags are: %08x", flags);
        READ_ALL_ERRCHK(sock, buf, 124, err_open, "Could not read reserved zeroes: %s", strerror(errno));
        goto end;
 err_open:
@@ -224,25 +255,28 @@ int read_packet_check_header(int sock, size_t datasize, long long int curhandle)
                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 testflags) {
        int retval=0;
        struct nbd_request req;
        struct nbd_reply rep;
        int request=0;
        int i=0;
+       int serverflags = 0;
        pid_t mypid = getpid();
        char buf[((1024*1024)+sizeof(struct nbd_request)/2)<<1];
        bool got_err;
 
        /* This should work */
        if(!sock_is_open) {
-               if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
+               if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
                        g_warning("Could not open socket: %s", errstr);
                        retval=-1;
                        goto err;
@@ -298,33 +332,46 @@ 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 testflags) {
        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;
+       int serverflags = 0;
        size_t tmp;
        signed int do_write=TRUE;
        pid_t mypid = getpid();
 
+
+       if (!(testflags & TEST_WRITE))
+               testflags &= ~TEST_FLUSH;
+
+       memset (writebuf, 'X', sizeof(1024));
        size=0;
        if(!sock_is_open) {
-               if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
+               if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL, &serverflags))<0) {
                        g_warning("Could not open socket: %s", errstr);
                        retval=-1;
                        goto err;
                }
        }
+       if ((testflags & TEST_FLUSH) && ((serverflags & (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))
+                                        != (NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA))) {
+               snprintf(errstr, errstr_len, "Server did not supply flush capability flags");
+               retval = -1;
+               goto err_open;
+       }
        req.magic=htonl(NBD_REQUEST_MAGIC);
-       req.type=htonl(NBD_CMD_READ);
        req.len=htonl(1024);
        if(gettimeofday(&start, NULL)<0) {
                retval=-1;
@@ -333,13 +380,35 @@ int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_i
        }
        for(i=0;i+1024<=size;i+=1024) {
                if(do_write) {
+                       int sendfua = (testflags & TEST_FLUSH) && ((i & 15) == 3);
+                       int sendflush = (testflags & TEST_FLUSH) && ((i & 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);
@@ -350,7 +419,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, (testflags & TEST_WRITE)?0:1024, i)<0) {
                                        retval=-1;
                                        goto err_open;
                                }
@@ -381,7 +450,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, (testflags & TEST_WRITE)?0:1024, i);
                        printf("%d: Requests(-): %d\n", (int)mypid, --requests);
                }
        } while (requests);
@@ -390,21 +459,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(), (testflags & TEST_WRITE)?"write":"read", timespan, speed, speedchar);
 
 err_open:
        if(close_sock) {
@@ -414,7 +483,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;
@@ -424,6 +493,7 @@ int main(int argc, char**argv) {
        int c;
        bool want_port = TRUE;
        int nonopt=0;
+       int testflags=0;
        testfunc test = throughput_test;
 
        if(argc<3) {
@@ -433,7 +503,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:owf"))>=0) {
                switch(c) {
                        case 1:
                                switch(nonopt) {
@@ -459,10 +529,16 @@ int main(int argc, char**argv) {
                        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)<0) {
+       if(test(hostname, (int)p, name, sock, FALSE, TRUE, testflags)<0) {
                g_warning("Could not run test: %s", errstr);
                exit(EXIT_FAILURE);
        }
diff --git a/nbd.h b/nbd.h
index 451f50c..875c215 100644 (file)
--- a/nbd.h
+++ b/nbd.h
 #define NBD_SET_SIZE_BLOCKS    _IO( 0xab, 7 )
 #define NBD_DISCONNECT  _IO( 0xab, 8 )
 #define NBD_SET_TIMEOUT _IO( 0xab, 9 )
+#define NBD_SET_FLAGS _IO( 0xab, 10 )
 
 enum {
        NBD_CMD_READ = 0,
        NBD_CMD_WRITE = 1,
-       NBD_CMD_DISC = 2
+       NBD_CMD_DISC = 2,
+       NBD_CMD_FLUSH = 3
 };
 
+#define NBD_CMD_MASK_COMMAND 0x0000ffff
+#define NBD_CMD_FLAG_FUA (1<<16)
+
+/* values for flags field */
+#define NBD_FLAG_HAS_FLAGS     (1 << 0)        /* Flags are there */
+#define NBD_FLAG_READ_ONLY     (1 << 1)        /* Device is read-only */
+#define NBD_FLAG_SEND_FLUSH    (1 << 2)        /* Send FLUSH */
+#define NBD_FLAG_SEND_FUA      (1 << 3)        /* Send FUA (Force Unit Access) */
+#define NBD_FLAG_ROTATIONAL    (1 << 4)        /* Use elevator algorithm - rotational media */
+
 #define nbd_cmd(req) ((req)->cmd[0])
 
 /* userspace doesn't need the nbd_device structure */
index 9acb4e1..a01d3dc 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,32 @@ 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=$?
+       ;;
+       */flush)
+               # 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 -f
+               retval=$?
+       ;;
        *)
                echo "E: unknown test $1"
                exit 1