*/
#include <stdlib.h>
#include <stdio.h>
+#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
CONNECTION_CLOSE_FAST,
} CLOSE_TYPE;
-inline int read_all(int f, void *buf, size_t len) {
+static inline int read_all(int f, void *buf, size_t len) {
ssize_t res;
size_t retval=0;
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; }
+
int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype) {
int sock;
struct hostent *host;
}
if(ctype<CONNECTION_TYPE_INIT_PASSWD)
goto end;
- if(read_all(sock, buf, strlen(INIT_PASSWD))<0) {
- snprintf(errstr, errstr_len, "Could not read INIT_PASSWD: %s",
- strerror(errno));
- goto err_open;
- }
+ READ_ALL_ERRCHK(sock, buf, strlen(INIT_PASSWD), err_open, "Could not read INIT_PASSWD: %s", strerror(errno));
if(strlen(buf)==0) {
snprintf(errstr, errstr_len, "Server closed connection");
goto err_open;
}
if(ctype<CONNECTION_TYPE_CLISERV)
goto end;
- if(read_all(sock, &tmp64, sizeof(tmp64))<0) {
- snprintf(errstr, errstr_len, "Could not read cliserv_magic: %s",
- strerror(errno));
- goto err_open;
- }
+ READ_ALL_ERRCHK(sock, &tmp64, sizeof(tmp64), err_open, "Could not read cliserv_magic: %s", strerror(errno));
tmp64=ntohll(tmp64);
if(tmp64 != mymagic) {
strncpy(errstr, "mymagic does not match", errstr_len);
if(ctype<CONNECTION_TYPE_FULL)
goto end;
if(!name) {
- read_all(sock, &size, sizeof(size));
+ READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
size=ntohll(size);
- read_all(sock, buf, 128);
+ READ_ALL_ERRCHK(sock, buf, 128, err_open, "Could not read data: %s", strerror(errno));
goto end;
}
/* flags */
- read_all(sock, buf, sizeof(uint16_t));
+ READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t), err_open, "Could not read flags: %s", strerror(errno));
/* reserved field */
write(sock, &tmp32, sizeof(tmp32));
/* magic */
tmp32 = htonl((uint32_t)strlen(name));
write(sock, &tmp32, sizeof(tmp32));
write(sock, name, strlen(name));
- read_all(sock, &size, sizeof(size));
+ READ_ALL_ERRCHK(sock, &size, sizeof(size), err_open, "Could not read size: %s", strerror(errno));
size = ntohll(size);
- read_all(sock, buf, sizeof(uint16_t)+124);
+ READ_ALL_ERRCHK(sock, buf, sizeof(uint16_t)+124, err_open, "Could not read reserved zeroes: %s", strerror(errno));
goto end;
err_open:
close(sock);
int retval=0;
char buf[datasize];
- read_all(sock, &rep, sizeof(rep));
+ READ_ALL_ERR_RT(sock, &rep, sizeof(rep), end, -1, "Could not read reply header: %s", strerror(errno));
rep.magic=ntohl(rep.magic);
rep.error=ntohl(rep.error);
if(rep.magic!=NBD_REPLY_MAGIC) {
retval=-1;
goto end;
}
- read_all(sock, &buf, 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 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_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));
+ if(rep.error) {
+ snprintf(errstr, errstr_len, "Received unexpected error: %d", rep.error);
+ 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_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));
+ 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_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));
+ 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];
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) {
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;
+ int c;
+ bool want_port = TRUE;
+ int nonopt=0;
+ testfunc test = throughput_test;
if(argc<3) {
g_message("%d: Not enough arguments", (int)getpid());
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);
}