2012-05-17 20:14:22 +01:00
|
|
|
#include "nbdtypes.h"
|
|
|
|
#include "ioutil.h"
|
2013-02-14 16:24:10 +00:00
|
|
|
#include "sockutil.h"
|
2012-05-17 20:14:22 +01:00
|
|
|
#include "util.h"
|
2013-02-14 16:24:10 +00:00
|
|
|
#include "serve.h"
|
2012-05-17 20:14:22 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
int socket_connect(struct sockaddr *to, struct sockaddr *from)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != from) {
|
|
|
|
if (0 > bind(fd, from, sizeof(struct sockaddr_in6))) {
|
|
|
|
warn(SHOW_ERRNO("bind() to source address failed"));
|
|
|
|
if (0 > close(fd)) { /* Non-fatal leak */
|
|
|
|
warn(SHOW_ERRNO("Failed to close fd %i", fd));
|
|
|
|
}
|
|
|
|
return -1;
|
2012-06-22 10:05:41 +01:00
|
|
|
}
|
2018-02-20 10:05:35 +00:00
|
|
|
}
|
2012-06-06 09:55:08 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
if (0 > sock_try_connect(fd, to, sizeof(struct sockaddr_in6), 15)) {
|
|
|
|
warn(SHOW_ERRNO("connect failed"));
|
|
|
|
if (0 > close(fd)) { /* Non-fatal leak */
|
|
|
|
warn(SHOW_ERRNO("Failed to close fd %i", fd));
|
2012-06-22 10:05:41 +01:00
|
|
|
}
|
2018-02-20 10:05:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-06-22 10:05:41 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
if (sock_set_tcp_nodelay(fd, 1) == -1) {
|
|
|
|
warn(SHOW_ERRNO("Failed to set TCP_NODELAY"));
|
|
|
|
}
|
2013-04-15 15:13:44 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
return fd;
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
int nbd_check_hello(struct nbd_init_raw *init_raw, uint64_t * out_size,
|
|
|
|
uint32_t * out_flags)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
if (strncmp(init_raw->passwd, INIT_PASSWD, 8) != 0) {
|
|
|
|
warn("wrong passwd");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (be64toh(init_raw->magic) != INIT_MAGIC) {
|
|
|
|
warn("wrong magic (%x)", be64toh(init_raw->magic));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != out_size) {
|
|
|
|
*out_size = be64toh(init_raw->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != out_flags) {
|
|
|
|
*out_flags = be32toh(init_raw->flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
fail:
|
|
|
|
return 0;
|
2013-06-18 15:36:15 +01:00
|
|
|
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
int socket_nbd_read_hello(int fd, uint64_t * out_size,
|
|
|
|
uint32_t * out_flags)
|
2013-02-08 15:53:27 +00:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
struct nbd_init_raw init_raw;
|
2013-02-08 15:53:27 +00:00
|
|
|
|
2013-06-18 15:36:15 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
if (0 > readloop(fd, &init_raw, sizeof(init_raw))) {
|
|
|
|
warn("Couldn't read init");
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-18 15:36:15 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
return nbd_check_hello(&init_raw, out_size, out_flags);
|
2013-06-18 15:36:15 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
void nbd_hello_to_buf(struct nbd_init_raw *buf, off64_t out_size,
|
|
|
|
uint32_t out_flags)
|
2013-06-18 15:36:15 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
struct nbd_init init;
|
2013-06-18 15:36:15 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
memcpy(&init.passwd, INIT_PASSWD, 8);
|
|
|
|
init.magic = INIT_MAGIC;
|
|
|
|
init.size = out_size;
|
|
|
|
init.flags = out_flags;
|
2013-02-08 15:53:27 +00:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
memset(buf, 0, sizeof(struct nbd_init_raw)); // ensure reserved is 0s
|
|
|
|
nbd_h2r_init(&init, buf);
|
2013-06-18 15:36:15 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
return;
|
2013-06-18 15:36:15 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
int socket_nbd_write_hello(int fd, off64_t out_size, uint32_t out_flags)
|
2013-06-18 15:36:15 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
struct nbd_init_raw init_raw;
|
|
|
|
nbd_hello_to_buf(&init_raw, out_size, out_flags);
|
2013-02-08 15:53:27 +00:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
if (0 > writeloop(fd, &init_raw, sizeof(init_raw))) {
|
|
|
|
warn(SHOW_ERRNO("failed to write hello to socket"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2013-02-08 15:53:27 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
void fill_request(struct nbd_request_raw *request_raw, uint16_t type,
|
|
|
|
uint16_t flags, uint64_t from, uint32_t len)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
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);
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
void read_reply(int fd, uint64_t request_raw_handle,
|
|
|
|
struct nbd_reply *reply)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
struct nbd_reply_raw reply_raw;
|
|
|
|
|
|
|
|
ERROR_IF_NEGATIVE(readloop
|
|
|
|
(fd, &reply_raw, sizeof(struct nbd_reply_raw)),
|
|
|
|
"Couldn't read reply");
|
|
|
|
|
|
|
|
nbd_r2h_reply(&reply_raw, reply);
|
|
|
|
|
|
|
|
if (reply->magic != REPLY_MAGIC) {
|
|
|
|
error("Reply magic incorrect (%x)", reply->magic);
|
|
|
|
}
|
|
|
|
if (reply->error != 0) {
|
|
|
|
error("Server replied with error %d", reply->error);
|
|
|
|
}
|
|
|
|
if (request_raw_handle != reply_raw.handle.w) {
|
|
|
|
error("Did not reply with correct handle");
|
|
|
|
}
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
void wait_for_data(int fd, int timeout_secs)
|
2012-06-28 14:45:53 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
fd_set fds;
|
|
|
|
struct timeval tv = { timeout_secs, 0 };
|
|
|
|
int selected;
|
2012-06-28 14:45:53 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(fd, &fds);
|
2013-02-14 16:24:10 +00:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
selected =
|
|
|
|
sock_try_select(FD_SETSIZE, &fds, NULL, NULL,
|
|
|
|
timeout_secs >= 0 ? &tv : NULL);
|
2012-06-28 14:45:53 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
FATAL_IF(-1 == selected, "Select failed");
|
|
|
|
ERROR_IF(0 == selected, "Timed out waiting for reply");
|
2012-06-28 14:45:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd,
|
|
|
|
void *out_buf, int timeout_secs)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
struct nbd_request_raw request_raw;
|
|
|
|
struct nbd_reply reply;
|
2013-02-14 16:24:10 +00:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
fill_request(&request_raw, REQUEST_READ, 0, from, len);
|
|
|
|
FATAL_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
|
|
|
|
"Couldn't write request");
|
2012-06-28 14:45:53 +01:00
|
|
|
|
2018-02-08 15:46:34 +00:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
wait_for_data(fd, timeout_secs);
|
|
|
|
read_reply(fd, request_raw.handle.w, &reply);
|
2013-02-14 16:24:10 +00:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
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");
|
|
|
|
}
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd,
|
|
|
|
void *in_buf, int timeout_secs)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
struct nbd_request_raw request_raw;
|
|
|
|
struct nbd_reply reply;
|
|
|
|
|
|
|
|
fill_request(&request_raw, REQUEST_WRITE, 0, from, len);
|
|
|
|
ERROR_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
|
|
|
|
"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");
|
|
|
|
}
|
|
|
|
|
|
|
|
wait_for_data(fd, timeout_secs);
|
|
|
|
read_reply(fd, request_raw.handle.w, &reply);
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
2012-06-21 17:12:06 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
int socket_nbd_disconnect(int fd)
|
2012-06-21 17:12:06 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
int success = 1;
|
|
|
|
struct nbd_request_raw request_raw;
|
|
|
|
|
|
|
|
fill_request(&request_raw, REQUEST_DISCONNECT, 0, 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)),
|
|
|
|
"Failed to write the disconnect request.");
|
|
|
|
return success;
|
2012-06-21 17:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-05-17 20:14:22 +01:00
|
|
|
#define CHECK_RANGE(error_type) { \
|
2014-02-27 16:04:25 +00:00
|
|
|
uint64_t size;\
|
2018-02-02 10:30:40 +00:00
|
|
|
uint32_t flags;\
|
|
|
|
int success = socket_nbd_read_hello(params->client, &size, &flags); \
|
2012-06-22 10:05:41 +01:00
|
|
|
if ( success ) {\
|
2014-02-27 16:04:25 +00:00
|
|
|
uint64_t endpoint = params->from + params->len; \
|
|
|
|
if (endpoint > size || \
|
|
|
|
endpoint < params->from ) { /* this happens on overflow */ \
|
2012-06-22 10:05:41 +01:00
|
|
|
fatal(error_type \
|
|
|
|
" request %d+%d is out of range given size %d", \
|
|
|
|
params->from, params->len, size\
|
|
|
|
);\
|
|
|
|
}\
|
|
|
|
}\
|
|
|
|
else {\
|
|
|
|
fatal( error_type " connection failed." );\
|
|
|
|
}\
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
2013-02-14 16:24:10 +00:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
void do_read(struct mode_readwrite_params *params)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
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,
|
|
|
|
params->data_fd, NULL, 10);
|
|
|
|
close(params->client);
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
void do_write(struct mode_readwrite_params *params)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2018-02-20 10:05:35 +00:00
|
|
|
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);
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|