Compare commits
3 Commits
release-to
...
change-mma
Author | SHA1 | Date | |
---|---|---|---|
![]() |
29306a716e | ||
![]() |
0ba90b39a3 | ||
![]() |
7f98f6ef9e |
@@ -1,24 +0,0 @@
|
||||
# Contribution guide
|
||||
|
||||
The code is formatted using the K&R style of "indent".
|
||||
|
||||
```
|
||||
indent -kr <files go here>
|
||||
```
|
||||
|
||||
The C unit tests have also been indented in the same way, but manually adjsted
|
||||
such that the functions follow the normal libcheck layout.
|
||||
|
||||
```c
|
||||
START_TEST( ... ) {
|
||||
|
||||
|
||||
}
|
||||
END TEST
|
||||
```
|
||||
|
||||
Indent tends to mangle the `END_TEST` macro, so that will need adjusting if
|
||||
`indent` is run over the test files again.
|
||||
|
||||
|
||||
|
6
Makefile
6
Makefile
@@ -12,6 +12,11 @@ else
|
||||
CFLAGS_EXTRA=-O2
|
||||
endif
|
||||
|
||||
NO_MSYNC=1
|
||||
ifdef NO_MSYNC
|
||||
CFLAGS_EXTRA += -DNO_MSYNC
|
||||
endif
|
||||
|
||||
CFLAGS_EXTRA += -fPIC --std=gnu99
|
||||
LDFLAGS_EXTRA += -Wl,--relax,--gc-sections -L$(LIB) -Wl,-rpath-link,$(LIB)
|
||||
|
||||
@@ -109,6 +114,7 @@ install:
|
||||
clean:
|
||||
rm -rf build/*
|
||||
|
||||
|
||||
.PHONY: clean objs check_objs all server proxy check_bins check doc build test acceptance
|
||||
|
||||
# Include extra dependencies at the end, NOT before 'all'
|
||||
|
30
debian/changelog
vendored
30
debian/changelog
vendored
@@ -1,36 +1,10 @@
|
||||
flexnbd (0.3.0) stable; urgency=medium
|
||||
flexnbd (0.1.8) UNRELEASED; urgency=medium
|
||||
|
||||
* Force a msync after every write, ignoring FUA flag, or lack thereof (!51).
|
||||
|
||||
-- Patrick J Cherry <patrick@bytemark.co.uk> Tue, 24 Apr 2018 12:05:43 +0100
|
||||
|
||||
flexnbd (0.2.0) stable; urgency=medium
|
||||
|
||||
[ James Carter ]
|
||||
* Set TCP keepalive on sockets so broken connections are reaped (#33, !33,
|
||||
!36)
|
||||
* Add a context to logs to make debugging problems easier (#34, !34)
|
||||
|
||||
[ Chris Cottam ]
|
||||
* Increased NBD_MAX_SIZE from 1MB to 32MB for qemu 2.11 (!35)
|
||||
|
||||
[ Patrick J Cherry ]
|
||||
* Added FLUSH and FUA support (!38)
|
||||
* Server returns ENOSPC in response to writes beyond the end of the
|
||||
filesystem, and EINVAL to unknown commands. (#36, !40)
|
||||
* Proxy passes all NBD protocol errors through to the client instead of
|
||||
disconnecting and retrying (#36, !40)
|
||||
* Fix struct types in readwrite.c (#35, !41)
|
||||
* Ignore ends of discs that stray outside of 512-byte sector sizes (!42).
|
||||
* Tweak logging for readloop failures (!44)
|
||||
* Alter semantics of NBD_MAX_BLOCK_SIZE to remove struct size overheads when
|
||||
calculating if a request exceeds the max block size (!45)
|
||||
* Added tests for setting TCP_NODELAY on upstream-reconnections in the
|
||||
proxy, and refactored the other LD_PRELOAD tests (!43)
|
||||
* Clean up dead threads before calculating the number of connected clients
|
||||
on the status command (!46)
|
||||
|
||||
-- Patrick J Cherry <patrick@bytemark.co.uk> Tue, 20 Feb 2018 11:43:22 +0000
|
||||
-- James Carter <james.carter@bytemark.co.uk> Thu, 11 Jan 2018 10:05:35 +0000
|
||||
|
||||
flexnbd (0.1.7) stable; urgency=medium
|
||||
|
||||
|
@@ -45,7 +45,8 @@ int build_allocation_map(struct bitset *allocation_map, int fd)
|
||||
if ( ioctl( fd, FS_IOC_FIEMAP, fiemap ) < 0 ) {
|
||||
debug( "Couldn't get fiemap, returning no allocation_map" );
|
||||
return 0; /* it's up to the caller to free the map */
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
for ( unsigned int i = 0; i < fiemap->fm_mapped_extents; i++ ) {
|
||||
bitset_set_range( allocation_map,
|
||||
fiemap->fm_extents[i].fe_logical,
|
||||
@@ -57,10 +58,12 @@ int build_allocation_map(struct bitset *allocation_map, int fd)
|
||||
* if we've actually hit max_offsets.
|
||||
*/
|
||||
if (fiemap->fm_mapped_extents > 0) {
|
||||
struct fiemap_extent *last =
|
||||
&fiemap->fm_extents[fiemap->fm_mapped_extents - 1];
|
||||
struct fiemap_extent *last = &fiemap->fm_extents[
|
||||
fiemap->fm_mapped_extents-1
|
||||
];
|
||||
offset = last->fe_logical + last->fe_length;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
offset += fiemap->fm_length;
|
||||
}
|
||||
}
|
||||
@@ -70,9 +73,7 @@ int build_allocation_map(struct bitset *allocation_map, int fd)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int open_and_mmap(const char *filename, int *out_fd, uint64_t * out_size,
|
||||
void **out_map)
|
||||
int open_and_mmap(const char* filename, int* out_fd, uint64_t *out_size, void **out_map)
|
||||
{
|
||||
/*
|
||||
* size and out_size are intentionally of different types.
|
||||
@@ -83,7 +84,7 @@ int open_and_mmap(const char *filename, int *out_fd, uint64_t * out_size,
|
||||
off64_t size;
|
||||
|
||||
/* O_DIRECT should not be used with mmap() */
|
||||
*out_fd = open(filename, O_RDWR | O_NOATIME);
|
||||
*out_fd = open(filename, O_RDWR | O_SYNC );
|
||||
|
||||
if (*out_fd < 1) {
|
||||
warn("open(%s) failed: does it exist?", filename);
|
||||
@@ -95,29 +96,20 @@ int open_and_mmap(const char *filename, int *out_fd, uint64_t * out_size,
|
||||
warn("lseek64() failed");
|
||||
return size;
|
||||
}
|
||||
|
||||
/* If discs are not in multiples of 512, then odd things happen,
|
||||
* resulting in reads/writes past the ends of files.
|
||||
*/
|
||||
if (size != (size & (~0x1ff))) {
|
||||
warn("file does not fit into 512-byte sectors; the end of the file will be ignored.");
|
||||
size &= ~0x1ff;
|
||||
}
|
||||
|
||||
if (out_size) {
|
||||
*out_size = size;
|
||||
}
|
||||
|
||||
if (out_map) {
|
||||
if (0) {
|
||||
*out_map = mmap64(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
|
||||
*out_fd, 0);
|
||||
if (((long) *out_map) == -1) {
|
||||
warn("mmap64() failed");
|
||||
return -1;
|
||||
}
|
||||
debug("opened %s size %ld on fd %d @ %p", filename, size, *out_fd,
|
||||
*out_map);
|
||||
} else {
|
||||
debug("opened %s size %ld on fd %d @ %p", filename, size, *out_fd, *out_map);
|
||||
}
|
||||
else {
|
||||
debug("opened %s size %ld on fd %d", filename, size, *out_fd);
|
||||
}
|
||||
|
||||
@@ -148,8 +140,7 @@ int readloop(int filedes, void *buffer, size_t size)
|
||||
ssize_t result = read(filedes, buffer+readden, size-readden);
|
||||
|
||||
if ( result == 0 /* EOF */ ) {
|
||||
warn("end-of-file detected while reading after %i bytes",
|
||||
readden);
|
||||
warn( "end-of-file detected while reading after %i bytes", readden );
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -169,9 +160,7 @@ int sendfileloop(int out_fd, int in_fd, off64_t * offset, size_t count)
|
||||
size_t sent=0;
|
||||
while (sent < count) {
|
||||
ssize_t result = sendfile64(out_fd, in_fd, offset, count-sent);
|
||||
debug
|
||||
("sendfile64(out_fd=%d, in_fd=%d, offset=%p, count-sent=%ld) = %ld",
|
||||
out_fd, in_fd, offset, count - sent, result);
|
||||
debug("sendfile64(out_fd=%d, in_fd=%d, offset=%p, count-sent=%ld) = %ld", out_fd, in_fd, offset, count-sent, result);
|
||||
|
||||
if (result == -1) {
|
||||
debug( "%s (%i) calling sendfile64()", strerror(errno), errno );
|
||||
@@ -185,8 +174,7 @@ int sendfileloop(int out_fd, int in_fd, off64_t * offset, size_t count)
|
||||
}
|
||||
|
||||
#include <errno.h>
|
||||
ssize_t spliceloop(int fd_in, loff_t * off_in, int fd_out,
|
||||
loff_t * off_out, size_t len, unsigned int flags2)
|
||||
ssize_t spliceloop(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags2)
|
||||
{
|
||||
const unsigned int flags = SPLICE_F_MORE|SPLICE_F_MOVE|flags2;
|
||||
size_t spliced=0;
|
||||
@@ -194,13 +182,13 @@ ssize_t spliceloop(int fd_in, loff_t * off_in, int fd_out,
|
||||
//debug("spliceloop(%d, %ld, %d, %ld, %ld)", fd_in, off_in ? *off_in : 0, fd_out, off_out ? *off_out : 0, len);
|
||||
|
||||
while (spliced < len) {
|
||||
ssize_t result =
|
||||
splice(fd_in, off_in, fd_out, off_out, len, flags);
|
||||
ssize_t result = splice(fd_in, off_in, fd_out, off_out, len, flags);
|
||||
if (result < 0) {
|
||||
//debug("result=%ld (%s), spliced=%ld, len=%ld", result, strerror(errno), spliced, len);
|
||||
if (errno == EAGAIN && (flags & SPLICE_F_NONBLOCK) ) {
|
||||
return spliced;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
@@ -224,19 +212,13 @@ int splice_via_pipe_loop(int fd_in, int fd_out, size_t len)
|
||||
|
||||
while (spliced < len) {
|
||||
ssize_t run = len-spliced;
|
||||
ssize_t s2, s1 =
|
||||
spliceloop(fd_in, NULL, pipefd[1], NULL, run,
|
||||
SPLICE_F_NONBLOCK);
|
||||
ssize_t s2, s1 = spliceloop(fd_in, NULL, pipefd[1], NULL, run, SPLICE_F_NONBLOCK);
|
||||
/*if (run > 65535)
|
||||
run = 65535;*/
|
||||
if (s1 < 0) {
|
||||
break;
|
||||
}
|
||||
if (s1 < 0) { break; }
|
||||
|
||||
s2 = spliceloop(pipefd[0], NULL, fd_out, NULL, s1, 0);
|
||||
if (s2 < 0) {
|
||||
break;
|
||||
}
|
||||
if (s2 < 0) { break; }
|
||||
spliced += s2;
|
||||
}
|
||||
close(pipefd[0]);
|
||||
@@ -258,9 +240,7 @@ int read_until_newline(int fd, char *buf, int bufsize)
|
||||
|
||||
for (cur=0; cur < bufsize; cur++) {
|
||||
int result = read(fd, buf+cur, 1);
|
||||
if (result <= 0) {
|
||||
return -1;
|
||||
}
|
||||
if (result <= 0) { return -1; }
|
||||
if (buf[cur] == 10) {
|
||||
buf[cur] = '\0';
|
||||
break;
|
||||
@@ -285,9 +265,7 @@ int read_lines_until_blankline(int fd, int max_line_length, char ***lines)
|
||||
* -1 for an eof
|
||||
* -1 for a read error
|
||||
*/
|
||||
if (readden <= 1) {
|
||||
return lines_count;
|
||||
}
|
||||
if (readden <= 1) { return lines_count; }
|
||||
*lines = xrealloc(*lines, (lines_count+1) * sizeof(char*));
|
||||
(*lines)[lines_count] = strdup(line);
|
||||
if ((*lines)[lines_count][0] == 0) {
|
||||
@@ -325,8 +303,7 @@ ssize_t iobuf_read(int fd, struct iobuf * iobuf, size_t default_size)
|
||||
}
|
||||
|
||||
left = iobuf->size - iobuf->needle;
|
||||
debug("Reading %" PRIu32 " of %" PRIu32 " bytes from fd %i", left,
|
||||
iobuf->size, fd);
|
||||
debug( "Reading %"PRIu32" of %"PRIu32" bytes from fd %i", left, iobuf->size, fd );
|
||||
|
||||
count = read( fd, iobuf->buf + iobuf->needle, left );
|
||||
|
||||
@@ -354,8 +331,7 @@ ssize_t iobuf_write(int fd, struct iobuf * iobuf)
|
||||
size_t left = iobuf->size - iobuf->needle;
|
||||
ssize_t count;
|
||||
|
||||
debug("Writing %" PRIu32 " of %" PRIu32 " bytes to fd %i", left,
|
||||
iobuf->size, fd);
|
||||
debug( "Writing %"PRIu32" of %"PRIu32" bytes to fd %i", left, iobuf->size, fd );
|
||||
count = write( fd, iobuf->buf + iobuf->needle, left );
|
||||
|
||||
if ( count >= 0 ) {
|
||||
@@ -372,3 +348,36 @@ ssize_t iobuf_write(int fd, struct iobuf * iobuf)
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
struct iommap *iommap_alloc(int fd, off64_t from, uint64_t len) {
|
||||
|
||||
off66_t mmap_from = from & ~((off64_t) getpagesize() - 1);
|
||||
uint64_t mmap_len = len + (from - mmap_from);
|
||||
void *mmap_buf = NULL;
|
||||
|
||||
// TODO: Check the error code from mmap64
|
||||
if(mmap_len)
|
||||
mmap_buf = mmap64(NULL, mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mmap_from);
|
||||
|
||||
struct iommap *im = xmalloc(sizeof(struct iommap ));
|
||||
|
||||
im->mmap_buf = mmap_buf;
|
||||
im->mmap_len = mmap_len;
|
||||
im->buf = (char *) mmap_buf + from - mmap_from;
|
||||
debug("mmapped file from %ld:%d", mmap_from, mmap_len);
|
||||
return im;
|
||||
}
|
||||
|
||||
void iommap_sync(struct iommap *im) {
|
||||
if (im->mmap_len)
|
||||
msync(im->mmap_buf, im->mmap_len, MS_SYNC | MS_INVALIDATE);
|
||||
return;
|
||||
}
|
||||
|
||||
void iommap_free(struct iommap *im) {
|
||||
if (im->mmap_len)
|
||||
munmap(im->mmap_buf, im->mmap_len);
|
||||
|
||||
free(im);
|
||||
}
|
||||
|
||||
|
@@ -11,7 +11,20 @@ struct iobuf {
|
||||
ssize_t iobuf_read( int fd, struct iobuf* iobuf, size_t default_size );
|
||||
ssize_t iobuf_write( int fd, struct iobuf* iobuf );
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
struct iommap {
|
||||
char *buf;
|
||||
char *mmap_buf;
|
||||
uint64_t mmap_len;
|
||||
};
|
||||
|
||||
struct iommap *iommap_alloc(int fd, off64_t from, uint64_t len);
|
||||
void iommap_sync(struct iommap *im);
|
||||
void iommap_free(struct iommap *im);
|
||||
|
||||
#include "serve.h"
|
||||
|
||||
struct bitset; /* don't need whole of bitset.h here */
|
||||
|
||||
/** Scan the file opened in ''fd'', set bits in ''allocation_map'' that
|
||||
@@ -38,8 +51,7 @@ int readloop(int filedes, void *buffer, size_t size);
|
||||
int sendfileloop(int out_fd, int in_fd, off64_t *offset, size_t count);
|
||||
|
||||
/** Repeat a splice() operation until we have 'len' bytes. */
|
||||
ssize_t spliceloop(int fd_in, loff_t * off_in, int fd_out,
|
||||
loff_t * off_out, size_t len, unsigned int flags2);
|
||||
ssize_t spliceloop(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags2);
|
||||
|
||||
/** Copy ''len'' bytes from ''fd_in'' to ''fd_out'' by creating a temporary
|
||||
* pipe and using the Linux splice call repeatedly until it has transferred
|
||||
@@ -66,8 +78,7 @@ int read_lines_until_blankline(int fd, int max_line_length, char ***lines);
|
||||
* ''out_size'' and the address of the mmap in ''out_map''. If anything goes
|
||||
* wrong, returns -1 setting errno, otherwise 0.
|
||||
*/
|
||||
int open_and_mmap(const char *filename, int *out_fd, uint64_t * out_size,
|
||||
void **out_map);
|
||||
int open_and_mmap( const char* filename, int* out_fd, uint64_t* out_size, void **out_map);
|
||||
|
||||
|
||||
/** Check to see whether the given file descriptor is closed.
|
||||
@@ -75,3 +86,4 @@ int open_and_mmap(const char *filename, int *out_fd, uint64_t * out_size,
|
||||
int fd_is_closed( int fd_in );
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -94,3 +94,4 @@ void mode(char *mode, int argc, char **argv);
|
||||
char * help_help_text;
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -13,7 +13,6 @@ void nbd_r2h_init(struct nbd_init_raw *from, struct nbd_init *to)
|
||||
memcpy( to->passwd, from->passwd, 8 );
|
||||
to->magic = be64toh( from->magic );
|
||||
to->size = be64toh( from->size );
|
||||
to->flags = be32toh(from->flags);
|
||||
}
|
||||
|
||||
void nbd_h2r_init( struct nbd_init * from, struct nbd_init_raw * to)
|
||||
@@ -21,41 +20,39 @@ void nbd_h2r_init(struct nbd_init *from, struct nbd_init_raw *to)
|
||||
memcpy( to->passwd, from->passwd, 8 );
|
||||
to->magic = htobe64( from->magic );
|
||||
to->size = htobe64( from->size );
|
||||
to->flags = htobe32(from->flags);
|
||||
}
|
||||
|
||||
|
||||
void nbd_r2h_request( struct nbd_request_raw *from, struct nbd_request * to )
|
||||
{
|
||||
to->magic = be32toh(from->magic);
|
||||
to->flags = be16toh(from->flags);
|
||||
to->type = be16toh(from->type);
|
||||
to->handle.w = from->handle.w;
|
||||
to->from = be64toh(from->from);
|
||||
to->len = be32toh(from->len);
|
||||
}
|
||||
|
||||
void nbd_h2r_request(struct nbd_request *from, struct nbd_request_raw *to)
|
||||
{
|
||||
to->magic = htobe32( from->magic );
|
||||
to->flags = htobe16(from->flags);
|
||||
to->type = htobe16(from->type);
|
||||
to->type = htobe32( from->type );
|
||||
to->handle.w = from->handle.w;
|
||||
to->from = htobe64( from->from );
|
||||
to->len = htobe32( from->len );
|
||||
}
|
||||
|
||||
void nbd_h2r_request( struct nbd_request * from, struct nbd_request_raw * to )
|
||||
{
|
||||
to->magic = be32toh( from->magic );
|
||||
to->type = be32toh( from->type );
|
||||
to->handle.w = from->handle.w;
|
||||
to->from = be64toh( from->from );
|
||||
to->len = be32toh( from->len );
|
||||
}
|
||||
|
||||
|
||||
void nbd_r2h_reply( struct nbd_reply_raw * from, struct nbd_reply * to )
|
||||
{
|
||||
to->magic = htobe32( from->magic );
|
||||
to->error = htobe32( from->error );
|
||||
to->handle.w = from->handle.w;
|
||||
}
|
||||
|
||||
void nbd_h2r_reply( struct nbd_reply * from, struct nbd_reply_raw * to )
|
||||
{
|
||||
to->magic = be32toh( from->magic );
|
||||
to->error = be32toh( from->error );
|
||||
to->handle.w = from->handle.w;
|
||||
}
|
||||
|
||||
void nbd_h2r_reply(struct nbd_reply *from, struct nbd_reply_raw *to)
|
||||
{
|
||||
to->magic = htobe32(from->magic);
|
||||
to->error = htobe32(from->error);
|
||||
to->handle.w = from->handle.w;
|
||||
}
|
||||
|
@@ -7,38 +7,15 @@
|
||||
#define INIT_MAGIC 0x0000420281861253
|
||||
#define REQUEST_MAGIC 0x25609513
|
||||
#define REPLY_MAGIC 0x67446698
|
||||
|
||||
#define REQUEST_READ 0
|
||||
#define REQUEST_WRITE 1
|
||||
#define REQUEST_DISCONNECT 2
|
||||
#define REQUEST_FLUSH 3
|
||||
|
||||
/* values for transmission flag field */
|
||||
#define FLAG_HAS_FLAGS (1 << 0) /* Flags are there */
|
||||
#define FLAG_SEND_FLUSH (1 << 2) /* Send FLUSH */
|
||||
#define FLAG_SEND_FUA (1 << 3) /* Send FUA (Force Unit Access) */
|
||||
|
||||
/* values for command flag field */
|
||||
#define CMD_FLAG_FUA (1 << 0)
|
||||
|
||||
#if 0
|
||||
/* Not yet implemented by flexnbd */
|
||||
#define REQUEST_TRIM 4
|
||||
#define REQUEST_WRITE_ZEROES 6
|
||||
|
||||
#define FLAG_READ_ONLY (1 << 1) /* Device is read-only */
|
||||
#define FLAG_ROTATIONAL (1 << 4) /* Use elevator algorithm - rotational media */
|
||||
#define FLAG_SEND_TRIM (1 << 5) /* Send TRIM (discard) */
|
||||
#define FLAG_SEND_WRITE_ZEROES (1 << 6) /* Send NBD_CMD_WRITE_ZEROES */
|
||||
#define FLAG_CAN_MULTI_CONN (1 << 8) /* multiple connections are okay */
|
||||
|
||||
#define CMD_FLAG_NO_HOLE (1 << 1)
|
||||
#endif
|
||||
/* The top 2 bytes of the type field are overloaded and can contain flags */
|
||||
#define REQUEST_MASK 0x0000ffff
|
||||
|
||||
|
||||
/* 32 MiB is the maximum qemu will send you:
|
||||
* https://github.com/qemu/qemu/blob/v2.11.0/include/block/nbd.h#L183
|
||||
*/
|
||||
/* 1MiB is the de-facto standard for maximum size of header + data */
|
||||
#define NBD_MAX_SIZE ( 32 * 1024 * 1024 )
|
||||
|
||||
#define NBD_REQUEST_SIZE ( sizeof( struct nbd_request_raw ) )
|
||||
@@ -61,14 +38,12 @@ struct nbd_init_raw {
|
||||
char passwd[8];
|
||||
__be64 magic;
|
||||
__be64 size;
|
||||
__be32 flags;
|
||||
char reserved[124];
|
||||
char reserved[128];
|
||||
};
|
||||
|
||||
struct nbd_request_raw {
|
||||
__be32 magic;
|
||||
__be16 flags;
|
||||
__be16 type; /* == READ || == WRITE || == FLUSH */
|
||||
__be32 type; /* == READ || == WRITE */
|
||||
nbd_handle_t handle;
|
||||
__be64 from;
|
||||
__be32 len;
|
||||
@@ -80,18 +55,18 @@ struct nbd_reply_raw {
|
||||
nbd_handle_t handle; /* handle you got from request */
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct nbd_init {
|
||||
char passwd[8];
|
||||
uint64_t magic;
|
||||
uint64_t size;
|
||||
uint32_t flags;
|
||||
char reserved[124];
|
||||
char reserved[128];
|
||||
};
|
||||
|
||||
struct nbd_request {
|
||||
uint32_t magic;
|
||||
uint16_t flags;
|
||||
uint16_t type; /* == READ || == WRITE || == DISCONNECT || == FLUSH */
|
||||
uint32_t type; /* == READ || == WRITE || == DISCONNECT */
|
||||
nbd_handle_t handle;
|
||||
uint64_t from;
|
||||
uint32_t len;
|
||||
@@ -112,3 +87,4 @@ void nbd_h2r_request(struct nbd_request *from, struct nbd_request_raw *to);
|
||||
void nbd_h2r_reply( struct nbd_reply * from, struct nbd_reply_raw * to );
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -22,9 +22,7 @@ int parse_ip_to_sockaddr(struct sockaddr *out, char *src)
|
||||
/* allow user to start with [ and end with any other invalid char */
|
||||
{
|
||||
int i=0, j=0;
|
||||
if (src[i] == '[') {
|
||||
i++;
|
||||
}
|
||||
if (src[i] == '[') { i++; }
|
||||
for (; i<64 && IS_IP_VALID_CHAR(src[i]); i++) {
|
||||
temp[j++] = src[i];
|
||||
}
|
||||
@@ -74,7 +72,8 @@ int parse_acl(struct ip_and_mask (**out)[], int max, char **entries)
|
||||
if (max == 0) {
|
||||
*out = NULL;
|
||||
return 0;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
list = xmalloc(max * sizeof(struct ip_and_mask));
|
||||
*out = (struct ip_and_mask (*)[])list;
|
||||
debug("acl alloc: %p", *out);
|
||||
@@ -89,14 +88,16 @@ int parse_acl(struct ip_and_mask (**out)[], int max, char **entries)
|
||||
return i;
|
||||
}
|
||||
|
||||
for (j = 0; entries[i][j] && entries[i][j] != '/'; j++); // increment j!
|
||||
for (j=0; entries[i][j] && entries[i][j] != '/'; j++)
|
||||
; // increment j!
|
||||
|
||||
if (entries[i][j] == '/') {
|
||||
outentry->mask = atoi(entries[i]+j+1);
|
||||
if (outentry->mask < 1 || outentry->mask > MAX_MASK_BITS) {
|
||||
return i;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
outentry->mask = MAX_MASK_BITS;
|
||||
}
|
||||
# undef MAX_MASK_BITS
|
||||
@@ -123,3 +124,4 @@ void parse_port(char *s_port, struct sockaddr_in *out)
|
||||
}
|
||||
out->sin_port = htobe16( raw_port );
|
||||
}
|
||||
|
||||
|
@@ -26,3 +26,4 @@ int parse_acl(struct ip_and_mask (**out)[], int max, char **entries);
|
||||
void parse_port( char *s_port, struct sockaddr_in *out );
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -10,9 +10,7 @@
|
||||
|
||||
int socket_connect(struct sockaddr* to, struct sockaddr* from)
|
||||
{
|
||||
int fd =
|
||||
socket(to->sa_family == AF_INET ? PF_INET : PF_INET6, SOCK_STREAM,
|
||||
0);
|
||||
int fd = socket(to->sa_family == AF_INET ? PF_INET : PF_INET6, SOCK_STREAM, 0);
|
||||
if( fd < 0 ){
|
||||
warn( "Couldn't create client socket");
|
||||
return -1;
|
||||
@@ -43,8 +41,7 @@ int socket_connect(struct sockaddr *to, struct sockaddr *from)
|
||||
return fd;
|
||||
}
|
||||
|
||||
int nbd_check_hello(struct nbd_init_raw *init_raw, uint64_t * out_size,
|
||||
uint32_t * out_flags)
|
||||
int nbd_check_hello( struct nbd_init_raw* init_raw, uint64_t* out_size )
|
||||
{
|
||||
if ( strncmp( init_raw->passwd, INIT_PASSWD, 8 ) != 0 ) {
|
||||
warn( "wrong passwd" );
|
||||
@@ -59,18 +56,13 @@ int nbd_check_hello(struct nbd_init_raw *init_raw, uint64_t * out_size,
|
||||
*out_size = be64toh( init_raw->size );
|
||||
}
|
||||
|
||||
if (NULL != out_flags) {
|
||||
*out_flags = be32toh(init_raw->flags);
|
||||
}
|
||||
|
||||
return 1;
|
||||
fail:
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int socket_nbd_read_hello(int fd, uint64_t * out_size,
|
||||
uint32_t * out_flags)
|
||||
int socket_nbd_read_hello( int fd, uint64_t* out_size )
|
||||
{
|
||||
struct nbd_init_raw init_raw;
|
||||
|
||||
@@ -80,18 +72,16 @@ int socket_nbd_read_hello(int fd, uint64_t * out_size,
|
||||
return 0;
|
||||
}
|
||||
|
||||
return nbd_check_hello(&init_raw, out_size, out_flags);
|
||||
return nbd_check_hello( &init_raw, out_size );
|
||||
}
|
||||
|
||||
void nbd_hello_to_buf(struct nbd_init_raw *buf, off64_t out_size,
|
||||
uint32_t out_flags)
|
||||
void nbd_hello_to_buf( struct nbd_init_raw *buf, off64_t out_size )
|
||||
{
|
||||
struct nbd_init init;
|
||||
|
||||
memcpy( &init.passwd, INIT_PASSWD, 8 );
|
||||
init.magic = INIT_MAGIC;
|
||||
init.size = out_size;
|
||||
init.flags = out_flags;
|
||||
|
||||
memset( buf, 0, sizeof( struct nbd_init_raw ) ); // ensure reserved is 0s
|
||||
nbd_h2r_init( &init, buf );
|
||||
@@ -99,10 +89,10 @@ void nbd_hello_to_buf(struct nbd_init_raw *buf, off64_t out_size,
|
||||
return;
|
||||
}
|
||||
|
||||
int socket_nbd_write_hello(int fd, off64_t out_size, uint32_t out_flags)
|
||||
int socket_nbd_write_hello(int fd, off64_t out_size)
|
||||
{
|
||||
struct nbd_init_raw init_raw;
|
||||
nbd_hello_to_buf(&init_raw, out_size, out_flags);
|
||||
nbd_hello_to_buf( &init_raw, out_size );
|
||||
|
||||
if ( 0 > writeloop( fd, &init_raw, sizeof( init_raw ) ) ) {
|
||||
warn( SHOW_ERRNO( "failed to write hello to socket" ) );
|
||||
@@ -111,25 +101,20 @@ int socket_nbd_write_hello(int fd, off64_t out_size, uint32_t out_flags)
|
||||
return 1;
|
||||
}
|
||||
|
||||
void fill_request(struct nbd_request_raw *request_raw, uint16_t type,
|
||||
uint16_t flags, uint64_t from, uint32_t len)
|
||||
void fill_request(struct nbd_request *request, int type, uint64_t from, uint32_t len)
|
||||
{
|
||||
request_raw->magic = htobe32(REQUEST_MAGIC);
|
||||
request_raw->type = htobe16(type);
|
||||
request_raw->flags = htobe16(flags);
|
||||
request_raw->handle.w =
|
||||
(((uint64_t) rand()) << 32) | ((uint64_t) rand());
|
||||
request_raw->from = htobe64(from);
|
||||
request_raw->len = htobe32(len);
|
||||
request->magic = htobe32(REQUEST_MAGIC);
|
||||
request->type = htobe32(type);
|
||||
request->handle.w = (((uint64_t)rand()) << 32) | ((uint64_t)rand());
|
||||
request->from = htobe64(from);
|
||||
request->len = htobe32(len);
|
||||
}
|
||||
|
||||
void read_reply(int fd, uint64_t request_raw_handle,
|
||||
struct nbd_reply *reply)
|
||||
void read_reply(int fd, struct nbd_request *request, struct nbd_reply *reply)
|
||||
{
|
||||
struct nbd_reply_raw reply_raw;
|
||||
|
||||
ERROR_IF_NEGATIVE(readloop
|
||||
(fd, &reply_raw, sizeof(struct nbd_reply_raw)),
|
||||
ERROR_IF_NEGATIVE(readloop(fd, &reply_raw, sizeof(struct nbd_reply_raw)),
|
||||
"Couldn't read reply");
|
||||
|
||||
nbd_r2h_reply( &reply_raw, reply );
|
||||
@@ -140,7 +125,7 @@ void read_reply(int fd, uint64_t request_raw_handle,
|
||||
if (reply->error != 0) {
|
||||
error("Server replied with error %d", reply->error);
|
||||
}
|
||||
if (request_raw_handle != reply_raw.handle.w) {
|
||||
if (request->handle.w != reply->handle.w) {
|
||||
error("Did not reply with correct handle");
|
||||
}
|
||||
}
|
||||
@@ -154,77 +139,81 @@ void wait_for_data(int fd, int timeout_secs)
|
||||
FD_ZERO( &fds );
|
||||
FD_SET( fd, &fds );
|
||||
|
||||
selected =
|
||||
sock_try_select(FD_SETSIZE, &fds, NULL, NULL,
|
||||
timeout_secs >= 0 ? &tv : NULL);
|
||||
selected = sock_try_select(
|
||||
FD_SETSIZE, &fds, NULL, NULL, timeout_secs >=0 ? &tv : NULL
|
||||
);
|
||||
|
||||
FATAL_IF( -1 == selected, "Select failed" );
|
||||
ERROR_IF( 0 == selected, "Timed out waiting for reply" );
|
||||
}
|
||||
|
||||
|
||||
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd,
|
||||
void *out_buf, int timeout_secs)
|
||||
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd, void* out_buf, int timeout_secs)
|
||||
{
|
||||
struct nbd_request_raw request_raw;
|
||||
struct nbd_request request;
|
||||
struct nbd_reply reply;
|
||||
|
||||
fill_request(&request_raw, REQUEST_READ, 0, from, len);
|
||||
FATAL_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
|
||||
fill_request(&request, REQUEST_READ, from, len);
|
||||
FATAL_IF_NEGATIVE(writeloop(fd, &request, sizeof(request)),
|
||||
"Couldn't write request");
|
||||
|
||||
|
||||
wait_for_data( fd, timeout_secs );
|
||||
read_reply(fd, request_raw.handle.w, &reply);
|
||||
read_reply(fd, &request, &reply);
|
||||
|
||||
if (out_buf) {
|
||||
FATAL_IF_NEGATIVE(readloop(fd, out_buf, len), "Read failed");
|
||||
} else {
|
||||
FATAL_IF_NEGATIVE(splice_via_pipe_loop(fd, out_fd, len),
|
||||
"Splice failed");
|
||||
FATAL_IF_NEGATIVE(readloop(fd, out_buf, len),
|
||||
"Read failed");
|
||||
}
|
||||
else {
|
||||
FATAL_IF_NEGATIVE(
|
||||
splice_via_pipe_loop(fd, out_fd, len),
|
||||
"Splice failed"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd,
|
||||
void *in_buf, int timeout_secs)
|
||||
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd, void* in_buf, int timeout_secs)
|
||||
{
|
||||
struct nbd_request_raw request_raw;
|
||||
struct nbd_request request;
|
||||
struct nbd_reply reply;
|
||||
|
||||
fill_request(&request_raw, REQUEST_WRITE, 0, from, len);
|
||||
ERROR_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
|
||||
fill_request(&request, REQUEST_WRITE, from, len);
|
||||
ERROR_IF_NEGATIVE(writeloop(fd, &request, sizeof(request)),
|
||||
"Couldn't write request");
|
||||
|
||||
if (in_buf) {
|
||||
ERROR_IF_NEGATIVE(writeloop(fd, in_buf, len), "Write failed");
|
||||
} else {
|
||||
ERROR_IF_NEGATIVE(splice_via_pipe_loop(in_fd, fd, len),
|
||||
"Splice failed");
|
||||
ERROR_IF_NEGATIVE(writeloop(fd, in_buf, len),
|
||||
"Write failed");
|
||||
}
|
||||
else {
|
||||
ERROR_IF_NEGATIVE(
|
||||
splice_via_pipe_loop(in_fd, fd, len),
|
||||
"Splice failed"
|
||||
);
|
||||
}
|
||||
|
||||
wait_for_data( fd, timeout_secs );
|
||||
read_reply(fd, request_raw.handle.w, &reply);
|
||||
read_reply(fd, &request, &reply);
|
||||
}
|
||||
|
||||
|
||||
int socket_nbd_disconnect( int fd )
|
||||
{
|
||||
int success = 1;
|
||||
struct nbd_request_raw request_raw;
|
||||
struct nbd_request request;
|
||||
|
||||
fill_request(&request_raw, REQUEST_DISCONNECT, 0, 0, 0);
|
||||
fill_request( &request, REQUEST_DISCONNECT, 0, 0 );
|
||||
/* FIXME: This shouldn't be a FATAL error. We should just drop
|
||||
* the mirror without affecting the main server.
|
||||
*/
|
||||
FATAL_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
|
||||
FATAL_IF_NEGATIVE( writeloop( fd, &request, sizeof( request ) ),
|
||||
"Failed to write the disconnect request." );
|
||||
return success;
|
||||
}
|
||||
|
||||
#define CHECK_RANGE(error_type) { \
|
||||
uint64_t size;\
|
||||
uint32_t flags;\
|
||||
int success = socket_nbd_read_hello(params->client, &size, &flags); \
|
||||
int success = socket_nbd_read_hello(params->client, &size); \
|
||||
if ( success ) {\
|
||||
uint64_t endpoint = params->from + params->len; \
|
||||
if (endpoint > size || \
|
||||
@@ -242,9 +231,7 @@ int socket_nbd_disconnect(int fd)
|
||||
|
||||
void do_read(struct mode_readwrite_params* params)
|
||||
{
|
||||
params->client =
|
||||
socket_connect(¶ms->connect_to.generic,
|
||||
¶ms->connect_from.generic);
|
||||
params->client = socket_connect(¶ms->connect_to.generic, ¶ms->connect_from.generic);
|
||||
FATAL_IF_NEGATIVE( params->client, "Couldn't connect." );
|
||||
CHECK_RANGE("read");
|
||||
socket_nbd_read(params->client, params->from, params->len,
|
||||
@@ -254,12 +241,11 @@ void do_read(struct mode_readwrite_params *params)
|
||||
|
||||
void do_write(struct mode_readwrite_params* params)
|
||||
{
|
||||
params->client =
|
||||
socket_connect(¶ms->connect_to.generic,
|
||||
¶ms->connect_from.generic);
|
||||
params->client = socket_connect(¶ms->connect_to.generic, ¶ms->connect_from.generic);
|
||||
FATAL_IF_NEGATIVE( params->client, "Couldn't connect." );
|
||||
CHECK_RANGE("write");
|
||||
socket_nbd_write(params->client, params->from, params->len,
|
||||
params->data_fd, NULL, 10);
|
||||
close(params->client);
|
||||
}
|
||||
|
||||
|
@@ -7,20 +7,17 @@
|
||||
#include "nbdtypes.h"
|
||||
|
||||
int socket_connect(struct sockaddr* to, struct sockaddr* from);
|
||||
int socket_nbd_read_hello(int fd, uint64_t * size, uint32_t * flags);
|
||||
int socket_nbd_write_hello(int fd, uint64_t size, uint32_t flags);
|
||||
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd,
|
||||
void *out_buf, int timeout_secs);
|
||||
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int out_fd,
|
||||
void *out_buf, int timeout_secs);
|
||||
int socket_nbd_read_hello(int fd, uint64_t* size);
|
||||
int socket_nbd_write_hello(int fd, uint64_t size);
|
||||
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd, void* out_buf, int timeout_secs);
|
||||
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int out_fd, void* out_buf, int timeout_secs);
|
||||
int socket_nbd_disconnect( int fd );
|
||||
|
||||
/* as you can see, we're slowly accumulating code that should really be in an
|
||||
* NBD library */
|
||||
|
||||
void nbd_hello_to_buf(struct nbd_init_raw *buf, uint64_t out_size,
|
||||
uint32_t out_flags);
|
||||
int nbd_check_hello(struct nbd_init_raw *init_raw, uint64_t * out_size,
|
||||
uint32_t * out_flags);
|
||||
void nbd_hello_to_buf( struct nbd_init_raw* buf, uint64_t out_size );
|
||||
int nbd_check_hello( struct nbd_init_raw* init_raw, uint64_t* out_size );
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -24,8 +24,7 @@ void print_response(const char *response)
|
||||
fprintf(out, "%s\n", response_text + 2);
|
||||
}
|
||||
|
||||
void do_remote_command(char *command, char *socket_name, int argc,
|
||||
char **argv)
|
||||
void do_remote_command(char* command, char* socket_name, int argc, char** argv)
|
||||
{
|
||||
char newline=10;
|
||||
int i;
|
||||
@@ -41,10 +40,10 @@ void do_remote_command(char *command, char *socket_name, int argc,
|
||||
address.sun_family = AF_UNIX;
|
||||
strncpy(address.sun_path, socket_name, sizeof(address.sun_path));
|
||||
|
||||
FATAL_IF_NEGATIVE(connect
|
||||
(remote, (struct sockaddr *) &address,
|
||||
sizeof(address)), "Couldn't connect to %s",
|
||||
socket_name);
|
||||
FATAL_IF_NEGATIVE(
|
||||
connect(remote, (struct sockaddr*) &address, sizeof(address)),
|
||||
"Couldn't connect to %s", socket_name
|
||||
);
|
||||
|
||||
write(remote, command, strlen(command));
|
||||
write(remote, &newline, 1);
|
||||
@@ -56,10 +55,13 @@ void do_remote_command(char *command, char *socket_name, int argc,
|
||||
}
|
||||
write(remote, &newline, 1);
|
||||
|
||||
FATAL_IF_NEGATIVE(read_until_newline(remote, response, max_response),
|
||||
"Couldn't read response from %s", socket_name);
|
||||
FATAL_IF_NEGATIVE(
|
||||
read_until_newline(remote, response, max_response),
|
||||
"Couldn't read response from %s", socket_name
|
||||
);
|
||||
|
||||
print_response( response );
|
||||
|
||||
exit(atoi(response));
|
||||
}
|
||||
|
||||
|
@@ -52,9 +52,7 @@ struct self_pipe *self_pipe_create(void)
|
||||
struct self_pipe *sig = xmalloc( sizeof( struct self_pipe ) );
|
||||
int fds[2];
|
||||
|
||||
if (NULL == sig) {
|
||||
return NULL;
|
||||
}
|
||||
if ( NULL == sig ) { return NULL; }
|
||||
|
||||
if ( pipe( fds ) ) {
|
||||
free( sig );
|
||||
@@ -62,8 +60,7 @@ struct self_pipe *self_pipe_create(void)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fcntl(fds[0], F_SETFL, O_NONBLOCK)
|
||||
|| fcntl(fds[1], F_SETFL, O_NONBLOCK)) {
|
||||
if ( fcntl( fds[0], F_SETFL, O_NONBLOCK ) || fcntl( fds[1], F_SETFL, O_NONBLOCK ) ) {
|
||||
int fcntl_err = errno;
|
||||
while( close( fds[0] ) == -1 && errno == EINTR );
|
||||
while( close( fds[1] ) == -1 && errno == EINTR );
|
||||
|
@@ -29,8 +29,7 @@ size_t sockaddr_size(const struct sockaddr * sa)
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char *sockaddr_address_string(const struct sockaddr *sa, char *dest,
|
||||
size_t len)
|
||||
const char* sockaddr_address_string( const struct sockaddr* sa, char* dest, size_t len )
|
||||
{
|
||||
NULLCHECK( sa );
|
||||
NULLCHECK( dest );
|
||||
@@ -66,8 +65,7 @@ const char *sockaddr_address_string(const struct sockaddr *sa, char *dest,
|
||||
|
||||
int sock_set_reuseaddr( int fd, int optval )
|
||||
{
|
||||
return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval,
|
||||
sizeof(optval));
|
||||
return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval) );
|
||||
}
|
||||
|
||||
int sock_set_keepalive_params( int fd, int time, int intvl, int probes)
|
||||
@@ -83,33 +81,28 @@ int sock_set_keepalive_params(int fd, int time, int intvl, int probes)
|
||||
|
||||
int sock_set_keepalive( int fd, int optval )
|
||||
{
|
||||
return setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &optval,
|
||||
sizeof(optval));
|
||||
return setsockopt( fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval) );
|
||||
}
|
||||
|
||||
int sock_set_tcp_keepidle( int fd, int optval )
|
||||
{
|
||||
return setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval,
|
||||
sizeof(optval));
|
||||
return setsockopt( fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval, sizeof(optval) );
|
||||
}
|
||||
|
||||
int sock_set_tcp_keepintvl( int fd, int optval )
|
||||
{
|
||||
return setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval,
|
||||
sizeof(optval));
|
||||
return setsockopt( fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, sizeof(optval) );
|
||||
}
|
||||
|
||||
int sock_set_tcp_keepcnt( int fd, int optval )
|
||||
{
|
||||
return setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &optval,
|
||||
sizeof(optval));
|
||||
return setsockopt( fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof(optval) );
|
||||
}
|
||||
|
||||
/* Set the tcp_nodelay option */
|
||||
int sock_set_tcp_nodelay( int fd, int optval )
|
||||
{
|
||||
return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval,
|
||||
sizeof(optval));
|
||||
return setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval) );
|
||||
}
|
||||
|
||||
int sock_set_tcp_cork( int fd, int optval )
|
||||
@@ -147,7 +140,8 @@ int sock_try_bind(int fd, const struct sockaddr *sa)
|
||||
if ( 0 == bind_result ) {
|
||||
info( "Bound to %s", s_address );
|
||||
break;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
warn( SHOW_ERRNO( "Couldn't bind to %s", s_address ) );
|
||||
|
||||
switch ( errno ) {
|
||||
@@ -183,8 +177,7 @@ int sock_try_bind(int fd, const struct sockaddr *sa)
|
||||
return bind_result;
|
||||
}
|
||||
|
||||
int sock_try_select(int nfds, fd_set * readfds, fd_set * writefds,
|
||||
fd_set * exceptfds, struct timeval *timeout)
|
||||
int sock_try_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
|
||||
{
|
||||
int result;
|
||||
|
||||
@@ -199,16 +192,14 @@ int sock_try_select(int nfds, fd_set * readfds, fd_set * writefds,
|
||||
return result;
|
||||
}
|
||||
|
||||
int sock_try_connect(int fd, struct sockaddr *to, socklen_t addrlen,
|
||||
int wait)
|
||||
int sock_try_connect( int fd, struct sockaddr* to, socklen_t addrlen, int wait )
|
||||
{
|
||||
fd_set fds;
|
||||
struct timeval tv = { wait, 0 };
|
||||
int result = 0;
|
||||
|
||||
if ( sock_set_nonblock( fd, 1 ) == -1 ) {
|
||||
warn(SHOW_ERRNO
|
||||
("Failed to set socket non-blocking for connect()"));
|
||||
warn( SHOW_ERRNO( "Failed to set socket non-blocking for connect()" ) );
|
||||
return connect( fd, to, addrlen );
|
||||
}
|
||||
|
||||
@@ -293,3 +284,4 @@ int sock_try_close(int fd)
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@@ -12,8 +12,7 @@ size_t sockaddr_size(const struct sockaddr *sa);
|
||||
/* Convert a sockaddr into an address. Like inet_ntop, it returns dest if
|
||||
* successful, NULL otherwise. In the latter case, dest will contain "???"
|
||||
*/
|
||||
const char *sockaddr_address_string(const struct sockaddr *sa, char *dest,
|
||||
size_t len);
|
||||
const char* sockaddr_address_string(const struct sockaddr* sa, char* dest, size_t len);
|
||||
|
||||
/* Configure TCP keepalive on a socket */
|
||||
int sock_set_keepalive_params( int fd, int time, int intvl, int probes);
|
||||
@@ -45,14 +44,13 @@ int sock_set_nonblock(int fd, int optval);
|
||||
int sock_try_bind(int fd, const struct sockaddr* sa);
|
||||
|
||||
/* Try to call select(), retrying EINTR */
|
||||
int sock_try_select(int nfds, fd_set * readfds, fd_set * writefds,
|
||||
fd_set * exceptfds, struct timeval *timeout);
|
||||
int sock_try_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
||||
|
||||
/* Try to call connect(), timing out after wait seconds */
|
||||
int sock_try_connect(int fd, struct sockaddr *to, socklen_t addrlen,
|
||||
int wait);
|
||||
int sock_try_connect( int fd, struct sockaddr* to, socklen_t addrlen, int wait );
|
||||
|
||||
/* Try to call close(), retrying EINTR */
|
||||
int sock_try_close( int fd );
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -26,12 +26,10 @@ void error_handler(int fatal)
|
||||
|
||||
if (context) {
|
||||
longjmp(context->jmp, fatal ? 1 : 2 );
|
||||
} else {
|
||||
if (fatal) {
|
||||
abort();
|
||||
} else {
|
||||
pthread_exit((void *) 1);
|
||||
}
|
||||
else {
|
||||
if ( fatal ) { abort(); }
|
||||
else { pthread_exit((void*) 1); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,9 +43,7 @@ void exit_err(const char *msg)
|
||||
|
||||
void mylog(int line_level, const char* format, ...)
|
||||
{
|
||||
if (line_level < log_level) {
|
||||
return;
|
||||
}
|
||||
if (line_level < log_level) { return; }
|
||||
|
||||
va_list argptr;
|
||||
|
||||
@@ -61,7 +57,8 @@ uint64_t monotonic_time_ms()
|
||||
struct timespec ts;
|
||||
uint64_t seconds_ms, nanoseconds_ms;
|
||||
|
||||
FATAL_IF_NEGATIVE(clock_gettime(CLOCK_MONOTONIC, &ts),
|
||||
FATAL_IF_NEGATIVE(
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts),
|
||||
SHOW_ERRNO( "clock_gettime failed" )
|
||||
);
|
||||
|
||||
@@ -78,8 +75,7 @@ uint64_t monotonic_time_ms()
|
||||
void* xrealloc(void* ptr, size_t size)
|
||||
{
|
||||
void* p = realloc(ptr, size);
|
||||
FATAL_IF_NULL(p, "couldn't xrealloc %d bytes",
|
||||
ptr ? "realloc" : "malloc", size);
|
||||
FATAL_IF_NULL(p, "couldn't xrealloc %d bytes", ptr ? "realloc" : "malloc", size);
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -89,3 +85,4 @@ void *xmalloc(size_t size)
|
||||
memset(p, 0, size);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@@ -13,8 +13,7 @@
|
||||
void* xrealloc(void* ptr, size_t size);
|
||||
void* xmalloc(size_t size);
|
||||
|
||||
typedef void (cleanup_handler) (void * /* context */ ,
|
||||
int /* is fatal? */ );
|
||||
typedef void (cleanup_handler)(void* /* context */, int /* is fatal? */);
|
||||
|
||||
/* set from 0 - 5 depending on what level of verbosity you want */
|
||||
extern int log_level;
|
||||
@@ -163,3 +162,4 @@ uint64_t monotonic_time_ms(void);
|
||||
#define WARN_IF_NEGATIVE( value, msg, ... ) if ( value < 0 ) { warn( msg, ##__VA_ARGS__ ); }
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -19,3 +19,4 @@ int main(int argc, char **argv)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -19,7 +19,6 @@ static struct option proxy_options[] = {
|
||||
GETOPT_VERBOSE,
|
||||
{0}
|
||||
};
|
||||
|
||||
static char proxy_short_options[] = "hl:p:C:P:b:" SOPT_QUIET SOPT_VERBOSE;
|
||||
static char proxy_help_text[] =
|
||||
"Usage: flexnbd-proxy <options>\n\n"
|
||||
@@ -27,24 +26,24 @@ static char proxy_help_text[] =
|
||||
"We can listen on TCP or UNIX socket, but only connect to TCP servers.\n\n"
|
||||
HELP_LINE
|
||||
"\t--" OPT_ADDR ",-l <ADDR>\tThe address we will bind to as a proxy.\n"
|
||||
"\t--" OPT_PORT
|
||||
",-p <PORT>\tThe port we will bind to as a proxy, if required.\n"
|
||||
"\t--" OPT_PORT ",-p <PORT>\tThe port we will bind to as a proxy, if required.\n"
|
||||
"\t--" OPT_CONNECT_ADDR ",-C <ADDR>\tAddress of the proxied server.\n"
|
||||
"\t--" OPT_CONNECT_PORT ",-P <PORT>\tPort of the proxied server.\n"
|
||||
"\t--" OPT_BIND
|
||||
",-b <ADDR>\tThe address we connect from, as a proxy.\n" "\t--"
|
||||
OPT_CACHE
|
||||
",-c[=<CACHE-BYTES>]\tUse a RAM read cache of the given size.\n"
|
||||
QUIET_LINE VERBOSE_LINE;
|
||||
"\t--" OPT_BIND ",-b <ADDR>\tThe address we connect from, as a proxy.\n"
|
||||
"\t--" OPT_CACHE ",-c[=<CACHE-BYTES>]\tUse a RAM read cache of the given size.\n"
|
||||
QUIET_LINE
|
||||
VERBOSE_LINE;
|
||||
|
||||
static char proxy_default_cache_size[] = "4096";
|
||||
|
||||
void read_proxy_param(int c,
|
||||
void read_proxy_param(
|
||||
int c,
|
||||
char **downstream_addr,
|
||||
char **downstream_port,
|
||||
char **upstream_addr,
|
||||
char **upstream_port,
|
||||
char **bind_addr, char **cache_bytes)
|
||||
char **bind_addr,
|
||||
char **cache_bytes)
|
||||
{
|
||||
switch( c ) {
|
||||
case 'h' :
|
||||
@@ -117,31 +116,34 @@ int main(int argc, char *argv[])
|
||||
srand(time(NULL));
|
||||
|
||||
while (1) {
|
||||
c = getopt_long(argc, argv, proxy_short_options, proxy_options,
|
||||
NULL);
|
||||
if (-1 == c) {
|
||||
break;
|
||||
}
|
||||
c = getopt_long( argc, argv, proxy_short_options, proxy_options, NULL );
|
||||
if ( -1 == c ) { break; }
|
||||
read_proxy_param( c,
|
||||
&downstream_addr,
|
||||
&downstream_port,
|
||||
&upstream_addr,
|
||||
&upstream_port, &bind_addr, &cache_bytes);
|
||||
&upstream_port,
|
||||
&bind_addr,
|
||||
&cache_bytes
|
||||
);
|
||||
}
|
||||
|
||||
if ( NULL == downstream_addr ){
|
||||
fprintf( stderr, "--addr is required.\n" );
|
||||
exit_err( proxy_help_text );
|
||||
} else if ( NULL == upstream_addr || NULL == upstream_port ){
|
||||
fprintf(stderr,
|
||||
"both --conn-addr and --conn-port are required.\n");
|
||||
fprintf( stderr, "both --conn-addr and --conn-port are required.\n" );
|
||||
exit_err( proxy_help_text );
|
||||
}
|
||||
|
||||
proxy = proxy_create(downstream_addr,
|
||||
proxy = proxy_create(
|
||||
downstream_addr,
|
||||
downstream_port,
|
||||
upstream_addr,
|
||||
upstream_port, bind_addr, cache_bytes);
|
||||
upstream_port,
|
||||
bind_addr,
|
||||
cache_bytes
|
||||
);
|
||||
|
||||
/* Set these *after* proxy has been assigned to */
|
||||
sigaction(SIGTERM, &exit_action, NULL);
|
||||
@@ -150,12 +152,15 @@ int main(int argc, char *argv[])
|
||||
signal(SIGPIPE, SIG_IGN); /* calls to splice() unhelpfully throw this */
|
||||
|
||||
if ( NULL != downstream_port ) {
|
||||
info("Proxying between %s %s (downstream) and %s %s (upstream)",
|
||||
downstream_addr, downstream_port, upstream_addr,
|
||||
upstream_port);
|
||||
info(
|
||||
"Proxying between %s %s (downstream) and %s %s (upstream)",
|
||||
downstream_addr, downstream_port, upstream_addr, upstream_port
|
||||
);
|
||||
} else {
|
||||
info("Proxying between %s (downstream) and %s %s (upstream)",
|
||||
downstream_addr, upstream_addr, upstream_port);
|
||||
info(
|
||||
"Proxying between %s (downstream) and %s %s (upstream)",
|
||||
downstream_addr, upstream_addr, upstream_port
|
||||
);
|
||||
}
|
||||
|
||||
success = do_proxy( proxy );
|
||||
@@ -163,3 +168,4 @@ int main(int argc, char *argv[])
|
||||
|
||||
return success ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -2,8 +2,7 @@
|
||||
#include "util.h"
|
||||
|
||||
|
||||
struct prefetch *prefetch_create(size_t size_bytes)
|
||||
{
|
||||
struct prefetch* prefetch_create( size_t size_bytes ){
|
||||
|
||||
struct prefetch* out = xmalloc( sizeof( struct prefetch ) );
|
||||
NULLCHECK( out );
|
||||
@@ -20,16 +19,14 @@ struct prefetch *prefetch_create(size_t size_bytes)
|
||||
|
||||
}
|
||||
|
||||
void prefetch_destroy(struct prefetch *prefetch)
|
||||
{
|
||||
void prefetch_destroy( struct prefetch *prefetch ) {
|
||||
if( prefetch ) {
|
||||
free( prefetch->buffer );
|
||||
free( prefetch );
|
||||
}
|
||||
}
|
||||
|
||||
size_t prefetch_size(struct prefetch *prefetch)
|
||||
{
|
||||
size_t prefetch_size( struct prefetch *prefetch){
|
||||
if ( prefetch ) {
|
||||
return prefetch->size;
|
||||
} else {
|
||||
@@ -37,25 +34,21 @@ size_t prefetch_size(struct prefetch *prefetch)
|
||||
}
|
||||
}
|
||||
|
||||
void prefetch_set_is_empty(struct prefetch *prefetch)
|
||||
{
|
||||
void prefetch_set_is_empty( struct prefetch *prefetch ){
|
||||
prefetch_set_full( prefetch, 0 );
|
||||
}
|
||||
|
||||
void prefetch_set_is_full(struct prefetch *prefetch)
|
||||
{
|
||||
void prefetch_set_is_full( struct prefetch *prefetch ){
|
||||
prefetch_set_full( prefetch, 1 );
|
||||
}
|
||||
|
||||
void prefetch_set_full(struct prefetch *prefetch, int val)
|
||||
{
|
||||
void prefetch_set_full( struct prefetch *prefetch, int val ){
|
||||
if( prefetch ) {
|
||||
prefetch->is_full = val;
|
||||
}
|
||||
}
|
||||
|
||||
int prefetch_is_full(struct prefetch *prefetch)
|
||||
{
|
||||
int prefetch_is_full( struct prefetch *prefetch ){
|
||||
if( prefetch ) {
|
||||
return prefetch->is_full;
|
||||
} else {
|
||||
@@ -63,16 +56,13 @@ int prefetch_is_full(struct prefetch *prefetch)
|
||||
}
|
||||
}
|
||||
|
||||
int prefetch_contains(struct prefetch *prefetch, uint64_t from,
|
||||
uint32_t len)
|
||||
{
|
||||
int prefetch_contains( struct prefetch *prefetch, uint64_t from, uint32_t len ){
|
||||
NULLCHECK( prefetch );
|
||||
return from >= prefetch->from &&
|
||||
from + len <= prefetch->from + prefetch->len;
|
||||
}
|
||||
|
||||
char *prefetch_offset(struct prefetch *prefetch, uint64_t from)
|
||||
{
|
||||
char *prefetch_offset( struct prefetch *prefetch, uint64_t from ){
|
||||
NULLCHECK( prefetch );
|
||||
return prefetch->buffer + (from - prefetch->from);
|
||||
}
|
||||
|
@@ -27,8 +27,7 @@ void prefetch_set_is_empty(struct prefetch *prefetch);
|
||||
void prefetch_set_is_full( struct prefetch *prefetch );
|
||||
void prefetch_set_full( struct prefetch *prefetch, int val );
|
||||
int prefetch_is_full( struct prefetch *prefetch );
|
||||
int prefetch_contains(struct prefetch *prefetch, uint64_t from,
|
||||
uint32_t len);
|
||||
int prefetch_contains( struct prefetch *prefetch, uint64_t from, uint32_t len );
|
||||
char *prefetch_offset( struct prefetch *prefetch, uint64_t from );
|
||||
|
||||
#endif
|
||||
|
@@ -13,11 +13,13 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/tcp.h>
|
||||
|
||||
struct proxier *proxy_create(char *s_downstream_address,
|
||||
struct proxier* proxy_create(
|
||||
char* s_downstream_address,
|
||||
char* s_downstream_port,
|
||||
char* s_upstream_address,
|
||||
char* s_upstream_port,
|
||||
char *s_upstream_bind, char *s_cache_bytes)
|
||||
char* s_upstream_bind,
|
||||
char* s_cache_bytes )
|
||||
{
|
||||
struct proxier* out;
|
||||
out = xmalloc( sizeof( struct proxier ) );
|
||||
@@ -25,9 +27,10 @@ struct proxier *proxy_create(char *s_downstream_address,
|
||||
FATAL_IF_NULL(s_downstream_address, "Listen address not specified");
|
||||
NULLCHECK( s_downstream_address );
|
||||
|
||||
FATAL_UNLESS(parse_to_sockaddr
|
||||
(&out->listen_on.generic, s_downstream_address),
|
||||
"Couldn't parse downstream address %s");
|
||||
FATAL_UNLESS(
|
||||
parse_to_sockaddr( &out->listen_on.generic, s_downstream_address ),
|
||||
"Couldn't parse downstream address %s"
|
||||
);
|
||||
|
||||
if ( out->listen_on.family != AF_UNIX ) {
|
||||
FATAL_IF_NULL( s_downstream_port, "Downstream port not specified" );
|
||||
@@ -38,19 +41,22 @@ struct proxier *proxy_create(char *s_downstream_address,
|
||||
FATAL_IF_NULL(s_upstream_address, "Upstream address not specified");
|
||||
NULLCHECK( s_upstream_address );
|
||||
|
||||
FATAL_UNLESS(parse_ip_to_sockaddr
|
||||
(&out->connect_to.generic, s_upstream_address),
|
||||
FATAL_UNLESS(
|
||||
parse_ip_to_sockaddr( &out->connect_to.generic, s_upstream_address ),
|
||||
"Couldn't parse upstream address '%s'",
|
||||
s_upstream_address);
|
||||
s_upstream_address
|
||||
);
|
||||
|
||||
FATAL_IF_NULL( s_upstream_port, "Upstream port not specified" );
|
||||
NULLCHECK( s_upstream_port );
|
||||
parse_port( s_upstream_port, &out->connect_to.v4 );
|
||||
|
||||
if ( s_upstream_bind ) {
|
||||
FATAL_IF_ZERO(parse_ip_to_sockaddr
|
||||
(&out->connect_from.generic, s_upstream_bind),
|
||||
"Couldn't parse bind address '%s'", s_upstream_bind);
|
||||
FATAL_IF_ZERO(
|
||||
parse_ip_to_sockaddr( &out->connect_from.generic, s_upstream_bind ),
|
||||
"Couldn't parse bind address '%s'",
|
||||
s_upstream_bind
|
||||
);
|
||||
out->bind = 1;
|
||||
}
|
||||
|
||||
@@ -70,28 +76,21 @@ struct proxier *proxy_create(char *s_downstream_address,
|
||||
}
|
||||
|
||||
out->init.buf = xmalloc( sizeof( struct nbd_init_raw ) );
|
||||
out->req.buf = xmalloc( NBD_MAX_SIZE );
|
||||
out->rsp.buf = xmalloc( NBD_MAX_SIZE );
|
||||
|
||||
/* Add on the request / reply size to our malloc to accommodate both
|
||||
* the struct and the data
|
||||
*/
|
||||
out->req.buf = xmalloc(NBD_MAX_SIZE + NBD_REQUEST_SIZE);
|
||||
out->rsp.buf = xmalloc(NBD_MAX_SIZE + NBD_REPLY_SIZE);
|
||||
|
||||
log_context =
|
||||
xmalloc(strlen(s_upstream_address) + strlen(s_upstream_port) + 2);
|
||||
log_context = xmalloc( strlen(s_upstream_address) + strlen(s_upstream_port) + 2 );
|
||||
sprintf(log_context, "%s:%s", s_upstream_address, s_upstream_port);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
int proxy_prefetches(struct proxier *proxy)
|
||||
{
|
||||
int proxy_prefetches( struct proxier* proxy ) {
|
||||
NULLCHECK( proxy );
|
||||
return proxy->prefetch != NULL;
|
||||
}
|
||||
|
||||
int proxy_prefetch_bufsize(struct proxier *proxy)
|
||||
{
|
||||
int proxy_prefetch_bufsize( struct proxier* proxy ){
|
||||
NULLCHECK( proxy );
|
||||
return prefetch_size( proxy->prefetch );
|
||||
}
|
||||
@@ -107,8 +106,7 @@ void proxy_destroy(struct proxier *proxy)
|
||||
}
|
||||
|
||||
/* Shared between our two different connect_to_upstream paths */
|
||||
void proxy_finish_connect_to_upstream(struct proxier *proxy, uint64_t size,
|
||||
uint32_t flags);
|
||||
void proxy_finish_connect_to_upstream( struct proxier *proxy, uint64_t size );
|
||||
|
||||
/* Try to establish a connection to our upstream server. Return 1 on success,
|
||||
* 0 on failure. this is a blocking call that returns a non-blocking socket.
|
||||
@@ -122,22 +120,22 @@ int proxy_connect_to_upstream(struct proxier *proxy)
|
||||
|
||||
int fd = socket_connect( &proxy->connect_to.generic, connect_from );
|
||||
uint64_t size = 0;
|
||||
uint32_t flags = 0;
|
||||
|
||||
if ( -1 == fd ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!socket_nbd_read_hello(fd, &size, &flags)) {
|
||||
WARN_IF_NEGATIVE(sock_try_close(fd),
|
||||
"Couldn't close() after failed read of NBD hello on fd %i",
|
||||
fd);
|
||||
if( !socket_nbd_read_hello( fd, &size ) ) {
|
||||
WARN_IF_NEGATIVE(
|
||||
sock_try_close( fd ),
|
||||
"Couldn't close() after failed read of NBD hello on fd %i", fd
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
proxy->upstream_fd = fd;
|
||||
sock_set_nonblock( fd, 1 );
|
||||
proxy_finish_connect_to_upstream(proxy, size, flags);
|
||||
proxy_finish_connect_to_upstream( proxy, size );
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -158,8 +156,7 @@ void proxy_start_connect_to_upstream(struct proxier *proxy)
|
||||
fd = socket( to->sa_family , SOCK_STREAM, 0 );
|
||||
|
||||
if( fd < 0 ) {
|
||||
warn(SHOW_ERRNO
|
||||
("Couldn't create socket to reconnect to upstream"));
|
||||
warn( SHOW_ERRNO( "Couldn't create socket to reconnect to upstream" ) );
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -194,28 +191,19 @@ void proxy_start_connect_to_upstream(struct proxier *proxy)
|
||||
return;
|
||||
}
|
||||
|
||||
void proxy_finish_connect_to_upstream(struct proxier *proxy, uint64_t size,
|
||||
uint32_t flags)
|
||||
{
|
||||
void proxy_finish_connect_to_upstream( struct proxier *proxy, uint64_t size ) {
|
||||
|
||||
if ( proxy->upstream_size == 0 ) {
|
||||
info( "Size of upstream image is %"PRIu64" bytes", size );
|
||||
} else if ( proxy->upstream_size != size ) {
|
||||
warn("Size changed from %" PRIu64 " to %" PRIu64 " bytes",
|
||||
proxy->upstream_size, size);
|
||||
warn(
|
||||
"Size changed from %"PRIu64" to %"PRIu64" bytes",
|
||||
proxy->upstream_size, size
|
||||
);
|
||||
}
|
||||
|
||||
proxy->upstream_size = size;
|
||||
|
||||
if (proxy->upstream_flags == 0) {
|
||||
info("Upstream transmission flags set to %" PRIu32 "", flags);
|
||||
} else if (proxy->upstream_flags != flags) {
|
||||
warn("Upstream transmission flags changed from %" PRIu32 " to %"
|
||||
PRIu32 "", proxy->upstream_flags, flags);
|
||||
}
|
||||
|
||||
proxy->upstream_flags = flags;
|
||||
|
||||
if ( AF_UNIX != proxy->connect_to.family ) {
|
||||
if ( sock_set_tcp_nodelay( proxy->upstream_fd, 1 ) == -1 ) {
|
||||
warn( SHOW_ERRNO( "Failed to set TCP_NODELAY" ) );
|
||||
@@ -233,9 +221,11 @@ void proxy_disconnect_from_upstream(struct proxier *proxy)
|
||||
info("Closing upstream connection on fd %i", proxy->upstream_fd );
|
||||
|
||||
/* TODO: An NBD disconnect would be pleasant here */
|
||||
WARN_IF_NEGATIVE(sock_try_close(proxy->upstream_fd),
|
||||
WARN_IF_NEGATIVE(
|
||||
sock_try_close( proxy->upstream_fd ),
|
||||
"Failed to close() fd %i when disconnecting from upstream",
|
||||
proxy->upstream_fd);
|
||||
proxy->upstream_fd
|
||||
);
|
||||
proxy->upstream_fd = -1;
|
||||
}
|
||||
}
|
||||
@@ -247,28 +237,31 @@ void proxy_open_listen_socket(struct proxier *params)
|
||||
NULLCHECK( params );
|
||||
|
||||
params->listen_fd = socket(params->listen_on.family, SOCK_STREAM, 0);
|
||||
FATAL_IF_NEGATIVE(params->listen_fd,
|
||||
SHOW_ERRNO("Couldn't create listen socket")
|
||||
FATAL_IF_NEGATIVE(
|
||||
params->listen_fd, SHOW_ERRNO( "Couldn't create listen socket" )
|
||||
);
|
||||
|
||||
/* Allow us to restart quickly */
|
||||
FATAL_IF_NEGATIVE(sock_set_reuseaddr(params->listen_fd, 1),
|
||||
FATAL_IF_NEGATIVE(
|
||||
sock_set_reuseaddr(params->listen_fd, 1),
|
||||
SHOW_ERRNO( "Couldn't set SO_REUSEADDR" )
|
||||
);
|
||||
|
||||
if( AF_UNIX != params->listen_on.family ) {
|
||||
FATAL_IF_NEGATIVE(sock_set_tcp_nodelay(params->listen_fd, 1),
|
||||
FATAL_IF_NEGATIVE(
|
||||
sock_set_tcp_nodelay(params->listen_fd, 1),
|
||||
SHOW_ERRNO( "Couldn't set TCP_NODELAY" )
|
||||
);
|
||||
}
|
||||
|
||||
FATAL_UNLESS_ZERO(sock_try_bind
|
||||
(params->listen_fd, ¶ms->listen_on.generic),
|
||||
FATAL_UNLESS_ZERO(
|
||||
sock_try_bind( params->listen_fd, ¶ms->listen_on.generic ),
|
||||
SHOW_ERRNO( "Failed to bind to listening socket" )
|
||||
);
|
||||
|
||||
/* We're only serving one client at a time, hence backlog of 1 */
|
||||
FATAL_IF_NEGATIVE(listen(params->listen_fd, 1),
|
||||
FATAL_IF_NEGATIVE(
|
||||
listen(params->listen_fd, 1),
|
||||
SHOW_ERRNO( "Failed to listen on listening socket" )
|
||||
);
|
||||
|
||||
@@ -299,8 +292,8 @@ static char *proxy_session_state_names[] = {
|
||||
|
||||
static inline int proxy_state_upstream( int state )
|
||||
{
|
||||
return state == CONNECT_TO_UPSTREAM || state == READ_INIT_FROM_UPSTREAM
|
||||
|| state == WRITE_TO_UPSTREAM || state == READ_FROM_UPSTREAM;
|
||||
return state == CONNECT_TO_UPSTREAM || state == READ_INIT_FROM_UPSTREAM ||
|
||||
state == WRITE_TO_UPSTREAM || state == READ_FROM_UPSTREAM;
|
||||
}
|
||||
|
||||
int proxy_prefetch_for_request( struct proxier* proxy, int state )
|
||||
@@ -309,12 +302,10 @@ int proxy_prefetch_for_request(struct proxier *proxy, int state)
|
||||
struct nbd_request* req = &proxy->req_hdr;
|
||||
struct nbd_reply* rsp = &proxy->rsp_hdr;
|
||||
|
||||
struct nbd_request_raw *req_raw =
|
||||
(struct nbd_request_raw *) proxy->req.buf;
|
||||
struct nbd_reply_raw *rsp_raw =
|
||||
(struct nbd_reply_raw *) proxy->rsp.buf;
|
||||
struct nbd_request_raw* req_raw = (struct nbd_request_raw*) proxy->req.buf;
|
||||
struct nbd_reply_raw *rsp_raw = (struct nbd_reply_raw*) proxy->rsp.buf;
|
||||
|
||||
int is_read = req->type == REQUEST_READ;
|
||||
int is_read = ( req->type & REQUEST_MASK ) == REQUEST_READ;
|
||||
|
||||
if ( is_read ) {
|
||||
/* See if we can respond with what's in our prefetch
|
||||
@@ -333,8 +324,11 @@ int proxy_prefetch_for_request(struct proxier *proxy, int state)
|
||||
nbd_h2r_reply( rsp, rsp_raw );
|
||||
|
||||
/* and the data */
|
||||
memcpy(proxy->rsp.buf + NBD_REPLY_SIZE,
|
||||
prefetch_offset(proxy->prefetch, req->from), req->len);
|
||||
memcpy(
|
||||
proxy->rsp.buf + NBD_REPLY_SIZE,
|
||||
prefetch_offset( proxy->prefetch, req->from ),
|
||||
req->len
|
||||
);
|
||||
|
||||
proxy->rsp.size = NBD_REPLY_SIZE + req->len;
|
||||
proxy->rsp.needle = 0;
|
||||
@@ -342,14 +336,14 @@ int proxy_prefetch_for_request(struct proxier *proxy, int state)
|
||||
/* return early, our work here is done */
|
||||
return WRITE_TO_DOWNSTREAM;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
/* Safety catch. If we're sending a write request, we
|
||||
* blow away the cache. This is very pessimistic, but
|
||||
* it's simpler (and therefore safer) than working out
|
||||
* whether we can keep it or not.
|
||||
*/
|
||||
debug("Blowing away prefetch cache on type %d request.",
|
||||
req->type);
|
||||
debug( "Blowing away prefetch cache on type %d request.", req->type );
|
||||
prefetch_set_is_empty( proxy->prefetch );
|
||||
}
|
||||
|
||||
@@ -397,12 +391,13 @@ int proxy_prefetch_for_reply(struct proxier *proxy, int state)
|
||||
prefetched_bytes = proxy->req_hdr.len - proxy->prefetch_req_orig_len;
|
||||
|
||||
debug( "Prefetched additional %d bytes", prefetched_bytes );
|
||||
memcpy(proxy->prefetch->buffer,
|
||||
memcpy(
|
||||
proxy->prefetch->buffer,
|
||||
proxy->rsp.buf + proxy->prefetch_req_orig_len + NBD_REPLY_SIZE,
|
||||
prefetched_bytes);
|
||||
prefetched_bytes
|
||||
);
|
||||
|
||||
proxy->prefetch->from =
|
||||
proxy->req_hdr.from + proxy->prefetch_req_orig_len;
|
||||
proxy->prefetch->from = proxy->req_hdr.from + proxy->prefetch_req_orig_len;
|
||||
proxy->prefetch->len = prefetched_bytes;
|
||||
|
||||
/* We've finished with proxy->req by now, so don't need to alter it to make
|
||||
@@ -425,14 +420,12 @@ int proxy_read_from_downstream(struct proxier *proxy, int state)
|
||||
{
|
||||
ssize_t count;
|
||||
|
||||
struct nbd_request_raw *request_raw =
|
||||
(struct nbd_request_raw *) proxy->req.buf;
|
||||
struct nbd_request_raw* request_raw = (struct nbd_request_raw*) proxy->req.buf;
|
||||
struct nbd_request* request = &(proxy->req_hdr);
|
||||
|
||||
// assert( state == READ_FROM_DOWNSTREAM );
|
||||
|
||||
count =
|
||||
iobuf_read(proxy->downstream_fd, &proxy->req, NBD_REQUEST_SIZE);
|
||||
count = iobuf_read( proxy->downstream_fd, &proxy->req, NBD_REQUEST_SIZE );
|
||||
|
||||
if ( count == -1 ) {
|
||||
warn( SHOW_ERRNO( "Couldn't read request from downstream" ) );
|
||||
@@ -442,26 +435,21 @@ int proxy_read_from_downstream(struct proxier *proxy, int state)
|
||||
if ( proxy->req.needle == NBD_REQUEST_SIZE ) {
|
||||
nbd_r2h_request( request_raw, request );
|
||||
|
||||
if (request->type == REQUEST_DISCONNECT) {
|
||||
if ( ( request->type & REQUEST_MASK ) == REQUEST_DISCONNECT ) {
|
||||
info( "Received disconnect request from client" );
|
||||
return EXIT;
|
||||
}
|
||||
|
||||
/* Simple validations -- the request / reply size have already
|
||||
* been taken into account in the xmalloc, so no need to worry
|
||||
* about them here
|
||||
*/
|
||||
if (request->type == REQUEST_READ) {
|
||||
if (request->len > NBD_MAX_SIZE) {
|
||||
warn("NBD read request size %" PRIu32 " too large",
|
||||
request->len);
|
||||
/* Simple validations */
|
||||
if ( ( request->type & REQUEST_MASK ) == REQUEST_READ ) {
|
||||
if (request->len > ( NBD_MAX_SIZE - NBD_REPLY_SIZE ) ) {
|
||||
warn( "NBD read request size %"PRIu32" too large", request->len );
|
||||
return EXIT;
|
||||
}
|
||||
}
|
||||
if (request->type == REQUEST_WRITE) {
|
||||
if (request->len > NBD_MAX_SIZE) {
|
||||
warn("NBD write request size %" PRIu32 " too large",
|
||||
request->len);
|
||||
if ( (request->type & REQUEST_MASK ) == REQUEST_WRITE ) {
|
||||
if (request->len > ( NBD_MAX_SIZE - NBD_REQUEST_SIZE ) ) {
|
||||
warn( "NBD write request size %"PRIu32" too large", request->len );
|
||||
return EXIT;
|
||||
}
|
||||
|
||||
@@ -470,9 +458,10 @@ int proxy_read_from_downstream(struct proxier *proxy, int state)
|
||||
}
|
||||
|
||||
if ( proxy->req.needle == proxy->req.size ) {
|
||||
debug("Received NBD request from downstream. type=%" PRIu16
|
||||
" flags=%" PRIu16 " from=%" PRIu64 " len=%" PRIu32,
|
||||
request->type, request->flags, request->from, request->len);
|
||||
debug(
|
||||
"Received NBD request from downstream. type=%"PRIu32" from=%"PRIu64" len=%"PRIu32,
|
||||
request->type, request->from, request->len
|
||||
);
|
||||
|
||||
/* Finished reading, so advance state. Leave size untouched so the next
|
||||
* state knows how many bytes to write */
|
||||
@@ -490,8 +479,9 @@ int proxy_continue_connecting_to_upstream(struct proxier *proxy, int state)
|
||||
|
||||
// assert( state == CONNECT_TO_UPSTREAM );
|
||||
|
||||
result =
|
||||
getsockopt(proxy->upstream_fd, SOL_SOCKET, SO_ERROR, &error, &len);
|
||||
result = getsockopt(
|
||||
proxy->upstream_fd, SOL_SOCKET, SO_ERROR, &error, &len
|
||||
);
|
||||
|
||||
if ( result == -1 ) {
|
||||
warn( SHOW_ERRNO( "Failed to tell if connected to upstream" ) );
|
||||
@@ -517,9 +507,7 @@ int proxy_read_init_from_upstream(struct proxier *proxy, int state)
|
||||
|
||||
// assert( state == READ_INIT_FROM_UPSTREAM );
|
||||
|
||||
count =
|
||||
iobuf_read(proxy->upstream_fd, &proxy->init,
|
||||
sizeof(struct nbd_init_raw));
|
||||
count = iobuf_read( proxy->upstream_fd, &proxy->init, sizeof( struct nbd_init_raw ) );
|
||||
|
||||
if ( count == -1 ) {
|
||||
warn( SHOW_ERRNO( "Failed to read init from upstream" ) );
|
||||
@@ -528,18 +516,11 @@ int proxy_read_init_from_upstream(struct proxier *proxy, int state)
|
||||
|
||||
if ( proxy->init.needle == proxy->init.size ) {
|
||||
uint64_t upstream_size;
|
||||
uint32_t upstream_flags;
|
||||
if (!nbd_check_hello
|
||||
((struct nbd_init_raw *) proxy->init.buf, &upstream_size,
|
||||
&upstream_flags)) {
|
||||
if ( !nbd_check_hello( (struct nbd_init_raw*) proxy->init.buf, &upstream_size ) ) {
|
||||
warn( "Upstream sent invalid init" );
|
||||
goto disconnect;
|
||||
}
|
||||
|
||||
/* record the flags, and log the reconnection, set TCP_NODELAY */
|
||||
proxy_finish_connect_to_upstream(proxy, upstream_size,
|
||||
upstream_flags);
|
||||
|
||||
/* Currently, we only get disconnected from upstream (so needing to come
|
||||
* here) when we have an outstanding request. If that becomes false,
|
||||
* we'll need to choose the right state to return to here */
|
||||
@@ -603,8 +584,7 @@ int proxy_read_from_upstream(struct proxier *proxy, int state)
|
||||
ssize_t count;
|
||||
|
||||
struct nbd_reply* reply = &(proxy->rsp_hdr);
|
||||
struct nbd_reply_raw *reply_raw =
|
||||
(struct nbd_reply_raw *) proxy->rsp.buf;
|
||||
struct nbd_reply_raw* reply_raw = (struct nbd_reply_raw*) proxy->rsp.buf;
|
||||
|
||||
/* We can't assume the NBD_REPLY_SIZE + req->len is what we'll get back */
|
||||
count = iobuf_read( proxy->upstream_fd, &proxy->rsp, NBD_REPLY_SIZE );
|
||||
@@ -622,7 +602,12 @@ int proxy_read_from_upstream(struct proxier *proxy, int state)
|
||||
goto disconnect;
|
||||
}
|
||||
|
||||
if (proxy->req_hdr.type == REQUEST_READ) {
|
||||
if ( reply->error != 0 ) {
|
||||
warn( "NBD error returned from upstream: %"PRIu32, reply->error );
|
||||
goto disconnect;
|
||||
}
|
||||
|
||||
if ( ( proxy->req_hdr.type & REQUEST_MASK ) == REQUEST_READ ) {
|
||||
/* Get the read reply data too. */
|
||||
proxy->rsp.size += proxy->req_hdr.len;
|
||||
}
|
||||
@@ -698,8 +683,7 @@ void proxy_session(struct proxier *proxy)
|
||||
|
||||
|
||||
/* First action: Write hello to downstream */
|
||||
nbd_hello_to_buf((struct nbd_init_raw *) proxy->rsp.buf,
|
||||
proxy->upstream_size, proxy->upstream_flags);
|
||||
nbd_hello_to_buf( (struct nbd_init_raw *) proxy->rsp.buf, proxy->upstream_size );
|
||||
proxy->rsp.size = sizeof( struct nbd_init_raw );
|
||||
proxy->rsp.needle = 0;
|
||||
state = WRITE_TO_DOWNSTREAM;
|
||||
@@ -727,13 +711,13 @@ void proxy_session(struct proxier *proxy)
|
||||
if ( state != old_state ) {
|
||||
state_started = monotonic_time_ms();
|
||||
|
||||
debug("State transition from %s to %s",
|
||||
debug(
|
||||
"State transition from %s to %s",
|
||||
proxy_session_state_names[old_state],
|
||||
proxy_session_state_names[state]
|
||||
);
|
||||
} else {
|
||||
debug("Proxy is in state %s", proxy_session_state_names[state],
|
||||
state);
|
||||
debug( "Proxy is in state %s", proxy_session_state_names[state], state );
|
||||
}
|
||||
|
||||
old_state = state;
|
||||
@@ -778,9 +762,7 @@ void proxy_session(struct proxier *proxy)
|
||||
select_timeout_ptr = &select_timeout;
|
||||
}
|
||||
|
||||
result =
|
||||
sock_try_select(FD_SETSIZE, &rfds, &wfds, NULL,
|
||||
select_timeout_ptr);
|
||||
result = sock_try_select( FD_SETSIZE, &rfds, &wfds, NULL, select_timeout_ptr );
|
||||
|
||||
if ( result == -1 ) {
|
||||
warn( SHOW_ERRNO( "select() failed: " ) );
|
||||
@@ -806,8 +788,7 @@ void proxy_session(struct proxier *proxy)
|
||||
break;
|
||||
case CONNECT_TO_UPSTREAM:
|
||||
if ( FD_ISSET( proxy->upstream_fd, &wfds ) ) {
|
||||
state =
|
||||
proxy_continue_connecting_to_upstream(proxy, state);
|
||||
state = proxy_continue_connecting_to_upstream( proxy, state );
|
||||
}
|
||||
/* Leaving state untouched will retry connecting to upstream -
|
||||
* so introduce a bit of sleep */
|
||||
@@ -849,7 +830,9 @@ void proxy_session(struct proxier *proxy)
|
||||
*/
|
||||
if ( old_state == state && proxy_state_upstream( state ) ) {
|
||||
if ( ( monotonic_time_ms() ) - state_started > UPSTREAM_TIMEOUT ) {
|
||||
warn("Timed out in state %s while communicating with upstream", proxy_session_state_names[state]
|
||||
warn(
|
||||
"Timed out in state %s while communicating with upstream",
|
||||
proxy_session_state_names[state]
|
||||
);
|
||||
state = CONNECT_TO_UPSTREAM;
|
||||
|
||||
@@ -863,8 +846,10 @@ void proxy_session(struct proxier *proxy)
|
||||
}
|
||||
}
|
||||
|
||||
info("Finished proxy session on fd %i after %" PRIu64
|
||||
" successful request(s)", proxy->downstream_fd, proxy->req_count);
|
||||
info(
|
||||
"Finished proxy session on fd %i after %"PRIu64" successful request(s)",
|
||||
proxy->downstream_fd, proxy->req_count
|
||||
);
|
||||
|
||||
/* Reset these two for the next session */
|
||||
proxy->req_count = 0;
|
||||
@@ -889,13 +874,13 @@ int proxy_accept(struct proxier *params)
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(params->listen_fd, &fds);
|
||||
|
||||
FATAL_IF_NEGATIVE(sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
|
||||
FATAL_IF_NEGATIVE(
|
||||
sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
|
||||
SHOW_ERRNO( "select() failed" )
|
||||
);
|
||||
|
||||
if ( FD_ISSET( params->listen_fd, &fds ) ) {
|
||||
client_fd =
|
||||
accept(params->listen_fd, &client_address.generic, &socklen);
|
||||
client_fd = accept( params->listen_fd, &client_address.generic, &socklen );
|
||||
|
||||
if ( client_address.family != AF_UNIX ) {
|
||||
if ( sock_set_tcp_nodelay(client_fd, 1) == -1 ) {
|
||||
@@ -908,9 +893,11 @@ int proxy_accept(struct proxier *params)
|
||||
params->downstream_fd = client_fd;
|
||||
proxy_session( params );
|
||||
|
||||
WARN_IF_NEGATIVE(sock_try_close(params->downstream_fd),
|
||||
WARN_IF_NEGATIVE(
|
||||
sock_try_close( params->downstream_fd ),
|
||||
"Couldn't close() downstram fd %i after proxy session",
|
||||
params->downstream_fd);
|
||||
params->downstream_fd
|
||||
);
|
||||
params->downstream_fd = -1;
|
||||
}
|
||||
|
||||
@@ -935,31 +922,33 @@ void proxy_cleanup(struct proxier *proxy)
|
||||
|
||||
if ( AF_UNIX == proxy->listen_on.family ) {
|
||||
if ( -1 == unlink( proxy->listen_on.un.sun_path ) ) {
|
||||
warn(SHOW_ERRNO
|
||||
("Failed to unlink %s",
|
||||
proxy->listen_on.un.sun_path));
|
||||
warn( SHOW_ERRNO( "Failed to unlink %s", proxy->listen_on.un.sun_path ) );
|
||||
}
|
||||
}
|
||||
|
||||
WARN_IF_NEGATIVE(sock_try_close(proxy->listen_fd),
|
||||
SHOW_ERRNO("Failed to close() listen fd %i",
|
||||
proxy->listen_fd)
|
||||
WARN_IF_NEGATIVE(
|
||||
sock_try_close( proxy->listen_fd ),
|
||||
SHOW_ERRNO( "Failed to close() listen fd %i", proxy->listen_fd )
|
||||
);
|
||||
proxy->listen_fd = -1;
|
||||
}
|
||||
|
||||
if ( -1 != proxy->downstream_fd ) {
|
||||
WARN_IF_NEGATIVE(sock_try_close(proxy->downstream_fd),
|
||||
SHOW_ERRNO("Failed to close() downstream fd %i",
|
||||
proxy->downstream_fd)
|
||||
WARN_IF_NEGATIVE(
|
||||
sock_try_close( proxy->downstream_fd ),
|
||||
SHOW_ERRNO(
|
||||
"Failed to close() downstream fd %i", proxy->downstream_fd
|
||||
)
|
||||
);
|
||||
proxy->downstream_fd = -1;
|
||||
}
|
||||
|
||||
if ( -1 != proxy->upstream_fd ) {
|
||||
WARN_IF_NEGATIVE(sock_try_close(proxy->upstream_fd),
|
||||
SHOW_ERRNO("Failed to close() upstream fd %i",
|
||||
proxy->upstream_fd)
|
||||
WARN_IF_NEGATIVE(
|
||||
sock_try_close( proxy->upstream_fd ),
|
||||
SHOW_ERRNO(
|
||||
"Failed to close() upstream fd %i", proxy->upstream_fd
|
||||
)
|
||||
);
|
||||
proxy->upstream_fd = -1;
|
||||
}
|
||||
@@ -986,3 +975,4 @@ int do_proxy(struct proxier *params)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -46,9 +46,6 @@ struct proxier {
|
||||
/* This is the size we advertise to the downstream server */
|
||||
uint64_t upstream_size;
|
||||
|
||||
/* These are the transmission flags sent as part of the handshake */
|
||||
uint32_t upstream_flags;
|
||||
|
||||
/* We transform the raw request header into here */
|
||||
struct nbd_request req_hdr;
|
||||
|
||||
@@ -85,13 +82,16 @@ struct proxier {
|
||||
/** */
|
||||
};
|
||||
|
||||
struct proxier *proxy_create(char *s_downstream_address,
|
||||
struct proxier* proxy_create(
|
||||
char* s_downstream_address,
|
||||
char* s_downstream_port,
|
||||
char* s_upstream_address,
|
||||
char* s_upstream_port,
|
||||
char *s_upstream_bind, char *s_cache_bytes);
|
||||
char* s_upstream_bind,
|
||||
char* s_cache_bytes);
|
||||
int do_proxy( struct proxier* proxy );
|
||||
void proxy_cleanup( struct proxier* proxy );
|
||||
void proxy_destroy( struct proxier* proxy );
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -22,9 +22,7 @@ static int testmasks[9] = { 0, 128, 192, 224, 240, 248, 252, 254, 255 };
|
||||
/** Test whether AF_INET or AF_INET6 sockaddr is included in the given access
|
||||
* control list, returning 1 if it is, and 0 if not.
|
||||
*/
|
||||
static int is_included_in_acl(int list_length,
|
||||
struct ip_and_mask (*list)[],
|
||||
union mysockaddr *test)
|
||||
static int is_included_in_acl(int list_length, struct ip_and_mask (*list)[], union mysockaddr* test)
|
||||
{
|
||||
NULLCHECK( test );
|
||||
|
||||
@@ -35,8 +33,7 @@ static int is_included_in_acl(int list_length,
|
||||
int testbits;
|
||||
unsigned char *raw_address1 = NULL, *raw_address2 = NULL;
|
||||
|
||||
debug("checking acl entry %d (%d/%d)", i, test->generic.sa_family,
|
||||
entry->ip.family);
|
||||
debug("checking acl entry %d (%d/%d)", i, test->generic.sa_family, entry->ip.family);
|
||||
|
||||
if (test->generic.sa_family != entry->ip.family) {
|
||||
continue;
|
||||
@@ -46,24 +43,24 @@ static int is_included_in_acl(int list_length,
|
||||
debug("it's an AF_INET");
|
||||
raw_address1 = (unsigned char*) &test->v4.sin_addr;
|
||||
raw_address2 = (unsigned char*) &entry->ip.v4.sin_addr;
|
||||
} else if (test->generic.sa_family == AF_INET6) {
|
||||
}
|
||||
else if (test->generic.sa_family == AF_INET6) {
|
||||
debug("it's an AF_INET6");
|
||||
raw_address1 = (unsigned char*) &test->v6.sin6_addr;
|
||||
raw_address2 = (unsigned char*) &entry->ip.v6.sin6_addr;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
fatal( "Can't check an ACL for this address type." );
|
||||
}
|
||||
|
||||
debug("testbits=%d", entry->mask);
|
||||
|
||||
for (testbits = entry->mask; testbits > 0; testbits -= 8) {
|
||||
debug("testbits=%d, c1=%02x, c2=%02x", testbits,
|
||||
raw_address1[0], raw_address2[0]);
|
||||
debug("testbits=%d, c1=%02x, c2=%02x", testbits, raw_address1[0], raw_address2[0]);
|
||||
if (testbits >= 8) {
|
||||
if (raw_address1[0] != raw_address2[0]) {
|
||||
goto no_match;
|
||||
if (raw_address1[0] != raw_address2[0]) { goto no_match; }
|
||||
}
|
||||
} else {
|
||||
else {
|
||||
if ((raw_address1[0] & testmasks[testbits%8]) !=
|
||||
(raw_address2[0] & testmasks[testbits%8]) ) {
|
||||
goto no_match;
|
||||
@@ -89,7 +86,8 @@ int acl_includes(struct acl *acl, union mysockaddr *addr)
|
||||
|
||||
if ( 0 == acl->len ) {
|
||||
return !( acl->default_deny );
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return is_included_in_acl( acl->len, acl->entries, addr );
|
||||
}
|
||||
}
|
||||
@@ -107,3 +105,4 @@ void acl_destroy(struct acl *acl)
|
||||
acl->entries = NULL;
|
||||
free( acl );
|
||||
}
|
||||
|
||||
|
@@ -30,41 +30,30 @@ typedef bitfield_word_t *bitfield_p;
|
||||
((_bytes + (BITFIELD_WORD_SIZE-1)) / BITFIELD_WORD_SIZE)
|
||||
|
||||
/** Return the bit value ''idx'' in array ''b'' */
|
||||
static inline int bit_get(bitfield_p b, uint64_t idx)
|
||||
{
|
||||
static inline int bit_get(bitfield_p b, uint64_t idx) {
|
||||
return (BIT_WORD(b, idx) >> (idx & (BITS_PER_WORD-1))) & 1;
|
||||
}
|
||||
|
||||
/** Return 1 if the bit at ''idx'' in array ''b'' is set */
|
||||
static inline int bit_is_set(bitfield_p b, uint64_t idx)
|
||||
{
|
||||
static inline int bit_is_set(bitfield_p b, uint64_t idx) {
|
||||
return bit_get(b, idx);
|
||||
}
|
||||
|
||||
/** Return 1 if the bit at ''idx'' in array ''b'' is clear */
|
||||
static inline int bit_is_clear(bitfield_p b, uint64_t idx)
|
||||
{
|
||||
static inline int bit_is_clear(bitfield_p b, uint64_t idx) {
|
||||
return !bit_get(b, idx);
|
||||
}
|
||||
|
||||
/** Tests whether the bit at ''idx'' in array ''b'' has value ''value'' */
|
||||
static inline int bit_has_value(bitfield_p b, uint64_t idx, int value)
|
||||
{
|
||||
static inline int bit_has_value(bitfield_p b, uint64_t idx, int value) {
|
||||
return bit_get(b, idx) == !!value;
|
||||
}
|
||||
|
||||
/** Sets the bit ''idx'' in array ''b'' */
|
||||
static inline void bit_set(bitfield_p b, uint64_t idx)
|
||||
{
|
||||
static inline void bit_set(bitfield_p b, uint64_t idx) {
|
||||
BIT_WORD(b, idx) |= BIT_MASK(idx);
|
||||
}
|
||||
|
||||
/** Clears the bit ''idx'' in array ''b'' */
|
||||
static inline void bit_clear(bitfield_p b, uint64_t idx)
|
||||
{
|
||||
static inline void bit_clear(bitfield_p b, uint64_t idx) {
|
||||
BIT_WORD(b, idx) &= ~BIT_MASK(idx);
|
||||
}
|
||||
|
||||
/** Sets ''len'' bits in array ''b'' starting at offset ''from'' */
|
||||
static inline void bit_set_range(bitfield_p b, uint64_t from, uint64_t len)
|
||||
{
|
||||
@@ -83,10 +72,8 @@ static inline void bit_set_range(bitfield_p b, uint64_t from, uint64_t len)
|
||||
bit_set( b, from++ );
|
||||
}
|
||||
}
|
||||
|
||||
/** Clears ''len'' bits in array ''b'' starting at offset ''from'' */
|
||||
static inline void bit_clear_range(bitfield_p b, uint64_t from,
|
||||
uint64_t len)
|
||||
static inline void bit_clear_range(bitfield_p b, uint64_t from, uint64_t len)
|
||||
{
|
||||
for ( ; (from % BITS_PER_WORD) != 0 && len > 0 ; len-- ) {
|
||||
bit_clear( b, from++ );
|
||||
@@ -109,9 +96,7 @@ static inline void bit_clear_range(bitfield_p b, uint64_t from,
|
||||
* bits that are the same as the first one specified. If ''run_is_set'' is
|
||||
* non-NULL, the value of that bit is placed into it.
|
||||
*/
|
||||
static inline uint64_t bit_run_count(bitfield_p b, uint64_t from,
|
||||
uint64_t len, int *run_is_set)
|
||||
{
|
||||
static inline uint64_t bit_run_count(bitfield_p b, uint64_t from, uint64_t len, int *run_is_set) {
|
||||
uint64_t count = 0;
|
||||
int first_value = bit_get(b, from);
|
||||
bitfield_word_t word_match = first_value ? -1 : 0;
|
||||
@@ -200,10 +185,8 @@ static inline struct bitset *bitset_alloc(uint64_t size, int resolution)
|
||||
// calculate a size to allocate that is a multiple of the size of the
|
||||
// bitfield word
|
||||
size_t bitfield_size =
|
||||
BIT_WORDS_FOR_SIZE(((size + resolution -
|
||||
1) / resolution)) * sizeof(bitfield_word_t);
|
||||
struct bitset *bitset =
|
||||
xmalloc(sizeof(struct bitset) + (bitfield_size / 8));
|
||||
BIT_WORDS_FOR_SIZE((( size + resolution - 1 ) / resolution)) * sizeof( bitfield_word_t );
|
||||
struct bitset *bitset = xmalloc(sizeof( struct bitset ) + ( bitfield_size / 8 ) );
|
||||
|
||||
bitset->size = size;
|
||||
bitset->resolution = resolution;
|
||||
@@ -241,9 +224,12 @@ static inline void bitset_free(struct bitset *set)
|
||||
FATAL_IF_NEGATIVE(pthread_mutex_unlock(&set->lock), "Error unlocking bitset")
|
||||
|
||||
|
||||
static inline void bitset_stream_enqueue(struct bitset *set,
|
||||
static inline void bitset_stream_enqueue(
|
||||
struct bitset * set,
|
||||
enum bitset_stream_events event,
|
||||
uint64_t from, uint64_t len)
|
||||
uint64_t from,
|
||||
uint64_t len
|
||||
)
|
||||
{
|
||||
struct bitset_stream * stream = set->stream;
|
||||
|
||||
@@ -268,8 +254,10 @@ static inline void bitset_stream_enqueue(struct bitset *set,
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void bitset_stream_dequeue(struct bitset *set,
|
||||
struct bitset_stream_entry *out)
|
||||
static inline void bitset_stream_dequeue(
|
||||
struct bitset * set,
|
||||
struct bitset_stream_entry * out
|
||||
)
|
||||
{
|
||||
struct bitset_stream * stream = set->stream;
|
||||
struct bitset_stream_entry * dequeued;
|
||||
@@ -310,9 +298,10 @@ static inline size_t bitset_stream_size(struct bitset *set)
|
||||
return size;
|
||||
}
|
||||
|
||||
static inline uint64_t bitset_stream_queued_bytes(struct bitset *set,
|
||||
enum bitset_stream_events
|
||||
event)
|
||||
static inline uint64_t bitset_stream_queued_bytes(
|
||||
struct bitset * set,
|
||||
enum bitset_stream_events event
|
||||
)
|
||||
{
|
||||
uint64_t total;
|
||||
|
||||
@@ -342,8 +331,10 @@ static inline void bitset_disable_stream(struct bitset *set)
|
||||
/** Set the bits in a bitset which correspond to the given bytes in the larger
|
||||
* file.
|
||||
*/
|
||||
static inline void bitset_set_range(struct bitset *set,
|
||||
uint64_t from, uint64_t len)
|
||||
static inline void bitset_set_range(
|
||||
struct bitset * set,
|
||||
uint64_t from,
|
||||
uint64_t len)
|
||||
{
|
||||
INT_FIRST_AND_LAST;
|
||||
BITSET_LOCK;
|
||||
@@ -366,8 +357,10 @@ static inline void bitset_set(struct bitset *set)
|
||||
/** Clear the bits in a bitset which correspond to the given bytes in the
|
||||
* larger file.
|
||||
*/
|
||||
static inline void bitset_clear_range(struct bitset *set,
|
||||
uint64_t from, uint64_t len)
|
||||
static inline void bitset_clear_range(
|
||||
struct bitset * set,
|
||||
uint64_t from,
|
||||
uint64_t len)
|
||||
{
|
||||
INT_FIRST_AND_LAST;
|
||||
BITSET_LOCK;
|
||||
@@ -390,9 +383,12 @@ static inline void bitset_clear(struct bitset *set)
|
||||
/** As per bitset_run_count but also tells you whether the run it found was set
|
||||
* or unset, atomically.
|
||||
*/
|
||||
static inline uint64_t bitset_run_count_ex(struct bitset *set,
|
||||
static inline uint64_t bitset_run_count_ex(
|
||||
struct bitset * set,
|
||||
uint64_t from,
|
||||
uint64_t len, int *run_is_set)
|
||||
uint64_t len,
|
||||
int* run_is_set
|
||||
)
|
||||
{
|
||||
uint64_t run;
|
||||
|
||||
@@ -405,9 +401,7 @@ static inline uint64_t bitset_run_count_ex(struct bitset *set,
|
||||
INT_FIRST_AND_LAST;
|
||||
|
||||
BITSET_LOCK;
|
||||
run =
|
||||
bit_run_count(set->bits, first, bitlen,
|
||||
run_is_set) * set->resolution;
|
||||
run = bit_run_count(set->bits, first, bitlen, run_is_set) * set->resolution;
|
||||
run -= (from % set->resolution);
|
||||
BITSET_UNLOCK;
|
||||
|
||||
@@ -417,8 +411,10 @@ static inline uint64_t bitset_run_count_ex(struct bitset *set,
|
||||
/** Counts the number of contiguous bytes that are represented as a run in
|
||||
* the bit field.
|
||||
*/
|
||||
static inline uint64_t bitset_run_count(struct bitset *set,
|
||||
uint64_t from, uint64_t len)
|
||||
static inline uint64_t bitset_run_count(
|
||||
struct bitset * set,
|
||||
uint64_t from,
|
||||
uint64_t len)
|
||||
{
|
||||
return bitset_run_count_ex( set, from, len, NULL );
|
||||
}
|
||||
@@ -439,3 +435,4 @@ static inline int bitset_is_set_at(struct bitset *set, uint64_t at)
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -18,16 +18,14 @@
|
||||
|
||||
// When this signal is invoked, we call shutdown() on the client fd, which
|
||||
// results in the thread being wound up
|
||||
void client_killswitch_hit(int signal
|
||||
__attribute__ ((unused)), siginfo_t * info,
|
||||
void *ptr __attribute__ ((unused)))
|
||||
void client_killswitch_hit(int signal __attribute__ ((unused)), siginfo_t *info, void *ptr __attribute__ ((unused)))
|
||||
{
|
||||
int fd = info->si_value.sival_int;
|
||||
warn( "Killswitch for fd %i activated, calling shutdown on socket", fd );
|
||||
|
||||
FATAL_IF(-1 == shutdown(fd, SHUT_RDWR),
|
||||
SHOW_ERRNO
|
||||
("Failed to shutdown() the socket, killing the server")
|
||||
FATAL_IF(
|
||||
-1 == shutdown( fd, SHUT_RDWR ),
|
||||
SHOW_ERRNO( "Failed to shutdown() the socket, killing the server" )
|
||||
);
|
||||
}
|
||||
|
||||
@@ -55,8 +53,8 @@ struct client *client_create(struct server *serve, int socket)
|
||||
|
||||
c->stop_signal = self_pipe_create();
|
||||
|
||||
FATAL_IF_NEGATIVE(timer_create
|
||||
(CLOCK_MONOTONIC, &evp, &(c->killswitch)),
|
||||
FATAL_IF_NEGATIVE(
|
||||
timer_create( CLOCK_MONOTONIC, &evp, &(c->killswitch) ),
|
||||
SHOW_ERRNO( "Failed to create killswitch timer" )
|
||||
);
|
||||
|
||||
@@ -69,8 +67,7 @@ void client_signal_stop(struct client *c)
|
||||
{
|
||||
NULLCHECK( c);
|
||||
|
||||
debug("client %p: signal stop (%d, %d)", c, c->stop_signal->read_fd,
|
||||
c->stop_signal->write_fd);
|
||||
debug("client %p: signal stop (%d, %d)", c,c->stop_signal->read_fd, c->stop_signal->write_fd );
|
||||
self_pipe_signal( c->stop_signal );
|
||||
}
|
||||
|
||||
@@ -78,7 +75,8 @@ void client_destroy(struct client *client)
|
||||
{
|
||||
NULLCHECK( client );
|
||||
|
||||
FATAL_IF_NEGATIVE(timer_delete(client->killswitch),
|
||||
FATAL_IF_NEGATIVE(
|
||||
timer_delete( client->killswitch ),
|
||||
SHOW_ERRNO( "Couldn't delete killswitch" )
|
||||
);
|
||||
|
||||
@@ -120,9 +118,9 @@ void write_not_zeroes(struct client *client, uint64_t from, uint64_t len)
|
||||
*/
|
||||
|
||||
uint64_t run = bitset_run_count(map, from, len);
|
||||
struct iommap *iommap = iommap_alloc(client->fileno, from, len);
|
||||
|
||||
debug("write_not_zeroes: from=%ld, len=%d, run=%d", from, len,
|
||||
run);
|
||||
debug("write_not_zeroes: from=%ld, len=%d, run=%d", from, len, run);
|
||||
|
||||
if (run > len) {
|
||||
run = len;
|
||||
@@ -158,7 +156,7 @@ void write_not_zeroes(struct client *client, uint64_t from, uint64_t len)
|
||||
if (bitset_is_set_at(map, from)) {
|
||||
debug("writing the lot: from=%ld, run=%d", from, run);
|
||||
/* already allocated, just write it all */
|
||||
DO_READ(client->mapped + from, run);
|
||||
DO_READ(iommap->buf, run);
|
||||
/* We know from our earlier call to bitset_run_count that the
|
||||
* bitset is all-1s at this point, but we need to dirty it for the
|
||||
* sake of the event stream - the actual bytes have changed, and we
|
||||
@@ -167,7 +165,8 @@ void write_not_zeroes(struct client *client, uint64_t from, uint64_t len)
|
||||
bitset_set_range( map, from, run );
|
||||
len -= run;
|
||||
from += run;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
char zerobuffer[block_allocation_resolution];
|
||||
/* not allocated, read in block_allocation_resoution */
|
||||
while (run > 0) {
|
||||
@@ -185,11 +184,10 @@ void write_not_zeroes(struct client *client, uint64_t from, uint64_t len)
|
||||
*/
|
||||
|
||||
int all_zeros = (zerobuffer[0] == 0) &&
|
||||
(0 ==
|
||||
memcmp(zerobuffer, zerobuffer + 1, blockrun - 1));
|
||||
(0 == memcmp( zerobuffer, zerobuffer+1, blockrun-1 ));
|
||||
|
||||
if ( !all_zeros ) {
|
||||
memcpy(client->mapped + from, zerobuffer, blockrun);
|
||||
memcpy(iommap->buf, zerobuffer, blockrun);
|
||||
bitset_set_range(map, from, blockrun);
|
||||
/* at this point we could choose to
|
||||
* short-cut the rest of the write for
|
||||
@@ -208,6 +206,8 @@ void write_not_zeroes(struct client *client, uint64_t from, uint64_t len)
|
||||
from += blockrun;
|
||||
}
|
||||
}
|
||||
iommap_sync(iommap);
|
||||
iommap_free(iommap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,8 +220,7 @@ int fd_read_request(int fd, struct nbd_request_raw *out_request)
|
||||
|
||||
/* Returns 1 if *request was filled with a valid request which we should
|
||||
* try to honour. 0 otherwise. */
|
||||
int client_read_request(struct client *client,
|
||||
struct nbd_request *out_request, int *disconnected)
|
||||
int client_read_request( struct client * client , struct nbd_request *out_request, int * disconnected )
|
||||
{
|
||||
NULLCHECK( client );
|
||||
NULLCHECK( out_request );
|
||||
@@ -232,13 +231,15 @@ int client_read_request(struct client *client,
|
||||
*disconnected = 1;
|
||||
switch( errno ){
|
||||
case 0:
|
||||
warn("EOF while reading request");
|
||||
debug( "EOF while reading request" );
|
||||
return 0;
|
||||
case ECONNRESET:
|
||||
warn("Connection reset while" " reading request");
|
||||
debug( "Connection reset while"
|
||||
" reading request" );
|
||||
return 0;
|
||||
case ETIMEDOUT:
|
||||
warn("Connection timed out while" " reading request");
|
||||
debug( "Connection timed out while"
|
||||
" reading request" );
|
||||
return 0;
|
||||
default:
|
||||
/* FIXME: I've seen this happen, but I
|
||||
@@ -248,7 +249,9 @@ int client_read_request(struct client *client,
|
||||
* again. It should *probably* be an
|
||||
* error() call, but I want to be sure.
|
||||
* */
|
||||
fatal("Error reading request: %d, %s", errno, strerror(errno));
|
||||
fatal("Error reading request: %d, %s",
|
||||
errno,
|
||||
strerror( errno ));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,8 +296,7 @@ int fd_write_reply(int fd, uint64_t handle, int error)
|
||||
* Returns 1; we don't check for errors on the write.
|
||||
* TODO: Check for errors on the write.
|
||||
*/
|
||||
int client_write_reply(struct client *client, struct nbd_request *request,
|
||||
int error)
|
||||
int client_write_reply( struct client * client, struct nbd_request *request, int error )
|
||||
{
|
||||
return fd_write_reply( client->socket, request->handle.w, error);
|
||||
}
|
||||
@@ -308,17 +310,14 @@ void client_write_init(struct client *client, uint64_t size)
|
||||
memcpy( init.passwd, INIT_PASSWD, sizeof( init.passwd ) );
|
||||
init.magic = INIT_MAGIC;
|
||||
init.size = size;
|
||||
/* As more features are implemented, this is the place to advertise
|
||||
* them.
|
||||
*/
|
||||
init.flags = FLAG_HAS_FLAGS | FLAG_SEND_FLUSH | FLAG_SEND_FUA;
|
||||
memset(init.reserved, 0, 124);
|
||||
memset( init.reserved, 0, 128 );
|
||||
|
||||
nbd_h2r_init( &init, &init_raw );
|
||||
|
||||
ERROR_IF_NEGATIVE(writeloop
|
||||
(client->socket, &init_raw, sizeof(init_raw)),
|
||||
"Couldn't send hello");
|
||||
ERROR_IF_NEGATIVE(
|
||||
writeloop(client->socket, &init_raw, sizeof(init_raw)),
|
||||
"Couldn't send hello"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -338,15 +337,23 @@ void client_flush(struct client *client, size_t len)
|
||||
size_t spliced = 0;
|
||||
|
||||
while ( spliced < len ) {
|
||||
ssize_t received = splice(client->socket, NULL,
|
||||
ssize_t received = splice(
|
||||
client->socket, NULL,
|
||||
pipes[1], NULL,
|
||||
len-spliced, flags );
|
||||
FATAL_IF_NEGATIVE(received, "splice error: %s", strerror(errno));
|
||||
FATAL_IF_NEGATIVE( received,
|
||||
"splice error: %s",
|
||||
strerror(errno));
|
||||
ssize_t junked = 0;
|
||||
while( junked < received ) {
|
||||
ssize_t junk;
|
||||
junk = splice(pipes[0], NULL, devnull, NULL, received, flags);
|
||||
FATAL_IF_NEGATIVE(junk, "splice error: %s", strerror(errno));
|
||||
junk = splice(
|
||||
pipes[0], NULL,
|
||||
devnull, NULL,
|
||||
received, flags );
|
||||
FATAL_IF_NEGATIVE( junk,
|
||||
"splice error: %s",
|
||||
strerror(errno));
|
||||
junked += junk;
|
||||
}
|
||||
spliced += received;
|
||||
@@ -378,26 +385,27 @@ int client_request_needs_reply(struct client *client,
|
||||
return 0;
|
||||
}
|
||||
|
||||
debug("request type=%" PRIu16 ", flags=%" PRIu16 ", from=%" PRIu64
|
||||
", len=%" PRIu32 ", handle=0x%08X", request.type, request.flags,
|
||||
request.from, request.len, request.handle);
|
||||
debug(
|
||||
"request type=%"PRIu32", from=%"PRIu64", len=%"PRIu32", handle=0x%08X",
|
||||
request.type, request.from, request.len, request.handle
|
||||
);
|
||||
|
||||
/* check it's not out of range. NBD protocol requires ENOSPC to be
|
||||
* returned in this instance
|
||||
*/
|
||||
/* check it's not out of range */
|
||||
if ( request.from+request.len > client->serve->size) {
|
||||
warn("write request %"PRIu64"+%"PRIu32" out of range",
|
||||
request.from, request.len);
|
||||
request.from, request.len
|
||||
);
|
||||
if ( request.type == REQUEST_WRITE ) {
|
||||
client_flush( client, request.len );
|
||||
}
|
||||
client_write_reply(client, &request, ENOSPC);
|
||||
client_write_reply( client, &request, EPERM ); /* TODO: Change to ERANGE ? */
|
||||
client->disconnect = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
switch (request.type) {
|
||||
switch (request.type)
|
||||
{
|
||||
case REQUEST_READ:
|
||||
break;
|
||||
case REQUEST_WRITE:
|
||||
@@ -406,22 +414,15 @@ int client_request_needs_reply(struct client *client,
|
||||
debug("request disconnect");
|
||||
client->disconnect = 1;
|
||||
return 0;
|
||||
case REQUEST_FLUSH:
|
||||
break;
|
||||
|
||||
default:
|
||||
/* NBD prototcol says servers SHOULD return EINVAL to unknown
|
||||
* commands */
|
||||
warn("Unknown request 0x%08X", request.type);
|
||||
client_write_reply(client, &request, EINVAL);
|
||||
client->disconnect = 0;
|
||||
return 0;
|
||||
fatal("Unknown request 0x%08X", request.type);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void client_reply_to_read(struct client *client,
|
||||
struct nbd_request request)
|
||||
void client_reply_to_read( struct client* client, struct nbd_request request )
|
||||
{
|
||||
off64_t offset;
|
||||
|
||||
@@ -434,74 +435,55 @@ void client_reply_to_read(struct client *client,
|
||||
/* If we get cut off partway through this sendfile, we don't
|
||||
* want to kill the server. This should be an error.
|
||||
*/
|
||||
ERROR_IF_NEGATIVE(sendfileloop(client->socket,
|
||||
ERROR_IF_NEGATIVE(
|
||||
sendfileloop(
|
||||
client->socket,
|
||||
client->fileno,
|
||||
&offset,
|
||||
request.len),
|
||||
"sendfile failed from=%ld, len=%d",
|
||||
offset, request.len);
|
||||
offset,
|
||||
request.len);
|
||||
|
||||
sock_set_tcp_cork( client->socket, 0 );
|
||||
}
|
||||
|
||||
|
||||
void client_reply_to_write(struct client *client,
|
||||
struct nbd_request request)
|
||||
void client_reply_to_write( struct client* client, struct nbd_request request )
|
||||
{
|
||||
debug("request write from=%" PRIu64 ", len=%" PRIu32 ", handle=0x%08X",
|
||||
request.from, request.len, request.handle);
|
||||
if (client->serve->allocation_map_built) {
|
||||
debug("request write from=%"PRIu64", len=%"PRIu32", handle=0x%08X", request.from, request.len, request.handle);
|
||||
// TODO: Just write directly for now. Not (yet) convinced my changes later on work.
|
||||
// if (client->serve->allocation_map_built) {
|
||||
if (0) {
|
||||
write_not_zeroes( client, request.from, request.len );
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
debug("No allocation map, writing directly.");
|
||||
/* If we get cut off partway through reading this data:
|
||||
* */
|
||||
ERROR_IF_NEGATIVE(readloop(client->socket,
|
||||
client->mapped + request.from,
|
||||
struct iommap *iommap = iommap_alloc(client->fileno, request.from, request.len);
|
||||
|
||||
ERROR_IF_NEGATIVE(
|
||||
readloop( client->socket,
|
||||
iommap->buf,
|
||||
request.len),
|
||||
"reading write data failed from=%ld, len=%d",
|
||||
request.from, request.len);
|
||||
request.from,
|
||||
request.len
|
||||
);
|
||||
|
||||
iommap_sync(iommap);
|
||||
iommap_free(iommap);
|
||||
/* the allocation_map is shared between client threads, and may be
|
||||
* being built. We need to reflect the write in it, as it may be in
|
||||
* a position the builder has already gone over.
|
||||
*/
|
||||
bitset_set_range(client->serve->allocation_map, request.from,
|
||||
request.len);
|
||||
bitset_set_range(client->serve->allocation_map, request.from, request.len);
|
||||
}
|
||||
|
||||
// Only flush if FUA is set -- overridden for now to force flush after each
|
||||
// write.
|
||||
// if (request.flags & CMD_FLAG_FUA) {
|
||||
if (1) {
|
||||
/* multiple of page size */
|
||||
uint64_t from_rounded =
|
||||
request.from & (~(sysconf(_SC_PAGE_SIZE) - 1));
|
||||
uint64_t len_rounded = request.len + (request.from - from_rounded);
|
||||
debug("Calling msync from=%" PRIu64 ", len=%" PRIu64 "",
|
||||
from_rounded, len_rounded);
|
||||
|
||||
FATAL_IF_NEGATIVE(msync(client->mapped + from_rounded,
|
||||
len_rounded,
|
||||
MS_SYNC | MS_INVALIDATE),
|
||||
"msync failed %ld %ld", request.from,
|
||||
request.len);
|
||||
}
|
||||
client_write_reply( client, &request, 0);
|
||||
}
|
||||
|
||||
void client_reply_to_flush(struct client *client,
|
||||
struct nbd_request request)
|
||||
{
|
||||
debug("request flush from=%" PRIu64 ", len=%" PRIu32 ", handle=0x%08X",
|
||||
request.from, request.len, request.handle);
|
||||
|
||||
ERROR_IF_NEGATIVE(msync
|
||||
(client->mapped, client->mapped_size,
|
||||
MS_SYNC | MS_INVALIDATE), "flush failed");
|
||||
|
||||
client_write_reply(client, &request, 0);
|
||||
}
|
||||
|
||||
void client_reply( struct client* client, struct nbd_request request )
|
||||
{
|
||||
@@ -512,9 +494,6 @@ void client_reply(struct client *client, struct nbd_request request)
|
||||
case REQUEST_WRITE:
|
||||
client_reply_to_write( client, request );
|
||||
break;
|
||||
case REQUEST_FLUSH:
|
||||
client_reply_to_flush(client, request);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -535,7 +514,8 @@ void client_arm_killswitch(struct client *client)
|
||||
|
||||
debug( "Arming killswitch" );
|
||||
|
||||
FATAL_IF_NEGATIVE(timer_settime(client->killswitch, 0, &its, NULL),
|
||||
FATAL_IF_NEGATIVE(
|
||||
timer_settime( client->killswitch, 0, &its, NULL ),
|
||||
SHOW_ERRNO( "Failed to arm killswitch" )
|
||||
);
|
||||
|
||||
@@ -555,7 +535,8 @@ void client_disarm_killswitch(struct client *client)
|
||||
|
||||
debug( "Disarming killswitch" );
|
||||
|
||||
FATAL_IF_NEGATIVE(timer_settime(client->killswitch, 0, &its, NULL),
|
||||
FATAL_IF_NEGATIVE(
|
||||
timer_settime( client->killswitch, 0, &its, NULL ),
|
||||
SHOW_ERRNO( "Failed to disarm killswitch" )
|
||||
);
|
||||
|
||||
@@ -591,9 +572,8 @@ int client_serve_request(struct client *client)
|
||||
if ( fd_count == 0 ) {
|
||||
/* This "can't ever happen" */
|
||||
fatal( "No FDs selected, and no timeout!" );
|
||||
} else if (fd_count < 0) {
|
||||
fatal("Select failed");
|
||||
}
|
||||
else if ( fd_count < 0 ) { fatal( "Select failed" ); }
|
||||
|
||||
if ( self_pipe_fd_isset( client->stop_signal, &rfds ) ){
|
||||
debug("Client received stop signal.");
|
||||
@@ -666,41 +646,33 @@ void client_cleanup(struct client *client,
|
||||
debug("Closed client socket fd %d", client->socket);
|
||||
client->socket = -1;
|
||||
}
|
||||
if (client->mapped) {
|
||||
munmap(client->mapped, client->serve->size);
|
||||
}
|
||||
if (client->fileno) {
|
||||
FATAL_IF_NEGATIVE( close(client->fileno),
|
||||
"Error closing file %d", client->fileno);
|
||||
"Error closing file %d",
|
||||
client->fileno );
|
||||
debug("Closed client file fd %d", client->fileno );
|
||||
client->fileno = -1;
|
||||
}
|
||||
|
||||
if (server_acl_locked(client->serve)) {
|
||||
server_unlock_acl(client->serve);
|
||||
}
|
||||
if ( server_acl_locked( client->serve ) ) { server_unlock_acl( client->serve ); }
|
||||
|
||||
}
|
||||
|
||||
void* client_serve(void* client_uncast)
|
||||
{
|
||||
struct client* client = (struct client*) client_uncast;
|
||||
void** a = NULL;
|
||||
|
||||
error_set_handler((cleanup_handler*) client_cleanup, client);
|
||||
|
||||
info("client: mmaping file");
|
||||
FATAL_IF_NEGATIVE(open_and_mmap(client->serve->filename,
|
||||
FATAL_IF_NEGATIVE(
|
||||
open_and_mmap(
|
||||
client->serve->filename,
|
||||
&client->fileno,
|
||||
&client->mapped_size,
|
||||
(void **) &client->mapped),
|
||||
"Couldn't open/mmap file %s: %s",
|
||||
client->serve->filename, strerror(errno)
|
||||
);
|
||||
|
||||
FATAL_IF_NEGATIVE(madvise
|
||||
(client->mapped, client->serve->size, MADV_RANDOM),
|
||||
SHOW_ERRNO("Failed to madvise() %s",
|
||||
client->serve->filename)
|
||||
NULL,
|
||||
a
|
||||
),
|
||||
"Couldn't open/mmap file %s: %s", client->serve->filename, strerror( errno )
|
||||
);
|
||||
|
||||
debug( "Opened client file fd %d", client->fileno);
|
||||
@@ -708,7 +680,8 @@ void *client_serve(void *client_uncast)
|
||||
client_send_hello(client);
|
||||
|
||||
debug("client: serving requests");
|
||||
while (client_serve_request(client) == 0);
|
||||
while (client_serve_request(client) == 0)
|
||||
;
|
||||
debug("client: stopped serving requests");
|
||||
client->stopped = 1;
|
||||
|
||||
@@ -717,10 +690,10 @@ void *client_serve(void *client_uncast)
|
||||
server_control_arrived( client->serve );
|
||||
}
|
||||
|
||||
debug("Cleaning client %p up normally in thread %p", client,
|
||||
pthread_self());
|
||||
debug("Cleaning client %p up normally in thread %p", client, pthread_self());
|
||||
client_cleanup(client, 0);
|
||||
debug("Client thread done" );
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
/** CLIENT_HANDLER_TIMEOUT
|
||||
* This is the length of time (in seconds) any request can be outstanding for.
|
||||
@@ -30,9 +29,6 @@ struct client {
|
||||
int socket;
|
||||
|
||||
int fileno;
|
||||
char *mapped;
|
||||
|
||||
uint64_t mapped_size;
|
||||
|
||||
struct self_pipe * stop_signal;
|
||||
|
||||
@@ -56,3 +52,4 @@ void client_destroy(struct client *client);
|
||||
void client_signal_stop( struct client * client );
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -44,7 +44,9 @@
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
struct control *control_create(struct flexnbd *flexnbd, const char *csn)
|
||||
struct control * control_create(
|
||||
struct flexnbd * flexnbd,
|
||||
const char * csn)
|
||||
{
|
||||
struct control * control = xmalloc( sizeof( struct control ) );
|
||||
|
||||
@@ -78,7 +80,8 @@ void control_destroy(struct control *control)
|
||||
free( control );
|
||||
}
|
||||
|
||||
struct control_client *control_client_create(struct flexnbd *flexnbd,
|
||||
struct control_client * control_client_create(
|
||||
struct flexnbd * flexnbd,
|
||||
int client_fd ,
|
||||
struct mbox * state_mbox )
|
||||
{
|
||||
@@ -109,7 +112,8 @@ void control_handle_client(struct control *control, int client_fd)
|
||||
NULLCHECK( control );
|
||||
NULLCHECK( control->flexnbd );
|
||||
struct control_client * control_client =
|
||||
control_client_create(control->flexnbd,
|
||||
control_client_create(
|
||||
control->flexnbd,
|
||||
client_fd ,
|
||||
control->mirror_state_mbox);
|
||||
|
||||
@@ -128,8 +132,7 @@ void control_accept_client(struct control *control)
|
||||
union mysockaddr client_address;
|
||||
socklen_t addrlen = sizeof( union mysockaddr );
|
||||
|
||||
client_fd =
|
||||
accept(control->control_fd, &client_address.generic, &addrlen);
|
||||
client_fd = accept( control->control_fd, &client_address.generic, &addrlen );
|
||||
FATAL_IF( -1 == client_fd, "control accept failed" );
|
||||
|
||||
control_handle_client( control, client_fd );
|
||||
@@ -175,23 +178,25 @@ int open_control_socket(const char *socket_name)
|
||||
}
|
||||
|
||||
control_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
FATAL_IF_NEGATIVE(control_fd, "Couldn't create control socket");
|
||||
FATAL_IF_NEGATIVE(control_fd ,
|
||||
"Couldn't create control socket");
|
||||
|
||||
memset(&bind_address, 0, sizeof(struct sockaddr_un));
|
||||
bind_address.sun_family = AF_UNIX;
|
||||
strncpy(bind_address.sun_path, socket_name,
|
||||
sizeof(bind_address.sun_path) - 1);
|
||||
strncpy(bind_address.sun_path, socket_name, sizeof(bind_address.sun_path)-1);
|
||||
|
||||
//unlink(socket_name); /* ignore failure */
|
||||
|
||||
FATAL_IF_NEGATIVE(bind
|
||||
(control_fd, &bind_address, sizeof(bind_address)),
|
||||
FATAL_IF_NEGATIVE(
|
||||
bind(control_fd , &bind_address, sizeof(bind_address)),
|
||||
"Couldn't bind control socket to %s: %s",
|
||||
socket_name, strerror( errno )
|
||||
);
|
||||
|
||||
FATAL_IF_NEGATIVE(listen(control_fd, 5),
|
||||
"Couldn't listen on control socket");
|
||||
FATAL_IF_NEGATIVE(
|
||||
listen(control_fd , 5),
|
||||
"Couldn't listen on control socket"
|
||||
);
|
||||
return control_fd;
|
||||
}
|
||||
|
||||
@@ -224,7 +229,8 @@ void control_serve(struct control *control)
|
||||
}
|
||||
|
||||
|
||||
void control_cleanup(struct control *control,
|
||||
void control_cleanup(
|
||||
struct control * control,
|
||||
int fatal __attribute__((unused)) )
|
||||
{
|
||||
NULLCHECK( control );
|
||||
@@ -250,8 +256,7 @@ void *control_runner(void *control_uncast)
|
||||
|
||||
#define write_socket(msg) write(client_fd, (msg "\n"), strlen((msg))+1)
|
||||
|
||||
void control_write_mirror_response(enum mirror_state mirror_state,
|
||||
int client_fd)
|
||||
void control_write_mirror_response( enum mirror_state mirror_state, int client_fd )
|
||||
{
|
||||
switch (mirror_state) {
|
||||
case MS_INIT:
|
||||
@@ -281,12 +286,12 @@ void control_write_mirror_response(enum mirror_state mirror_state,
|
||||
fatal( "Unhandled mirror state: %d", mirror_state );
|
||||
}
|
||||
}
|
||||
|
||||
#undef write_socket
|
||||
|
||||
|
||||
/* Call this in the thread where you want to receive the mirror state */
|
||||
enum mirror_state control_client_mirror_wait(struct control_client *client)
|
||||
enum mirror_state control_client_mirror_wait(
|
||||
struct control_client* client)
|
||||
{
|
||||
NULLCHECK( client );
|
||||
NULLCHECK( client->mirror_state_mbox );
|
||||
@@ -340,11 +345,14 @@ int control_mirror(struct control_client *client, int linesc, char **lines)
|
||||
if (linesc > 2) {
|
||||
if (strcmp("exit", lines[2]) == 0) {
|
||||
action_at_finish = ACTION_EXIT;
|
||||
} else if (strcmp("unlink", lines[2]) == 0) {
|
||||
}
|
||||
else if (strcmp( "unlink", lines[2]) == 0 ) {
|
||||
action_at_finish = ACTION_UNLINK;
|
||||
} else if (strcmp("nothing", lines[2]) == 0) {
|
||||
}
|
||||
else if (strcmp("nothing", lines[2]) == 0) {
|
||||
action_at_finish = ACTION_NOTHING;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
write_socket("1: action must be 'exit' or 'nothing'");
|
||||
return -1;
|
||||
}
|
||||
@@ -381,13 +389,13 @@ int control_mirror(struct control_client *client, int linesc, char **lines)
|
||||
server_lock_start_mirror( serve );
|
||||
{
|
||||
if ( server_mirror_can_start( serve ) ) {
|
||||
serve->mirror_super = mirror_super_create(serve->filename,
|
||||
serve->mirror_super = mirror_super_create(
|
||||
serve->filename,
|
||||
connect_to,
|
||||
connect_from,
|
||||
max_Bps ,
|
||||
action_at_finish,
|
||||
client->
|
||||
mirror_state_mbox);
|
||||
client->mirror_state_mbox );
|
||||
serve->mirror = serve->mirror_super->mirror;
|
||||
server_prevent_mirror_start( serve );
|
||||
} else {
|
||||
@@ -407,14 +415,18 @@ int control_mirror(struct control_client *client, int linesc, char **lines)
|
||||
* sighandler can block the serve thread
|
||||
*/
|
||||
if ( serve->mirror_super ) {
|
||||
FATAL_IF(0 != pthread_create(&serve->mirror_super->thread,
|
||||
FATAL_IF( 0 != pthread_create(
|
||||
&serve->mirror_super->thread,
|
||||
NULL,
|
||||
mirror_super_runner,
|
||||
serve),
|
||||
"Failed to create mirror thread");
|
||||
serve
|
||||
),
|
||||
"Failed to create mirror thread"
|
||||
);
|
||||
|
||||
debug("Control thread mirror super waiting");
|
||||
enum mirror_state state = control_client_mirror_wait(client);
|
||||
enum mirror_state state =
|
||||
control_client_mirror_wait( client );
|
||||
debug("Control thread writing response");
|
||||
control_write_mirror_response( state, client->socket );
|
||||
}
|
||||
@@ -424,8 +436,7 @@ int control_mirror(struct control_client *client, int linesc, char **lines)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int control_mirror_max_bps(struct control_client *client, int linesc,
|
||||
char **lines)
|
||||
int control_mirror_max_bps( struct control_client* client, int linesc, char** lines )
|
||||
{
|
||||
NULLCHECK( client );
|
||||
NULLCHECK( client->flexnbd );
|
||||
@@ -478,7 +489,8 @@ int control_acl(struct control_client *client, int linesc, char **lines)
|
||||
strlen(lines[new_acl->len]));
|
||||
write(client->socket, "\n", 1);
|
||||
acl_destroy( new_acl );
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
flexnbd_replace_acl( flexnbd, new_acl );
|
||||
info("ACL set");
|
||||
write( client->socket, "0: updated\n", 11);
|
||||
@@ -488,7 +500,8 @@ int control_acl(struct control_client *client, int linesc, char **lines)
|
||||
}
|
||||
|
||||
|
||||
int control_break(struct control_client *client,
|
||||
int control_break(
|
||||
struct control_client* client,
|
||||
int linesc __attribute__ ((unused)),
|
||||
char** lines __attribute__((unused))
|
||||
)
|
||||
@@ -511,10 +524,13 @@ int control_break(struct control_client *client,
|
||||
|
||||
if ( server_is_closed( serve ) ) {
|
||||
info( "Mirror completed while canceling" );
|
||||
write(client->socket, "1: mirror completed\n", 20);
|
||||
} else {
|
||||
write( client->socket,
|
||||
"1: mirror completed\n", 20 );
|
||||
}
|
||||
else {
|
||||
info( "Mirror successfully stopped." );
|
||||
write(client->socket, "0: mirror stopped\n", 18);
|
||||
write( client->socket,
|
||||
"0: mirror stopped\n", 18 );
|
||||
result = 1;
|
||||
}
|
||||
|
||||
@@ -530,7 +546,8 @@ int control_break(struct control_client *client,
|
||||
|
||||
|
||||
/** FIXME: add some useful statistics */
|
||||
int control_status(struct control_client *client,
|
||||
int control_status(
|
||||
struct control_client* client,
|
||||
int linesc __attribute__ ((unused)),
|
||||
char** lines __attribute__((unused))
|
||||
)
|
||||
@@ -549,14 +566,10 @@ int control_status(struct control_client *client,
|
||||
void control_client_cleanup(struct control_client* client,
|
||||
int fatal __attribute__ ((unused)) )
|
||||
{
|
||||
if (client->socket) {
|
||||
close(client->socket);
|
||||
}
|
||||
if (client->socket) { close(client->socket); }
|
||||
|
||||
/* This is wrongness */
|
||||
if (server_acl_locked(client->flexnbd->serve)) {
|
||||
server_unlock_acl(client->flexnbd->serve);
|
||||
}
|
||||
if ( server_acl_locked( client->flexnbd->serve ) ) { server_unlock_acl( client->flexnbd->serve ); }
|
||||
|
||||
control_client_destroy( client );
|
||||
}
|
||||
@@ -571,25 +584,30 @@ void control_respond(struct control_client *client)
|
||||
int i, linesc;
|
||||
linesc = read_lines_until_blankline(client->socket, 256, &lines);
|
||||
|
||||
if (linesc < 1) {
|
||||
if (linesc < 1)
|
||||
{
|
||||
write(client->socket, "9: missing command\n", 19);
|
||||
/* ignore failure */
|
||||
} else if (strcmp(lines[0], "acl") == 0) {
|
||||
}
|
||||
else if (strcmp(lines[0], "acl") == 0) {
|
||||
info("acl command received" );
|
||||
if (control_acl(client, linesc-1, lines+1) < 0) {
|
||||
debug("acl command failed");
|
||||
}
|
||||
} else if (strcmp(lines[0], "mirror") == 0) {
|
||||
}
|
||||
else if (strcmp(lines[0], "mirror") == 0) {
|
||||
info("mirror command received" );
|
||||
if (control_mirror(client, linesc-1, lines+1) < 0) {
|
||||
debug("mirror command failed");
|
||||
}
|
||||
} else if (strcmp(lines[0], "break") == 0) {
|
||||
}
|
||||
else if (strcmp(lines[0], "break") == 0) {
|
||||
info( "break command received" );
|
||||
if ( control_break( client, linesc-1, lines+1) < 0) {
|
||||
debug( "break command failed" );
|
||||
}
|
||||
} else if (strcmp(lines[0], "status") == 0) {
|
||||
}
|
||||
else if (strcmp(lines[0], "status") == 0) {
|
||||
info("status command received" );
|
||||
if (control_status(client, linesc-1, lines+1) < 0) {
|
||||
debug("status command failed");
|
||||
@@ -599,7 +617,8 @@ void control_respond(struct control_client *client)
|
||||
if( control_mirror_max_bps( client, linesc-1, lines+1 ) < 0 ) {
|
||||
debug( "mirror_max_bps command failed" );
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
write(client->socket, "10: unknown command\n", 23);
|
||||
}
|
||||
|
||||
@@ -611,3 +630,4 @@ void control_respond(struct control_client *client)
|
||||
control_client_cleanup(client, 0);
|
||||
debug("control command handled" );
|
||||
}
|
||||
|
||||
|
@@ -44,15 +44,16 @@ struct control_client {
|
||||
struct mbox * mirror_state_mbox;
|
||||
};
|
||||
|
||||
struct control *control_create(struct flexnbd *,
|
||||
struct control * control_create(
|
||||
struct flexnbd *,
|
||||
const char * control_socket_name );
|
||||
void control_signal_close( struct control * );
|
||||
void control_destroy( struct control * );
|
||||
|
||||
void * control_runner( void * );
|
||||
|
||||
void accept_control_connection(struct server *params, int client_fd,
|
||||
union mysockaddr *client_address);
|
||||
void accept_control_connection(struct server* params, int client_fd, union mysockaddr* client_address);
|
||||
void serve_open_control_socket(struct server* params);
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -61,13 +61,16 @@ int flexnbd_build_signal_fd(void)
|
||||
}
|
||||
|
||||
|
||||
void flexnbd_create_shared(struct flexnbd *flexnbd,
|
||||
void flexnbd_create_shared(
|
||||
struct flexnbd * flexnbd,
|
||||
const char * s_ctrl_sock)
|
||||
{
|
||||
NULLCHECK( flexnbd );
|
||||
if ( s_ctrl_sock ){
|
||||
flexnbd->control = control_create(flexnbd, s_ctrl_sock);
|
||||
} else {
|
||||
flexnbd->control =
|
||||
control_create( flexnbd, s_ctrl_sock );
|
||||
}
|
||||
else {
|
||||
flexnbd->control = NULL;
|
||||
}
|
||||
|
||||
@@ -75,7 +78,8 @@ void flexnbd_create_shared(struct flexnbd *flexnbd,
|
||||
}
|
||||
|
||||
|
||||
struct flexnbd *flexnbd_create_serving(char *s_ip_address,
|
||||
struct flexnbd * flexnbd_create_serving(
|
||||
char* s_ip_address,
|
||||
char* s_port,
|
||||
char* s_file,
|
||||
char* s_ctrl_sock,
|
||||
@@ -86,14 +90,17 @@ struct flexnbd *flexnbd_create_serving(char *s_ip_address,
|
||||
int use_killswitch)
|
||||
{
|
||||
struct flexnbd * flexnbd = xmalloc( sizeof( struct flexnbd ) );
|
||||
flexnbd->serve = server_create(flexnbd,
|
||||
flexnbd->serve = server_create(
|
||||
flexnbd,
|
||||
s_ip_address,
|
||||
s_port,
|
||||
s_file,
|
||||
default_deny,
|
||||
acl_entries,
|
||||
s_acl_entries,
|
||||
max_nbd_clients, use_killswitch, 1);
|
||||
max_nbd_clients,
|
||||
use_killswitch,
|
||||
1);
|
||||
flexnbd_create_shared( flexnbd, s_ctrl_sock );
|
||||
|
||||
// Beats installing one handler per client instance
|
||||
@@ -103,14 +110,17 @@ struct flexnbd *flexnbd_create_serving(char *s_ip_address,
|
||||
.sa_flags = SA_RESTART | SA_SIGINFO
|
||||
};
|
||||
|
||||
FATAL_UNLESS(0 == sigaction(CLIENT_KILLSWITCH_SIGNAL, &act, NULL),
|
||||
"Installing client killswitch signal failed");
|
||||
FATAL_UNLESS(
|
||||
0 == sigaction( CLIENT_KILLSWITCH_SIGNAL, &act, NULL ),
|
||||
"Installing client killswitch signal failed"
|
||||
);
|
||||
}
|
||||
|
||||
return flexnbd;
|
||||
}
|
||||
|
||||
struct flexnbd *flexnbd_create_listening(char *s_ip_address,
|
||||
struct flexnbd * flexnbd_create_listening(
|
||||
char* s_ip_address,
|
||||
char* s_port,
|
||||
char* s_file,
|
||||
char* s_ctrl_sock,
|
||||
@@ -119,12 +129,15 @@ struct flexnbd *flexnbd_create_listening(char *s_ip_address,
|
||||
char** s_acl_entries )
|
||||
{
|
||||
struct flexnbd * flexnbd = xmalloc( sizeof( struct flexnbd ) );
|
||||
flexnbd->serve = server_create(flexnbd,
|
||||
flexnbd->serve = server_create(
|
||||
flexnbd,
|
||||
s_ip_address,
|
||||
s_port,
|
||||
s_file,
|
||||
default_deny,
|
||||
acl_entries, s_acl_entries, 1, 0, 0);
|
||||
acl_entries,
|
||||
s_acl_entries,
|
||||
1, 0, 0);
|
||||
flexnbd_create_shared( flexnbd, s_ctrl_sock );
|
||||
|
||||
// listen can't use killswitch, as mirror may pause on sending things
|
||||
@@ -140,7 +153,8 @@ void flexnbd_spawn_control(struct flexnbd *flexnbd)
|
||||
|
||||
pthread_t * control_thread = &flexnbd->control->thread;
|
||||
|
||||
FATAL_UNLESS(0 == pthread_create(control_thread,
|
||||
FATAL_UNLESS( 0 == pthread_create(
|
||||
control_thread,
|
||||
NULL,
|
||||
control_runner,
|
||||
flexnbd->control ),
|
||||
@@ -219,7 +233,9 @@ void make_writable(const char *filename)
|
||||
{
|
||||
NULLCHECK( filename );
|
||||
FATAL_IF_NEGATIVE( chmod( filename, S_IWUSR ),
|
||||
"Couldn't chmod %s: %s", filename, strerror(errno));
|
||||
"Couldn't chmod %s: %s",
|
||||
filename,
|
||||
strerror( errno ) );
|
||||
}
|
||||
|
||||
|
||||
@@ -246,3 +262,4 @@ int flexnbd_serve(struct flexnbd *flexnbd)
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
|
@@ -29,7 +29,8 @@ struct flexnbd {
|
||||
|
||||
|
||||
struct flexnbd * flexnbd_create(void);
|
||||
struct flexnbd *flexnbd_create_serving(char *s_ip_address,
|
||||
struct flexnbd * flexnbd_create_serving(
|
||||
char* s_ip_address,
|
||||
char* s_port,
|
||||
char* s_file,
|
||||
char* s_ctrl_sock,
|
||||
@@ -39,7 +40,8 @@ struct flexnbd *flexnbd_create_serving(char *s_ip_address,
|
||||
int max_nbd_clients,
|
||||
int use_killswitch);
|
||||
|
||||
struct flexnbd *flexnbd_create_listening(char *s_ip_address,
|
||||
struct flexnbd * flexnbd_create_listening(
|
||||
char* s_ip_address,
|
||||
char* s_port,
|
||||
char* s_file,
|
||||
char* s_ctrl_sock,
|
||||
@@ -61,3 +63,4 @@ struct server *flexnbd_server(struct flexnbd *flexnbd);
|
||||
void flexnbd_replace_acl( struct flexnbd * flexnbd, struct acl * acl );
|
||||
struct status * flexnbd_status_create( struct flexnbd * flexnbd );
|
||||
#endif
|
||||
|
||||
|
@@ -22,14 +22,15 @@ void flexthread_mutex_destroy(struct flexthread_mutex *ftm)
|
||||
|
||||
if( flexthread_mutex_held( ftm ) ) {
|
||||
flexthread_mutex_unlock( ftm );
|
||||
} else if ((pthread_t) NULL != ftm->holder) {
|
||||
}
|
||||
else if ( (pthread_t)NULL != ftm->holder ) {
|
||||
/* This "should never happen": if we can try to destroy
|
||||
* a mutex currently held by another thread, there's a
|
||||
* logic bug somewhere. I know the test here is racy,
|
||||
* but there's not a lot we can do about it at this
|
||||
* point.
|
||||
*/
|
||||
fatal("Attempted to destroy a flexthread_mutex"
|
||||
fatal( "Attempted to destroy a flexthread_mutex"\
|
||||
" held by another thread!" );
|
||||
}
|
||||
|
||||
@@ -71,3 +72,4 @@ int flexthread_mutex_held(struct flexthread_mutex *ftm)
|
||||
NULLCHECK( ftm );
|
||||
return pthread_self() == ftm->holder;
|
||||
}
|
||||
|
||||
|
@@ -90,7 +90,8 @@ struct mirror_ctrl {
|
||||
|
||||
};
|
||||
|
||||
struct mirror *mirror_alloc(union mysockaddr *connect_to,
|
||||
struct mirror * mirror_alloc(
|
||||
union mysockaddr * connect_to,
|
||||
union mysockaddr * connect_from,
|
||||
uint64_t max_Bps,
|
||||
enum mirror_finish_action action_at_finish,
|
||||
@@ -143,13 +144,19 @@ void mirror_init(struct mirror *mirror, const char *filename)
|
||||
NULLCHECK( mirror );
|
||||
NULLCHECK( filename );
|
||||
|
||||
FATAL_IF_NEGATIVE(open_and_mmap(filename,
|
||||
FATAL_IF_NEGATIVE(
|
||||
open_and_mmap(
|
||||
filename,
|
||||
&map_fd,
|
||||
&size,
|
||||
(void **) &mirror->mapped),
|
||||
"Failed to open and mmap %s", filename);
|
||||
(void**) &mirror->mapped
|
||||
),
|
||||
"Failed to open and mmap %s",
|
||||
filename
|
||||
);
|
||||
|
||||
FATAL_IF_NEGATIVE(madvise(mirror->mapped, size, MADV_SEQUENTIAL),
|
||||
FATAL_IF_NEGATIVE(
|
||||
madvise( mirror->mapped, size, MADV_SEQUENTIAL ),
|
||||
SHOW_ERRNO( "Failed to madvise() %s", filename )
|
||||
);
|
||||
}
|
||||
@@ -169,7 +176,8 @@ void mirror_reset(struct mirror *mirror)
|
||||
}
|
||||
|
||||
|
||||
struct mirror *mirror_create(const char *filename,
|
||||
struct mirror * mirror_create(
|
||||
const char * filename,
|
||||
union mysockaddr * connect_to,
|
||||
union mysockaddr * connect_from,
|
||||
uint64_t max_Bps,
|
||||
@@ -181,7 +189,9 @@ struct mirror *mirror_create(const char *filename,
|
||||
|
||||
mirror = mirror_alloc( connect_to,
|
||||
connect_from,
|
||||
max_Bps, action_at_finish, commit_signal);
|
||||
max_Bps,
|
||||
action_at_finish,
|
||||
commit_signal);
|
||||
|
||||
mirror_init( mirror, filename );
|
||||
mirror_reset( mirror );
|
||||
@@ -271,8 +281,7 @@ int mirror_connect(struct mirror *mirror, uint64_t local_size)
|
||||
|
||||
NULLCHECK( mirror->connect_to );
|
||||
|
||||
mirror->client =
|
||||
socket_connect(&mirror->connect_to->generic, connect_from);
|
||||
mirror->client = socket_connect(&mirror->connect_to->generic, connect_from);
|
||||
if ( 0 < mirror->client ) {
|
||||
fd_set fds;
|
||||
struct timeval tv = { MS_HELLO_TIME_SECS, 0};
|
||||
@@ -284,30 +293,30 @@ int mirror_connect(struct mirror *mirror, uint64_t local_size)
|
||||
|
||||
if( FD_ISSET( mirror->client, &fds ) ){
|
||||
uint64_t remote_size;
|
||||
uint32_t remote_flags;
|
||||
if (socket_nbd_read_hello
|
||||
(mirror->client, &remote_size, &remote_flags)) {
|
||||
if ( socket_nbd_read_hello( mirror->client, &remote_size ) ) {
|
||||
if( remote_size == local_size ){
|
||||
connected = 1;
|
||||
mirror_set_state( mirror, MS_GO );
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
warn("Remote size (%d) doesn't match local (%d)",
|
||||
remote_size, local_size );
|
||||
mirror_set_state( mirror, MS_FAIL_SIZE_MISMATCH );
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
warn( "Mirror attempt rejected." );
|
||||
mirror_set_state( mirror, MS_FAIL_REJECTED );
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
warn( "No NBD Hello received." );
|
||||
mirror_set_state( mirror, MS_FAIL_NO_HELLO );
|
||||
}
|
||||
|
||||
if (!connected) {
|
||||
close(mirror->client);
|
||||
if ( !connected ) { close( mirror->client ); }
|
||||
}
|
||||
} else {
|
||||
else {
|
||||
warn( "Mirror failed to connect.");
|
||||
mirror_set_state( mirror, MS_FAIL_CONNECT );
|
||||
}
|
||||
@@ -362,15 +371,12 @@ int mirror_setup_next_xfer(struct mirror_ctrl *ctrl)
|
||||
* full, and stop when it's a quarter full. This stops a busy client from
|
||||
* stalling a migration forever. FIXME: made-up numbers.
|
||||
*/
|
||||
if (mirror->offset < serve->size
|
||||
&& bitset_stream_size(serve->allocation_map) >
|
||||
BITSET_STREAM_SIZE / 2) {
|
||||
if ( mirror->offset < serve->size && bitset_stream_size( serve->allocation_map ) > BITSET_STREAM_SIZE / 2 ) {
|
||||
ctrl->clear_events = 1;
|
||||
}
|
||||
|
||||
|
||||
while ((mirror->offset == serve->size || ctrl->clear_events)
|
||||
&& e.event != BITSET_STREAM_SET) {
|
||||
while ( ( mirror->offset == serve->size || ctrl->clear_events ) && e.event != BITSET_STREAM_SET ) {
|
||||
uint64_t events = bitset_stream_size( serve->allocation_map );
|
||||
|
||||
if ( events == 0 ) {
|
||||
@@ -402,8 +408,7 @@ int mirror_setup_next_xfer(struct mirror_ctrl *ctrl)
|
||||
return 0;
|
||||
}
|
||||
|
||||
debug("Next transfer: current=%" PRIu64 ", run=%" PRIu64, current,
|
||||
run);
|
||||
debug( "Next transfer: current=%"PRIu64", run=%"PRIu64, current, run );
|
||||
struct nbd_request req = {
|
||||
.magic = REQUEST_MAGIC,
|
||||
.type = REQUEST_WRITE,
|
||||
@@ -455,8 +460,7 @@ static void mirror_write_cb(struct ev_loop *loop, ev_io * w, int revents)
|
||||
return;
|
||||
}
|
||||
|
||||
debug("Mirror write callback invoked with events %d. fd: %i", revents,
|
||||
ctrl->mirror->client);
|
||||
debug( "Mirror write callback invoked with events %d. fd: %i", revents, ctrl->mirror->client );
|
||||
|
||||
/* FIXME: We can end up corking multiple times in unusual circumstances; this
|
||||
* is annoying, but harmless */
|
||||
@@ -468,8 +472,7 @@ static void mirror_write_cb(struct ev_loop *loop, ev_io * w, int revents)
|
||||
data_loc = ( (char*) &xfer->hdr.req_raw ) + ctrl->xfer.written;
|
||||
to_write = hdr_size - xfer->written;
|
||||
} else {
|
||||
data_loc =
|
||||
ctrl->mirror->mapped + xfer->from + (xfer->written - hdr_size);
|
||||
data_loc = ctrl->mirror->mapped + xfer->from + ( xfer->written - hdr_size );
|
||||
to_write = xfer->len - ( ctrl->xfer.written - hdr_size );
|
||||
}
|
||||
|
||||
@@ -482,14 +485,14 @@ static void mirror_write_cb(struct ev_loop *loop, ev_io * w, int revents)
|
||||
return;
|
||||
}
|
||||
debug( "Wrote %"PRIu64" bytes", count );
|
||||
debug("to_write was %" PRIu64 ", xfer->written was %" PRIu64, to_write,
|
||||
xfer->written);
|
||||
debug( "to_write was %"PRIu64", xfer->written was %"PRIu64, to_write, xfer->written );
|
||||
|
||||
// We wrote some bytes, so reset the timer and keep track for the next pass
|
||||
if ( count > 0 ) {
|
||||
ctrl->xfer.written += count;
|
||||
ev_timer_again( ctrl->ev_loop, &ctrl->timeout_watcher );
|
||||
}
|
||||
|
||||
// All bytes written, so now we need to read the NBD reply back.
|
||||
if ( ctrl->xfer.written == ctrl->xfer.len + hdr_size ) {
|
||||
sock_set_tcp_cork( ctrl->mirror->client, 0 ) ;
|
||||
@@ -520,13 +523,10 @@ static void mirror_read_cb(struct ev_loop *loop, ev_io * w, int revents)
|
||||
ssize_t count;
|
||||
uint64_t left = sizeof( struct nbd_reply_raw ) - xfer->read;
|
||||
|
||||
debug("Mirror read callback invoked with events %d. fd:%i", revents,
|
||||
m->client);
|
||||
debug( "Mirror read callback invoked with events %d. fd:%i", revents, m->client );
|
||||
|
||||
/* Start / continue reading the NBD response from the mirror. */
|
||||
if ((count =
|
||||
read(m->client, ((void *) &xfer->hdr.rsp_raw) + xfer->read,
|
||||
left)) < 0) {
|
||||
if ( ( count = read( m->client, ((void*) &xfer->hdr.rsp_raw) + xfer->read, left ) ) < 0 ) {
|
||||
if ( errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR ) {
|
||||
warn( SHOW_ERRNO( "Couldn't read from listener" ) );
|
||||
ev_break( loop, EVBREAK_ONE );
|
||||
@@ -540,12 +540,12 @@ static void mirror_read_cb(struct ev_loop *loop, ev_io * w, int revents)
|
||||
ev_break( loop, EVBREAK_ONE );
|
||||
return;
|
||||
}
|
||||
|
||||
// We read some bytes, so reset the timer
|
||||
ev_timer_again( ctrl->ev_loop, &ctrl->timeout_watcher );
|
||||
|
||||
debug( "Read %i bytes", count );
|
||||
debug("left was %" PRIu64 ", xfer->read was %" PRIu64, left,
|
||||
xfer->read);
|
||||
debug( "left was %"PRIu64", xfer->read was %"PRIu64, left, xfer->read );
|
||||
xfer->read += count;
|
||||
|
||||
if ( xfer->read < sizeof( struct nbd_reply_raw ) ) {
|
||||
@@ -602,9 +602,7 @@ static void mirror_read_cb(struct ev_loop *loop, ev_io * w, int revents)
|
||||
debug( "next_xfer: %d", next_xfer );
|
||||
|
||||
/* Regardless of time estimates, if there's no waiting transfer, we can start closing clients down. */
|
||||
if (!ctrl->clients_closed
|
||||
&& (!next_xfer
|
||||
|| server_mirror_eta(ctrl->serve) < MS_CONVERGE_TIME_SECS)) {
|
||||
if ( !ctrl->clients_closed && ( !next_xfer || server_mirror_eta( ctrl->serve ) < MS_CONVERGE_TIME_SECS ) ) {
|
||||
info( "Closing clients to allow mirroring to converge" );
|
||||
server_forbid_new_clients( ctrl->serve );
|
||||
server_close_clients( ctrl->serve );
|
||||
@@ -645,8 +643,7 @@ static void mirror_read_cb(struct ev_loop *loop, ev_io * w, int revents)
|
||||
return;
|
||||
}
|
||||
|
||||
static void mirror_timeout_cb(struct ev_loop *loop, ev_timer * w
|
||||
__attribute__ ((unused)), int revents)
|
||||
static void mirror_timeout_cb( struct ev_loop *loop, ev_timer *w __attribute__((unused)), int revents )
|
||||
{
|
||||
if ( !(revents & EV_TIMER ) ) {
|
||||
warn( "Mirror timeout called but no timer event signalled" );
|
||||
@@ -676,8 +673,7 @@ static void mirror_abandon_cb(struct ev_loop *loop, ev_io * w, int revents)
|
||||
}
|
||||
|
||||
|
||||
static void mirror_limit_cb(struct ev_loop *loop, ev_timer * w,
|
||||
int revents)
|
||||
static void mirror_limit_cb( struct ev_loop *loop, ev_timer *w, int revents )
|
||||
{
|
||||
struct mirror_ctrl* ctrl = (struct mirror_ctrl*) w->data;
|
||||
NULLCHECK( ctrl );
|
||||
@@ -688,8 +684,7 @@ static void mirror_limit_cb(struct ev_loop *loop, ev_timer * w,
|
||||
}
|
||||
|
||||
if ( mirror_should_wait( ctrl ) ) {
|
||||
debug("max_bps exceeded, waiting",
|
||||
ctrl->mirror->max_bytes_per_second);
|
||||
debug( "max_bps exceeded, waiting", ctrl->mirror->max_bytes_per_second );
|
||||
ev_timer_again( loop, w );
|
||||
} else {
|
||||
/* We're below the limit, so do the next request */
|
||||
@@ -706,8 +701,7 @@ static void mirror_limit_cb(struct ev_loop *loop, ev_timer * w,
|
||||
* if it has, start migrating. If it's not finished, then enabling the bitset
|
||||
* stream does not go well for us.
|
||||
*/
|
||||
static void mirror_begin_cb(struct ev_loop *loop, ev_timer * w,
|
||||
int revents)
|
||||
static void mirror_begin_cb( struct ev_loop *loop, ev_timer *w, int revents )
|
||||
{
|
||||
struct mirror_ctrl* ctrl = (struct mirror_ctrl*) w->data;
|
||||
NULLCHECK( ctrl );
|
||||
@@ -717,8 +711,7 @@ static void mirror_begin_cb(struct ev_loop *loop, ev_timer * w,
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctrl->serve->allocation_map_built
|
||||
|| ctrl->serve->allocation_map_not_built) {
|
||||
if ( ctrl->serve->allocation_map_built || ctrl->serve->allocation_map_not_built ) {
|
||||
info( "allocation map builder is finished, beginning migration" );
|
||||
ev_timer_stop( loop, w );
|
||||
/* Start by writing xfer 0 to the listener */
|
||||
@@ -803,8 +796,10 @@ void mirror_run(struct server *serve)
|
||||
ctrl.abandon_watcher.data = (void*) &ctrl;
|
||||
ev_io_start( ctrl.ev_loop, &ctrl.abandon_watcher );
|
||||
|
||||
ERROR_UNLESS(mirror_setup_next_xfer(&ctrl),
|
||||
"Couldn't find first transfer for mirror!");
|
||||
ERROR_UNLESS(
|
||||
mirror_setup_next_xfer( &ctrl ),
|
||||
"Couldn't find first transfer for mirror!"
|
||||
);
|
||||
|
||||
|
||||
if ( serve->allocation_map_built ) {
|
||||
@@ -835,8 +830,7 @@ void mirror_run(struct server *serve)
|
||||
* it to something sane - they just terminate the event loop with state !=
|
||||
* MS_DONE. We re-allow new clients here if necessary.
|
||||
*/
|
||||
if (m->action_at_finish == ACTION_NOTHING
|
||||
|| m->commit_state != MS_DONE) {
|
||||
if ( m->action_at_finish == ACTION_NOTHING || m->commit_state != MS_DONE ) {
|
||||
server_allow_new_clients( serve );
|
||||
}
|
||||
|
||||
@@ -902,9 +896,7 @@ void *mirror_runner(void *serve_params_uncast)
|
||||
time_t start_time = time(NULL);
|
||||
int connected = mirror_connect( mirror, serve->size );
|
||||
mirror_signal_commit( mirror );
|
||||
if (!connected) {
|
||||
goto abandon_mirror;
|
||||
}
|
||||
if ( !connected ) { goto abandon_mirror; }
|
||||
|
||||
/* After this point, if we see a failure we need to disconnect
|
||||
* and retry everything from mirror_set_state(_, MS_INIT), but
|
||||
@@ -940,33 +932,38 @@ void *mirror_runner(void *serve_params_uncast)
|
||||
}
|
||||
|
||||
|
||||
struct mirror_super *mirror_super_create(const char *filename,
|
||||
struct mirror_super * mirror_super_create(
|
||||
const char * filename,
|
||||
union mysockaddr * connect_to,
|
||||
union mysockaddr * connect_from,
|
||||
uint64_t max_Bps,
|
||||
enum mirror_finish_action
|
||||
action_at_finish,
|
||||
enum mirror_finish_action action_at_finish,
|
||||
struct mbox * state_mbox)
|
||||
{
|
||||
struct mirror_super * super = xmalloc( sizeof( struct mirror_super) );
|
||||
super->mirror = mirror_create(filename,
|
||||
super->mirror = mirror_create(
|
||||
filename,
|
||||
connect_to,
|
||||
connect_from,
|
||||
max_Bps,
|
||||
action_at_finish, mbox_create());
|
||||
action_at_finish,
|
||||
mbox_create() ) ;
|
||||
super->state_mbox = state_mbox;
|
||||
return super;
|
||||
}
|
||||
|
||||
|
||||
/* Post the current state of the mirror into super->state_mbox.*/
|
||||
void mirror_super_signal_committed(struct mirror_super *super,
|
||||
void mirror_super_signal_committed(
|
||||
struct mirror_super * super ,
|
||||
enum mirror_state commit_state )
|
||||
{
|
||||
NULLCHECK( super );
|
||||
NULLCHECK( super->state_mbox );
|
||||
|
||||
mbox_post_mirror_state(super->state_mbox, commit_state);
|
||||
mbox_post_mirror_state(
|
||||
super->state_mbox,
|
||||
commit_state );
|
||||
}
|
||||
|
||||
|
||||
@@ -1000,7 +997,8 @@ void *mirror_super_runner(void *serve_uncast)
|
||||
struct mirror_super * super = serve->mirror_super;
|
||||
|
||||
do {
|
||||
FATAL_IF(0 != pthread_create(&mirror->thread,
|
||||
FATAL_IF( 0 != pthread_create(
|
||||
&mirror->thread,
|
||||
NULL,
|
||||
mirror_runner,
|
||||
serve),
|
||||
@@ -1020,7 +1018,9 @@ void *mirror_super_runner(void *serve_uncast)
|
||||
should_retry = *commit_state == MS_GO;
|
||||
|
||||
/* Only send this signal the first time */
|
||||
mirror_super_signal_committed(super, *commit_state);
|
||||
mirror_super_signal_committed(
|
||||
super,
|
||||
*commit_state);
|
||||
debug("Mirror supervisor committed");
|
||||
}
|
||||
/* We only care about the value of the commit signal on
|
||||
|
@@ -127,13 +127,15 @@ struct mirror_super {
|
||||
struct server;
|
||||
struct flexnbd;
|
||||
|
||||
struct mirror_super *mirror_super_create(const char *filename,
|
||||
struct mirror_super * mirror_super_create(
|
||||
const char * filename,
|
||||
union mysockaddr * connect_to,
|
||||
union mysockaddr * connect_from,
|
||||
uint64_t max_Bps,
|
||||
enum mirror_finish_action
|
||||
action_at_finish,
|
||||
struct mbox *state_mbox);
|
||||
enum mirror_finish_action action_at_finish,
|
||||
struct mbox * state_mbox
|
||||
);
|
||||
void * mirror_super_runner( void * serve_uncast );
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -19,7 +19,6 @@ static struct option serve_options[] = {
|
||||
GETOPT_VERBOSE,
|
||||
{0}
|
||||
};
|
||||
|
||||
static char serve_short_options[] = "hl:p:f:s:dk" SOPT_QUIET SOPT_VERBOSE;
|
||||
static char serve_help_text[] =
|
||||
"Usage: flexnbd " CMD_SERVE " <options> [<acl address>*]\n\n"
|
||||
@@ -29,9 +28,10 @@ static char serve_help_text[] =
|
||||
"\t--" OPT_PORT ",-p <PORT>\tThe port to serve on.\n"
|
||||
"\t--" OPT_FILE ",-f <FILE>\tThe file to serve.\n"
|
||||
"\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n"
|
||||
"\t--" OPT_KILLSWITCH
|
||||
",-k \tKill the server if a request takes 120 seconds.\n" SOCK_LINE
|
||||
VERBOSE_LINE QUIET_LINE;
|
||||
"\t--" OPT_KILLSWITCH",-k \tKill the server if a request takes 120 seconds.\n"
|
||||
SOCK_LINE
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
|
||||
static struct option listen_options[] = {
|
||||
@@ -45,7 +45,6 @@ static struct option listen_options[] = {
|
||||
GETOPT_VERBOSE,
|
||||
{0}
|
||||
};
|
||||
|
||||
static char listen_short_options[] = "hl:p:f:s:d" SOPT_QUIET SOPT_VERBOSE;
|
||||
static char listen_help_text[] =
|
||||
"Usage: flexnbd " CMD_LISTEN " <options> [<acl_address>*]\n\n"
|
||||
@@ -55,7 +54,9 @@ static char listen_help_text[] =
|
||||
"\t--" OPT_PORT ",-p <PORT>\tThe port to listen on.\n"
|
||||
"\t--" OPT_FILE ",-f <FILE>\tThe file to serve.\n"
|
||||
"\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n"
|
||||
SOCK_LINE VERBOSE_LINE QUIET_LINE;
|
||||
SOCK_LINE
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
static struct option read_options[] = {
|
||||
GETOPT_HELP,
|
||||
@@ -68,7 +69,6 @@ static struct option read_options[] = {
|
||||
GETOPT_VERBOSE,
|
||||
{0}
|
||||
};
|
||||
|
||||
static char read_short_options[] = "hl:p:F:S:b:" SOPT_QUIET SOPT_VERBOSE;
|
||||
static char read_help_text[] =
|
||||
"Usage: flexnbd " CMD_READ " <options>\n\n"
|
||||
@@ -78,7 +78,9 @@ static char read_help_text[] =
|
||||
"\t--" OPT_PORT ",-p <PORT>\tThe port to read from.\n"
|
||||
"\t--" OPT_FROM ",-F <OFFSET>\tByte offset to read from.\n"
|
||||
"\t--" OPT_SIZE ",-S <SIZE>\tBytes to read.\n"
|
||||
BIND_LINE VERBOSE_LINE QUIET_LINE;
|
||||
BIND_LINE
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
|
||||
static struct option *write_options = read_options;
|
||||
@@ -91,7 +93,9 @@ static char write_help_text[] =
|
||||
"\t--" OPT_PORT ",-p <PORT>\tThe port to write to.\n"
|
||||
"\t--" OPT_FROM ",-F <OFFSET>\tByte offset to write from.\n"
|
||||
"\t--" OPT_SIZE ",-S <SIZE>\tBytes to write.\n"
|
||||
BIND_LINE VERBOSE_LINE QUIET_LINE;
|
||||
BIND_LINE
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
static struct option acl_options[] = {
|
||||
GETOPT_HELP,
|
||||
@@ -100,12 +104,14 @@ static struct option acl_options[] = {
|
||||
GETOPT_VERBOSE,
|
||||
{0}
|
||||
};
|
||||
|
||||
static char acl_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
|
||||
static char acl_help_text[] =
|
||||
"Usage: flexnbd " CMD_ACL " <options> [<acl address>+]\n\n"
|
||||
"Set the access control list for a server with control socket SOCK.\n\n"
|
||||
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
|
||||
HELP_LINE
|
||||
SOCK_LINE
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
static struct option mirror_speed_options[] = {
|
||||
GETOPT_HELP,
|
||||
@@ -115,12 +121,15 @@ static struct option mirror_speed_options[] = {
|
||||
GETOPT_VERBOSE,
|
||||
{0}
|
||||
};
|
||||
|
||||
static char mirror_speed_short_options[] = "hs:m:" SOPT_QUIET SOPT_VERBOSE;
|
||||
static char mirror_speed_help_text[] =
|
||||
"Usage: flexnbd " CMD_MIRROR_SPEED " <options>\n\n"
|
||||
"Set the maximum speed of a migration from a mirring server listening on SOCK.\n\n"
|
||||
HELP_LINE SOCK_LINE MAX_SPEED_LINE VERBOSE_LINE QUIET_LINE;
|
||||
HELP_LINE
|
||||
SOCK_LINE
|
||||
MAX_SPEED_LINE
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
static struct option mirror_options[] = {
|
||||
GETOPT_HELP,
|
||||
@@ -133,7 +142,6 @@ static struct option mirror_options[] = {
|
||||
GETOPT_VERBOSE,
|
||||
{0}
|
||||
};
|
||||
|
||||
static char mirror_short_options[] = "hs:l:p:ub:" SOPT_QUIET SOPT_VERBOSE;
|
||||
static char mirror_help_text[] =
|
||||
"Usage: flexnbd " CMD_MIRROR " <options>\n\n"
|
||||
@@ -143,7 +151,9 @@ static char mirror_help_text[] =
|
||||
"\t--" OPT_PORT ",-p <PORT>\tThe port to mirror to.\n"
|
||||
SOCK_LINE
|
||||
"\t--" OPT_UNLINK ",-u\tUnlink the local file when done.\n"
|
||||
BIND_LINE VERBOSE_LINE QUIET_LINE;
|
||||
BIND_LINE
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
static struct option break_options[] = {
|
||||
GETOPT_HELP,
|
||||
@@ -152,12 +162,14 @@ static struct option break_options[] = {
|
||||
GETOPT_VERBOSE,
|
||||
{0}
|
||||
};
|
||||
|
||||
static char break_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
|
||||
static char break_help_text[] =
|
||||
"Usage: flexnbd " CMD_BREAK " <options>\n\n"
|
||||
"Stop mirroring from the server with control socket SOCK.\n\n"
|
||||
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
|
||||
HELP_LINE
|
||||
SOCK_LINE
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
|
||||
static struct option status_options[] = {
|
||||
@@ -167,12 +179,14 @@ static struct option status_options[] = {
|
||||
GETOPT_VERBOSE,
|
||||
{0}
|
||||
};
|
||||
|
||||
static char status_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
|
||||
static char status_help_text[] =
|
||||
"Usage: flexnbd " CMD_STATUS " <options>\n\n"
|
||||
"Get the status for a server with control socket SOCK.\n\n"
|
||||
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
|
||||
HELP_LINE
|
||||
SOCK_LINE
|
||||
VERBOSE_LINE
|
||||
QUIET_LINE;
|
||||
|
||||
char help_help_text_arr[] =
|
||||
"Usage: flexnbd <cmd> [cmd options]\n\n"
|
||||
@@ -186,7 +200,8 @@ char help_help_text_arr[] =
|
||||
"\tflexnbd mirror-speed\n"
|
||||
"\tflexnbd break\n"
|
||||
"\tflexnbd status\n"
|
||||
"\tflexnbd help\n\n" "See flexnbd help <cmd> for further info\n";
|
||||
"\tflexnbd help\n\n"
|
||||
"See flexnbd help <cmd> for further info\n";
|
||||
/* Slightly odd array/pointer pair to stop the compiler from complaining
|
||||
* about symbol sizes
|
||||
*/
|
||||
@@ -199,8 +214,7 @@ void do_write(struct mode_readwrite_params *params);
|
||||
void do_remote_command(char* command, char* mode, int argc, char** argv);
|
||||
|
||||
|
||||
void read_serve_param(int c, char **ip_addr, char **ip_port, char **file,
|
||||
char **sock, int *default_deny, int *use_killswitch)
|
||||
void read_serve_param( int c, char **ip_addr, char **ip_port, char **file, char **sock, int *default_deny, int *use_killswitch )
|
||||
{
|
||||
switch(c){
|
||||
case 'h':
|
||||
@@ -240,7 +254,9 @@ void read_serve_param(int c, char **ip_addr, char **ip_port, char **file,
|
||||
void read_listen_param( int c,
|
||||
char **ip_addr,
|
||||
char **ip_port,
|
||||
char **file, char **sock, int *default_deny)
|
||||
char **file,
|
||||
char **sock,
|
||||
int *default_deny )
|
||||
{
|
||||
switch(c){
|
||||
case 'h':
|
||||
@@ -273,9 +289,7 @@ void read_listen_param(int c,
|
||||
}
|
||||
}
|
||||
|
||||
void read_readwrite_param(int c, char **ip_addr, char **ip_port,
|
||||
char **bind_addr, char **from, char **size,
|
||||
char *err_text)
|
||||
void read_readwrite_param( int c, char **ip_addr, char **ip_port, char **bind_addr, char **from, char **size, char *err_text )
|
||||
{
|
||||
switch(c){
|
||||
case 'h':
|
||||
@@ -334,7 +348,11 @@ void read_acl_param(int c, char **sock)
|
||||
read_sock_param( c, sock, acl_help_text );
|
||||
}
|
||||
|
||||
void read_mirror_speed_param(int c, char **sock, char **max_speed)
|
||||
void read_mirror_speed_param(
|
||||
int c,
|
||||
char **sock,
|
||||
char **max_speed
|
||||
)
|
||||
{
|
||||
switch( c ) {
|
||||
case 'h':
|
||||
@@ -359,10 +377,13 @@ void read_mirror_speed_param(int c, char **sock, char **max_speed)
|
||||
}
|
||||
}
|
||||
|
||||
void read_mirror_param(int c,
|
||||
void read_mirror_param(
|
||||
int c,
|
||||
char **sock,
|
||||
char **ip_addr,
|
||||
char **ip_port, int *unlink, char **bind_addr)
|
||||
char **ip_port,
|
||||
int *unlink,
|
||||
char **bind_addr )
|
||||
{
|
||||
switch( c ){
|
||||
case 'h':
|
||||
@@ -438,14 +459,10 @@ int mode_serve(int argc, char *argv[])
|
||||
struct flexnbd * flexnbd;
|
||||
|
||||
while (1) {
|
||||
c = getopt_long(argc, argv, serve_short_options, serve_options,
|
||||
NULL);
|
||||
if (c == -1) {
|
||||
break;
|
||||
}
|
||||
c = getopt_long(argc, argv, serve_short_options, serve_options, NULL);
|
||||
if ( c == -1 ) { break; }
|
||||
|
||||
read_serve_param(c, &ip_addr, &ip_port, &file, &sock,
|
||||
&default_deny, &use_killswitch);
|
||||
read_serve_param( c, &ip_addr, &ip_port, &file, &sock, &default_deny, &use_killswitch );
|
||||
}
|
||||
|
||||
if ( NULL == ip_addr || NULL == ip_port ) {
|
||||
@@ -456,14 +473,9 @@ int mode_serve(int argc, char *argv[])
|
||||
err = 1;
|
||||
fprintf( stderr, "--file is required\n" );
|
||||
}
|
||||
if (err) {
|
||||
exit_err(serve_help_text);
|
||||
}
|
||||
if ( err ) { exit_err( serve_help_text ); }
|
||||
|
||||
flexnbd =
|
||||
flexnbd_create_serving(ip_addr, ip_port, file, sock, default_deny,
|
||||
argc - optind, argv + optind,
|
||||
MAX_NBD_CLIENTS, use_killswitch);
|
||||
flexnbd = flexnbd_create_serving( ip_addr, ip_port, file, sock, default_deny, argc - optind, argv + optind, MAX_NBD_CLIENTS, use_killswitch );
|
||||
info( "Serving file %s", file );
|
||||
success = flexnbd_serve( flexnbd );
|
||||
flexnbd_destroy( flexnbd );
|
||||
@@ -487,11 +499,8 @@ int mode_listen(int argc, char *argv[])
|
||||
struct flexnbd * flexnbd;
|
||||
|
||||
while (1) {
|
||||
c = getopt_long(argc, argv, listen_short_options, listen_options,
|
||||
NULL);
|
||||
if (c == -1) {
|
||||
break;
|
||||
}
|
||||
c = getopt_long(argc, argv, listen_short_options, listen_options, NULL);
|
||||
if ( c == -1 ) { break; }
|
||||
|
||||
read_listen_param( c, &ip_addr, &ip_port,
|
||||
&file, &sock, &default_deny );
|
||||
@@ -505,16 +514,16 @@ int mode_listen(int argc, char *argv[])
|
||||
err = 1;
|
||||
fprintf( stderr, "--file is required\n" );
|
||||
}
|
||||
if (err) {
|
||||
exit_err(listen_help_text);
|
||||
}
|
||||
if ( err ) { exit_err( listen_help_text ); }
|
||||
|
||||
flexnbd = flexnbd_create_listening(ip_addr,
|
||||
flexnbd = flexnbd_create_listening(
|
||||
ip_addr,
|
||||
ip_port,
|
||||
file,
|
||||
sock,
|
||||
default_deny,
|
||||
argc - optind, argv + optind);
|
||||
argc - optind,
|
||||
argv + optind);
|
||||
success = flexnbd_serve( flexnbd );
|
||||
flexnbd_destroy( flexnbd );
|
||||
|
||||
@@ -536,25 +545,29 @@ int mode_listen(int argc, char *argv[])
|
||||
* char *s_length,
|
||||
* char *s_filename )
|
||||
*/
|
||||
void params_readwrite(int write_not_read,
|
||||
void params_readwrite(
|
||||
int write_not_read,
|
||||
struct mode_readwrite_params* out,
|
||||
char* s_ip_address,
|
||||
char* s_port,
|
||||
char* s_bind_address,
|
||||
char *s_from, char *s_length_or_filename)
|
||||
char* s_from,
|
||||
char* s_length_or_filename
|
||||
)
|
||||
{
|
||||
FATAL_IF_NULL(s_ip_address, "No IP address supplied");
|
||||
FATAL_IF_NULL(s_port, "No port number supplied");
|
||||
FATAL_IF_NULL(s_from, "No from supplied");
|
||||
FATAL_IF_NULL(s_length_or_filename, "No length supplied");
|
||||
|
||||
FATAL_IF_ZERO(parse_ip_to_sockaddr
|
||||
(&out->connect_to.generic, s_ip_address),
|
||||
"Couldn't parse connection address '%s'", s_ip_address);
|
||||
FATAL_IF_ZERO(
|
||||
parse_ip_to_sockaddr(&out->connect_to.generic, s_ip_address),
|
||||
"Couldn't parse connection address '%s'",
|
||||
s_ip_address
|
||||
);
|
||||
|
||||
if (s_bind_address != NULL &&
|
||||
parse_ip_to_sockaddr(&out->connect_from.generic,
|
||||
s_bind_address) == 0) {
|
||||
parse_ip_to_sockaddr(&out->connect_from.generic, s_bind_address) == 0) {
|
||||
fatal("Couldn't parse bind address '%s'", s_bind_address);
|
||||
}
|
||||
|
||||
@@ -562,27 +575,30 @@ void params_readwrite(int write_not_read,
|
||||
|
||||
long signed_from = atol(s_from);
|
||||
FATAL_IF_NEGATIVE( signed_from,
|
||||
"Can't read from a negative offset %d.",
|
||||
signed_from);
|
||||
"Can't read from a negative offset %d.", signed_from);
|
||||
out->from = signed_from;
|
||||
|
||||
if (write_not_read) {
|
||||
if (s_length_or_filename[0]-48 < 10) {
|
||||
out->len = atol(s_length_or_filename);
|
||||
out->data_fd = 0;
|
||||
} else {
|
||||
out->data_fd = open(s_length_or_filename, O_RDONLY);
|
||||
}
|
||||
else {
|
||||
out->data_fd = open(
|
||||
s_length_or_filename, O_RDONLY);
|
||||
FATAL_IF_NEGATIVE(out->data_fd,
|
||||
"Couldn't open %s", s_length_or_filename);
|
||||
off64_t signed_len = lseek64(out->data_fd, 0, SEEK_END);
|
||||
FATAL_IF_NEGATIVE(signed_len,
|
||||
"Couldn't find length of %s",
|
||||
s_length_or_filename);
|
||||
"Couldn't find length of %s", s_length_or_filename);
|
||||
out->len = signed_len;
|
||||
FATAL_IF_NEGATIVE(lseek64(out->data_fd, 0, SEEK_SET),
|
||||
"Couldn't rewind %s", s_length_or_filename);
|
||||
FATAL_IF_NEGATIVE(
|
||||
lseek64(out->data_fd, 0, SEEK_SET),
|
||||
"Couldn't rewind %s", s_length_or_filename
|
||||
);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
out->len = atol(s_length_or_filename);
|
||||
out->data_fd = 1;
|
||||
}
|
||||
@@ -602,15 +618,11 @@ int mode_read(int argc, char *argv[])
|
||||
struct mode_readwrite_params readwrite;
|
||||
|
||||
while (1){
|
||||
c = getopt_long(argc, argv, read_short_options, read_options,
|
||||
NULL);
|
||||
c = getopt_long(argc, argv, read_short_options, read_options, NULL);
|
||||
|
||||
if (c == -1) {
|
||||
break;
|
||||
}
|
||||
if ( c == -1 ) { break; }
|
||||
|
||||
read_readwrite_param(c, &ip_addr, &ip_port, &bind_addr, &from,
|
||||
&size, read_help_text);
|
||||
read_readwrite_param( c, &ip_addr, &ip_port, &bind_addr, &from, &size, read_help_text );
|
||||
}
|
||||
|
||||
if ( NULL == ip_addr || NULL == ip_port ) {
|
||||
@@ -621,13 +633,10 @@ int mode_read(int argc, char *argv[])
|
||||
err = 1;
|
||||
fprintf( stderr, "both --from and --size are required.\n" );
|
||||
}
|
||||
if (err) {
|
||||
exit_err(read_help_text);
|
||||
}
|
||||
if ( err ) { exit_err( read_help_text ); }
|
||||
|
||||
memset( &readwrite, 0, sizeof( readwrite ) );
|
||||
params_readwrite(0, &readwrite, ip_addr, ip_port, bind_addr, from,
|
||||
size);
|
||||
params_readwrite( 0, &readwrite, ip_addr, ip_port, bind_addr, from, size );
|
||||
do_read( &readwrite );
|
||||
return 0;
|
||||
}
|
||||
@@ -645,14 +654,10 @@ int mode_write(int argc, char *argv[])
|
||||
struct mode_readwrite_params readwrite;
|
||||
|
||||
while (1){
|
||||
c = getopt_long(argc, argv, write_short_options, write_options,
|
||||
NULL);
|
||||
if (c == -1) {
|
||||
break;
|
||||
}
|
||||
c = getopt_long(argc, argv, write_short_options, write_options, NULL);
|
||||
if ( c == -1 ) { break; }
|
||||
|
||||
read_readwrite_param(c, &ip_addr, &ip_port, &bind_addr, &from,
|
||||
&size, write_help_text);
|
||||
read_readwrite_param( c, &ip_addr, &ip_port, &bind_addr, &from, &size, write_help_text );
|
||||
}
|
||||
|
||||
if ( NULL == ip_addr || NULL == ip_port ) {
|
||||
@@ -663,13 +668,10 @@ int mode_write(int argc, char *argv[])
|
||||
err = 1;
|
||||
fprintf( stderr, "both --from and --size are required.\n" );
|
||||
}
|
||||
if (err) {
|
||||
exit_err(write_help_text);
|
||||
}
|
||||
if ( err ) { exit_err( write_help_text ); }
|
||||
|
||||
memset( &readwrite, 0, sizeof( readwrite ) );
|
||||
params_readwrite(1, &readwrite, ip_addr, ip_port, bind_addr, from,
|
||||
size);
|
||||
params_readwrite( 1, &readwrite, ip_addr, ip_port, bind_addr, from, size );
|
||||
do_write( &readwrite );
|
||||
return 0;
|
||||
}
|
||||
@@ -681,9 +683,7 @@ int mode_acl(int argc, char *argv[])
|
||||
|
||||
while (1) {
|
||||
c = getopt_long( argc, argv, acl_short_options, acl_options, NULL );
|
||||
if (c == -1) {
|
||||
break;
|
||||
}
|
||||
if ( c == -1 ) { break; }
|
||||
read_acl_param( c, &sock );
|
||||
}
|
||||
|
||||
@@ -708,11 +708,8 @@ int mode_mirror_speed(int argc, char *argv[])
|
||||
char *speed = NULL;
|
||||
|
||||
while( 1 ) {
|
||||
c = getopt_long(argc, argv, mirror_speed_short_options,
|
||||
mirror_speed_options, NULL);
|
||||
if (-1 == c) {
|
||||
break;
|
||||
}
|
||||
c = getopt_long( argc, argv, mirror_speed_short_options, mirror_speed_options, NULL );
|
||||
if ( -1 == c ) { break; }
|
||||
read_mirror_speed_param( c, &sock, &speed );
|
||||
}
|
||||
|
||||
@@ -742,15 +739,14 @@ int mode_mirror(int argc, char *argv[])
|
||||
remote_argv[2] = "exit";
|
||||
|
||||
while (1) {
|
||||
c = getopt_long(argc, argv, mirror_short_options, mirror_options,
|
||||
NULL);
|
||||
if (-1 == c) {
|
||||
break;
|
||||
}
|
||||
c = getopt_long( argc, argv, mirror_short_options, mirror_options, NULL);
|
||||
if ( -1 == c ) { break; }
|
||||
read_mirror_param( c,
|
||||
&sock,
|
||||
&remote_argv[0],
|
||||
&remote_argv[1], &unlink, &remote_argv[3]);
|
||||
&remote_argv[1],
|
||||
&unlink,
|
||||
&remote_argv[3] );
|
||||
}
|
||||
|
||||
if ( NULL == sock ){
|
||||
@@ -761,16 +757,13 @@ int mode_mirror(int argc, char *argv[])
|
||||
fprintf( stderr, "both --addr and --port are required.\n");
|
||||
err = 1;
|
||||
}
|
||||
if (err) {
|
||||
exit_err(mirror_help_text);
|
||||
}
|
||||
if (unlink) {
|
||||
remote_argv[2] = "unlink";
|
||||
}
|
||||
if ( err ) { exit_err( mirror_help_text ); }
|
||||
if ( unlink ) { remote_argv[2] = "unlink"; }
|
||||
|
||||
if (remote_argv[3] == NULL) {
|
||||
do_remote_command( "mirror", sock, 3, remote_argv );
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
do_remote_command( "mirror", sock, 4, remote_argv );
|
||||
}
|
||||
|
||||
@@ -784,11 +777,8 @@ int mode_break(int argc, char *argv[])
|
||||
char *sock = NULL;
|
||||
|
||||
while (1) {
|
||||
c = getopt_long(argc, argv, break_short_options, break_options,
|
||||
NULL);
|
||||
if (-1 == c) {
|
||||
break;
|
||||
}
|
||||
c = getopt_long( argc, argv, break_short_options, break_options, NULL );
|
||||
if ( -1 == c ) { break; }
|
||||
read_break_param( c, &sock );
|
||||
}
|
||||
|
||||
@@ -808,11 +798,8 @@ int mode_status(int argc, char *argv[])
|
||||
char *sock = NULL;
|
||||
|
||||
while (1) {
|
||||
c = getopt_long(argc, argv, status_short_options, status_options,
|
||||
NULL);
|
||||
if (-1 == c) {
|
||||
break;
|
||||
}
|
||||
c = getopt_long( argc, argv, status_short_options, status_options, NULL );
|
||||
if ( -1 == c ) { break; }
|
||||
read_status_param( c, &sock );
|
||||
}
|
||||
|
||||
@@ -849,9 +836,7 @@ int mode_help(int argc, char *argv[])
|
||||
help_text = mirror_help_text;
|
||||
} else if ( IS_CMD( CMD_STATUS, cmd ) ) {
|
||||
help_text = status_help_text;
|
||||
} else {
|
||||
exit_err(help_help_text);
|
||||
}
|
||||
} else { exit_err( help_help_text ); }
|
||||
}
|
||||
|
||||
fprintf( stdout, "%s\n", help_text );
|
||||
@@ -863,27 +848,37 @@ void mode(char *mode, int argc, char **argv)
|
||||
{
|
||||
if ( IS_CMD( CMD_SERVE, mode ) ) {
|
||||
exit( mode_serve( argc, argv ) );
|
||||
} else if (IS_CMD(CMD_LISTEN, mode)) {
|
||||
}
|
||||
else if ( IS_CMD( CMD_LISTEN, mode ) ) {
|
||||
exit( mode_listen( argc, argv ) );
|
||||
} else if (IS_CMD(CMD_READ, mode)) {
|
||||
}
|
||||
else if ( IS_CMD( CMD_READ, mode ) ) {
|
||||
mode_read( argc, argv );
|
||||
} else if (IS_CMD(CMD_WRITE, mode)) {
|
||||
}
|
||||
else if ( IS_CMD( CMD_WRITE, mode ) ) {
|
||||
mode_write( argc, argv );
|
||||
} else if (IS_CMD(CMD_ACL, mode)) {
|
||||
}
|
||||
else if ( IS_CMD( CMD_ACL, mode ) ) {
|
||||
mode_acl( argc, argv );
|
||||
} else if ( IS_CMD ( CMD_MIRROR_SPEED, mode ) ) {
|
||||
mode_mirror_speed( argc, argv );
|
||||
} else if (IS_CMD(CMD_MIRROR, mode)) {
|
||||
}
|
||||
else if ( IS_CMD( CMD_MIRROR, mode ) ) {
|
||||
mode_mirror( argc, argv );
|
||||
} else if (IS_CMD(CMD_BREAK, mode)) {
|
||||
}
|
||||
else if ( IS_CMD( CMD_BREAK, mode ) ) {
|
||||
mode_break( argc, argv );
|
||||
} else if (IS_CMD(CMD_STATUS, mode)) {
|
||||
}
|
||||
else if ( IS_CMD( CMD_STATUS, mode ) ) {
|
||||
mode_status( argc, argv );
|
||||
} else if (IS_CMD(CMD_HELP, mode)) {
|
||||
}
|
||||
else if ( IS_CMD( CMD_HELP, mode ) ) {
|
||||
mode_help( argc-1, argv+1 );
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
mode_help( argc-1, argv+1 );
|
||||
exit( 1 );
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
@@ -21,7 +21,8 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/tcp.h>
|
||||
|
||||
struct server *server_create(struct flexnbd *flexnbd,
|
||||
struct server * server_create (
|
||||
struct flexnbd * flexnbd,
|
||||
char* s_ip_address,
|
||||
char* s_port,
|
||||
char* s_file,
|
||||
@@ -29,7 +30,8 @@ struct server *server_create(struct flexnbd *flexnbd,
|
||||
int acl_entries,
|
||||
char** s_acl_entries,
|
||||
int max_nbd_clients,
|
||||
int use_killswitch, int success)
|
||||
int use_killswitch,
|
||||
int success)
|
||||
{
|
||||
NULLCHECK( flexnbd );
|
||||
struct server * out;
|
||||
@@ -41,18 +43,19 @@ struct server *server_create(struct flexnbd *flexnbd,
|
||||
|
||||
server_allow_new_clients( out );
|
||||
|
||||
out->nbd_client =
|
||||
xmalloc(max_nbd_clients * sizeof(struct client_tbl_entry));
|
||||
out->nbd_client = xmalloc( max_nbd_clients * sizeof( struct client_tbl_entry ) );
|
||||
out->tcp_backlog = 10; /* does this need to be settable? */
|
||||
|
||||
FATAL_IF_NULL(s_ip_address, "No IP address supplied");
|
||||
FATAL_IF_NULL(s_port, "No port number supplied");
|
||||
FATAL_IF_NULL(s_file, "No filename supplied");
|
||||
NULLCHECK( s_ip_address );
|
||||
FATAL_IF_ZERO(parse_ip_to_sockaddr
|
||||
(&out->bind_to.generic, s_ip_address),
|
||||
FATAL_IF_ZERO(
|
||||
parse_ip_to_sockaddr(&out->bind_to.generic, s_ip_address),
|
||||
"Couldn't parse server address '%s' (use 0 if "
|
||||
"you want to bind to all IPs)", s_ip_address);
|
||||
"you want to bind to all IPs)",
|
||||
s_ip_address
|
||||
);
|
||||
|
||||
|
||||
out->acl = acl_create( acl_entries, s_acl_entries, default_deny );
|
||||
@@ -107,7 +110,8 @@ void server_unlink(struct server *serve)
|
||||
|
||||
FATAL_IF_NEGATIVE( unlink( serve->filename ),
|
||||
"Failed to unlink %s: %s",
|
||||
serve->filename, strerror(errno));
|
||||
serve->filename,
|
||||
strerror( errno ) );
|
||||
|
||||
}
|
||||
|
||||
@@ -151,8 +155,7 @@ void server_unlock_start_mirror(struct server *serve)
|
||||
{
|
||||
debug("Mirror start unlocking");
|
||||
|
||||
SERVER_UNLOCK(serve, l_start_mirror,
|
||||
"Problem with start mirror unlock");
|
||||
SERVER_UNLOCK( serve, l_start_mirror, "Problem with start mirror unlock" );
|
||||
}
|
||||
|
||||
int server_start_mirror_locked( struct server * serve )
|
||||
@@ -183,9 +186,8 @@ void serve_open_server_socket(struct server *params)
|
||||
{
|
||||
NULLCHECK( params );
|
||||
|
||||
params->server_fd =
|
||||
socket(params->bind_to.generic.sa_family ==
|
||||
AF_INET ? PF_INET : PF_INET6, SOCK_STREAM, 0);
|
||||
params->server_fd = socket(params->bind_to.generic.sa_family == AF_INET ?
|
||||
PF_INET : PF_INET6, SOCK_STREAM, 0);
|
||||
|
||||
FATAL_IF_NEGATIVE( params->server_fd, "Couldn't create server socket" );
|
||||
|
||||
@@ -197,32 +199,35 @@ void serve_open_server_socket(struct server *params)
|
||||
* problem. It's also indicative of something odd going on, so
|
||||
* we barf.
|
||||
*/
|
||||
FATAL_IF_NEGATIVE(sock_set_reuseaddr(params->server_fd, 1),
|
||||
"Couldn't set SO_REUSEADDR");
|
||||
FATAL_IF_NEGATIVE(
|
||||
sock_set_reuseaddr( params->server_fd, 1 ), "Couldn't set SO_REUSEADDR"
|
||||
);
|
||||
|
||||
/* TCP_NODELAY makes everything not be slow. If we can't set
|
||||
* this, again, there's something odd going on which we don't
|
||||
* understand.
|
||||
*/
|
||||
FATAL_IF_NEGATIVE(sock_set_tcp_nodelay(params->server_fd, 1),
|
||||
"Couldn't set TCP_NODELAY");
|
||||
FATAL_IF_NEGATIVE(
|
||||
sock_set_tcp_nodelay( params->server_fd, 1 ), "Couldn't set TCP_NODELAY"
|
||||
);
|
||||
|
||||
/* If we can't bind, presumably that's because someone else is
|
||||
* squatting on our ip/port combo, or the ip isn't yet
|
||||
* configured. Ideally we want to retry this. */
|
||||
FATAL_UNLESS_ZERO(sock_try_bind
|
||||
(params->server_fd, ¶ms->bind_to.generic),
|
||||
FATAL_UNLESS_ZERO(
|
||||
sock_try_bind( params->server_fd, ¶ms->bind_to.generic ),
|
||||
SHOW_ERRNO( "Failed to bind() socket" )
|
||||
);
|
||||
|
||||
FATAL_IF_NEGATIVE(listen(params->server_fd, params->tcp_backlog),
|
||||
"Couldn't listen on server socket");
|
||||
FATAL_IF_NEGATIVE(
|
||||
listen(params->server_fd, params->tcp_backlog),
|
||||
"Couldn't listen on server socket"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int tryjoin_client_thread(struct client_tbl_entry *entry,
|
||||
int (*joinfunc) (pthread_t, void **))
|
||||
int tryjoin_client_thread( struct client_tbl_entry *entry, int (*joinfunc)(pthread_t, void **) )
|
||||
{
|
||||
|
||||
NULLCHECK( entry );
|
||||
@@ -234,25 +239,25 @@ int tryjoin_client_thread(struct client_tbl_entry *entry,
|
||||
if (entry->thread != 0) {
|
||||
char s_client_address[128];
|
||||
|
||||
sockaddr_address_string(&entry->address.generic,
|
||||
&s_client_address[0], 128);
|
||||
sockaddr_address_string( &entry->address.generic, &s_client_address[0], 128 );
|
||||
|
||||
debug("%s(%p,...)",
|
||||
joinfunc == pthread_join ? "joining" : "tryjoining",
|
||||
entry->thread);
|
||||
debug( "%s(%p,...)", joinfunc == pthread_join ? "joining" : "tryjoining", entry->thread );
|
||||
int join_errno = joinfunc(entry->thread, &status);
|
||||
|
||||
/* join_errno can legitimately be ESRCH if the thread is
|
||||
* already dead, but the client still needs tidying up. */
|
||||
if (join_errno != 0 && !entry->client->stopped ) {
|
||||
debug("join_errno was %s, stopped was %d",
|
||||
strerror(join_errno), entry->client->stopped);
|
||||
debug( "join_errno was %s, stopped was %d", strerror( join_errno ), entry->client->stopped );
|
||||
FATAL_UNLESS( join_errno == EBUSY,
|
||||
"Problem with joining thread %p: %s",
|
||||
entry->thread, strerror(join_errno));
|
||||
} else if (join_errno == 0) {
|
||||
entry->thread,
|
||||
strerror(join_errno) );
|
||||
}
|
||||
else if ( join_errno == 0 ) {
|
||||
debug("nbd thread %016x exited (%s) with status %ld",
|
||||
entry->thread, s_client_address, (uintptr_t) status);
|
||||
entry->thread,
|
||||
s_client_address,
|
||||
(uintptr_t)status);
|
||||
client_destroy( entry->client );
|
||||
entry->client = NULL;
|
||||
entry->thread = 0;
|
||||
@@ -282,8 +287,7 @@ int cleanup_client_thread(struct client_tbl_entry *entry)
|
||||
return tryjoin_client_thread( entry, pthread_tryjoin_np );
|
||||
}
|
||||
|
||||
void cleanup_client_threads(struct client_tbl_entry *entries,
|
||||
size_t entries_len)
|
||||
void cleanup_client_threads( struct client_tbl_entry * entries, size_t entries_len )
|
||||
{
|
||||
size_t i;
|
||||
for( i = 0; i < entries_len; i++ ) {
|
||||
@@ -329,8 +333,6 @@ int server_count_clients(struct server *params)
|
||||
NULLCHECK( params );
|
||||
int i, count = 0;
|
||||
|
||||
cleanup_client_threads(params->nbd_client, params->max_nbd_clients);
|
||||
|
||||
for ( i = 0 ; i < params->max_nbd_clients ; i++ ) {
|
||||
if ( params->nbd_client[i].thread != 0 ) {
|
||||
count++;
|
||||
@@ -345,8 +347,7 @@ int server_count_clients(struct server *params)
|
||||
* to the current acl. If params->acl is NULL, the result will be 1,
|
||||
* otherwise it will be the result of acl_includes().
|
||||
*/
|
||||
int server_acl_accepts(struct server *params,
|
||||
union mysockaddr *client_address)
|
||||
int server_acl_accepts( struct server *params, union mysockaddr * client_address )
|
||||
{
|
||||
NULLCHECK( params );
|
||||
NULLCHECK( client_address );
|
||||
@@ -365,7 +366,8 @@ int server_acl_accepts(struct server *params,
|
||||
}
|
||||
|
||||
|
||||
int server_should_accept_client(struct server *params,
|
||||
int server_should_accept_client(
|
||||
struct server * params,
|
||||
union mysockaddr * client_address,
|
||||
char *s_client_address,
|
||||
size_t s_client_address_len )
|
||||
@@ -374,9 +376,9 @@ int server_should_accept_client(struct server *params,
|
||||
NULLCHECK( client_address );
|
||||
NULLCHECK( s_client_address );
|
||||
|
||||
const char *result =
|
||||
sockaddr_address_string(&client_address->generic, s_client_address,
|
||||
s_client_address_len);
|
||||
const char* result = sockaddr_address_string(
|
||||
&client_address->generic, s_client_address, s_client_address_len
|
||||
);
|
||||
|
||||
if ( NULL == result ) {
|
||||
warn( "Rejecting client %s: Bad client_address", s_client_address );
|
||||
@@ -384,8 +386,7 @@ int server_should_accept_client(struct server *params,
|
||||
}
|
||||
|
||||
if ( !server_acl_accepts( params, client_address ) ) {
|
||||
warn("Rejecting client %s: Access control error",
|
||||
s_client_address);
|
||||
warn( "Rejecting client %s: Access control error", s_client_address );
|
||||
debug( "We %s have an acl, and default_deny is %s",
|
||||
(params->acl ? "do" : "do not"),
|
||||
(params->acl->default_deny ? "true" : "false") );
|
||||
@@ -397,11 +398,11 @@ int server_should_accept_client(struct server *params,
|
||||
|
||||
|
||||
|
||||
int spawn_client_thread(struct client *client_params,
|
||||
int spawn_client_thread(
|
||||
struct client * client_params,
|
||||
pthread_t *out_thread)
|
||||
{
|
||||
int result =
|
||||
pthread_create(out_thread, NULL, client_serve, client_params);
|
||||
int result = pthread_create(out_thread, NULL, client_serve, client_params);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -411,8 +412,10 @@ int spawn_client_thread(struct client *client_params,
|
||||
* to handle it. Rejects the connection if there is an ACL, and the far end's
|
||||
* address doesn't match, or if there are too many clients already connected.
|
||||
*/
|
||||
void accept_nbd_client(struct server *params,
|
||||
int client_fd, union mysockaddr *client_address)
|
||||
void accept_nbd_client(
|
||||
struct server* params,
|
||||
int client_fd,
|
||||
union mysockaddr* client_address)
|
||||
{
|
||||
NULLCHECK(params);
|
||||
NULLCHECK(client_address);
|
||||
@@ -421,15 +424,11 @@ void accept_nbd_client(struct server *params,
|
||||
int slot;
|
||||
char s_client_address[64] = {0};
|
||||
|
||||
FATAL_IF_NEGATIVE(sock_set_keepalive_params
|
||||
(client_fd, CLIENT_KEEPALIVE_TIME,
|
||||
CLIENT_KEEPALIVE_INTVL, CLIENT_KEEPALIVE_PROBES),
|
||||
"Error setting keepalive parameters on client socket fd %d",
|
||||
client_fd);
|
||||
FATAL_IF_NEGATIVE( sock_set_keepalive_params( client_fd, CLIENT_KEEPALIVE_TIME, CLIENT_KEEPALIVE_INTVL, CLIENT_KEEPALIVE_PROBES),
|
||||
"Error setting keepalive parameters on client socket fd %d", client_fd );
|
||||
|
||||
|
||||
if (!server_should_accept_client
|
||||
(params, client_address, s_client_address, 64)) {
|
||||
if ( !server_should_accept_client( params, client_address, s_client_address, 64 ) ) {
|
||||
FATAL_IF_NEGATIVE( close( client_fd ),
|
||||
"Error closing client socket fd %d", client_fd );
|
||||
debug("Closed client socket fd %d", client_fd);
|
||||
@@ -463,8 +462,7 @@ void accept_nbd_client(struct server *params,
|
||||
return;
|
||||
}
|
||||
|
||||
debug("nbd thread %p started (%s)", params->nbd_client[slot].thread,
|
||||
s_client_address);
|
||||
debug("nbd thread %p started (%s)", params->nbd_client[slot].thread, s_client_address);
|
||||
}
|
||||
|
||||
|
||||
@@ -485,12 +483,8 @@ void server_audit_clients(struct server *serve)
|
||||
*/
|
||||
for( i = 0; i < serve->max_nbd_clients; i++ ) {
|
||||
entry = &serve->nbd_client[i];
|
||||
if (0 == entry->thread) {
|
||||
continue;
|
||||
}
|
||||
if (server_acl_accepts(serve, &entry->address)) {
|
||||
continue;
|
||||
}
|
||||
if ( 0 == entry->thread ) { continue; }
|
||||
if ( server_acl_accepts( serve, &entry->address ) ) { continue; }
|
||||
client_signal_stop( entry->client );
|
||||
}
|
||||
}
|
||||
@@ -550,9 +544,7 @@ void server_replace_acl(struct server *serve, struct acl *new_acl)
|
||||
struct acl * old_acl = serve->acl;
|
||||
serve->acl = new_acl;
|
||||
/* We should always have an old_acl, but just in case... */
|
||||
if (old_acl) {
|
||||
acl_destroy(old_acl);
|
||||
}
|
||||
if ( old_acl ) { acl_destroy( old_acl ); }
|
||||
}
|
||||
server_unlock_acl( serve );
|
||||
|
||||
@@ -620,13 +612,12 @@ int server_accept(struct server *params)
|
||||
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(params->server_fd, &fds);
|
||||
if (0 < signal_fd) {
|
||||
FD_SET(signal_fd, &fds);
|
||||
}
|
||||
if( 0 < signal_fd ) { FD_SET(signal_fd, &fds); }
|
||||
self_pipe_fd_set( params->close_signal, &fds );
|
||||
self_pipe_fd_set( params->acl_updated_signal, &fds );
|
||||
|
||||
FATAL_IF_NEGATIVE(sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
|
||||
FATAL_IF_NEGATIVE(
|
||||
sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
|
||||
SHOW_ERRNO( "select() failed" )
|
||||
);
|
||||
|
||||
@@ -639,8 +630,7 @@ int server_accept(struct server *params)
|
||||
if ( 0 < signal_fd && FD_ISSET( signal_fd, &fds ) ){
|
||||
debug( "Stop signal received." );
|
||||
server_close_clients( params );
|
||||
params->success = params->success
|
||||
&& serve_shutdown_is_graceful(params);
|
||||
params->success = params->success && serve_shutdown_is_graceful( params );
|
||||
should_continue = 0;
|
||||
}
|
||||
|
||||
@@ -651,8 +641,7 @@ int server_accept(struct server *params)
|
||||
}
|
||||
|
||||
if ( FD_ISSET( params->server_fd, &fds ) ){
|
||||
int client_fd =
|
||||
accept(params->server_fd, &client_address.generic, &socklen);
|
||||
int client_fd = accept( params->server_fd, &client_address.generic, &socklen );
|
||||
|
||||
if ( params->allow_new_clients ) {
|
||||
debug("Accepted nbd client socket fd %d", client_fd);
|
||||
@@ -687,7 +676,8 @@ void *build_allocation_map_thread(void *serve_uncast)
|
||||
|
||||
if ( build_allocation_map( serve->allocation_map, fd ) ) {
|
||||
serve->allocation_map_built = 1;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
/* We can operate without it, but we can't free it without a race.
|
||||
* All that happens if we leave it is that it gradually builds up an
|
||||
* *incomplete* record of writes. Nobody will use it, as
|
||||
@@ -720,17 +710,9 @@ void serve_init_allocation_map(struct server *params)
|
||||
|
||||
FATAL_IF_NEGATIVE(fd, "Couldn't open %s", params->filename );
|
||||
size = lseek64( fd, 0, SEEK_END );
|
||||
|
||||
/* If discs are not in multiples of 512, then odd things happen,
|
||||
* resulting in reads/writes past the ends of files.
|
||||
*/
|
||||
if (size != (size & ~0x1ff)) {
|
||||
warn("file does not fit into 512-byte sectors; the end of the file will be ignored.");
|
||||
size &= ~0x1ff;
|
||||
}
|
||||
|
||||
params->size = size;
|
||||
FATAL_IF_NEGATIVE(size, "Couldn't find size of %s", params->filename);
|
||||
FATAL_IF_NEGATIVE( size, "Couldn't find size of %s",
|
||||
params->filename );
|
||||
|
||||
params->allocation_map =
|
||||
bitset_alloc( params->size, block_allocation_resolution );
|
||||
@@ -756,8 +738,7 @@ void server_allow_new_clients(struct server *serve)
|
||||
return;
|
||||
}
|
||||
|
||||
void server_join_clients(struct server *serve)
|
||||
{
|
||||
void server_join_clients( struct server * serve ) {
|
||||
int i;
|
||||
void* status;
|
||||
|
||||
@@ -770,8 +751,7 @@ void server_join_clients(struct server *serve)
|
||||
if ( 0 == err ) {
|
||||
serve->nbd_client[i].thread = 0;
|
||||
} else {
|
||||
warn("Error %s (%i) joining thread %p", strerror(err), err,
|
||||
thread_id);
|
||||
warn( "Error %s (%i) joining thread %p", strerror( err ), err, thread_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -823,9 +803,7 @@ void serve_cleanup(struct server *params,
|
||||
|
||||
info("cleaning up");
|
||||
|
||||
if (params->server_fd) {
|
||||
close(params->server_fd);
|
||||
}
|
||||
if (params->server_fd){ close(params->server_fd); }
|
||||
|
||||
/* need to stop background build if we're killed very early on */
|
||||
pthread_cancel(params->allocation_map_builder_thread);
|
||||
@@ -834,18 +812,14 @@ void serve_cleanup(struct server *params,
|
||||
int need_mirror_lock;
|
||||
need_mirror_lock = !server_start_mirror_locked( params );
|
||||
|
||||
if (need_mirror_lock) {
|
||||
server_lock_start_mirror(params);
|
||||
}
|
||||
if ( need_mirror_lock ) { server_lock_start_mirror( params ); }
|
||||
{
|
||||
if ( server_is_mirroring( params ) ) {
|
||||
server_abandon_mirror( params );
|
||||
}
|
||||
server_prevent_mirror_start( params );
|
||||
}
|
||||
if (need_mirror_lock) {
|
||||
server_unlock_start_mirror(params);
|
||||
}
|
||||
if ( need_mirror_lock ) { server_unlock_start_mirror( params ); }
|
||||
|
||||
server_join_clients( params );
|
||||
|
||||
@@ -890,11 +864,8 @@ uint64_t server_mirror_bytes_remaining(struct server * serve)
|
||||
{
|
||||
if ( server_is_mirroring( serve ) ) {
|
||||
uint64_t bytes_to_xfer =
|
||||
bitset_stream_queued_bytes(serve->allocation_map,
|
||||
BITSET_STREAM_SET) + (serve->size -
|
||||
serve->
|
||||
mirror->
|
||||
offset);
|
||||
bitset_stream_queued_bytes( serve->allocation_map, BITSET_STREAM_SET ) +
|
||||
( serve->size - serve->mirror->offset );
|
||||
|
||||
return bytes_to_xfer;
|
||||
}
|
||||
@@ -940,8 +911,10 @@ void server_abandon_mirror(struct server *serve)
|
||||
* We can set abandon_signal after mirror_super has checked it, but
|
||||
* before the reset. However, mirror_reset doesn't clear abandon_signal
|
||||
* so it'll just terminate early on the next pass. */
|
||||
ERROR_UNLESS(self_pipe_signal(serve->mirror->abandon_signal),
|
||||
"Failed to signal abandon to mirror");
|
||||
ERROR_UNLESS(
|
||||
self_pipe_signal( serve->mirror->abandon_signal ),
|
||||
"Failed to signal abandon to mirror"
|
||||
);
|
||||
|
||||
pthread_t tid = serve->mirror_super->thread;
|
||||
pthread_join( tid, NULL );
|
||||
@@ -975,9 +948,7 @@ int do_serve(struct server *params, struct self_pipe *open_signal)
|
||||
|
||||
/* Only signal that we are open for business once the server
|
||||
socket is open */
|
||||
if (NULL != open_signal) {
|
||||
self_pipe_signal(open_signal);
|
||||
}
|
||||
if ( NULL != open_signal ) { self_pipe_signal( open_signal ); }
|
||||
|
||||
serve_init_allocation_map(params);
|
||||
serve_accept_loop(params);
|
||||
|
@@ -103,7 +103,8 @@ struct server {
|
||||
int success;
|
||||
};
|
||||
|
||||
struct server *server_create(struct flexnbd *flexnbd,
|
||||
struct server * server_create(
|
||||
struct flexnbd * flexnbd,
|
||||
char* s_ip_address,
|
||||
char* s_port,
|
||||
char* s_file,
|
||||
@@ -111,7 +112,8 @@ struct server *server_create(struct flexnbd *flexnbd,
|
||||
int acl_entries,
|
||||
char** s_acl_entries,
|
||||
int max_nbd_clients,
|
||||
int use_killswitch, int success);
|
||||
int use_killswitch,
|
||||
int success );
|
||||
void server_destroy( struct server * );
|
||||
int server_is_closed(struct server* serve);
|
||||
void serve_signal_close( struct server *serve );
|
||||
@@ -165,3 +167,4 @@ struct mode_readwrite_params {
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -21,20 +21,17 @@ struct status *status_create(struct server *serve)
|
||||
if ( status->is_mirroring ) {
|
||||
status->migration_duration = monotonic_time_ms();
|
||||
|
||||
if ((serve->mirror->migration_started) <
|
||||
status->migration_duration) {
|
||||
if ( ( serve->mirror->migration_started ) < status->migration_duration ) {
|
||||
status->migration_duration -= serve->mirror->migration_started;
|
||||
} else {
|
||||
status->migration_duration = 0;
|
||||
}
|
||||
status->migration_duration /= 1000;
|
||||
status->migration_speed = server_mirror_bps( serve );
|
||||
status->migration_speed_limit =
|
||||
serve->mirror->max_bytes_per_second;
|
||||
status->migration_speed_limit = serve->mirror->max_bytes_per_second;
|
||||
|
||||
status->migration_seconds_left = server_mirror_eta( serve );
|
||||
status->migration_bytes_left =
|
||||
server_mirror_bytes_remaining(serve);
|
||||
status->migration_bytes_left = server_mirror_bytes_remaining( serve );
|
||||
}
|
||||
|
||||
server_unlock_start_mirror( serve );
|
||||
@@ -80,3 +77,4 @@ void status_destroy(struct status *status)
|
||||
NULLCHECK( status );
|
||||
free( status );
|
||||
}
|
||||
|
||||
|
@@ -101,3 +101,4 @@ void status_destroy(struct status *);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -1,3 +1,5 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'flexnbd'
|
||||
require 'file_writer'
|
||||
|
||||
@@ -7,23 +9,18 @@ class Environment
|
||||
|
||||
def initialize
|
||||
@blocksize = 1024
|
||||
@filename1 = "/tmp/.flexnbd.test.#{$PROCESS_ID}.#{Time.now.to_i}.1"
|
||||
@filename2 = "/tmp/.flexnbd.test.#{$PROCESS_ID}.#{Time.now.to_i}.2"
|
||||
@ip = '127.0.0.1'
|
||||
@available_ports = [*40_000..41_000] - listening_ports
|
||||
@filename1 = "/tmp/.flexnbd.test.#{$$}.#{Time.now.to_i}.1"
|
||||
@filename2 = "/tmp/.flexnbd.test.#{$$}.#{Time.now.to_i}.2"
|
||||
@ip = "127.0.0.1"
|
||||
@available_ports = [*40000..41000] - listening_ports
|
||||
@port1 = @available_ports.shift
|
||||
@port2 = @available_ports.shift
|
||||
@nbd1 = FlexNBD::FlexNBD.new('../../build/flexnbd', @ip, @port1)
|
||||
@nbd2 = FlexNBD::FlexNBD.new('../../build/flexnbd', @ip, @port2)
|
||||
@nbd1 = FlexNBD::FlexNBD.new("../../build/flexnbd", @ip, @port1)
|
||||
@nbd2 = FlexNBD::FlexNBD.new("../../build/flexnbd", @ip, @port2)
|
||||
|
||||
@fake_pid = nil
|
||||
end
|
||||
|
||||
def blocksize=(b)
|
||||
raise RuntimeError, "Unable to change blocksize after files have been opened" if @file1 or @file2
|
||||
@blocksize = b
|
||||
end
|
||||
|
||||
def prefetch_proxy!
|
||||
@nbd1.prefetch_proxy = true
|
||||
@nbd2.prefetch_proxy = true
|
||||
@@ -32,11 +29,11 @@ class Environment
|
||||
def proxy1(port=@port2)
|
||||
@nbd1.proxy(@ip, port)
|
||||
end
|
||||
|
||||
def proxy2(port=@port1)
|
||||
@nbd2.proxy(@ip, port)
|
||||
end
|
||||
|
||||
|
||||
def serve1(*acl)
|
||||
@nbd1.serve(@filename1, *acl)
|
||||
end
|
||||
@@ -45,6 +42,7 @@ class Environment
|
||||
@nbd2.serve(@filename2, *acl)
|
||||
end
|
||||
|
||||
|
||||
def listen1( *acl )
|
||||
@nbd1.listen( @filename1, *(acl.empty? ? @acl1: acl) )
|
||||
end
|
||||
@@ -53,6 +51,7 @@ class Environment
|
||||
@nbd2.listen( @filename2, *acl )
|
||||
end
|
||||
|
||||
|
||||
def break1
|
||||
@nbd1.break
|
||||
end
|
||||
@@ -65,6 +64,7 @@ class Environment
|
||||
@nbd2.acl( *acl )
|
||||
end
|
||||
|
||||
|
||||
def status1
|
||||
@nbd1.status.first
|
||||
end
|
||||
@@ -73,6 +73,8 @@ class Environment
|
||||
@nbd2.status.first
|
||||
end
|
||||
|
||||
|
||||
|
||||
def mirror12
|
||||
@nbd1.mirror( @nbd2.ip, @nbd2.port )
|
||||
end
|
||||
@@ -85,6 +87,7 @@ class Environment
|
||||
@nbd1.mirror_unlink( @nbd2.ip, @nbd2.port, 2 )
|
||||
end
|
||||
|
||||
|
||||
def write1( data )
|
||||
@nbd1.write( 0, data )
|
||||
end
|
||||
@@ -97,17 +100,20 @@ class Environment
|
||||
@file2 = FileWriter.new(@filename2, @blocksize).write(data)
|
||||
end
|
||||
|
||||
|
||||
def truncate1( size )
|
||||
system "truncate -s #{size} #{@filename1}"
|
||||
end
|
||||
|
||||
|
||||
def listening_ports
|
||||
`netstat -ltn`
|
||||
.split("\n")
|
||||
.map { |x| x.split(/\s+/) }[2..-1]
|
||||
.map { |l| l[3].split(':')[-1].to_i }
|
||||
`netstat -ltn`.
|
||||
split("\n").
|
||||
map { |x| x.split(/\s+/) }[2..-1].
|
||||
map { |l| l[3].split(":")[-1].to_i }
|
||||
end
|
||||
|
||||
|
||||
def cleanup
|
||||
if @fake_pid
|
||||
begin
|
||||
@@ -116,35 +122,41 @@ class Environment
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@nbd1.can_die(0)
|
||||
@nbd1.kill
|
||||
@nbd2.kill
|
||||
|
||||
[@filename1, @filename2].each do |f|
|
||||
File.unlink(f) if File.exist?(f)
|
||||
File.unlink(f) if File.exists?(f)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def run_fake( name, addr, port, sock=nil )
|
||||
fakedir = File.join(File.dirname(__FILE__), 'fakes')
|
||||
fakeglob = File.join(fakedir, name) + '*'
|
||||
fake = Dir[fakeglob].sort.find do |fn|
|
||||
fakedir = File.join( File.dirname( __FILE__ ), "fakes" )
|
||||
fakeglob = File.join( fakedir, name ) + "*"
|
||||
fake = Dir[fakeglob].sort.find { |fn|
|
||||
File.executable?( fn )
|
||||
end
|
||||
}
|
||||
|
||||
raise "no fake executable at #{fakeglob}" unless fake
|
||||
raise 'no addr' unless addr
|
||||
raise 'no port' unless port
|
||||
raise "no addr" unless addr
|
||||
raise "no port" unless port
|
||||
|
||||
@fake_pid = fork do
|
||||
exec [fake, addr, port, @nbd1.pid, sock].map(&:to_s).join(' ')
|
||||
exec [fake, addr, port, @nbd1.pid, sock].map{|x| x.to_s}.join(" ")
|
||||
end
|
||||
sleep(0.5)
|
||||
end
|
||||
|
||||
|
||||
def fake_reports_success
|
||||
_,status = Process.waitpid2( @fake_pid )
|
||||
@fake_pid = nil
|
||||
status.success?
|
||||
end
|
||||
|
||||
|
||||
end # class Environment
|
||||
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
# Open a server, accept a client, then cancel the migration by issuing
|
||||
# a break command.
|
||||
|
||||
@@ -11,22 +13,23 @@ client = server.accept
|
||||
|
||||
ctrl = UNIXSocket.open( sock )
|
||||
|
||||
Process.kill('STOP', src_pid.to_i)
|
||||
Process.kill("STOP", src_pid.to_i)
|
||||
ctrl.write( "break\n" )
|
||||
ctrl.close_write
|
||||
client.write_hello
|
||||
Process.kill('CONT', src_pid.to_i)
|
||||
Process.kill("CONT", src_pid.to_i)
|
||||
|
||||
raise 'Unexpected control response' unless
|
||||
fail "Unexpected control response" unless
|
||||
ctrl.read =~ /0: mirror stopped/
|
||||
|
||||
client2 = nil
|
||||
begin
|
||||
client2 = server.accept('Expected timeout')
|
||||
raise 'Unexpected reconnection'
|
||||
client2 = server.accept( "Expected timeout" )
|
||||
fail "Unexpected reconnection"
|
||||
rescue Timeout::Error
|
||||
# expected
|
||||
end
|
||||
client.close
|
||||
|
||||
exit(0)
|
||||
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
# Receive a mirror, and disconnect after sending the entrust reply but
|
||||
# before it can send the disconnect signal.
|
||||
#
|
||||
@@ -13,7 +15,7 @@ server = FakeDest.new(addr, port)
|
||||
client = server.accept
|
||||
|
||||
client.write_hello
|
||||
while req = client.read_request; req[:type] == 1
|
||||
while (req = client.read_request; req[:type] == 1)
|
||||
client.read_data( req[:len] )
|
||||
client.write_reply( req[:handle] )
|
||||
end
|
||||
@@ -24,10 +26,11 @@ client.close
|
||||
system "kill -CONT #{src_pid}"
|
||||
|
||||
sleep( 0.25 )
|
||||
client2 = server.accept('Timed out waiting for a reconnection')
|
||||
client2 = server.accept( "Timed out waiting for a reconnection" )
|
||||
|
||||
client2.close
|
||||
server.close
|
||||
|
||||
warn 'done'
|
||||
$stderr.puts "done"
|
||||
exit(0)
|
||||
|
||||
|
@@ -11,11 +11,11 @@ include FlexNBD
|
||||
|
||||
addr, port = *ARGV
|
||||
server = FakeDest.new( addr, port )
|
||||
client = server.accept('Timed out waiting for a connection')
|
||||
client = server.accept( "Timed out waiting for a connection" )
|
||||
client.write_hello
|
||||
client.close
|
||||
|
||||
new_client = server.accept('Timed out waiting for a reconnection')
|
||||
new_client = server.accept( "Timed out waiting for a reconnection" )
|
||||
new_client.close
|
||||
|
||||
server.close
|
||||
|
@@ -12,12 +12,12 @@ include FlexNBD
|
||||
|
||||
addr, port = *ARGV
|
||||
server = FakeDest.new( addr, port )
|
||||
client = server.accept('Timed out waiting for a connection')
|
||||
client = server.accept( "Timed out waiting for a connection" )
|
||||
client.write_hello
|
||||
client.read_request
|
||||
client.close
|
||||
|
||||
new_client = server.accept('Timed out waiting for a reconnection')
|
||||
new_client = server.accept( "Timed out waiting for a reconnection" )
|
||||
new_client.close
|
||||
|
||||
server.close
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
# Open a server, accept a client, then we expect a single write
|
||||
# followed by an entrust. However, we disconnect after the write so
|
||||
# the entrust will fail. We don't expect a reconnection: the sender
|
||||
@@ -15,9 +17,9 @@ client.write_hello
|
||||
req = client.read_request
|
||||
data = client.read_data( req[:len] )
|
||||
|
||||
Process.kill('STOP', src_pid.to_i)
|
||||
Process.kill("STOP", src_pid.to_i)
|
||||
client.write_reply( req[:handle], 0 )
|
||||
client.close
|
||||
Process.kill('CONT', src_pid.to_i)
|
||||
Process.kill("CONT", src_pid.to_i)
|
||||
|
||||
exit(0)
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
require 'flexnbd/fake_dest'
|
||||
include FlexNBD
|
||||
|
||||
@@ -10,7 +12,8 @@ client.write_hello
|
||||
handle = client.read_request[:handle]
|
||||
client.write_error( handle )
|
||||
|
||||
client2 = server.accept('Timed out waiting for a reconnection')
|
||||
|
||||
client2 = server.accept( "Timed out waiting for a reconnection" )
|
||||
|
||||
client.close
|
||||
client2.close
|
||||
|
@@ -26,8 +26,8 @@ client.close
|
||||
# Invert the sense of the timeout exception, since we *don't* want a
|
||||
# connection attempt
|
||||
begin
|
||||
server.accept('Expected timeout')
|
||||
raise 'Unexpected reconnection'
|
||||
server.accept( "Expected timeout" )
|
||||
fail "Unexpected reconnection"
|
||||
rescue Timeout::Error
|
||||
# expected
|
||||
end
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
# Open a socket, say hello, receive a write, then sleep for >
|
||||
# MS_REQUEST_LIMIT_SECS seconds. This should tell the source that the
|
||||
# write has gone MIA, and we expect a reconnect.
|
||||
@@ -13,12 +15,12 @@ client1.write_hello
|
||||
client1.read_request
|
||||
|
||||
t = Thread.start do
|
||||
client2 = server.accept('Timed out waiting for a reconnection',
|
||||
client2 = server.accept( "Timed out waiting for a reconnection",
|
||||
FlexNBD::MS_REQUEST_LIMIT_SECS + 2 )
|
||||
client2.close
|
||||
end
|
||||
|
||||
sleep_time = if ENV.key?('FLEXNBD_MS_REQUEST_LIMIT_SECS')
|
||||
sleep_time = if ENV.has_key?('FLEXNBD_MS_REQUEST_LIMIT_SECS')
|
||||
ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS'].to_f
|
||||
else
|
||||
FlexNBD::MS_REQUEST_LIMIT_SECS
|
||||
|
@@ -13,15 +13,15 @@ client1 = server.accept
|
||||
# We don't expect a reconnection attempt.
|
||||
t = Thread.new do
|
||||
begin
|
||||
client2 = server.accept('Timed out waiting for a reconnection',
|
||||
client2 = server.accept( "Timed out waiting for a reconnection",
|
||||
FlexNBD::MS_RETRY_DELAY_SECS + 1 )
|
||||
raise 'Unexpected reconnection'
|
||||
fail "Unexpected reconnection"
|
||||
rescue Timeout::Error
|
||||
#expected
|
||||
end
|
||||
end
|
||||
|
||||
client1.write_hello(magic: :wrong)
|
||||
client1.write_hello( :magic => :wrong )
|
||||
|
||||
t.join
|
||||
|
||||
|
@@ -18,21 +18,21 @@ t = Thread.new do
|
||||
# so it makes no sense to continue. This means we have to invert the
|
||||
# sense of the exception.
|
||||
begin
|
||||
client2 = server.accept('Timed out waiting for a reconnection',
|
||||
client2 = server.accept( "Timed out waiting for a reconnection",
|
||||
FlexNBD::MS_RETRY_DELAY_SECS + 1 )
|
||||
client2.close
|
||||
raise 'Unexpected reconnection.'
|
||||
fail "Unexpected reconnection."
|
||||
rescue Timeout::Error
|
||||
end
|
||||
end
|
||||
|
||||
client.write_hello(size: :wrong)
|
||||
client.write_hello( :size => :wrong )
|
||||
|
||||
t.join
|
||||
|
||||
# Now check that the source closed the first socket (yes, this was an
|
||||
# actual bug)
|
||||
|
||||
raise "Didn't close socket" unless client.disconnected?
|
||||
fail "Didn't close socket" unless client.disconnected?
|
||||
|
||||
exit 0
|
||||
|
@@ -10,13 +10,15 @@ addr, port = *ARGV
|
||||
server = FakeDest.new( addr, port )
|
||||
server.accept.close
|
||||
|
||||
|
||||
begin
|
||||
server.accept
|
||||
raise 'Unexpected reconnection'
|
||||
fail "Unexpected reconnection"
|
||||
rescue Timeout::Error
|
||||
# expected
|
||||
end
|
||||
|
||||
server.close
|
||||
|
||||
|
||||
exit(0)
|
||||
|
@@ -9,7 +9,7 @@ include FlexNBD
|
||||
|
||||
addr, port, pid = *ARGV
|
||||
server = FakeDest.new( addr, port )
|
||||
client = server.accept('Timed out waiting for a connection')
|
||||
client = server.accept( "Timed out waiting for a connection" )
|
||||
client.write_hello
|
||||
|
||||
Process.kill(15, pid.to_i)
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
# Accept a connection, write hello, wait for a write request, read the
|
||||
# data, then write back a reply with a bad magic field. We then
|
||||
# expect a reconnect.
|
||||
@@ -13,7 +15,7 @@ client = server.accept
|
||||
client.write_hello
|
||||
req = client.read_request
|
||||
client.read_data( req[:len] )
|
||||
client.write_reply(req[:handle], 0, magic: :wrong)
|
||||
client.write_reply( req[:handle], 0, :magic => :wrong )
|
||||
|
||||
client2 = server.accept
|
||||
client.close
|
||||
|
@@ -11,13 +11,13 @@ include FlexNBD
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
FakeSource.new(addr, port, 'Failed to connect').close
|
||||
FakeSource.new( addr, port, "Failed to connect" ).close
|
||||
# Sleep to be sure we don't try to connect too soon. That wouldn't
|
||||
# be a problem for the destination, but it would prevent us from
|
||||
# determining success or failure here in the case where we try to
|
||||
# reconnect before the destination has tidied up after the first
|
||||
# thread went away.
|
||||
sleep(0.5)
|
||||
FakeSource.new(addr, port, 'Failed to reconnect').close
|
||||
FakeSource.new( addr, port, "Failed to reconnect" ).close
|
||||
|
||||
exit 0
|
||||
|
@@ -11,10 +11,10 @@ include FlexNBD
|
||||
|
||||
addr, port, srv_pid = *ARGV
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting')
|
||||
client = FakeSource.new( addr, port, "Timed out connecting" )
|
||||
client.read_hello
|
||||
client.write_write_request( 0, 8 )
|
||||
client.write_data('12345678')
|
||||
client.write_data( "12345678" )
|
||||
|
||||
# Use system "kill" rather than Process.kill because Process.kill
|
||||
# doesn't seem to work
|
||||
@@ -25,11 +25,12 @@ client.close
|
||||
|
||||
system "kill -CONT #{srv_pid}"
|
||||
|
||||
|
||||
sleep(0.25)
|
||||
|
||||
begin
|
||||
client2 = FakeSource.new(addr, port, 'Expected timeout')
|
||||
raise 'Unexpected reconnection'
|
||||
client2 = FakeSource.new( addr, port, "Expected timeout" )
|
||||
fail "Unexpected reconnection"
|
||||
rescue Timeout::Error
|
||||
# expected
|
||||
end
|
||||
|
@@ -10,10 +10,10 @@ include FlexNBD
|
||||
|
||||
addr, port, srv_pid = *ARGV
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting')
|
||||
client = FakeSource.new( addr, port, "Timed out connecting" )
|
||||
client.read_hello
|
||||
client.write_write_request( 0, 8 )
|
||||
client.write_data('12345678')
|
||||
client.write_data( "12345678" )
|
||||
|
||||
client.write_entrust_request
|
||||
client.read_response
|
||||
@@ -21,11 +21,13 @@ client.close
|
||||
|
||||
sleep(0.25)
|
||||
|
||||
|
||||
begin
|
||||
client2 = FakeSource.new(addr, port, 'Expected timeout')
|
||||
raise 'Unexpected reconnection'
|
||||
client2 = FakeSource.new( addr, port, "Expected timeout" )
|
||||
fail "Unexpected reconnection"
|
||||
rescue Timeout::Error
|
||||
# expected
|
||||
end
|
||||
|
||||
exit(0)
|
||||
|
||||
|
@@ -12,12 +12,13 @@ include FlexNBD
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting.')
|
||||
|
||||
client = FakeSource.new( addr, port, "Timed out connecting." )
|
||||
client.read_hello
|
||||
client.close
|
||||
|
||||
sleep(0.2)
|
||||
|
||||
FakeSource.new(addr, port, 'Timed out reconnecting.')
|
||||
FakeSource.new( addr, port, "Timed out reconnecting." )
|
||||
|
||||
exit(0)
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
# We connect, pause the server, issue a write request, disconnect,
|
||||
# then cont the server. This ensures that our disconnect happens
|
||||
# while the server is trying to read the write data.
|
||||
@@ -8,7 +10,7 @@ include FlexNBD
|
||||
|
||||
addr, port, srv_pid = *ARGV
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting')
|
||||
client = FakeSource.new( addr, port, "Timed out connecting" )
|
||||
client.read_hello
|
||||
|
||||
system "kill -STOP #{srv_pid}"
|
||||
@@ -22,7 +24,7 @@ system "kill -CONT #{srv_pid}"
|
||||
sleep(0.25)
|
||||
|
||||
# ...and can we reconnect?
|
||||
client2 = FakeSource.new(addr, port, 'Timed out connecting')
|
||||
client2 = FakeSource.new( addr, port, "Timed out connecting" )
|
||||
client2.close
|
||||
|
||||
exit(0)
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
# We connect, pause the server, issue a write request, send data,
|
||||
# disconnect, then cont the server. This ensures that our disconnect
|
||||
# happens before the server can try to write the reply.
|
||||
@@ -8,13 +10,13 @@ include FlexNBD
|
||||
|
||||
addr, port, srv_pid = *ARGV
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting')
|
||||
client = FakeSource.new( addr, port, "Timed out connecting" )
|
||||
client.read_hello
|
||||
|
||||
system "kill -STOP #{srv_pid}"
|
||||
|
||||
client.write_write_request( 0, 8 )
|
||||
client.write_data('12345678')
|
||||
client.write_data( "12345678" )
|
||||
client.close
|
||||
|
||||
system "kill -CONT #{srv_pid}"
|
||||
@@ -25,7 +27,7 @@ system "kill -CONT #{srv_pid}"
|
||||
sleep(0.25)
|
||||
|
||||
# ...and can we reconnect?
|
||||
client2 = FakeSource.new(addr, port, 'Timed out reconnecting')
|
||||
client2 = FakeSource.new( addr, port, "Timed out reconnecting" )
|
||||
client2.close
|
||||
|
||||
exit(0)
|
||||
|
@@ -8,9 +8,10 @@ include FlexNBD
|
||||
|
||||
addr, port, srv_pid, newaddr, newport = *ARGV
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting')
|
||||
client = FakeSource.new( addr, port, "Timed out connecting" )
|
||||
client.write_read_request( 0, 8 )
|
||||
client.read_raw( 4 )
|
||||
client.close
|
||||
|
||||
|
||||
exit(0)
|
||||
|
@@ -10,9 +10,9 @@ include FlexNBD
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
client1 = FakeSource.new(addr, port, 'Timed out connecting')
|
||||
client1 = FakeSource.new( addr, port, "Timed out connecting" )
|
||||
sleep(0.25)
|
||||
client2 = FakeSource.new(addr, port, 'Timed out connecting a second time')
|
||||
client2 = FakeSource.new( addr, port, "Timed out connecting a second time" )
|
||||
|
||||
# This is the expected source crashing after connect
|
||||
client1.close
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
# We connect from a local address which should be blocked, sleep for a
|
||||
# bit, then try to read from the socket. We should get an instant EOF
|
||||
# as we've been cut off by the destination.
|
||||
@@ -9,9 +11,10 @@ include FlexNBD
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting', '127.0.0.6')
|
||||
client = FakeSource.new( addr, port, "Timed out connecting", "127.0.0.6" )
|
||||
sleep( 0.25 )
|
||||
|
||||
rsp = client.disconnected? ? 0 : 1
|
||||
client.close
|
||||
exit(rsp)
|
||||
|
||||
|
@@ -7,10 +7,10 @@
|
||||
# listening for an incoming migration.
|
||||
|
||||
addr, port = *ARGV
|
||||
require 'flexnbd/fake_source'
|
||||
require "flexnbd/fake_source"
|
||||
include FlexNBD
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting')
|
||||
client = FakeSource.new( addr, port, "Timed out connecting" )
|
||||
client.read_hello
|
||||
|
||||
# Now we do two things:
|
||||
@@ -25,7 +25,7 @@ kidpid = fork do
|
||||
client.close
|
||||
new_client = nil
|
||||
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 1 )
|
||||
new_client = FakeSource.new(addr, port, 'Timed out reconnecting.')
|
||||
new_client = FakeSource.new( addr, port, "Timed out reconnecting." )
|
||||
new_client.read_hello
|
||||
exit 0
|
||||
end
|
||||
|
@@ -9,10 +9,10 @@ include FlexNBD
|
||||
|
||||
addr, port, pid = *ARGV
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting.')
|
||||
client = FakeSource.new( addr, port, "Timed out connecting." )
|
||||
client.read_hello
|
||||
|
||||
Process.kill('TERM', pid.to_i)
|
||||
Process.kill( "TERM", pid.to_i )
|
||||
|
||||
sleep(0.2)
|
||||
client.close
|
||||
|
@@ -9,9 +9,10 @@ include FlexNBD
|
||||
|
||||
addr, port, srv_pid, newaddr, newport = *ARGV
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting')
|
||||
client.send_mirror
|
||||
client = FakeSource.new( addr, port, "Timed out connecting" )
|
||||
client.send_mirror()
|
||||
|
||||
sleep(1)
|
||||
|
||||
|
||||
exit( 0 )
|
||||
|
@@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
# Connect, read the hello then make a write request with an impossible
|
||||
# (from,len) pair. We expect an error response, and not to be
|
||||
# disconnected.
|
||||
@@ -11,20 +13,20 @@ include FlexNBD
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
client = FakeSource.new(addr, port, 'Timed out connecting')
|
||||
client = FakeSource.new( addr, port, "Timed out connecting" )
|
||||
hello = client.read_hello
|
||||
client.write_write_request(hello[:size] + 1, 32, 'myhandle')
|
||||
client.write_data('1' * 32)
|
||||
client.write_write_request( hello[:size]+1, 32, "myhandle" )
|
||||
client.write_data("1"*32)
|
||||
response = client.read_response
|
||||
|
||||
raise 'Not an error' if response[:error] == 0
|
||||
raise 'Wrong handle' unless response[:handle] == 'myhandle'
|
||||
fail "Not an error" if response[:error] == 0
|
||||
fail "Wrong handle" unless "myhandle" == response[:handle]
|
||||
|
||||
client.write_write_request( 0, 32 )
|
||||
client.write_data('2' * 32)
|
||||
client.write_data( "2"*32 )
|
||||
success_response = client.read_response
|
||||
|
||||
raise 'Second write failed' unless success_response[:error] == 0
|
||||
fail "Second write failed" unless success_response[:error] == 0
|
||||
|
||||
client.close
|
||||
exit(0)
|
||||
|
@@ -3,13 +3,13 @@
|
||||
#
|
||||
class FileWriter
|
||||
def initialize(filename, blocksize)
|
||||
@fh = File.open(filename, 'w+')
|
||||
@fh = File.open(filename, "w+")
|
||||
@blocksize = blocksize
|
||||
@pattern = ''
|
||||
@pattern = ""
|
||||
end
|
||||
|
||||
def size
|
||||
@blocksize * @pattern.split('').size
|
||||
@blocksize * @pattern.split("").size
|
||||
end
|
||||
|
||||
# We write in fixed block sizes, given by "blocksize"
|
||||
@@ -20,8 +20,8 @@ class FileWriter
|
||||
def write(data)
|
||||
@pattern += data
|
||||
|
||||
data.split('').each do |code|
|
||||
if code == '_'
|
||||
data.split("").each do |code|
|
||||
if code == "_"
|
||||
@fh.seek(@blocksize, IO::SEEK_CUR)
|
||||
else
|
||||
@fh.write(data(code))
|
||||
@@ -31,14 +31,15 @@ class FileWriter
|
||||
self
|
||||
end
|
||||
|
||||
|
||||
# Returns what the data ought to be at the given offset and length
|
||||
#
|
||||
def read_original( off, len )
|
||||
patterns = @pattern.split('')
|
||||
patterns.zip((0...patterns.length).to_a)
|
||||
.map do |blk, blk_off|
|
||||
patterns = @pattern.split( "" )
|
||||
patterns.zip( (0...patterns.length).to_a ).
|
||||
map { |blk, blk_off|
|
||||
data(blk, blk_off)
|
||||
end.join[off...(off + len)]
|
||||
}.join[off...(off+len)]
|
||||
end
|
||||
|
||||
# Read what's actually in the file
|
||||
@@ -61,14 +62,14 @@ class FileWriter
|
||||
|
||||
def data(code, at=@fh.tell)
|
||||
case code
|
||||
when '0', '_'
|
||||
when "0", "_"
|
||||
"\0" * @blocksize
|
||||
when 'X'
|
||||
'X' * @blocksize
|
||||
when 'f'
|
||||
r = ''
|
||||
when "X"
|
||||
"X" * @blocksize
|
||||
when "f"
|
||||
r = ""
|
||||
(@blocksize/4).times do
|
||||
r += [at].pack('I')
|
||||
r += [at].pack("I")
|
||||
at += 4
|
||||
end
|
||||
r
|
||||
@@ -76,49 +77,51 @@ class FileWriter
|
||||
raise "Unknown character '#{block}'"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if $PROGRAM_NAME == __FILE__
|
||||
if __FILE__==$0
|
||||
require 'tempfile'
|
||||
require 'test/unit'
|
||||
|
||||
class FileWriterTest < Test::Unit::TestCase
|
||||
def test_read_original_zeros
|
||||
Tempfile.open('test_read_original_zeros') do |tempfile|
|
||||
Tempfile.open("test_read_original_zeros") do |tempfile|
|
||||
tempfile.close
|
||||
file = FileWriter.new( tempfile.path, 4096 )
|
||||
file.write('0')
|
||||
file.write( "0" )
|
||||
assert_equal file.read( 0, 4096 ), file.read_original( 0, 4096 )
|
||||
assert(file.untouched?(0, 4096), 'Untouched file was touched.')
|
||||
assert( file.untouched?(0,4096) , "Untouched file was touched." )
|
||||
end
|
||||
end
|
||||
|
||||
def test_read_original_offsets
|
||||
Tempfile.open('test_read_original_offsets') do |tempfile|
|
||||
Tempfile.open("test_read_original_offsets") do |tempfile|
|
||||
tempfile.close
|
||||
file = FileWriter.new( tempfile.path, 4096 )
|
||||
file.write('f')
|
||||
file.write( "f" )
|
||||
assert_equal file.read( 0, 4096 ), file.read_original( 0, 4096 )
|
||||
assert(file.untouched?(0, 4096), 'Untouched file was touched.')
|
||||
assert( file.untouched?(0,4096) , "Untouched file was touched." )
|
||||
end
|
||||
end
|
||||
|
||||
def test_file_size
|
||||
Tempfile.open('test_file_size') do |tempfile|
|
||||
Tempfile.open("test_file_size") do |tempfile|
|
||||
tempfile.close
|
||||
file = FileWriter.new( tempfile.path, 4096 )
|
||||
file.write('f')
|
||||
file.write( "f" )
|
||||
assert_equal 4096, File.stat( tempfile.path ).size
|
||||
end
|
||||
end
|
||||
|
||||
def test_read_original_size
|
||||
Tempfile.open('test_read_original_offsets') do |tempfile|
|
||||
Tempfile.open("test_read_original_offsets") do |tempfile|
|
||||
tempfile.close
|
||||
file = FileWriter.new( tempfile.path, 4)
|
||||
file.write('f' * 4)
|
||||
file.write( "f"*4 )
|
||||
assert_equal 4, file.read_original(0, 4).length
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@@ -4,26 +4,28 @@ require 'open3'
|
||||
require 'timeout'
|
||||
require 'rexml/document'
|
||||
require 'rexml/streamlistener'
|
||||
require 'English'
|
||||
|
||||
Thread.abort_on_exception = true
|
||||
|
||||
|
||||
class Executor
|
||||
attr_reader :pid
|
||||
|
||||
def run( cmd )
|
||||
@pid = fork { exec cmd }
|
||||
@pid = fork do exec cmd end
|
||||
end
|
||||
end # class Executor
|
||||
|
||||
|
||||
class ValgrindExecutor
|
||||
attr_reader :pid
|
||||
|
||||
def run( cmd )
|
||||
@pid = fork { exec "valgrind --track-origins=yes --suppressions=custom.supp #{cmd}" }
|
||||
@pid = fork do exec "valgrind --track-origins=yes --suppressions=custom.supp #{cmd}" end
|
||||
end
|
||||
end # class ValgrindExecutor
|
||||
|
||||
|
||||
class ValgrindKillingExecutor
|
||||
attr_reader :pid
|
||||
|
||||
@@ -32,9 +34,9 @@ class ValgrindKillingExecutor
|
||||
attr_reader :backtrace
|
||||
def initialize
|
||||
@backtrace=[]
|
||||
@what = ''
|
||||
@kind = ''
|
||||
@pid = ''
|
||||
@what = ""
|
||||
@kind = ""
|
||||
@pid = ""
|
||||
end
|
||||
|
||||
def add_frame
|
||||
@@ -56,8 +58,10 @@ class ValgrindKillingExecutor
|
||||
def to_s
|
||||
([@what + " (#{@kind}) in #{@pid}"] + @backtrace.map{|h| "#{h[:file]}:#{h[:line]} #{h[:fn]}" }).join("\n")
|
||||
end
|
||||
|
||||
end # class Error
|
||||
|
||||
|
||||
class ErrorListener
|
||||
include REXML::StreamListener
|
||||
def initialize( killer )
|
||||
@@ -70,36 +74,39 @@ class ValgrindKillingExecutor
|
||||
@text = text
|
||||
end
|
||||
|
||||
def tag_start(tag, _attrs)
|
||||
def tag_start(tag, attrs)
|
||||
case tag.to_s
|
||||
when 'error'
|
||||
when "error"
|
||||
@found = true
|
||||
when 'frame'
|
||||
when "frame"
|
||||
@error.add_frame
|
||||
end
|
||||
end
|
||||
|
||||
def tag_end(tag)
|
||||
case tag.to_s
|
||||
when 'what'
|
||||
when "what"
|
||||
@error.what = @text if @found
|
||||
@text = ''
|
||||
when 'kind'
|
||||
@text = ""
|
||||
when "kind"
|
||||
@error.kind = @text if @found
|
||||
when 'file'
|
||||
when "file"
|
||||
@error.add_file( @text ) if @found
|
||||
when 'fn'
|
||||
when "fn"
|
||||
@error.add_fn( @text ) if @found
|
||||
when 'line'
|
||||
when "line"
|
||||
@error.add_line( @text ) if @found
|
||||
when 'error', 'stack'
|
||||
@killer.call(@error) if @found
|
||||
when 'pid'
|
||||
when "error", "stack"
|
||||
if @found
|
||||
@killer.call( @error )
|
||||
end
|
||||
when "pid"
|
||||
@error.pid=@text
|
||||
end
|
||||
end
|
||||
end # class ErrorListener
|
||||
|
||||
|
||||
class DebugErrorListener < ErrorListener
|
||||
def text( txt )
|
||||
print txt
|
||||
@@ -117,41 +124,47 @@ class ValgrindKillingExecutor
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def initialize
|
||||
@pid = nil
|
||||
end
|
||||
|
||||
def run( cmd )
|
||||
@io_r, io_w = IO.pipe
|
||||
@pid = fork { exec("valgrind --suppressions=custom.supp --xml=yes --xml-fd=#{io_w.fileno} " + cmd) }
|
||||
@pid = fork do exec( "valgrind --suppressions=custom.supp --xml=yes --xml-fd=#{io_w.fileno} " + cmd ) end
|
||||
launch_watch_thread( @pid, @io_r )
|
||||
@pid
|
||||
end
|
||||
|
||||
|
||||
def call( err )
|
||||
warn '*' * 72
|
||||
warn '* Valgrind error spotted:'
|
||||
warn err.to_s.split("\n").map { |s| " #{s}" }
|
||||
warn '*' * 72
|
||||
Process.kill('KILL', @pid)
|
||||
$stderr.puts "*"*72
|
||||
$stderr.puts "* Valgrind error spotted:"
|
||||
$stderr.puts err.to_s.split("\n").map{|s| " #{s}"}
|
||||
$stderr.puts "*"*72
|
||||
Process.kill( "KILL", @pid )
|
||||
exit(1)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def pick_listener
|
||||
ENV['DEBUG'] ? DebugErrorListener : ErrorListener
|
||||
end
|
||||
|
||||
def launch_watch_thread(_pid, io_r)
|
||||
def launch_watch_thread(pid, io_r)
|
||||
Thread.start do
|
||||
io_source = REXML::IOSource.new( io_r )
|
||||
listener = pick_listener.new( self )
|
||||
REXML::Document.parse_stream( io_source, listener )
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end # class ValgrindExecutor
|
||||
|
||||
|
||||
module FlexNBD
|
||||
# Noddy test class to exercise FlexNBD from the outside for testing.
|
||||
#
|
||||
@@ -176,11 +189,12 @@ module FlexNBD
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def build_debug_opt
|
||||
if @do_debug
|
||||
'--verbose'
|
||||
"--verbose"
|
||||
else
|
||||
'--quiet'
|
||||
"--quiet"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -195,19 +209,20 @@ module FlexNBD
|
||||
@ctrl = "/tmp/.flexnbd.ctrl.#{Time.now.to_i}.#{rand}"
|
||||
@ip = ip
|
||||
@port = port
|
||||
@pid = @wait_thread = nil
|
||||
@kill = []
|
||||
@prefetch_proxy = false
|
||||
end
|
||||
|
||||
|
||||
def debug?
|
||||
!!@do_debug
|
||||
end
|
||||
|
||||
def debug( msg )
|
||||
warn msg if debug?
|
||||
$stderr.puts msg if debug?
|
||||
end
|
||||
|
||||
|
||||
def serve_cmd( file, acl )
|
||||
"#{bin} serve "\
|
||||
"--addr #{ip} "\
|
||||
@@ -218,6 +233,7 @@ module FlexNBD
|
||||
"#{acl.join(' ')}"
|
||||
end
|
||||
|
||||
|
||||
def listen_cmd( file, acl )
|
||||
"#{bin} listen "\
|
||||
"--addr #{ip} "\
|
||||
@@ -234,10 +250,11 @@ module FlexNBD
|
||||
"--port #{port} "\
|
||||
"--conn-addr #{connect_ip} "\
|
||||
"--conn-port #{connect_port} "\
|
||||
"#{prefetch_proxy ? '--cache ' : ''}"\
|
||||
"#{prefetch_proxy ? "--cache " : ""}"\
|
||||
"#{@debug}"
|
||||
end
|
||||
|
||||
|
||||
def read_cmd( offset, length )
|
||||
"#{bin} read "\
|
||||
"--addr #{ip} "\
|
||||
@@ -247,6 +264,7 @@ module FlexNBD
|
||||
"--size #{length}"
|
||||
end
|
||||
|
||||
|
||||
def write_cmd( offset, data )
|
||||
"#{bin} write "\
|
||||
"--addr #{ip} "\
|
||||
@@ -256,6 +274,7 @@ module FlexNBD
|
||||
"--size #{data.length}"
|
||||
end
|
||||
|
||||
|
||||
def base_mirror_opts( dest_ip, dest_port )
|
||||
"--addr #{dest_ip} "\
|
||||
"--port #{dest_port} "\
|
||||
@@ -264,7 +283,7 @@ module FlexNBD
|
||||
|
||||
def unlink_mirror_opts( dest_ip, dest_port )
|
||||
"#{base_mirror_opts( dest_ip, dest_port )} "\
|
||||
'--unlink '
|
||||
"--unlink "
|
||||
end
|
||||
|
||||
def base_mirror_cmd( opts )
|
||||
@@ -297,52 +316,46 @@ module FlexNBD
|
||||
"#{@bin} acl " \
|
||||
"--sock #{ctrl} "\
|
||||
"#{@debug} "\
|
||||
"#{acl.join ' '}"
|
||||
"#{acl.join " "}"
|
||||
end
|
||||
|
||||
|
||||
def run_serve_cmd(cmd)
|
||||
File.unlink(ctrl) if File.exist?(ctrl)
|
||||
File.unlink(ctrl) if File.exists?(ctrl)
|
||||
debug( cmd )
|
||||
|
||||
@pid = @executor.run( cmd )
|
||||
|
||||
until File.socket?(ctrl)
|
||||
while !File.socket?(ctrl)
|
||||
pid, status = Process.wait2(@pid, Process::WNOHANG)
|
||||
raise "server did not start (#{cmd})" if pid
|
||||
sleep 0.1
|
||||
end
|
||||
|
||||
|
||||
start_wait_thread( @pid )
|
||||
at_exit { kill }
|
||||
end
|
||||
private :run_serve_cmd
|
||||
|
||||
|
||||
def serve( file, *acl)
|
||||
cmd = serve_cmd( file, acl )
|
||||
run_serve_cmd( cmd )
|
||||
sleep(0.2) until File.exist?(ctrl)
|
||||
sleep( 0.2 ) until File.exists?( ctrl )
|
||||
end
|
||||
|
||||
|
||||
def listen(file, *acl)
|
||||
run_serve_cmd( listen_cmd( file, acl ) )
|
||||
end
|
||||
|
||||
def tcp_server_open?
|
||||
# raises if the other side doesn't accept()
|
||||
sock = begin
|
||||
TCPSocket.new(ip, port)
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
sock = TCPSocket.new(ip, port) rescue nil
|
||||
|
||||
success = !!sock
|
||||
if sock
|
||||
(begin
|
||||
sock.close
|
||||
rescue StandardError
|
||||
nil
|
||||
end)
|
||||
end
|
||||
( sock.close rescue nil) if sock
|
||||
success
|
||||
end
|
||||
|
||||
@@ -362,25 +375,27 @@ module FlexNBD
|
||||
at_exit { kill }
|
||||
end
|
||||
|
||||
|
||||
def start_wait_thread( pid )
|
||||
@wait_thread = Thread.start do
|
||||
_, status = Process.waitpid2( pid )
|
||||
|
||||
if @kill
|
||||
if status.signaled?
|
||||
raise "flexnbd quit with a bad signal: #{status.inspect}" unless
|
||||
fail "flexnbd quit with a bad signal: #{status.inspect}" unless
|
||||
@kill.include? status.termsig
|
||||
else
|
||||
raise "flexnbd quit with a bad status: #{status.inspect}" unless
|
||||
fail "flexnbd quit with a bad status: #{status.inspect}" unless
|
||||
@kill.include? status.exitstatus
|
||||
end
|
||||
else
|
||||
warn "flexnbd #{self.pid} quit"
|
||||
raise "flexnbd #{self.pid} quit early with status #{status.to_i}"
|
||||
$stderr.puts "flexnbd #{self.pid} quit"
|
||||
fail "flexnbd #{self.pid} quit early with status #{status.to_i}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def can_die(*status)
|
||||
status = [0] if status.empty?
|
||||
@kill += status
|
||||
@@ -392,7 +407,7 @@ module FlexNBD
|
||||
can_die(1)
|
||||
if @pid
|
||||
begin
|
||||
Process.kill('INT', @pid)
|
||||
Process.kill("INT", @pid)
|
||||
rescue Errno::ESRCH => e
|
||||
# already dead. Presumably this means it went away after a
|
||||
# can_die() call.
|
||||
@@ -408,7 +423,7 @@ module FlexNBD
|
||||
IO.popen(cmd) do |fh|
|
||||
return fh.read
|
||||
end
|
||||
raise IOError, 'NBD read failed' unless $CHILD_STATUS.success?
|
||||
raise IOError.new "NBD read failed" unless $?.success?
|
||||
out
|
||||
end
|
||||
|
||||
@@ -416,24 +431,27 @@ module FlexNBD
|
||||
cmd = write_cmd( offset, data )
|
||||
debug( cmd )
|
||||
|
||||
IO.popen(cmd, 'w') do |fh|
|
||||
IO.popen(cmd, "w") do |fh|
|
||||
fh.write(data)
|
||||
end
|
||||
raise IOError, 'NBD write failed' unless $CHILD_STATUS.success?
|
||||
raise IOError.new "NBD write failed" unless $?.success?
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
def join
|
||||
@wait_thread.join
|
||||
end
|
||||
|
||||
def mirror_unchecked(dest_ip, dest_port, _bandwidth = nil, _action = nil, timeout = nil)
|
||||
|
||||
def mirror_unchecked( dest_ip, dest_port, bandwidth=nil, action=nil, timeout=nil )
|
||||
cmd = mirror_cmd( dest_ip, dest_port)
|
||||
debug( cmd )
|
||||
|
||||
maybe_timeout( cmd, timeout )
|
||||
end
|
||||
|
||||
|
||||
def mirror_unlink( dest_ip, dest_port, timeout=nil )
|
||||
cmd = mirror_unlink_cmd( dest_ip, dest_port )
|
||||
debug( cmd )
|
||||
@@ -441,11 +459,11 @@ module FlexNBD
|
||||
maybe_timeout( cmd, timeout )
|
||||
end
|
||||
|
||||
|
||||
def maybe_timeout(cmd, timeout=nil )
|
||||
stdout = ''
|
||||
stderr = ''
|
||||
stdout, stderr = "",""
|
||||
stat = nil
|
||||
run = proc do
|
||||
run = Proc.new do
|
||||
# Ruby 1.9 changed the popen3 api. instead of 3 args, the block
|
||||
# gets 4. Not only that, but it no longer sets $?, so we have to
|
||||
# go elsewhere for the process' exit status.
|
||||
@@ -455,7 +473,7 @@ module FlexNBD
|
||||
stderr.replace io_err.read
|
||||
stat = maybe_thr.value if maybe_thr
|
||||
end
|
||||
stat ||= $CHILD_STATUS
|
||||
stat ||= $?
|
||||
end
|
||||
|
||||
if timeout
|
||||
@@ -467,13 +485,16 @@ module FlexNBD
|
||||
[stdout, stderr, stat]
|
||||
end
|
||||
|
||||
|
||||
def mirror(dest_ip, dest_port, bandwidth=nil, action=nil)
|
||||
stdout, stderr, status = mirror_unchecked( dest_ip, dest_port, bandwidth, action )
|
||||
raise IOError, "Migrate command failed\n" + stderr unless status.success?
|
||||
raise IOError.new( "Migrate command failed\n" + stderr) unless status.success?
|
||||
|
||||
stdout
|
||||
end
|
||||
|
||||
|
||||
|
||||
def break(timeout=nil)
|
||||
cmd = break_cmd
|
||||
debug( cmd )
|
||||
@@ -481,6 +502,7 @@ module FlexNBD
|
||||
maybe_timeout( cmd, timeout )
|
||||
end
|
||||
|
||||
|
||||
def acl(*acl)
|
||||
cmd = acl_cmd( *acl )
|
||||
debug( cmd )
|
||||
@@ -488,8 +510,9 @@ module FlexNBD
|
||||
maybe_timeout( cmd, 2 )
|
||||
end
|
||||
|
||||
|
||||
def status( timeout = nil )
|
||||
cmd = status_cmd
|
||||
cmd = status_cmd()
|
||||
debug( cmd )
|
||||
|
||||
o,e = maybe_timeout( cmd, timeout )
|
||||
@@ -497,43 +520,50 @@ module FlexNBD
|
||||
[parse_status(o), e]
|
||||
end
|
||||
|
||||
|
||||
def launched?
|
||||
!!@pid
|
||||
end
|
||||
|
||||
|
||||
def paused
|
||||
Process.kill('STOP', @pid)
|
||||
Process.kill( "STOP", @pid )
|
||||
yield
|
||||
ensure
|
||||
Process.kill('CONT', @pid)
|
||||
Process.kill( "CONT", @pid )
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
protected
|
||||
def control_command(*args)
|
||||
raise 'Server not running' unless @pid
|
||||
raise "Server not running" unless @pid
|
||||
args = args.compact
|
||||
UNIXSocket.open(@ctrl) do |u|
|
||||
u.write(args.join("\n") + "\n")
|
||||
code, message = u.readline.split(': ', 2)
|
||||
code, message = u.readline.split(": ", 2)
|
||||
return [code, message]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def parse_status( status )
|
||||
hsh = {}
|
||||
|
||||
status.split(' ').each do |part|
|
||||
status.split(" ").each do |part|
|
||||
next if part.strip.empty?
|
||||
a, b = part.split('=')
|
||||
a,b = part.split("=")
|
||||
b.strip!
|
||||
b = true if b == 'true'
|
||||
b = false if b == 'false'
|
||||
b = true if b == "true"
|
||||
b = false if b == "false"
|
||||
|
||||
hsh[a.strip] = b
|
||||
end
|
||||
|
||||
hsh
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
@@ -1,7 +1,10 @@
|
||||
# encoding: utf-8
|
||||
|
||||
module FlexNBD
|
||||
|
||||
def self.binary( str )
|
||||
if str.respond_to? :force_encoding
|
||||
str.force_encoding 'ASCII-8BIT'
|
||||
str.force_encoding "ASCII-8BIT"
|
||||
else
|
||||
str
|
||||
end
|
||||
@@ -10,33 +13,36 @@ module FlexNBD
|
||||
# eeevil is his one and only name...
|
||||
def self.read_constants
|
||||
parents = []
|
||||
current = File.expand_path('.')
|
||||
while current != '/'
|
||||
current = File.expand_path(".")
|
||||
while current != "/"
|
||||
parents << current
|
||||
current = File.expand_path(File.join(current, '..'))
|
||||
current = File.expand_path( File.join( current, ".." ) )
|
||||
end
|
||||
|
||||
source_root = parents.find do |dirname|
|
||||
File.directory?(File.join(dirname, 'src'))
|
||||
File.directory?( File.join( dirname, "src" ) )
|
||||
end
|
||||
|
||||
raise 'No source root!' unless source_root
|
||||
fail "No source root!" unless source_root
|
||||
|
||||
headers = Dir[File.join(source_root, 'src', '{common,proxy,server}', '*.h')]
|
||||
headers = Dir[File.join( source_root, "src", "{common,proxy,server}","*.h" ) ]
|
||||
|
||||
headers.each do |header_filename|
|
||||
txt_lines = File.readlines( header_filename )
|
||||
txt_lines.each do |line|
|
||||
if line =~ /^#\s*define\s+([A-Z0-9_]+)\s+(\d+)\s*$/
|
||||
# Bodge until I can figure out what to do with #ifdefs
|
||||
const_set(Regexp.last_match(1), Regexp.last_match(2).to_i) unless const_defined?(Regexp.last_match(1))
|
||||
end
|
||||
const_set($1, $2.to_i) unless const_defined?( $1 )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
read_constants
|
||||
end
|
||||
|
||||
read_constants()
|
||||
|
||||
REQUEST_MAGIC = binary("\x25\x60\x95\x13") unless defined?(REQUEST_MAGIC)
|
||||
REPLY_MAGIC = binary("\x67\x44\x66\x98") unless defined?(REPLY_MAGIC)
|
||||
|
||||
end # module FlexNBD
|
||||
|
||||
|
@@ -1,3 +1,5 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
|
||||
@@ -5,13 +7,15 @@ require 'flexnbd/constants'
|
||||
|
||||
module FlexNBD
|
||||
class FakeDest
|
||||
|
||||
class Client
|
||||
def initialize( sock )
|
||||
@sock = sock
|
||||
end
|
||||
|
||||
|
||||
def write_hello( opts = {} )
|
||||
@sock.write('NBDMAGIC')
|
||||
@sock.write( "NBDMAGIC" )
|
||||
|
||||
if opts[:magic] == :wrong
|
||||
write_rand( @sock, 8 )
|
||||
@@ -28,11 +32,13 @@ module FlexNBD
|
||||
@sock.write( "\x00" * 128 )
|
||||
end
|
||||
|
||||
|
||||
def write_rand( sock, len )
|
||||
len.times { sock.write(rand(256).chr) }
|
||||
len.times do sock.write( rand(256).chr ) end
|
||||
end
|
||||
|
||||
def read_request
|
||||
|
||||
def read_request()
|
||||
req = @sock.read(28)
|
||||
|
||||
magic_s = req[0 ... 4 ]
|
||||
@@ -42,11 +48,11 @@ module FlexNBD
|
||||
len_s = req[24 ... 28]
|
||||
|
||||
{
|
||||
magic: magic_s,
|
||||
type: type_s.unpack('N').first,
|
||||
handle: handle_s,
|
||||
from: self.class.parse_be64(from_s),
|
||||
len: len_s.unpack('N').first
|
||||
:magic => magic_s,
|
||||
:type => type_s.unpack("N").first,
|
||||
:handle => handle_s,
|
||||
:from => self.class.parse_be64( from_s ),
|
||||
:len => len_s.unpack( "N").first
|
||||
}
|
||||
end
|
||||
|
||||
@@ -55,12 +61,14 @@ module FlexNBD
|
||||
end
|
||||
|
||||
def disconnected?
|
||||
begin
|
||||
Timeout.timeout(2) do
|
||||
@sock.read(1).nil?
|
||||
@sock.read(1) == nil
|
||||
end
|
||||
rescue Timeout::Error
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def write_reply( handle, err=0, opts={} )
|
||||
if opts[:magic] == :wrong
|
||||
@@ -69,14 +77,16 @@ module FlexNBD
|
||||
@sock.write( ::FlexNBD::REPLY_MAGIC )
|
||||
end
|
||||
|
||||
@sock.write([err].pack('N'))
|
||||
@sock.write( [err].pack("N") )
|
||||
@sock.write( handle )
|
||||
end
|
||||
|
||||
|
||||
def close
|
||||
@sock.close
|
||||
end
|
||||
|
||||
|
||||
def read_data( len )
|
||||
@sock.read( len )
|
||||
end
|
||||
@@ -85,23 +95,25 @@ module FlexNBD
|
||||
@sock.write( len )
|
||||
end
|
||||
|
||||
|
||||
def self.parse_be64(str)
|
||||
raise "String is the wrong length: 8 bytes expected (#{str.length} received)" unless
|
||||
str.length == 8
|
||||
|
||||
top, bottom = str.unpack('NN')
|
||||
top, bottom = str.unpack("NN")
|
||||
(top << 32) + bottom
|
||||
end
|
||||
|
||||
|
||||
def receive_mirror( opts = {} )
|
||||
write_hello
|
||||
write_hello()
|
||||
loop do
|
||||
req = read_request
|
||||
case req[:type]
|
||||
when 1
|
||||
read_data( req[:len] )
|
||||
write_reply( req[:handle] )
|
||||
when 65_536
|
||||
when 65536
|
||||
write_reply( req[:handle], opts[:err] == :entrust ? 1 : 0 )
|
||||
break
|
||||
else
|
||||
@@ -117,13 +129,16 @@ module FlexNBD
|
||||
raise "Not a disconnect: #{req.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
end # class Client
|
||||
|
||||
|
||||
def initialize( addr, port )
|
||||
@sock = TCPServer.new( addr, port )
|
||||
end
|
||||
|
||||
def accept(err_msg = 'Timed out waiting for a connection', timeout = 5)
|
||||
|
||||
def accept( err_msg = "Timed out waiting for a connection", timeout = 5)
|
||||
client_sock = nil
|
||||
|
||||
begin
|
||||
@@ -131,7 +146,7 @@ module FlexNBD
|
||||
client_sock = @sock.accept
|
||||
end
|
||||
rescue Timeout::Error
|
||||
raise Timeout::Error, err_msg
|
||||
raise Timeout::Error.new(err_msg)
|
||||
end
|
||||
|
||||
client_sock
|
||||
@@ -139,8 +154,13 @@ module FlexNBD
|
||||
Client.new( client_sock )
|
||||
end
|
||||
|
||||
|
||||
def close
|
||||
@sock.close
|
||||
end
|
||||
|
||||
|
||||
|
||||
end # module FakeDest
|
||||
end # module FlexNBD
|
||||
|
||||
|
@@ -1,9 +1,12 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
require "timeout"
|
||||
require 'flexnbd/constants'
|
||||
|
||||
module FlexNBD
|
||||
class FakeSource
|
||||
|
||||
def initialize( addr, port, err_msg, source_addr=nil, source_port=0 )
|
||||
timing_out( 2, err_msg ) do
|
||||
begin
|
||||
@@ -13,76 +16,75 @@ module FlexNBD
|
||||
TCPSocket.new( addr, port )
|
||||
end
|
||||
rescue Errno::ECONNREFUSED
|
||||
warn 'Connection refused, retrying'
|
||||
$stderr.puts "Connection refused, retrying"
|
||||
sleep(0.2)
|
||||
retry
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def close
|
||||
@sock.close
|
||||
end
|
||||
|
||||
def read_hello
|
||||
|
||||
def read_hello()
|
||||
timing_out( ::FlexNBD::MS_HELLO_TIME_SECS,
|
||||
'Timed out waiting for hello.') do
|
||||
raise 'No hello.' unless (hello = @sock.read(152)) &&
|
||||
"Timed out waiting for hello." ) do
|
||||
fail "No hello." unless (hello = @sock.read( 152 )) &&
|
||||
hello.length==152
|
||||
|
||||
passwd_s = hello[0..7]
|
||||
magic = hello[8..15].unpack('Q>').first
|
||||
size = hello[16..23].unpack('Q>').first
|
||||
flags = hello[24..27].unpack('L>').first
|
||||
reserved = hello[28..-1]
|
||||
magic_s = hello[0..7]
|
||||
ignore_s= hello[8..15]
|
||||
size_s = hello[16..23]
|
||||
|
||||
return { passwd: passwd_s, magic: magic, size: size, flags: flags, reserved: reserved }
|
||||
size_h, size_l = size_s.unpack("NN")
|
||||
size = (size_h << 32) + size_l
|
||||
|
||||
return { :magic => magic_s, :size => size }
|
||||
end
|
||||
end
|
||||
|
||||
def send_request(type, handle = 'myhandle', from = 0, len = 0, magic = REQUEST_MAGIC, flags = 0)
|
||||
raise 'Bad handle' unless handle.length == 8
|
||||
|
||||
def send_request( type, handle="myhandle", from=0, len=0, magic=REQUEST_MAGIC )
|
||||
fail "Bad handle" unless handle.length == 8
|
||||
|
||||
@sock.write( magic )
|
||||
@sock.write([flags].pack('n'))
|
||||
@sock.write([type].pack('n'))
|
||||
@sock.write( [type].pack( 'N' ) )
|
||||
@sock.write( handle )
|
||||
@sock.write( [n64( from )].pack( 'q' ) )
|
||||
@sock.write( [len].pack( 'N' ) )
|
||||
end
|
||||
|
||||
def write_write_request(from, len, handle = 'myhandle')
|
||||
|
||||
def write_write_request( from, len, handle="myhandle" )
|
||||
send_request( 1, handle, from, len )
|
||||
end
|
||||
|
||||
def write_write_request_with_fua(from, len, handle = 'myhandle')
|
||||
send_request(1, handle, from, len, REQUEST_MAGIC, 1)
|
||||
|
||||
def write_entrust_request( handle="myhandle" )
|
||||
send_request( 65536, handle )
|
||||
end
|
||||
|
||||
def write_flush_request(handle = 'myhandle')
|
||||
send_request(3, handle, 0, 0)
|
||||
end
|
||||
|
||||
def write_entrust_request(handle = 'myhandle')
|
||||
send_request(65_536, handle)
|
||||
end
|
||||
|
||||
def write_disconnect_request(handle = 'myhandle')
|
||||
def write_disconnect_request( handle="myhandle" )
|
||||
send_request( 2, handle )
|
||||
end
|
||||
|
||||
def write_read_request(from, len, _handle = 'myhandle')
|
||||
send_request(0, 'myhandle', from, len)
|
||||
def write_read_request( from, len, handle="myhandle" )
|
||||
send_request( 0, "myhandle", from, len )
|
||||
end
|
||||
|
||||
|
||||
def write_data( data )
|
||||
@sock.write( data )
|
||||
end
|
||||
|
||||
|
||||
# Handy utility
|
||||
def read( from, len )
|
||||
timing_out(2, 'Timed out reading') do
|
||||
send_request(0, 'myhandle', from, len)
|
||||
timing_out( 2, "Timed out reading" ) do
|
||||
send_request( 0, "myhandle", from, len )
|
||||
read_raw( len )
|
||||
end
|
||||
end
|
||||
@@ -92,26 +94,19 @@ module FlexNBD
|
||||
end
|
||||
|
||||
def send_mirror
|
||||
read_hello
|
||||
write(0, '12345678')
|
||||
read_response
|
||||
write_disconnect_request
|
||||
close
|
||||
read_hello()
|
||||
write( 0, "12345678" )
|
||||
read_response()
|
||||
write_disconnect_request()
|
||||
close()
|
||||
end
|
||||
|
||||
|
||||
def write( from, data )
|
||||
write_write_request( from, data.length )
|
||||
write_data( data )
|
||||
end
|
||||
|
||||
def write_with_fua(from, data)
|
||||
write_write_request_with_fua(from, data.length)
|
||||
write_data(data)
|
||||
end
|
||||
|
||||
def flush
|
||||
write_flush_request
|
||||
end
|
||||
|
||||
def read_response
|
||||
magic = @sock.read(4)
|
||||
@@ -119,26 +114,30 @@ module FlexNBD
|
||||
handle = @sock.read(8)
|
||||
|
||||
{
|
||||
magic: magic,
|
||||
error: error_s.unpack('N').first,
|
||||
handle: handle
|
||||
:magic => magic,
|
||||
:error => error_s.unpack("N").first,
|
||||
:handle => handle
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
def disconnected?
|
||||
result = nil
|
||||
Timeout.timeout(2) { result = @sock.read(1).nil? }
|
||||
Timeout.timeout( 2 ) { result = ( @sock.read(1) == nil ) }
|
||||
result
|
||||
end
|
||||
|
||||
|
||||
def timing_out( time, msg )
|
||||
begin
|
||||
Timeout.timeout( time ) do
|
||||
yield
|
||||
end
|
||||
rescue Timeout::Error
|
||||
warn msg
|
||||
$stderr.puts msg
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -155,5 +154,7 @@ module FlexNBD
|
||||
((b & 0x000000000000ff00) << 40) |
|
||||
((b & 0x00000000000000ff) << 56)
|
||||
end
|
||||
|
||||
end # class FakeSource
|
||||
end # module FlexNBD
|
||||
|
||||
|
@@ -1,55 +0,0 @@
|
||||
require 'tempfile'
|
||||
|
||||
#
|
||||
# LdPreload is a little wrapper for using LD_PRELOAD loggers to pick up system
|
||||
# calls when testing flexnbd.
|
||||
#
|
||||
module LdPreload
|
||||
#
|
||||
# This takes an object name, sets up a temporary log file, whose name is
|
||||
# recorded in the environment as OUTPUT_obj_name, where obj_name is the
|
||||
# name of the preload module to build and load.
|
||||
def with_ld_preload(obj_name)
|
||||
@ld_preload_logs ||= {}
|
||||
flunk 'Can only load a preload module once!' if @ld_preload_logs[obj_name]
|
||||
|
||||
system("make -C ld_preloads/ #{obj_name}.o > /dev/null") ||
|
||||
flunk("Failed to build object #{obj_name}")
|
||||
orig_env = ENV['LD_PRELOAD']
|
||||
ENV['LD_PRELOAD'] = [orig_env, File.expand_path("./ld_preloads/#{obj_name}.o")].compact.join(' ')
|
||||
|
||||
# Open the log, and stick it in a hash
|
||||
@ld_preload_logs[obj_name] = Tempfile.new(obj_name)
|
||||
ENV['OUTPUT_' + obj_name] = @ld_preload_logs[obj_name].path
|
||||
|
||||
yield
|
||||
ensure
|
||||
if @ld_preload_logs[obj_name]
|
||||
@ld_preload_logs[obj_name].close
|
||||
@ld_preload_logs.delete(obj_name)
|
||||
end
|
||||
ENV['LD_PRELOAD'] = orig_env
|
||||
end
|
||||
|
||||
def read_ld_preload_log(obj_name)
|
||||
lines = []
|
||||
lines << @ld_preload_logs[obj_name].readline.chomp until
|
||||
@ld_preload_logs[obj_name].eof?
|
||||
lines
|
||||
end
|
||||
|
||||
#
|
||||
# The next to methods assume the log file has one entry per line, and that
|
||||
# each entry is a series of values separated by colons.
|
||||
#
|
||||
def parse_ld_preload_logs(obj_name)
|
||||
read_ld_preload_log(obj_name).map do |l|
|
||||
l.split(':').map { |i| i =~ /^\d+$/ ? i.to_i : i }
|
||||
end
|
||||
end
|
||||
|
||||
def assert_func_call(loglines, args, msg)
|
||||
re = Regexp.new('^' + args.join(':'))
|
||||
assert(loglines.any? { |l| l.match(re) }, msg)
|
||||
end
|
||||
end
|
@@ -1,13 +0,0 @@
|
||||
|
||||
SRC := $(wildcard *.c)
|
||||
OBJS := $(SRC:%.c=%.o)
|
||||
|
||||
all: $(OBJS)
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJS)
|
||||
|
||||
%.o: %.c
|
||||
gcc -shared -fPIC -ldl -o $@ $<
|
||||
|
||||
.PHONY: all clean
|
@@ -1,33 +0,0 @@
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
typedef int (*real_msync_t)(void *addr, size_t length, int flags);
|
||||
|
||||
int real_msync(void *addr, size_t length, int flags) {
|
||||
return ((real_msync_t)dlsym(RTLD_NEXT, "msync"))(addr, length, flags);
|
||||
}
|
||||
|
||||
/*
|
||||
* Noddy LD_PRELOAD wrapper to catch msync calls, and log them to a file.
|
||||
*/
|
||||
|
||||
int msync(void *addr, size_t length, int flags) {
|
||||
FILE *fd;
|
||||
char *fn;
|
||||
int retval;
|
||||
|
||||
retval = real_msync(addr, length, flags);
|
||||
|
||||
fn = getenv("OUTPUT_msync_logger");
|
||||
if ( fn != NULL ) {
|
||||
fd = fopen(fn,"a");
|
||||
fprintf(fd,"msync:%d:%i:%i:%i\n", addr, length, flags, retval);
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
@@ -1,38 +0,0 @@
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
/*
|
||||
* Noddy LD_PRELOAD wrapper to catch setsockopt calls, and log them to a file.
|
||||
*/
|
||||
|
||||
typedef int (*real_setsockopt_t)(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
|
||||
|
||||
int real_setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)
|
||||
{
|
||||
return ((real_setsockopt_t)dlsym(RTLD_NEXT, "setsockopt"))(sockfd, level, optname, optval, optlen);
|
||||
}
|
||||
|
||||
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)
|
||||
{
|
||||
FILE *fd;
|
||||
char *fn;
|
||||
int retval;
|
||||
|
||||
retval = real_setsockopt(sockfd, level, optname, optval, optlen);
|
||||
fn = getenv("OUTPUT_setsockopt_logger");
|
||||
|
||||
/*
|
||||
* Only interested in catching non-null 4-byte (integer) values
|
||||
*/
|
||||
if ( fn != NULL && optval != NULL && optlen == 4) {
|
||||
fd = fopen(fn,"a");
|
||||
fprintf(fd,"setsockopt:%i:%i:%i:%i:%i\n", sockfd, level, optname, *(int *)optval, retval);
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
2
tests/acceptance/nbd_scenarios
Executable file → Normal file
2
tests/acceptance/nbd_scenarios
Executable file → Normal file
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/ruby
|
||||
|
||||
test_files = Dir[File.dirname(__FILE__) + '/test*.rb']
|
||||
test_files = Dir[File.dirname( __FILE__ ) + "/test*.rb"]
|
||||
for filename in test_files
|
||||
require filename
|
||||
end
|
||||
|
@@ -1,12 +1,8 @@
|
||||
|
||||
# encoding: utf-8
|
||||
require 'flexnbd/fake_source'
|
||||
require 'flexnbd/fake_dest'
|
||||
require 'ld_preload'
|
||||
|
||||
module ProxyTests
|
||||
|
||||
include LdPreload
|
||||
|
||||
def b
|
||||
"\xFF".b
|
||||
end
|
||||
@@ -17,17 +13,14 @@ module ProxyTests
|
||||
@env.nbd2.can_die(0)
|
||||
client = FlexNBD::FakeSource.new(@env.ip, @env.port2, "Couldn't connect to proxy")
|
||||
begin
|
||||
|
||||
result = client.read_hello
|
||||
assert_equal 'NBDMAGIC', result[:passwd]
|
||||
assert_equal "NBDMAGIC", result[:magic]
|
||||
assert_equal override_size || @env.file1.size, result[:size]
|
||||
|
||||
yield client
|
||||
ensure
|
||||
begin
|
||||
client.close
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
client.close rescue nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -39,11 +32,11 @@ module ProxyTests
|
||||
with_proxied_client do |client|
|
||||
(0..3).each do |n|
|
||||
offset = n * 4096
|
||||
client.write_read_request(offset, 4096, 'myhandle')
|
||||
client.write_read_request(offset, 4096, "myhandle")
|
||||
rsp = client.read_response
|
||||
|
||||
assert_equal ::FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 'myhandle', rsp[:handle]
|
||||
assert_equal "myhandle", rsp[:handle]
|
||||
assert_equal 0, rsp[:error]
|
||||
|
||||
orig_data = @env.file1.read(offset, 4096)
|
||||
@@ -66,7 +59,7 @@ module ProxyTests
|
||||
rsp = client.read_response
|
||||
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 'myhandle', rsp[:handle]
|
||||
assert_equal "myhandle", rsp[:handle]
|
||||
assert_equal 0, rsp[:error]
|
||||
|
||||
data = @env.file1.read(offset, 4096)
|
||||
@@ -76,20 +69,6 @@ module ProxyTests
|
||||
end
|
||||
end
|
||||
|
||||
def test_write_request_past_end_of_disc_returns_to_client
|
||||
with_proxied_client do |client|
|
||||
n = 1000
|
||||
offset = n * 4096
|
||||
client.write(offset, b * 4096)
|
||||
rsp = client.read_response
|
||||
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 'myhandle', rsp[:handle]
|
||||
# NBD protocol say ENOSPC (28) in this situation
|
||||
assert_equal 28, rsp[:error]
|
||||
end
|
||||
end
|
||||
|
||||
def make_fake_server
|
||||
server = FlexNBD::FakeDest.new(@env.ip, @env.port1)
|
||||
@server_up = true
|
||||
@@ -135,23 +114,23 @@ module ProxyTests
|
||||
sc2.write_data( b * 4096 )
|
||||
|
||||
# Check it to make sure it's correct
|
||||
rsp = Timeout.timeout(15) { client.read_response }
|
||||
rsp = timeout(15) { client.read_response }
|
||||
assert_equal ::FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 0, rsp[:error]
|
||||
assert_equal req1[:handle], rsp[:handle]
|
||||
|
||||
data = client.read_raw( 4096 )
|
||||
assert_equal((b * 4096), data, 'Wrong data returned')
|
||||
assert_equal( (b * 4096), data, "Wrong data returned" )
|
||||
|
||||
sc2.close
|
||||
server.close
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def test_write_request_retried_when_upstream_dies_partway
|
||||
maker = make_fake_server
|
||||
|
||||
with_ld_preload('setsockopt_logger') do
|
||||
with_proxied_client(4096) do |client|
|
||||
server, sc1 = maker.value
|
||||
|
||||
@@ -165,11 +144,7 @@ module ProxyTests
|
||||
assert_equal 0, req1[:from]
|
||||
assert_equal 4096, req1[:len]
|
||||
data1 = sc1.read_data( 4096 )
|
||||
assert_equal((b * 4096), data1, 'Data not proxied successfully')
|
||||
|
||||
# Read the setsockopt logs, so we can check that TCP_NODELAY is re-set
|
||||
# later
|
||||
read_ld_preload_log('setsockopt_logger')
|
||||
assert_equal( ( b * 4096 ), data1, "Data not proxied successfully" )
|
||||
|
||||
# Kill the server again, now we're sure the read request has been sent once
|
||||
sc1.close
|
||||
@@ -188,53 +163,32 @@ module ProxyTests
|
||||
sc2.write_reply( req2[:handle] )
|
||||
|
||||
# Check it to make sure it's correct
|
||||
rsp = Timeout.timeout(15) { client.read_response }
|
||||
rsp = timeout(15) { client.read_response }
|
||||
assert_equal ::FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 0, rsp[:error]
|
||||
assert_equal req1[:handle], rsp[:handle]
|
||||
|
||||
sc2.close
|
||||
server.close
|
||||
|
||||
# Check TCP_NODELAY was set on the upstream socket
|
||||
log = read_ld_preload_log('setsockopt_logger')
|
||||
assert_func_call(log,
|
||||
['setsockopt', 3,
|
||||
Socket::SOL_TCP, Socket::TCP_NODELAY, 1, 0],
|
||||
'TCP_NODELAY not set on upstream fd 3')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_only_one_client_can_connect_to_proxy_at_a_time
|
||||
with_proxied_client do |_client|
|
||||
with_proxied_client do |client|
|
||||
|
||||
c2 = nil
|
||||
assert_raises(Timeout::Error) do
|
||||
Timeout.timeout(1) do
|
||||
timeout(1) do
|
||||
c2 = FlexNBD::FakeSource.new(@env.ip, @env.port2, "Couldn't connect to proxy (2)")
|
||||
c2.read_hello
|
||||
end
|
||||
end
|
||||
if c2
|
||||
begin
|
||||
c2.close
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
c2.close rescue nil if c2
|
||||
end
|
||||
|
||||
def test_maximum_write_request_size
|
||||
# Defined in src/common/nbdtypes.h
|
||||
nbd_max_block_size = 32 * 1024 * 1024
|
||||
@env.writefile1('0' * 40 * 1024)
|
||||
with_proxied_client do |client|
|
||||
# This will crash with EPIPE if the proxy dies.
|
||||
client.write(0, b * nbd_max_block_size)
|
||||
rsp = client.read_response
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 0, rsp[:error]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
@@ -1,10 +1,13 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'test/unit'
|
||||
require 'environment'
|
||||
|
||||
class TestDestErrorHandling < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
@env = Environment.new
|
||||
@env.writefile1('0' * 4)
|
||||
@env.writefile1( "0" * 4 )
|
||||
@env.listen1
|
||||
end
|
||||
|
||||
@@ -12,77 +15,89 @@ class TestDestErrorHandling < Test::Unit::TestCase
|
||||
@env.cleanup
|
||||
end
|
||||
|
||||
|
||||
def test_hello_blocked_by_disconnect_causes_error_not_fatal
|
||||
run_fake('source/close_after_connect')
|
||||
run_fake( "source/close_after_connect" )
|
||||
assert_no_control
|
||||
end
|
||||
|
||||
# # This is disabled while CLIENT_MAX_WAIT_SECS is removed
|
||||
# def test_hello_goes_astray_causes_timeout_error
|
||||
# run_fake( "source/hang_after_hello" )
|
||||
# assert_no_control
|
||||
# end
|
||||
=begin
|
||||
# This is disabled while CLIENT_MAX_WAIT_SECS is removed
|
||||
def test_hello_goes_astray_causes_timeout_error
|
||||
run_fake( "source/hang_after_hello" )
|
||||
assert_no_control
|
||||
end
|
||||
=end
|
||||
|
||||
def test_sigterm_has_bad_exit_status
|
||||
@env.nbd1.can_die(1)
|
||||
run_fake('source/sigterm_after_hello')
|
||||
run_fake( "source/sigterm_after_hello" )
|
||||
end
|
||||
|
||||
def test_disconnect_after_hello_causes_error_not_fatal
|
||||
run_fake('source/close_after_hello')
|
||||
run_fake( "source/close_after_hello" )
|
||||
assert_no_control
|
||||
end
|
||||
|
||||
|
||||
def test_partial_read_causes_error
|
||||
run_fake('source/close_mid_read')
|
||||
run_fake( "source/close_mid_read" )
|
||||
end
|
||||
|
||||
def test_double_connect_during_hello
|
||||
run_fake('source/connect_during_hello')
|
||||
run_fake( "source/connect_during_hello" )
|
||||
end
|
||||
|
||||
|
||||
def test_acl_rejection
|
||||
@env.acl1('127.0.0.1')
|
||||
run_fake('source/connect_from_banned_ip')
|
||||
@env.acl1("127.0.0.1")
|
||||
run_fake( "source/connect_from_banned_ip")
|
||||
end
|
||||
|
||||
|
||||
def test_bad_write
|
||||
run_fake('source/write_out_of_range')
|
||||
run_fake( "source/write_out_of_range" )
|
||||
end
|
||||
|
||||
|
||||
def test_disconnect_before_write_data_causes_error
|
||||
run_fake('source/close_after_write')
|
||||
run_fake( "source/close_after_write" )
|
||||
end
|
||||
|
||||
|
||||
def test_disconnect_before_write_reply_causes_error
|
||||
# Note that this is an odd case: writing the reply doesn't fail.
|
||||
# The test passes because the next attempt by flexnbd to read a
|
||||
# request returns EOF.
|
||||
run_fake('source/close_after_write_data')
|
||||
run_fake( "source/close_after_write_data" )
|
||||
end
|
||||
|
||||
|
||||
|
||||
def test_straight_migration
|
||||
@env.nbd1.can_die(0)
|
||||
run_fake('source/successful_transfer')
|
||||
run_fake( "source/successful_transfer" )
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
private
|
||||
def run_fake( name )
|
||||
@env.run_fake( name, @env.ip, @env.port1 )
|
||||
assert @env.fake_reports_success, "#{name} failed."
|
||||
end
|
||||
|
||||
def status
|
||||
stat, = @env.status1
|
||||
stat, _ = @env.status1
|
||||
stat
|
||||
end
|
||||
|
||||
def assert_no_control
|
||||
assert !status['has_control'], 'Thought it had control'
|
||||
assert !status['has_control'], "Thought it had control"
|
||||
end
|
||||
|
||||
def assert_control
|
||||
assert status['has_control'], "Didn't think it had control"
|
||||
end
|
||||
|
||||
end # class TestDestErrorHandling
|
||||
|
||||
|
@@ -1,3 +1,5 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'test/unit'
|
||||
require 'environment'
|
||||
require 'flexnbd/constants'
|
||||
@@ -17,18 +19,20 @@ class TestHappyPath < Test::Unit::TestCase
|
||||
@env.cleanup
|
||||
end
|
||||
|
||||
|
||||
def test_read1
|
||||
@env.writefile1('f' * 64)
|
||||
@env.writefile1("f"*64)
|
||||
@env.serve1
|
||||
|
||||
[0, 12, 63].each do |num|
|
||||
|
||||
assert_equal(
|
||||
bin( @env.nbd1.read(num*@env.blocksize, @env.blocksize) ),
|
||||
bin( @env.file1.read(num*@env.blocksize, @env.blocksize) )
|
||||
)
|
||||
end
|
||||
|
||||
[124, 1200, 10_028, 25_488].each do |num|
|
||||
[124, 1200, 10028, 25488].each do |num|
|
||||
assert_equal(bin(@env.nbd1.read(num, 4)), bin(@env.file1.read(num, 4)))
|
||||
end
|
||||
end
|
||||
@@ -36,11 +40,11 @@ class TestHappyPath < Test::Unit::TestCase
|
||||
# Check that we're not
|
||||
#
|
||||
def test_writeread1
|
||||
@env.writefile1('0' * 64)
|
||||
@env.writefile1("0"*64)
|
||||
@env.serve1
|
||||
|
||||
[0, 12, 63].each do |num|
|
||||
data = 'X' * @env.blocksize
|
||||
data = "X"*@env.blocksize
|
||||
@env.nbd1.write(num*@env.blocksize, data)
|
||||
assert_equal(data, @env.file1.read(num*@env.blocksize, data.size))
|
||||
assert_equal(data, @env.nbd1.read(num*@env.blocksize, data.size))
|
||||
@@ -51,11 +55,11 @@ class TestHappyPath < Test::Unit::TestCase
|
||||
# up.
|
||||
#
|
||||
def test_writeread2
|
||||
@env.writefile1('0' * 1024)
|
||||
@env.writefile1("0"*1024)
|
||||
@env.serve1
|
||||
|
||||
d0 = "\0"*@env.blocksize
|
||||
d1 = 'X' * @env.blocksize
|
||||
d1 = "X"*@env.blocksize
|
||||
(0..63).each do |num|
|
||||
@env.nbd1.write(num*@env.blocksize*2, d1)
|
||||
end
|
||||
@@ -64,18 +68,20 @@ class TestHappyPath < Test::Unit::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def setup_to_mirror
|
||||
@env.writefile1('f' * 4)
|
||||
@env.writefile1( "f"*4 )
|
||||
@env.serve1
|
||||
|
||||
@env.writefile2('0' * 4)
|
||||
@env.writefile2( "0"*4 )
|
||||
@env.listen2
|
||||
end
|
||||
|
||||
|
||||
def test_mirror
|
||||
@env.nbd1.can_die
|
||||
@env.nbd2.can_die(0)
|
||||
setup_to_mirror
|
||||
setup_to_mirror()
|
||||
|
||||
stdout, stderr = @env.mirror12
|
||||
|
||||
@@ -83,15 +89,16 @@ class TestHappyPath < Test::Unit::TestCase
|
||||
@env.nbd2.join
|
||||
|
||||
assert( File.file?( @env.filename1 ),
|
||||
'The source file was incorrectly deleted')
|
||||
"The source file was incorrectly deleted")
|
||||
assert_equal(@env.file1.read_original( 0, @env.blocksize ),
|
||||
@env.file2.read( 0, @env.blocksize ) )
|
||||
end
|
||||
|
||||
|
||||
def test_mirror_unlink
|
||||
@env.nbd1.can_die(0)
|
||||
@env.nbd2.can_die(0)
|
||||
setup_to_mirror
|
||||
setup_to_mirror()
|
||||
|
||||
assert File.file?( @env.filename1 )
|
||||
|
||||
@@ -99,40 +106,45 @@ class TestHappyPath < Test::Unit::TestCase
|
||||
|
||||
assert_no_match( /unrecognized/, stderr )
|
||||
|
||||
Timeout.timeout(10) { @env.nbd1.join }
|
||||
|
||||
Timeout.timeout(10) do @env.nbd1.join end
|
||||
|
||||
assert !File.file?( @env.filename1 )
|
||||
end
|
||||
|
||||
|
||||
|
||||
def test_write_to_high_block
|
||||
#
|
||||
# This test does not work on 32 bit platforms.
|
||||
#
|
||||
skip('Not relevant on 32-bit platforms') if ['a'].pack('p').size < 8
|
||||
skip("Not relevant on 32-bit platforms") if ( ["a"].pack("p").size < 8 )
|
||||
|
||||
# Create a large file, then try to write to somewhere after the 2G boundary
|
||||
@env.truncate1 '4G'
|
||||
@env.truncate1 "4G"
|
||||
@env.serve1
|
||||
|
||||
@env.nbd1.write(2**31 + 2**29, '12345678')
|
||||
@env.nbd1.write( 2**31+2**29, "12345678" )
|
||||
sleep(1)
|
||||
assert_equal '12345678', @env.nbd1.read(2**31 + 2**29, 8)
|
||||
assert_equal "12345678", @env.nbd1.read( 2**31+2**29, 8 )
|
||||
end
|
||||
|
||||
|
||||
def test_set_acl
|
||||
# Just check that we get sane feedback here
|
||||
@env.writefile1('f' * 4)
|
||||
@env.writefile1( "f"*4 )
|
||||
@env.serve1
|
||||
|
||||
_, stderr = @env.acl1('127.0.0.1')
|
||||
_,stderr = @env.acl1("127.0.0.1")
|
||||
assert_no_match( /^(F|E):/, stderr )
|
||||
end
|
||||
|
||||
|
||||
def test_write_more_than_one_run
|
||||
one_mb = 2**20
|
||||
data = "\0" * 256 * one_mb
|
||||
|
||||
File.open(@env.filename1, 'wb') { |f| f.write('1' * 256 * one_mb) }
|
||||
File.open(@env.filename1, "wb") do |f| f.write( "1" * 256 * one_mb ) end
|
||||
|
||||
@env.serve1
|
||||
sleep 5
|
||||
@@ -141,15 +153,17 @@ class TestHappyPath < Test::Unit::TestCase
|
||||
@env.nbd1.kill
|
||||
|
||||
i = 0
|
||||
File.open(@env.filename1, 'rb') do |f|
|
||||
File.open(@env.filename1, "rb") do |f|
|
||||
while mb = f.read( one_mb )
|
||||
unless "\0"*one_mb == mb
|
||||
msg = format("Read non-zeros after offset %x:\n", (i * one_mb))
|
||||
msg = "Read non-zeros after offset %x:\n"%(i * one_mb)
|
||||
msg += `hexdump #{@env.filename1} | head -n5`
|
||||
raise msg
|
||||
fail msg
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
@@ -2,6 +2,7 @@ require 'test/unit'
|
||||
require 'environment'
|
||||
require 'proxy_tests'
|
||||
|
||||
|
||||
class TestPrefetchProxyMode < Test::Unit::TestCase
|
||||
include ProxyTests
|
||||
|
||||
@@ -9,7 +10,7 @@ class TestPrefetchProxyMode < Test::Unit::TestCase
|
||||
super
|
||||
@env = Environment.new
|
||||
@env.prefetch_proxy!
|
||||
@env.writefile1('f' * 16)
|
||||
@env.writefile1( "f" * 16 )
|
||||
end
|
||||
|
||||
def teardown
|
||||
@@ -17,3 +18,5 @@ class TestPrefetchProxyMode < Test::Unit::TestCase
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
@@ -2,13 +2,14 @@ require 'test/unit'
|
||||
require 'environment'
|
||||
require 'proxy_tests'
|
||||
|
||||
|
||||
class TestProxyMode < Test::Unit::TestCase
|
||||
include ProxyTests
|
||||
|
||||
def setup
|
||||
super
|
||||
@env = Environment.new
|
||||
@env.writefile1('f' * 16)
|
||||
@env.writefile1( "f" * 16 )
|
||||
end
|
||||
|
||||
def teardown
|
||||
@@ -16,3 +17,4 @@ class TestProxyMode < Test::Unit::TestCase
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
|
@@ -1,15 +1,15 @@
|
||||
require 'test/unit'
|
||||
require 'environment'
|
||||
require 'flexnbd/fake_source'
|
||||
require 'ld_preload'
|
||||
|
||||
class TestServeMode < Test::Unit::TestCase
|
||||
include LdPreload
|
||||
|
||||
def setup
|
||||
super
|
||||
@b = "\xFF".b
|
||||
@env = Environment.new
|
||||
@env.writefile1( "0" )
|
||||
@env.serve1
|
||||
end
|
||||
|
||||
def teardown
|
||||
@@ -18,44 +18,35 @@ class TestServeMode < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def connect_to_server
|
||||
@env.writefile1('0')
|
||||
@env.serve1
|
||||
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
|
||||
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, "Connecting to server failed")
|
||||
begin
|
||||
result = client.read_hello
|
||||
assert_equal 'NBDMAGIC', result[:passwd]
|
||||
assert_equal 0x00420281861253, result[:magic]
|
||||
assert_equal "NBDMAGIC", result[:magic]
|
||||
assert_equal @env.file1.size, result[:size]
|
||||
# See src/common/nbdtypes.h for the various flags. At the moment we
|
||||
# support HAS_FLAGS (1), SEND_FLUSH (4), SEND_FUA (8)
|
||||
assert_equal (1 | 4 | 8), result[:flags]
|
||||
assert_equal "\x0" * 124, result[:reserved]
|
||||
yield client
|
||||
ensure
|
||||
begin
|
||||
client.close
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
client.close rescue nil
|
||||
end
|
||||
end
|
||||
|
||||
def test_bad_request_magic_receives_error_response
|
||||
connect_to_server do |client|
|
||||
|
||||
# replace REQUEST_MAGIC with all 0s to make it look bad
|
||||
client.send_request(0, 'myhandle', 0, 0, "\x00\x00\x00\x00")
|
||||
client.send_request( 0, "myhandle", 0, 0, "\x00\x00\x00\x00" )
|
||||
rsp = client.read_response
|
||||
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 'myhandle', rsp[:handle]
|
||||
assert_equal "myhandle", rsp[:handle]
|
||||
assert rsp[:error] != 0, "Server sent success reply back: #{rsp[:error]}"
|
||||
|
||||
# The client should be disconnected now
|
||||
assert client.disconnected?, 'Server not disconnected'
|
||||
assert client.disconnected?, "Server not disconnected"
|
||||
end
|
||||
end
|
||||
|
||||
def test_long_write_on_top_of_short_write_is_respected
|
||||
|
||||
connect_to_server do |client|
|
||||
# Start with a file of all-zeroes.
|
||||
client.write( 0, "\x00" * @env.file1.size )
|
||||
@@ -77,15 +68,15 @@ class TestServeMode < Test::Unit::TestCase
|
||||
assert_equal @b * 2, @env.file1.read( 0, 2 )
|
||||
end
|
||||
|
||||
|
||||
def test_read_request_out_of_bounds_receives_error_response
|
||||
connect_to_server do |client|
|
||||
client.write_read_request( @env.file1.size, 4096 )
|
||||
rsp = client.read_response
|
||||
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 'myhandle', rsp[:handle]
|
||||
# NBD protocol suggests ENOSPC (28) is returned
|
||||
assert_equal 28, rsp[:error], 'Server sent incorrect response'
|
||||
assert_equal "myhandle", rsp[:handle]
|
||||
assert rsp[:error] != 0, "Server sent success reply back: #{rsp[:error]}"
|
||||
|
||||
# Ensure we're not disconnected by sending a request. We don't care about
|
||||
# whether the reply is good or not, here.
|
||||
@@ -101,9 +92,8 @@ class TestServeMode < Test::Unit::TestCase
|
||||
rsp = client.read_response
|
||||
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 'myhandle', rsp[:handle]
|
||||
# NBD protocol suggests ENOSPC (28) is returned
|
||||
assert_equal 28, rsp[:error], 'Server sent incorrect response'
|
||||
assert_equal "myhandle", rsp[:handle]
|
||||
assert rsp[:error] != 0, "Server sent success reply back: #{rsp[:error]}"
|
||||
|
||||
# Ensure we're not disconnected by sending a request. We don't care about
|
||||
# whether the reply is good or not, here.
|
||||
@@ -111,113 +101,10 @@ class TestServeMode < Test::Unit::TestCase
|
||||
rsp = client.read_response
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def test_unknown_command_receives_error_response
|
||||
connect_to_server do |client|
|
||||
client.send_request(123)
|
||||
rsp = client.read_response
|
||||
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 'myhandle', rsp[:handle]
|
||||
# NBD protocol suggests EINVAL (22) is returned
|
||||
assert_equal 22, rsp[:error], 'Server sent incorrect response'
|
||||
|
||||
# Ensure we're not disconnected by sending a request. We don't care about
|
||||
# whether the reply is good or not, here.
|
||||
client.write(0, "\x00" * @env.file1.size)
|
||||
rsp = client.read_response
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
end
|
||||
end
|
||||
|
||||
def test_flush_is_accepted
|
||||
with_ld_preload('msync_logger') do
|
||||
connect_to_server do |client|
|
||||
client.flush
|
||||
rsp = client.read_response
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 0, rsp[:error]
|
||||
end
|
||||
op = parse_ld_preload_logs('msync_logger')
|
||||
assert_equal 1, op.count, 'Only one msync expected'
|
||||
assert_equal @env.blocksize, op.first[2], 'msync length wrong'
|
||||
assert_equal 6, op.first[3], 'msync called with incorrect flags'
|
||||
end
|
||||
end
|
||||
|
||||
def test_write_with_fua_is_accepted
|
||||
with_ld_preload('msync_logger') do
|
||||
page_size = Integer(`getconf PAGESIZE`)
|
||||
|
||||
@env.blocksize = page_size * 10
|
||||
connect_to_server do |client|
|
||||
# Write somewhere in the third page
|
||||
pos = page_size * 3 + 100
|
||||
client.write_with_fua(pos, "\x00" * 33)
|
||||
rsp = client.read_response
|
||||
assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic]
|
||||
assert_equal 0, rsp[:error]
|
||||
end
|
||||
op = parse_ld_preload_logs('msync_logger')
|
||||
|
||||
assert_equal 1, op.count, 'Only one msync expected'
|
||||
|
||||
# Should be 100 + 33, as we've started writing 100 bytes into a page, for
|
||||
# 33 bytes
|
||||
assert_equal 133, op.first[2], 'msync length wrong'
|
||||
assert_equal 6, op.first[3], 'msync called with incorrect flags'
|
||||
end
|
||||
end
|
||||
|
||||
def test_odd_size_discs_are_truncated_to_nearest_512
|
||||
# This should get rounded down to 1024
|
||||
@env.blocksize = 1024 + 511
|
||||
@env.writefile1('0')
|
||||
@env.serve1
|
||||
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
|
||||
begin
|
||||
result = client.read_hello
|
||||
assert_equal 'NBDMAGIC', result[:passwd]
|
||||
assert_equal 0x00420281861253, result[:magic]
|
||||
assert_equal 1024, result[:size]
|
||||
client.close
|
||||
end
|
||||
end
|
||||
|
||||
def test_server_sets_tcpkeepalive
|
||||
with_ld_preload('setsockopt_logger') do
|
||||
connect_to_server(&:close)
|
||||
op = read_ld_preload_log('setsockopt_logger')
|
||||
assert_func_call(op,
|
||||
['setsockopt', '\d+',
|
||||
Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1, 0],
|
||||
'TCP Keepalive not successfully set')
|
||||
assert_func_call(op,
|
||||
['setsockopt', '\d+',
|
||||
Socket::SOL_TCP, Socket::TCP_KEEPIDLE, 30, 0],
|
||||
'TCP Keepalive idle timeout not set to 30s')
|
||||
assert_func_call(op,
|
||||
['setsockopt', '\d+',
|
||||
Socket::SOL_TCP, Socket::TCP_KEEPINTVL, 10, 0],
|
||||
'TCP keepalive retry time not set to 10s')
|
||||
assert_func_call(op,
|
||||
['setsockopt', '\d+',
|
||||
Socket::SOL_TCP, Socket::TCP_KEEPCNT, 3, 0],
|
||||
'TCP keepalive count not set to 3')
|
||||
end
|
||||
end
|
||||
|
||||
def test_status_returns_correct_client_count
|
||||
@env.writefile1('0')
|
||||
@env.serve1
|
||||
assert_equal('0', @env.status1['num_clients'])
|
||||
client = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
|
||||
assert_equal('1', @env.status1['num_clients'])
|
||||
client2 = FlexNBD::FakeSource.new(@env.ip, @env.port1, 'Connecting to server failed')
|
||||
assert_equal('2', @env.status1['num_clients'])
|
||||
client2.close
|
||||
client.close
|
||||
assert_equal('0', @env.status1['num_clients'])
|
||||
end
|
||||
end
|
||||
|
@@ -1,105 +1,126 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'test/unit'
|
||||
require 'environment'
|
||||
|
||||
|
||||
class TestSourceErrorHandling < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
@old_env = ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS']
|
||||
ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS'] = '4.0'
|
||||
ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS'] = "4.0"
|
||||
|
||||
@env = Environment.new
|
||||
@env.writefile1('f' * 4)
|
||||
@env.writefile1( "f" * 4 )
|
||||
@env.serve1
|
||||
end
|
||||
|
||||
|
||||
def teardown
|
||||
@env.nbd1.can_die(0)
|
||||
@env.cleanup
|
||||
ENV['FLEXNBD_MS_REQUEST_LIMIT_SECS'] = @old_env
|
||||
end
|
||||
|
||||
|
||||
def expect_term_during_migration
|
||||
@env.nbd1.can_die(1,9)
|
||||
end
|
||||
|
||||
|
||||
def test_failure_to_connect_reported_in_mirror_cmd_response
|
||||
stdout, stderr = @env.mirror12_unchecked
|
||||
expect_term_during_migration
|
||||
assert_match( /failed to connect/, stderr )
|
||||
end
|
||||
|
||||
|
||||
def test_sigterm_after_hello_quits_with_status_of_1
|
||||
expect_term_during_migration
|
||||
run_fake('dest/sigterm_after_hello')
|
||||
run_fake( "dest/sigterm_after_hello" )
|
||||
end
|
||||
|
||||
|
||||
def test_destination_hangs_after_connect_reports_error_at_source
|
||||
run_fake('dest/hang_after_connect',
|
||||
err: /Remote server failed to respond/)
|
||||
run_fake( "dest/hang_after_connect",
|
||||
:err => /Remote server failed to respond/ )
|
||||
end
|
||||
|
||||
|
||||
def test_destination_rejects_connection_reports_error_at_source
|
||||
run_fake('dest/reject_acl',
|
||||
err: /Mirror was rejected/)
|
||||
run_fake( "dest/reject_acl",
|
||||
:err => /Mirror was rejected/ )
|
||||
end
|
||||
|
||||
|
||||
def test_wrong_size_causes_disconnect
|
||||
run_fake('dest/hello_wrong_size',
|
||||
err: /Remote size does not match local size/)
|
||||
run_fake( "dest/hello_wrong_size",
|
||||
:err => /Remote size does not match local size/ )
|
||||
end
|
||||
|
||||
|
||||
def test_wrong_magic_causes_disconnect
|
||||
expect_term_during_migration
|
||||
run_fake('dest/hello_wrong_magic',
|
||||
err: /Mirror was rejected/)
|
||||
run_fake( "dest/hello_wrong_magic",
|
||||
:err => /Mirror was rejected/ )
|
||||
end
|
||||
|
||||
|
||||
def test_disconnect_after_hello_causes_retry
|
||||
expect_term_during_migration
|
||||
run_fake('dest/close_after_hello',
|
||||
out: /Mirror started/)
|
||||
run_fake( "dest/close_after_hello",
|
||||
:out => /Mirror started/ )
|
||||
end
|
||||
|
||||
|
||||
def test_write_times_out_causes_retry
|
||||
expect_term_during_migration
|
||||
run_fake('dest/hang_after_write')
|
||||
run_fake( "dest/hang_after_write" )
|
||||
end
|
||||
|
||||
|
||||
def test_rejected_write_causes_retry
|
||||
expect_term_during_migration
|
||||
run_fake('dest/error_on_write')
|
||||
run_fake( "dest/error_on_write" )
|
||||
end
|
||||
|
||||
|
||||
def test_disconnect_before_write_reply_causes_retry
|
||||
expect_term_during_migration
|
||||
run_fake('dest/close_after_write')
|
||||
run_fake( "dest/close_after_write" )
|
||||
end
|
||||
|
||||
|
||||
def test_bad_write_reply_causes_retry
|
||||
expect_term_during_migration
|
||||
run_fake('dest/write_wrong_magic')
|
||||
run_fake( "dest/write_wrong_magic" )
|
||||
end
|
||||
|
||||
|
||||
def test_pre_entrust_disconnect_causes_retry
|
||||
expect_term_during_migration
|
||||
run_fake('dest/close_after_writes')
|
||||
run_fake( "dest/close_after_writes" )
|
||||
end
|
||||
|
||||
|
||||
def test_cancel_migration
|
||||
run_fake('dest/break_after_hello')
|
||||
run_fake( "dest/break_after_hello" )
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
private
|
||||
def run_fake(name, opts = {})
|
||||
@env.run_fake( name, @env.ip, @env.port2, @env.nbd1.ctrl )
|
||||
stdout, stderr = @env.mirror12_unchecked
|
||||
assert_success
|
||||
assert_match( opts[:err], stderr ) if opts[:err]
|
||||
assert_match( opts[:out], stdout ) if opts[:out]
|
||||
[stdout, stderr]
|
||||
return stdout, stderr
|
||||
end
|
||||
|
||||
def assert_success( msg=nil )
|
||||
assert @env.fake_reports_success, msg || 'Fake failed'
|
||||
assert @env.fake_reports_success, msg || "Fake failed"
|
||||
end
|
||||
|
||||
|
||||
end # class TestSourceErrorHandling
|
||||
|
99
tests/acceptance/test_write_during_migration.rb
Normal file → Executable file
99
tests/acceptance/test_write_during_migration.rb
Normal file → Executable file
@@ -9,98 +9,102 @@ require 'tmpdir'
|
||||
Thread.abort_on_exception = true
|
||||
|
||||
class TestWriteDuringMigration < Test::Unit::TestCase
|
||||
def setup
|
||||
@flexnbd = File.expand_path('../../build/flexnbd')
|
||||
|
||||
raise 'No binary!' unless File.executable?(@flexnbd)
|
||||
def setup
|
||||
@flexnbd = File.expand_path("../../build/flexnbd")
|
||||
|
||||
raise "No binary!" unless File.executable?( @flexnbd )
|
||||
|
||||
|
||||
@size = 20*1024*1024 # 20MB
|
||||
@write_data = 'foo!' * 2048 # 8K write
|
||||
@write_data = "foo!" * 2048 # 8K write
|
||||
@source_port = 9990
|
||||
@dest_port = 9991
|
||||
@source_sock = 'src.sock'
|
||||
@dest_sock = 'dst.sock'
|
||||
@source_file = 'src.file'
|
||||
@dest_file = 'dst.file'
|
||||
@source_sock = "src.sock"
|
||||
@dest_sock = "dst.sock"
|
||||
@source_file = "src.file"
|
||||
@dest_file = "dst.file"
|
||||
end
|
||||
|
||||
|
||||
def teardown
|
||||
[@dst_proc, @src_proc].each do |pid|
|
||||
next unless pid
|
||||
begin
|
||||
Process.kill('KILL', pid)
|
||||
rescue StandardError
|
||||
nil
|
||||
if pid
|
||||
Process.kill( "KILL", pid ) rescue nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def debug_arg
|
||||
ENV['DEBUG'] ? '--verbose' : ''
|
||||
ENV['DEBUG'] ? "--verbose" : ""
|
||||
end
|
||||
|
||||
|
||||
def launch_servers
|
||||
@dst_proc = fork do
|
||||
@dst_proc = fork() {
|
||||
cmd = "#{@flexnbd} listen -l 127.0.0.1 -p #{@dest_port} -f #{@dest_file} -s #{@dest_sock} #{debug_arg}"
|
||||
exec cmd
|
||||
end
|
||||
}
|
||||
|
||||
@src_proc = fork do
|
||||
@src_proc = fork() {
|
||||
cmd = "#{@flexnbd} serve -l 127.0.0.1 -p #{@source_port} -f #{@source_file} -s #{@source_sock} #{debug_arg}"
|
||||
exec cmd
|
||||
end
|
||||
}
|
||||
begin
|
||||
awaiting = nil
|
||||
Timeout.timeout(10) do
|
||||
awaiting = :source
|
||||
sleep 0.1 until File.exist?(@source_sock)
|
||||
sleep 0.1 while !File.exists?( @source_sock )
|
||||
awaiting = :dest
|
||||
sleep 0.1 until File.exist?(@dest_sock)
|
||||
sleep 0.1 while !File.exists?( @dest_sock )
|
||||
end
|
||||
rescue Timeout::Error
|
||||
case awaiting
|
||||
when :source
|
||||
raise "Couldn't get a source socket."
|
||||
fail "Couldn't get a source socket."
|
||||
when :dest
|
||||
raise "Couldn't get a destination socket."
|
||||
fail "Couldn't get a destination socket."
|
||||
else
|
||||
raise "Something went wrong I don't understand."
|
||||
fail "Something went wrong I don't understand."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_files
|
||||
|
||||
def make_files()
|
||||
FileUtils.touch(@source_file)
|
||||
File.truncate(@source_file, @size)
|
||||
FileUtils.touch(@dest_file)
|
||||
File.truncate(@dest_file, @size)
|
||||
|
||||
File.open(@source_file, 'wb') { |f| f.write 'a' * @size }
|
||||
File.open(@source_file, "wb"){|f| f.write "a"*@size }
|
||||
end
|
||||
|
||||
|
||||
def start_mirror
|
||||
UNIXSocket.open(@source_sock) do |sock|
|
||||
sock.write(['mirror', '127.0.0.1', @dest_port.to_s, 'exit'].join("\x0A") + "\x0A\x0A")
|
||||
UNIXSocket.open(@source_sock) {|sock|
|
||||
sock.write(["mirror", "127.0.0.1", @dest_port.to_s, "exit"].join("\x0A") + "\x0A\x0A")
|
||||
sock.flush
|
||||
rsp = sock.readline
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
def wait_for_quit
|
||||
|
||||
def wait_for_quit()
|
||||
Timeout.timeout( 10 ) do
|
||||
start_time = Time.now
|
||||
dst_result = Process.waitpid2(@dst_proc)
|
||||
src_result = Process.waitpid2(@src_proc)
|
||||
dst_result = Process::waitpid2(@dst_proc)
|
||||
src_result = Process::waitpid2(@src_proc)
|
||||
end
|
||||
end
|
||||
|
||||
def source_writer
|
||||
client = FlexNBD::FakeSource.new('127.0.0.1', @source_port, 'Timed out connecting')
|
||||
client = FlexNBD::FakeSource.new( "127.0.0.1", @source_port, "Timed out connecting" )
|
||||
offsets = Range.new(0, (@size - @write_data.size) / 4096 ).to_a
|
||||
loop do
|
||||
begin
|
||||
client.write(offsets[rand(offsets.size)] * 4096, @write_data)
|
||||
rescue StandardError => err
|
||||
rescue => err
|
||||
# We expect a broken write at some point, so ignore it
|
||||
break
|
||||
end
|
||||
@@ -111,8 +115,8 @@ class TestWriteDuringMigration < Test::Unit::TestCase
|
||||
# puts `md5sum #{@source_file} #{@dest_file}`
|
||||
|
||||
# Ensure each block matches
|
||||
File.open(@source_file, 'r') do |source|
|
||||
File.open(@dest_file, 'r') do |dest|
|
||||
File.open(@source_file, "r") do |source|
|
||||
File.open(@dest_file, "r") do |dest|
|
||||
0.upto( @size / 4096 ) do |block_num|
|
||||
s_data = source.read( 4096 )
|
||||
d_data = dest.read( 4096 )
|
||||
@@ -127,16 +131,16 @@ class TestWriteDuringMigration < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_write_during_migration
|
||||
Dir.mktmpdir do |tmpdir|
|
||||
Dir.mktmpdir() do |tmpdir|
|
||||
Dir.chdir( tmpdir ) do
|
||||
make_files
|
||||
make_files()
|
||||
|
||||
launch_servers
|
||||
launch_servers()
|
||||
|
||||
src_writer = Thread.new { source_writer }
|
||||
|
||||
start_mirror
|
||||
wait_for_quit
|
||||
start_mirror()
|
||||
wait_for_quit()
|
||||
src_writer.join
|
||||
assert_both_sides_identical
|
||||
end
|
||||
@@ -144,21 +148,24 @@ class TestWriteDuringMigration < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_many_clients_during_migration
|
||||
Dir.mktmpdir do |tmpdir|
|
||||
Dir.mktmpdir() do |tmpdir|
|
||||
Dir.chdir( tmpdir ) do
|
||||
make_files
|
||||
make_files()
|
||||
|
||||
launch_servers
|
||||
launch_servers()
|
||||
|
||||
src_writers_1 = (1..5).collect { Thread.new { source_writer } }
|
||||
|
||||
start_mirror
|
||||
start_mirror()
|
||||
|
||||
src_writers_2 = (1..5).collect { Thread.new { source_writer } }
|
||||
|
||||
wait_for_quit
|
||||
(src_writers_1 + src_writers_2).each(&:join)
|
||||
wait_for_quit()
|
||||
( src_writers_1 + src_writers_2 ).each {|t| t.join }
|
||||
assert_both_sides_identical
|
||||
end
|
||||
end end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
@@ -13,6 +13,7 @@ START_TEST(test_null_acl)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_parses_single_line )
|
||||
{
|
||||
char *lines[] = {"127.0.0.1"};
|
||||
@@ -36,11 +37,9 @@ START_TEST(test_parses_multiple_lines)
|
||||
|
||||
struct ip_and_mask *entry;
|
||||
entry = &(*acl->entries)[0];
|
||||
fail_unless(entry->ip.family == e0.family,
|
||||
"entry 0 has wrong family!");
|
||||
fail_unless(entry->ip.family == e0.family, "entry 0 has wrong family!");
|
||||
entry = &(*acl->entries)[1];
|
||||
fail_unless(entry->ip.family == e1.family,
|
||||
"entry 1 has wrong family!");
|
||||
fail_unless(entry->ip.family == e1.family, "entry 1 has wrong family!");
|
||||
}
|
||||
END_TEST
|
||||
|
||||
@@ -53,6 +52,7 @@ START_TEST(test_destroy_doesnt_crash)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_includes_single_address )
|
||||
{
|
||||
char *lines[] = {"127.0.0.1"};
|
||||
@@ -109,13 +109,12 @@ START_TEST(test_includes_single_address_when_multiple_entries_exist)
|
||||
parse_ip_to_sockaddr( &e0.generic, "127.0.0.1" );
|
||||
parse_ip_to_sockaddr( &e1.generic, "::1" );
|
||||
|
||||
fail_unless(acl_includes(acl, &e0),
|
||||
"Included address 0 wasn't covered");
|
||||
fail_unless(acl_includes(acl, &e1),
|
||||
"Included address 1 wasn't covered");
|
||||
fail_unless( acl_includes( acl, &e0 ), "Included address 0 wasn't covered" );
|
||||
fail_unless( acl_includes( acl, &e1 ), "Included address 1 wasn't covered" );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_doesnt_include_other_address )
|
||||
{
|
||||
char *lines[] = {"127.0.0.1"};
|
||||
@@ -164,6 +163,7 @@ START_TEST(test_default_deny_rejects)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_default_accept_rejects )
|
||||
{
|
||||
struct acl * acl = acl_create( 0, NULL, 0 );
|
||||
@@ -175,6 +175,7 @@ START_TEST(test_default_accept_rejects)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite* acl_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("acl");
|
||||
@@ -188,19 +189,14 @@ Suite * acl_suite(void)
|
||||
tcase_add_test(tc_includes, test_parses_multiple_lines);
|
||||
|
||||
tcase_add_test(tc_includes, test_includes_single_address);
|
||||
tcase_add_test(tc_includes,
|
||||
test_includes_single_address_when_netmask_specified_ipv4);
|
||||
tcase_add_test(tc_includes,
|
||||
test_includes_single_address_when_netmask_specified_ipv6);
|
||||
tcase_add_test(tc_includes, test_includes_single_address_when_netmask_specified_ipv4);
|
||||
tcase_add_test(tc_includes, test_includes_single_address_when_netmask_specified_ipv6);
|
||||
|
||||
tcase_add_test(tc_includes,
|
||||
test_includes_single_address_when_multiple_entries_exist);
|
||||
tcase_add_test(tc_includes, test_includes_single_address_when_multiple_entries_exist);
|
||||
|
||||
tcase_add_test(tc_includes, test_doesnt_include_other_address);
|
||||
tcase_add_test(tc_includes,
|
||||
test_doesnt_include_other_address_when_netmask_specified);
|
||||
tcase_add_test(tc_includes,
|
||||
test_doesnt_include_other_address_when_multiple_entries_exist);
|
||||
tcase_add_test(tc_includes, test_doesnt_include_other_address_when_netmask_specified);
|
||||
tcase_add_test(tc_includes, test_doesnt_include_other_address_when_multiple_entries_exist);
|
||||
|
||||
tcase_add_test(tc_includes, test_default_deny_rejects);
|
||||
tcase_add_test(tc_includes, test_default_accept_rejects);
|
||||
@@ -230,3 +226,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -66,18 +66,18 @@ START_TEST(test_bit_ranges)
|
||||
|
||||
for (i=0; i<64; i++) {
|
||||
bit_set_range(buffer, i*64, i);
|
||||
fail_unless(longs[i] == (1ULL << i) - 1,
|
||||
fail_unless(
|
||||
longs[i] == (1ULL<<i)-1,
|
||||
"longs[%ld] = %lx SHOULD BE %lx",
|
||||
i, longs[i], (1ULL << i) - 1);
|
||||
i, longs[i], (1ULL<<i)-1
|
||||
);
|
||||
|
||||
fail_unless(longs[i + 1] == 0, "bit_set_range overshot at i=%d",
|
||||
i);
|
||||
fail_unless(longs[i+1] == 0, "bit_set_range overshot at i=%d", i);
|
||||
}
|
||||
|
||||
for (i=0; i<64; i++) {
|
||||
bit_clear_range(buffer, i*64, i);
|
||||
fail_unless(longs[i] == 0, "bit_clear_range didn't work at i=%d",
|
||||
i);
|
||||
fail_unless(longs[i] == 0, "bit_clear_range didn't work at i=%d", i);
|
||||
}
|
||||
}
|
||||
END_TEST
|
||||
@@ -86,8 +86,7 @@ START_TEST(test_bit_runs)
|
||||
{
|
||||
bitfield_word_t buffer[BIT_WORDS_FOR_SIZE(256)];
|
||||
int i, ptr=0, runs[] = {
|
||||
56, 97, 22, 12, 83, 1, 45, 80, 85, 51, 64, 40, 63, 67, 75, 64, 94,
|
||||
81, 79, 62
|
||||
56,97,22,12,83,1,45,80,85,51,64,40,63,67,75,64,94,81,79,62
|
||||
};
|
||||
|
||||
memset(buffer,0,256);
|
||||
@@ -102,8 +101,11 @@ START_TEST(test_bit_runs)
|
||||
|
||||
for (i=0; i < 20; i += 1) {
|
||||
int run = bit_run_count(buffer, ptr, 2048-ptr, NULL);
|
||||
fail_unless(run == runs[i],
|
||||
"run %d should have been %d, was %d", i, runs[i], run);
|
||||
fail_unless(
|
||||
run == runs[i],
|
||||
"run %d should have been %d, was %d",
|
||||
i, runs[i], run
|
||||
);
|
||||
ptr += runs[i];
|
||||
}
|
||||
}
|
||||
@@ -143,6 +145,7 @@ START_TEST(test_bitset)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_bitset_set )
|
||||
{
|
||||
struct bitset * map;
|
||||
@@ -173,6 +176,7 @@ START_TEST(test_bitset_set)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_bitset_clear )
|
||||
{
|
||||
struct bitset * map;
|
||||
@@ -439,14 +443,10 @@ START_TEST(test_bitset_stream_queued_bytes)
|
||||
bitset_clear_range( map, 0, 2 );
|
||||
bitset_disable_stream( map );
|
||||
|
||||
ck_assert_int_eq(64,
|
||||
bitset_stream_queued_bytes(map, BITSET_STREAM_ON));
|
||||
ck_assert_int_eq(80,
|
||||
bitset_stream_queued_bytes(map, BITSET_STREAM_SET));
|
||||
ck_assert_int_eq(82,
|
||||
bitset_stream_queued_bytes(map, BITSET_STREAM_UNSET));
|
||||
ck_assert_int_eq(64,
|
||||
bitset_stream_queued_bytes(map, BITSET_STREAM_OFF));
|
||||
ck_assert_int_eq( 64, bitset_stream_queued_bytes( map, BITSET_STREAM_ON ) );
|
||||
ck_assert_int_eq( 80, bitset_stream_queued_bytes( map, BITSET_STREAM_SET ) );
|
||||
ck_assert_int_eq( 82, bitset_stream_queued_bytes( map, BITSET_STREAM_UNSET ) );
|
||||
ck_assert_int_eq( 64, bitset_stream_queued_bytes( map, BITSET_STREAM_OFF ) );
|
||||
bitset_free( map );
|
||||
}
|
||||
END_TEST
|
||||
@@ -471,8 +471,7 @@ Suite * bitset_suite(void)
|
||||
tcase_add_test(tc_bitset, test_bitset_set_range);
|
||||
tcase_add_test(tc_bitset, test_bitset_clear_range);
|
||||
tcase_add_test(tc_bitset, test_bitset_set_range_doesnt_push_to_stream);
|
||||
tcase_add_test(tc_bitset,
|
||||
test_bitset_clear_range_doesnt_push_to_stream);
|
||||
tcase_add_test(tc_bitset, test_bitset_clear_range_doesnt_push_to_stream);
|
||||
suite_add_tcase(s, tc_bitset);
|
||||
|
||||
|
||||
@@ -498,3 +497,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -10,7 +10,6 @@
|
||||
#include <unistd.h>
|
||||
|
||||
struct server fake_server = {0};
|
||||
|
||||
#define FAKE_SERVER &fake_server
|
||||
#define FAKE_SOCKET (42)
|
||||
|
||||
@@ -24,6 +23,7 @@ START_TEST(test_assigns_socket)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_assigns_server )
|
||||
{
|
||||
struct client * c;
|
||||
@@ -37,6 +37,7 @@ START_TEST(test_assigns_server)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_opens_stop_signal )
|
||||
{
|
||||
struct client *c = client_create( FAKE_SERVER, FAKE_SOCKET );
|
||||
@@ -49,6 +50,7 @@ START_TEST(test_opens_stop_signal)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
int fd_is_closed(int);
|
||||
|
||||
START_TEST( test_closes_stop_signal )
|
||||
@@ -64,6 +66,7 @@ START_TEST(test_closes_stop_signal)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_read_request_quits_on_stop_signal )
|
||||
{
|
||||
int fds[2];
|
||||
@@ -81,6 +84,7 @@ START_TEST(test_read_request_quits_on_stop_signal)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite *client_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("client");
|
||||
@@ -115,3 +119,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -15,6 +15,7 @@ START_TEST(test_assigns_sock_name)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite *control_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("control");
|
||||
@@ -38,3 +39,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -5,7 +5,8 @@
|
||||
|
||||
START_TEST( test_listening_assigns_sock )
|
||||
{
|
||||
struct flexnbd *flexnbd = flexnbd_create_listening("127.0.0.1",
|
||||
struct flexnbd * flexnbd = flexnbd_create_listening(
|
||||
"127.0.0.1",
|
||||
"4777",
|
||||
"fakefile",
|
||||
"fakesock",
|
||||
@@ -16,6 +17,7 @@ START_TEST(test_listening_assigns_sock)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite *flexnbd_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("flexnbd");
|
||||
@@ -39,3 +41,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -12,23 +12,22 @@ START_TEST(test_mutex_create)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_mutex_lock )
|
||||
{
|
||||
struct flexthread_mutex * ftm = flexthread_mutex_create();
|
||||
|
||||
fail_if(flexthread_mutex_held(ftm),
|
||||
"Flexthread_mutex is held before lock");
|
||||
fail_if( flexthread_mutex_held( ftm ), "Flexthread_mutex is held before lock" );
|
||||
flexthread_mutex_lock( ftm );
|
||||
fail_unless(flexthread_mutex_held(ftm),
|
||||
"Flexthread_mutex is not held inside lock");
|
||||
fail_unless( flexthread_mutex_held( ftm ), "Flexthread_mutex is not held inside lock" );
|
||||
flexthread_mutex_unlock( ftm );
|
||||
fail_if(flexthread_mutex_held(ftm),
|
||||
"Flexthread_mutex is held after unlock");
|
||||
fail_if( flexthread_mutex_held( ftm ), "Flexthread_mutex is held after unlock" );
|
||||
|
||||
flexthread_mutex_destroy( ftm );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite* flexthread_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("flexthread");
|
||||
@@ -60,3 +59,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -17,6 +17,7 @@ START_TEST(test_read_until_newline_returns_line_length_plus_null)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_read_until_newline_inserts_null )
|
||||
{
|
||||
int fds[2];
|
||||
@@ -33,6 +34,7 @@ START_TEST(test_read_until_newline_inserts_null)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_read_empty_line_inserts_null )
|
||||
{
|
||||
int fds[2];
|
||||
@@ -48,6 +50,7 @@ START_TEST(test_read_empty_line_inserts_null)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_read_eof_returns_err )
|
||||
{
|
||||
int fds[2];
|
||||
@@ -62,6 +65,7 @@ START_TEST(test_read_eof_returns_err)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_read_eof_fills_line )
|
||||
{
|
||||
int fds[2];
|
||||
@@ -78,6 +82,7 @@ START_TEST(test_read_eof_fills_line)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_read_lines_until_blankline )
|
||||
{
|
||||
char **lines = NULL;
|
||||
@@ -93,25 +98,21 @@ START_TEST(test_read_lines_until_blankline)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite *ioutil_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("ioutil");
|
||||
|
||||
TCase *tc_read_until_newline = tcase_create("read_until_newline");
|
||||
TCase *tc_read_lines_until_blankline =
|
||||
tcase_create("read_lines_until_blankline");
|
||||
TCase *tc_read_lines_until_blankline = tcase_create("read_lines_until_blankline");
|
||||
|
||||
tcase_add_test(tc_read_until_newline,
|
||||
test_read_until_newline_returns_line_length_plus_null);
|
||||
tcase_add_test(tc_read_until_newline,
|
||||
test_read_until_newline_inserts_null);
|
||||
tcase_add_test(tc_read_until_newline,
|
||||
test_read_empty_line_inserts_null);
|
||||
tcase_add_test(tc_read_until_newline, test_read_until_newline_returns_line_length_plus_null);
|
||||
tcase_add_test(tc_read_until_newline, test_read_until_newline_inserts_null);
|
||||
tcase_add_test(tc_read_until_newline, test_read_empty_line_inserts_null);
|
||||
tcase_add_test(tc_read_until_newline, test_read_eof_returns_err);
|
||||
tcase_add_test(tc_read_until_newline, test_read_eof_fills_line );
|
||||
|
||||
tcase_add_test(tc_read_lines_until_blankline,
|
||||
test_read_lines_until_blankline);
|
||||
tcase_add_test(tc_read_lines_until_blankline, test_read_lines_until_blankline );
|
||||
|
||||
suite_add_tcase(s, tc_read_until_newline);
|
||||
suite_add_tcase(s, tc_read_lines_until_blankline);
|
||||
@@ -130,3 +131,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -14,11 +14,12 @@ START_TEST(test_allocs_cvar)
|
||||
memset( &cond_zero, 'X', sizeof( cond_zero ) );
|
||||
fail_if( memcmp( &cond_zero, &mbox->filled_cond, sizeof( cond_zero ) ) == 0 ,
|
||||
"Condition variable not allocated" );
|
||||
fail_if(memcmp(&cond_zero, &mbox->emptied_cond, sizeof(cond_zero)) ==
|
||||
0, "Condition variable not allocated");
|
||||
fail_if( memcmp( &cond_zero, &mbox->emptied_cond, sizeof( cond_zero ) ) == 0 ,
|
||||
"Condition variable not allocated" );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_post_stores_value )
|
||||
{
|
||||
struct mbox * mbox = mbox_create();
|
||||
@@ -31,6 +32,7 @@ START_TEST(test_post_stores_value)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
void * mbox_receive_runner( void * mbox_uncast )
|
||||
{
|
||||
struct mbox * mbox = (struct mbox *)mbox_uncast;
|
||||
@@ -56,12 +58,14 @@ START_TEST(test_receive_blocks_until_post)
|
||||
mbox_post( mbox, deadbeef );
|
||||
fail_unless( 0 == pthread_join( receiver, &retval ),
|
||||
"Failed to join the receiver thread" );
|
||||
fail_unless(retval == deadbeef, "Return value was wrong");
|
||||
fail_unless( retval == deadbeef,
|
||||
"Return value was wrong" );
|
||||
|
||||
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite* mbox_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("mbox");
|
||||
@@ -97,3 +101,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -13,13 +13,12 @@ START_TEST(test_init_passwd)
|
||||
memset( init_raw.passwd, 0, 8 );
|
||||
nbd_h2r_init( &init, &init_raw );
|
||||
|
||||
fail_unless(memcmp(init.passwd, INIT_PASSWD, 8) == 0,
|
||||
"The password was not copied.");
|
||||
fail_unless(memcmp(init_raw.passwd, INIT_PASSWD, 8) == 0,
|
||||
"The password was not copied back.");
|
||||
fail_unless( memcmp( init.passwd, INIT_PASSWD, 8 ) == 0, "The password was not copied." );
|
||||
fail_unless( memcmp( init_raw.passwd, INIT_PASSWD, 8 ) == 0, "The password was not copied back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST(test_init_magic)
|
||||
{
|
||||
struct nbd_init_raw init_raw;
|
||||
@@ -31,11 +30,11 @@ START_TEST(test_init_magic)
|
||||
|
||||
init.magic = 67890;
|
||||
nbd_h2r_init( &init, &init_raw );
|
||||
fail_unless(htobe64(67890) == init_raw.magic,
|
||||
"Magic was not converted back.");
|
||||
fail_unless( htobe64( 67890 ) == init_raw.magic, "Magic was not converted back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST(test_init_size)
|
||||
{
|
||||
struct nbd_init_raw init_raw;
|
||||
@@ -47,11 +46,11 @@ START_TEST(test_init_size)
|
||||
|
||||
init.size = 67890;
|
||||
nbd_h2r_init( &init, &init_raw );
|
||||
fail_unless(htobe64(67890) == init_raw.size,
|
||||
"Size was not converted back.");
|
||||
fail_unless( htobe64( 67890 ) == init_raw.size, "Size was not converted back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST(test_request_magic )
|
||||
{
|
||||
struct nbd_request_raw request_raw;
|
||||
@@ -59,13 +58,11 @@ START_TEST(test_request_magic)
|
||||
|
||||
request_raw.magic = 12345;
|
||||
nbd_r2h_request( &request_raw, &request );
|
||||
fail_unless(be32toh(12345) == request.magic,
|
||||
"Magic was not converted.");
|
||||
fail_unless( be32toh( 12345 ) == request.magic, "Magic was not converted." );
|
||||
|
||||
request.magic = 67890;
|
||||
nbd_h2r_request( &request, &request_raw );
|
||||
fail_unless(htobe32(67890) == request_raw.magic,
|
||||
"Magic was not converted back.");
|
||||
fail_unless( htobe32( 67890 ) == request_raw.magic, "Magic was not converted back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
@@ -74,33 +71,17 @@ START_TEST(test_request_type)
|
||||
struct nbd_request_raw request_raw;
|
||||
struct nbd_request request;
|
||||
|
||||
request_raw.type = 123;
|
||||
request_raw.type = 12345;
|
||||
nbd_r2h_request( &request_raw, &request );
|
||||
fail_unless(be16toh(123) == request.type, "Type was not converted.");
|
||||
fail_unless( be32toh( 12345 ) == request.type, "Type was not converted." );
|
||||
|
||||
request.type = 234;
|
||||
request.type = 67890;
|
||||
nbd_h2r_request( &request, &request_raw );
|
||||
fail_unless(htobe16(234) == request_raw.type,
|
||||
"Type was not converted back.");
|
||||
fail_unless( htobe32( 67890 ) == request_raw.type, "Type was not converted back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_request_flags)
|
||||
{
|
||||
struct nbd_request_raw request_raw;
|
||||
struct nbd_request request;
|
||||
|
||||
request_raw.flags = 123;
|
||||
nbd_r2h_request(&request_raw, &request);
|
||||
fail_unless(be16toh(123) == request.flags,
|
||||
"Flags were not converted.");
|
||||
|
||||
request.flags = 234;
|
||||
nbd_h2r_request(&request, &request_raw);
|
||||
fail_unless(htobe16(234) == request_raw.flags,
|
||||
"Flags were not converted back.");
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_request_handle)
|
||||
{
|
||||
@@ -113,13 +94,13 @@ START_TEST(test_request_handle)
|
||||
request_raw.handle.w = 0;
|
||||
nbd_h2r_request( &request, &request_raw );
|
||||
|
||||
fail_unless(memcmp(request.handle.b, "MYHANDLE", 8) == 0,
|
||||
"The handle was not copied.");
|
||||
fail_unless(memcmp(request_raw.handle.b, "MYHANDLE", 8) == 0,
|
||||
"The handle was not copied back.");
|
||||
fail_unless( memcmp( request.handle.b, "MYHANDLE", 8 ) == 0, "The handle was not copied." );
|
||||
fail_unless( memcmp( request_raw.handle.b, "MYHANDLE", 8 ) == 0, "The handle was not copied back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
|
||||
START_TEST(test_request_from )
|
||||
{
|
||||
struct nbd_request_raw request_raw;
|
||||
@@ -131,11 +112,12 @@ START_TEST(test_request_from)
|
||||
|
||||
request.from = 67890;
|
||||
nbd_h2r_request( &request, &request_raw );
|
||||
fail_unless(htobe64(67890) == request_raw.from,
|
||||
"From was not converted back.");
|
||||
fail_unless( htobe64( 67890 ) == request_raw.from, "From was not converted back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
|
||||
START_TEST(test_request_len )
|
||||
{
|
||||
struct nbd_request_raw request_raw;
|
||||
@@ -147,11 +129,11 @@ START_TEST(test_request_len)
|
||||
|
||||
request.len = 67890;
|
||||
nbd_h2r_request( &request, &request_raw );
|
||||
fail_unless(htobe32(67890) == request_raw.len,
|
||||
"Type was not converted back.");
|
||||
fail_unless( htobe32( 67890 ) == request_raw.len, "Type was not converted back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST(test_reply_magic )
|
||||
{
|
||||
struct nbd_reply_raw reply_raw;
|
||||
@@ -163,11 +145,11 @@ START_TEST(test_reply_magic)
|
||||
|
||||
reply.magic = 67890;
|
||||
nbd_h2r_reply( &reply, &reply_raw );
|
||||
fail_unless(htobe32(67890) == reply_raw.magic,
|
||||
"Magic was not converted back.");
|
||||
fail_unless( htobe32( 67890 ) == reply_raw.magic, "Magic was not converted back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST(test_reply_error )
|
||||
{
|
||||
struct nbd_reply_raw reply_raw;
|
||||
@@ -179,8 +161,7 @@ START_TEST(test_reply_error)
|
||||
|
||||
reply.error = 67890;
|
||||
nbd_h2r_reply( &reply, &reply_raw );
|
||||
fail_unless(htobe32(67890) == reply_raw.error,
|
||||
"Error was not converted back.");
|
||||
fail_unless( htobe32( 67890 ) == reply_raw.error, "Error was not converted back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
@@ -195,13 +176,12 @@ START_TEST(test_reply_handle)
|
||||
reply_raw.handle.w = 0;
|
||||
nbd_h2r_reply( &reply, &reply_raw );
|
||||
|
||||
fail_unless(memcmp(reply.handle.b, "MYHANDLE", 8) == 0,
|
||||
"The handle was not copied.");
|
||||
fail_unless(memcmp(reply_raw.handle.b, "MYHANDLE", 8) == 0,
|
||||
"The handle was not copied back.");
|
||||
fail_unless( memcmp( reply.handle.b, "MYHANDLE", 8 ) == 0, "The handle was not copied." );
|
||||
fail_unless( memcmp( reply_raw.handle.b, "MYHANDLE", 8 ) == 0, "The handle was not copied back." );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_convert_from )
|
||||
{
|
||||
/* Check that we can correctly pull numbers out of an
|
||||
@@ -233,7 +213,6 @@ Suite * nbdtypes_suite(void)
|
||||
tcase_add_test( tc_init, test_init_size );
|
||||
tcase_add_test( tc_request, test_request_magic );
|
||||
tcase_add_test( tc_request, test_request_type );
|
||||
tcase_add_test(tc_request, test_request_flags);
|
||||
tcase_add_test( tc_request, test_request_handle );
|
||||
tcase_add_test( tc_request, test_request_from );
|
||||
tcase_add_test( tc_request, test_request_len );
|
||||
@@ -261,3 +240,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -13,6 +13,8 @@ START_TEST(test_can_parse_ip_address_twice)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
|
||||
Suite* parse_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("parse");
|
||||
@@ -42,3 +44,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -54,7 +54,8 @@ void *responder(void *respond_uncast)
|
||||
nbd_r2h_request( &request_raw, &resp->received);
|
||||
if (resp->do_fail){
|
||||
fd_write_reply( sock_fd, wrong_handle, 0 );
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
fd_write_reply( sock_fd, resp->received.handle.w, 0 );
|
||||
}
|
||||
write( sock_fd, "12345678", 8 );
|
||||
@@ -65,20 +66,17 @@ void *responder(void *respond_uncast)
|
||||
|
||||
struct respond * respond_create( int do_fail )
|
||||
{
|
||||
struct respond *respond =
|
||||
(struct respond *) calloc(1, sizeof(struct respond));
|
||||
struct respond * respond = (struct respond *)calloc( 1, sizeof( struct respond ) );
|
||||
socketpair( PF_UNIX, SOCK_STREAM, 0, respond->sock_fds );
|
||||
respond->do_fail = do_fail;
|
||||
|
||||
pthread_attr_init( &respond->thread_attr );
|
||||
pthread_create(&respond->thread_id, &respond->thread_attr, responder,
|
||||
respond);
|
||||
pthread_create( &respond->thread_id, &respond->thread_attr, responder, respond );
|
||||
|
||||
return respond;
|
||||
}
|
||||
|
||||
void respond_destroy(struct respond *respond)
|
||||
{
|
||||
void respond_destroy( struct respond * respond ){
|
||||
NULLCHECK( respond );
|
||||
|
||||
pthread_join( respond->thread_id, NULL );
|
||||
@@ -123,6 +121,7 @@ START_TEST(test_rejects_mismatched_handle)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_accepts_matched_handle )
|
||||
{
|
||||
struct respond * respond = respond_create( 0 );
|
||||
@@ -136,6 +135,7 @@ START_TEST(test_accepts_matched_handle)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_disconnect_doesnt_read_reply )
|
||||
{
|
||||
struct respond * respond = respond_create( 1 );
|
||||
@@ -146,6 +146,7 @@ START_TEST(test_disconnect_doesnt_read_reply)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite* readwrite_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("readwrite");
|
||||
@@ -161,8 +162,7 @@ Suite * readwrite_suite(void)
|
||||
* because we want to know that the sender won't even try to
|
||||
* read the response.
|
||||
*/
|
||||
tcase_add_exit_test(tc_disconnect, test_disconnect_doesnt_read_reply,
|
||||
0);
|
||||
tcase_add_exit_test( tc_disconnect, test_disconnect_doesnt_read_reply,0 );
|
||||
|
||||
suite_add_tcase(s, tc_transfer);
|
||||
suite_add_tcase(s, tc_disconnect);
|
||||
@@ -190,3 +190,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -24,6 +24,7 @@ START_TEST(test_opens_pipe)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
void * signal_thread( void * thing )
|
||||
{
|
||||
struct self_pipe *sig = (struct self_pipe *)thing;
|
||||
@@ -62,14 +63,14 @@ START_TEST(test_signals)
|
||||
}
|
||||
self_pipe_signal_clear( sig );
|
||||
|
||||
fail_unless(self_pipe_fd_isset(sig, &fds),
|
||||
"Signalled pipe was not FD_ISSET.");
|
||||
fail_unless( self_pipe_fd_isset( sig, &fds ), "Signalled pipe was not FD_ISSET." );
|
||||
pthread_join( signal_thread_id, NULL );
|
||||
|
||||
self_pipe_destroy( sig );
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_clear_returns_immediately )
|
||||
{
|
||||
struct self_pipe *sig;
|
||||
@@ -78,6 +79,7 @@ START_TEST(test_clear_returns_immediately)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_destroy_closes_read_pipe )
|
||||
{
|
||||
struct self_pipe* sig;
|
||||
@@ -114,6 +116,7 @@ START_TEST(test_destroy_closes_read_pipe)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_destroy_closes_write_pipe )
|
||||
{
|
||||
struct self_pipe * sig;
|
||||
@@ -124,8 +127,7 @@ START_TEST(test_destroy_closes_write_pipe)
|
||||
orig_write_fd = sig->write_fd;
|
||||
self_pipe_destroy( sig );
|
||||
|
||||
while ((write_len = write(orig_write_fd, "", 0)) == -1
|
||||
&& errno == EINTR);
|
||||
while ( ( write_len = write( orig_write_fd, "", 0 ) ) == -1 && errno == EINTR );
|
||||
|
||||
switch( write_len ) {
|
||||
case 0:
|
||||
@@ -156,6 +158,8 @@ START_TEST(test_destroy_closes_write_pipe)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
|
||||
Suite *self_pipe_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("self_pipe");
|
||||
@@ -191,3 +195,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -54,9 +54,7 @@ void setup(void)
|
||||
|
||||
void teardown( void )
|
||||
{
|
||||
if (dummy_file) {
|
||||
unlink(dummy_file);
|
||||
}
|
||||
if( dummy_file ){ unlink( dummy_file ); }
|
||||
free( dummy_file );
|
||||
dummy_file = NULL;
|
||||
}
|
||||
@@ -66,9 +64,7 @@ START_TEST(test_replaces_acl)
|
||||
{
|
||||
struct flexnbd flexnbd;
|
||||
flexnbd.signal_fd = -1;
|
||||
struct server *s =
|
||||
server_create(&flexnbd, "127.0.0.1", "0", dummy_file, 0, 0, NULL,
|
||||
1, 0, 1);
|
||||
struct server * s = server_create( &flexnbd, "127.0.0.1", "0", dummy_file, 0, 0, NULL, 1, 0, 1 );
|
||||
struct acl * new_acl = acl_create( 0, NULL, 0 );
|
||||
|
||||
server_replace_acl( s, new_acl );
|
||||
@@ -78,13 +74,12 @@ START_TEST(test_replaces_acl)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_signals_acl_updated )
|
||||
{
|
||||
struct flexnbd flexnbd;
|
||||
flexnbd.signal_fd = -1;
|
||||
struct server *s =
|
||||
server_create(&flexnbd, "127.0.0.1", "0", dummy_file, 0, 0, NULL,
|
||||
1, 0, 1);
|
||||
struct server * s = server_create( &flexnbd, "127.0.0.1", "0", dummy_file, 0, 0, NULL, 1, 0, 1 );
|
||||
struct acl * new_acl = acl_create( 0, NULL, 0 );
|
||||
|
||||
server_replace_acl( s, new_acl );
|
||||
@@ -95,6 +90,7 @@ START_TEST(test_signals_acl_updated)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
int connect_client( char *addr, int actual_port, char *source_addr )
|
||||
{
|
||||
int client_fd = -1;
|
||||
@@ -108,15 +104,12 @@ int connect_client(char *addr, int actual_port, char *source_addr)
|
||||
memset( &hint, '\0', sizeof( struct addrinfo ) );
|
||||
hint.ai_socktype = SOCK_STREAM;
|
||||
|
||||
myfail_if(getaddrinfo(addr, NULL, &hint, &ailist) != 0,
|
||||
"getaddrinfo failed.");
|
||||
myfail_if( getaddrinfo( addr, NULL, &hint, &ailist ) != 0, "getaddrinfo failed." );
|
||||
|
||||
int connected = 0;
|
||||
for( aip = ailist; aip; aip = aip->ai_next ) {
|
||||
((struct sockaddr_in *) aip->ai_addr)->sin_port =
|
||||
htons(actual_port);
|
||||
client_fd =
|
||||
socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol);
|
||||
((struct sockaddr_in *)aip->ai_addr)->sin_port = htons( actual_port );
|
||||
client_fd = socket( aip->ai_family, aip->ai_socktype, aip->ai_protocol );
|
||||
|
||||
if (source_addr) {
|
||||
struct sockaddr src;
|
||||
@@ -127,9 +120,7 @@ int connect_client(char *addr, int actual_port, char *source_addr)
|
||||
bind(client_fd, &src, sizeof(struct sockaddr_in6));
|
||||
}
|
||||
|
||||
if (client_fd == -1) {
|
||||
continue;
|
||||
}
|
||||
if( client_fd == -1) { continue; }
|
||||
if( connect( client_fd, aip->ai_addr, aip->ai_addrlen) == 0 ) {
|
||||
connected = 1;
|
||||
break;
|
||||
@@ -157,9 +148,7 @@ START_TEST(test_acl_update_closes_bad_client)
|
||||
*/
|
||||
struct flexnbd flexnbd;
|
||||
flexnbd.signal_fd = -1;
|
||||
struct server *s =
|
||||
server_create(&flexnbd, "127.0.0.7", "0", dummy_file, 0, 0, NULL,
|
||||
1, 0, 1);
|
||||
struct server * s = server_create( &flexnbd, "127.0.0.7", "0", dummy_file, 0, 0, NULL, 1, 0, 1 );
|
||||
struct acl * new_acl = acl_create( 0, NULL, 1 );
|
||||
struct client * c;
|
||||
struct client_tbl_entry * entry;
|
||||
@@ -198,14 +187,13 @@ START_TEST(test_acl_update_closes_bad_client)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_acl_update_leaves_good_client )
|
||||
{
|
||||
struct flexnbd flexnbd;
|
||||
flexnbd.signal_fd = -1;
|
||||
|
||||
struct server *s =
|
||||
server_create(&flexnbd, "127.0.0.7", "0", dummy_file, 0, 0, NULL,
|
||||
1, 0, 1);
|
||||
struct server * s = server_create( &flexnbd, "127.0.0.7", "0", dummy_file, 0, 0, NULL, 1, 0, 1 );
|
||||
|
||||
char *lines[] = {"127.0.0.1"};
|
||||
struct acl * new_acl = acl_create( 1, lines, 1 );
|
||||
@@ -242,6 +230,7 @@ START_TEST(test_acl_update_leaves_good_client)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite* serve_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("serve");
|
||||
@@ -252,10 +241,8 @@ Suite * serve_suite(void)
|
||||
tcase_add_test(tc_acl_update, test_replaces_acl);
|
||||
tcase_add_test(tc_acl_update, test_signals_acl_updated);
|
||||
|
||||
tcase_add_exit_test(tc_acl_update, test_acl_update_closes_bad_client,
|
||||
0);
|
||||
tcase_add_exit_test(tc_acl_update, test_acl_update_leaves_good_client,
|
||||
0);
|
||||
tcase_add_exit_test(tc_acl_update, test_acl_update_closes_bad_client, 0);
|
||||
tcase_add_exit_test(tc_acl_update, test_acl_update_leaves_good_client, 0);
|
||||
|
||||
suite_add_tcase(s, tc_acl_update);
|
||||
|
||||
@@ -274,3 +261,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -24,6 +24,7 @@ START_TEST(test_sockaddr_address_string_af_inet_converts_to_string)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_sockaddr_address_string_af_inet6_converts_to_string )
|
||||
{
|
||||
struct sockaddr_in6 v6_raw;
|
||||
@@ -89,17 +90,12 @@ Suite * sockutil_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("sockutil");
|
||||
|
||||
TCase *tc_sockaddr_address_string =
|
||||
tcase_create("sockaddr_address_string");
|
||||
TCase *tc_sockaddr_address_string = tcase_create("sockaddr_address_string");
|
||||
|
||||
tcase_add_test(tc_sockaddr_address_string,
|
||||
test_sockaddr_address_string_af_inet_converts_to_string);
|
||||
tcase_add_test(tc_sockaddr_address_string,
|
||||
test_sockaddr_address_string_af_inet6_converts_to_string);
|
||||
tcase_add_test(tc_sockaddr_address_string,
|
||||
test_sockaddr_address_string_af_unspec_is_failure);
|
||||
tcase_add_test(tc_sockaddr_address_string,
|
||||
test_sockaddr_address_string_doesnt_overflow_short_buffer);
|
||||
tcase_add_test(tc_sockaddr_address_string, test_sockaddr_address_string_af_inet_converts_to_string);
|
||||
tcase_add_test(tc_sockaddr_address_string, test_sockaddr_address_string_af_inet6_converts_to_string);
|
||||
tcase_add_test(tc_sockaddr_address_string, test_sockaddr_address_string_af_unspec_is_failure);
|
||||
tcase_add_test(tc_sockaddr_address_string, test_sockaddr_address_string_doesnt_overflow_short_buffer);
|
||||
suite_add_tcase(s, tc_sockaddr_address_string);
|
||||
|
||||
return s;
|
||||
@@ -116,3 +112,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
@@ -68,6 +68,7 @@ START_TEST(test_gets_has_control)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_gets_is_mirroring )
|
||||
{
|
||||
struct server * server = mock_server();
|
||||
@@ -104,6 +105,25 @@ START_TEST(test_gets_clients_allowed)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST( test_gets_num_clients )
|
||||
{
|
||||
struct server * server = mock_server();
|
||||
struct status * status = status_create( server );
|
||||
|
||||
fail_if( status->num_clients != 0, "num_clients was wrong" );
|
||||
status_destroy( status );
|
||||
|
||||
server->nbd_client[0].thread = 1;
|
||||
server->nbd_client[1].thread = 1;
|
||||
status = status_create( server );
|
||||
|
||||
fail_unless( status->num_clients == 2, "num_clients was wrong" );
|
||||
status_destroy( status );
|
||||
destroy_mock_server( server );
|
||||
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST( test_gets_pid )
|
||||
{
|
||||
struct server * server = mock_server();
|
||||
@@ -142,31 +162,34 @@ START_TEST(test_gets_migration_statistics)
|
||||
|
||||
struct status * status = status_create( server );
|
||||
|
||||
fail_unless(0 == status->migration_duration ||
|
||||
fail_unless (
|
||||
0 == status->migration_duration ||
|
||||
1 == status->migration_duration ||
|
||||
2 == status->migration_duration,
|
||||
"migration_duration is unreasonable!");
|
||||
"migration_duration is unreasonable!"
|
||||
);
|
||||
|
||||
fail_unless(16384 / (status->migration_duration + 1) ==
|
||||
status->migration_speed,
|
||||
"migration_speed not calculated correctly");
|
||||
fail_unless(
|
||||
16384 / ( status->migration_duration + 1 ) == status->migration_speed,
|
||||
"migration_speed not calculated correctly"
|
||||
);
|
||||
|
||||
fail_unless(32768 == status->migration_speed_limit,
|
||||
"migration_speed_limit not read");
|
||||
fail_unless( 32768 == status->migration_speed_limit, "migration_speed_limit not read" );
|
||||
|
||||
// ( size / current_bps ) + 1 happens to be 3 for this test
|
||||
fail_unless(3 == status->migration_seconds_left,
|
||||
"migration_seconds_left not gathered");
|
||||
fail_unless( 3 == status->migration_seconds_left, "migration_seconds_left not gathered" );
|
||||
|
||||
status_destroy( status );
|
||||
destroy_mock_server( server );
|
||||
}
|
||||
|
||||
END_TEST
|
||||
|
||||
|
||||
#define RENDER_TEST_SETUP \
|
||||
struct status status; \
|
||||
int fds[2]; \
|
||||
pipe( fds );
|
||||
|
||||
void fail_unless_rendered( int fd, char *fragment )
|
||||
{
|
||||
char buf[1024] = {0};
|
||||
@@ -199,7 +222,9 @@ void fail_if_rendered(int fd, char *fragment)
|
||||
|
||||
START_TEST( test_renders_has_control )
|
||||
{
|
||||
RENDER_TEST_SETUP status.has_control = 1;
|
||||
RENDER_TEST_SETUP
|
||||
|
||||
status.has_control = 1;
|
||||
status_write( &status, fds[1] );
|
||||
fail_unless_rendered( fds[0], "has_control=true" );
|
||||
|
||||
@@ -209,9 +234,12 @@ START_TEST(test_renders_has_control)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_renders_is_mirroring )
|
||||
{
|
||||
RENDER_TEST_SETUP status.is_mirroring = 1;
|
||||
RENDER_TEST_SETUP
|
||||
|
||||
status.is_mirroring = 1;
|
||||
status_write( &status, fds[1] );
|
||||
fail_unless_rendered( fds[0], "is_mirroring=true" );
|
||||
|
||||
@@ -223,7 +251,9 @@ END_TEST
|
||||
|
||||
START_TEST( test_renders_clients_allowed )
|
||||
{
|
||||
RENDER_TEST_SETUP status.clients_allowed = 1;
|
||||
RENDER_TEST_SETUP
|
||||
|
||||
status.clients_allowed = 1;
|
||||
status_write( &status, fds[1] );
|
||||
fail_unless_rendered( fds[0], "clients_allowed=true" );
|
||||
|
||||
@@ -235,7 +265,9 @@ END_TEST
|
||||
|
||||
START_TEST( test_renders_num_clients )
|
||||
{
|
||||
RENDER_TEST_SETUP status.num_clients = 0;
|
||||
RENDER_TEST_SETUP
|
||||
|
||||
status.num_clients = 0;
|
||||
status_write( &status, fds[1] );
|
||||
fail_unless_rendered( fds[0], "num_clients=0" );
|
||||
|
||||
@@ -246,9 +278,12 @@ START_TEST(test_renders_num_clients)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST( test_renders_pid )
|
||||
{
|
||||
RENDER_TEST_SETUP status.pid = 42;
|
||||
RENDER_TEST_SETUP
|
||||
|
||||
status.pid = 42;
|
||||
status_write( &status, fds[1] );
|
||||
fail_unless_rendered( fds[0], "pid=42" );
|
||||
}
|
||||
@@ -256,7 +291,9 @@ END_TEST
|
||||
|
||||
START_TEST( test_renders_size )
|
||||
{
|
||||
RENDER_TEST_SETUP status.size = ((uint64_t) 1 << 33);
|
||||
RENDER_TEST_SETUP
|
||||
|
||||
status.size = ( (uint64_t)1 << 33 );
|
||||
status_write( &status, fds[1] );
|
||||
fail_unless_rendered( fds[0], "size=8589934592" );
|
||||
}
|
||||
@@ -264,7 +301,9 @@ END_TEST
|
||||
|
||||
START_TEST( test_renders_migration_statistics )
|
||||
{
|
||||
RENDER_TEST_SETUP status.is_mirroring = 0;
|
||||
RENDER_TEST_SETUP
|
||||
|
||||
status.is_mirroring = 0;
|
||||
status.migration_duration = 8;
|
||||
status.migration_speed = 40000000;
|
||||
status.migration_speed_limit = 40000001;
|
||||
@@ -307,6 +346,7 @@ START_TEST(test_renders_migration_statistics)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite *status_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("status");
|
||||
@@ -317,6 +357,7 @@ Suite * status_suite(void)
|
||||
tcase_add_test(tc_create, test_gets_has_control);
|
||||
tcase_add_test(tc_create, test_gets_is_mirroring);
|
||||
tcase_add_test(tc_create, test_gets_clients_allowed);
|
||||
tcase_add_test(tc_create, test_gets_num_clients);
|
||||
tcase_add_test(tc_create, test_gets_pid);
|
||||
tcase_add_test(tc_create, test_gets_size);
|
||||
tcase_add_test(tc_create, test_gets_migration_statistics);
|
||||
@@ -347,3 +388,4 @@ int main(void)
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user