/* * 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 "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; 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; } 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(ctypeh_addr); if((connect(sock, (struct sockaddr *)&addr, sizeof(addr))<0)) { strncpy(errstr, strerror(errno), errstr_len); goto err_open; } if(ctype1024) { speed>>=10; speedchar[0]='K'; } if(speed>1024) { speed>>=10; speedchar[0]='M'; } if(speed>1024) { speed>>=10; speedchar[0]='G'; } g_message("%d: Throughput test complete. Took %.3f seconds to complete, %d%sB/s", (int)getpid(), timespan,speed,speedchar); err_open: if(close_sock) { close_connection(sock, CONNECTION_CLOSE_PROPERLY); } err: return retval; } int main(int argc, char**argv) { gchar *hostname; 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 ", (int)getpid(), argv[0]); g_message("%d: Or: %s -N ", (int)getpid(), argv[0]); 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); } } port=(int)p; if(throughput_test(hostname, port, name, sock, FALSE, TRUE)<0) { g_warning("Could not run throughput test: %s", errstr); exit(EXIT_FAILURE); } return 0; }