From 9434be09a2a906d52ab113d7352dc5bb5d280314 Mon Sep 17 00:00:00 2001 From: Wouter Verhelst Date: Fri, 28 Jan 2011 13:45:48 +0100 Subject: [PATCH] Test request blocks that are larger than 1MB, too. --- Makefile.am | 3 +- nbd-server.c | 2 +- nbd-tester-client.c | 118 +++++++++++++++++++++++++++++++++++++++++++++------ simple_test | 13 +++++- 4 files changed, 118 insertions(+), 18 deletions(-) diff --git a/Makefile.am b/Makefile.am index b7d50c9..20edd8c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 +TESTS = cmd cfg1 cfgmulti cfgnew cfgsize check_PROGRAMS = nbd-tester-client knbd_client_SOURCES = nbd-client.c cliserv.h nbd_client_SOURCES = nbd-client.c cliserv.h @@ -20,3 +20,4 @@ cmd: cfg1: cfgmulti: cfgnew: +cfgsize: diff --git a/nbd-server.c b/nbd-server.c index 51b725c..edb3eee 100644 --- a/nbd-server.c +++ b/nbd-server.c @@ -1394,7 +1394,7 @@ int mainloop(CLIENT *client) { err("Not enough magic."); if (len > BUFSIZE - sizeof(struct nbd_reply)) { currlen = BUFSIZE - sizeof(struct nbd_reply); - msg("INFO: oversized request (this is not a problem)"); + msg2(LOG_INFO, "oversized request (this is not a problem)"); } else { currlen = len; } diff --git a/nbd-tester-client.c b/nbd-tester-client.c index 3169ab1..f97a4ee 100644 --- a/nbd-tester-client.c +++ b/nbd-tester-client.c @@ -23,6 +23,7 @@ */ #include #include +#include #include #include #include @@ -212,6 +213,74 @@ end: return retval; } +int oversize_test(gchar* hostname, int port, char* name, int sock, char sock_is_open, char close_sock) { + int retval=0; + struct nbd_request req; + struct nbd_reply rep; + int request=0; + int i=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) { + g_warning("Could not open socket: %s", errstr); + retval=-1; + goto err; + } + } + req.magic=htonl(NBD_REQUEST_MAGIC); + req.type=htonl(NBD_CMD_READ); + req.len=htonl(1024*1024); + memcpy(&(req.handle),&i,sizeof(i)); + req.from=htonll(i); + write(sock, &req, sizeof(req)); + printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len)); + read_all(sock, &rep, sizeof(struct nbd_reply)); + read_all(sock, &buf, ntohl(req.len)); + if(rep.error) { + printf("Received unexpected error\n"); + retval=-1; + goto err; + } else { + printf("OK\n"); + } + /* 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)); + printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len)); + read_all(sock, &rep, sizeof(struct nbd_reply)); + read_all(sock, &buf, ntohl(req.len)); + if(rep.error) { + printf("Received expected error\n"); + got_err=true; + } else { + printf("OK\n"); + got_err=false; + } + /* ... unless this works, too */ + i++; req.from=htonll(i); + req.len = htonl(ntohl(req.len) << 1); + write(sock, &req, sizeof(req)); + printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len)); + read_all(sock, &rep, sizeof(struct nbd_reply)); + read_all(sock, &buf, ntohl(req.len)); + if(rep.error) { + printf("error\n"); + } else { + printf("OK\n"); + } + if((rep.error && !got_err) || (!rep.error && got_err)) { + printf("Received unexpected error\n"); + retval=-1; + } + err: + return retval; +} + 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]; @@ -315,7 +384,7 @@ int throughput_test(gchar* hostname, int port, char* name, int sock, char sock_i speed>>=10; speedchar[0]='G'; } - g_message("%d: Throughput test complete. Took %.3f seconds to complete, %d%sB/s", (int)getpid(), timespan,speed,speedchar); + g_message("%d: Throughput test complete. Took %.3f seconds to complete, %d%siB/s", (int)getpid(), timespan,speed,speedchar); err_open: if(close_sock) { @@ -325,12 +394,17 @@ err: return retval; } +typedef int (*testfunc)(gchar*, int, char*, int, char, char); + int main(int argc, char**argv) { gchar *hostname; long int p = 0; - int port; char* name = NULL; int sock=0; + char c; + bool want_port = TRUE; + int nonopt=0; + testfunc test = throughput_test; if(argc<3) { g_message("%d: Not enough arguments", (int)getpid()); @@ -339,21 +413,37 @@ int main(int argc, char**argv) { exit(EXIT_FAILURE); } logging(); - hostname=g_strdup(argv[1]); - 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); + while((c=getopt(argc, argv, "-N:o"))>=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; } } - port=(int)p; - if(throughput_test(hostname, port, name, sock, FALSE, TRUE)<0) { - g_warning("Could not run throughput test: %s", errstr); + if(test(hostname, (int)p, name, sock, FALSE, TRUE)<0) { + g_warning("Could not run test: %s", errstr); exit(EXIT_FAILURE); } diff --git a/simple_test b/simple_test index d7a698c..9acb4e1 100755 --- a/simple_test +++ b/simple_test @@ -6,7 +6,7 @@ tmpnam=`mktemp` ulimit -c unlimited # Create a one-meg device -dd if=/dev/zero of=$tmpnam bs=1024 count=1024 +dd if=/dev/zero of=$tmpnam bs=1024 count=4096 echo $1 @@ -21,7 +21,16 @@ case $1 in ./nbd-tester-client 127.0.0.1 11111 retval=$? ;; - + */cfgsize) + # Test oversized requests + ./nbd-server -C /dev/null -p `pwd`/nbd-server.pid 11111 $tmpnam & + # -p only works if nbd-server wasn't compiled with -DNOFORK or + # -DNODAEMON, which I sometimes do for testing and debugging. + PID=$! + sleep 1 + ./nbd-tester-client 127.0.0.1 11111 -o + retval=$? + ;; */cfg1) # Test with export specified in config file cat > nbd-server.conf <