[sheepdog] [QEMU PATCH] sheepdog: add support for connecting to unix domain socket
MORITA Kazutaka
morita.kazutaka at lab.ntt.co.jp
Fri Nov 2 14:25:49 CET 2012
This patch adds support for a unix domain socket for a connection
between qemu and local sheepdog server. You can use the unix domain
socket with the following syntax like NBD driver:
$ qemu sheepdog:unix:<socket path>:<image name>
Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
This is a patch for QEMU to use a unix domain socket. If my sheep
patch is okay, I'll send this to qemu mailing list.
block/sheepdog.c | 142 +++++++++++++++++++++---------------------------------
qemu-options.hx | 18 +++----
2 files changed, 63 insertions(+), 97 deletions(-)
diff --git a/block/sheepdog.c b/block/sheepdog.c
index 9306174..61be9b1 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -20,8 +20,7 @@
#define SD_PROTO_VER 0x01
-#define SD_DEFAULT_ADDR "localhost"
-#define SD_DEFAULT_PORT "7000"
+#define SD_DEFAULT_ADDR_AND_PORT "localhost:7000"
#define SD_OP_CREATE_AND_WRITE_OBJ 0x01
#define SD_OP_READ_OBJ 0x02
@@ -295,8 +294,11 @@ typedef struct BDRVSheepdogState {
bool is_snapshot;
bool cache_enabled;
- char *addr;
- char *port;
+ /* If it begins with 'unix:/', this is a UNIX domain socket. Otherwise,
+ * it's a string of the form <hostname>:<port>
+ */
+ char *host_spec;
+
int fd;
int flush_fd;
@@ -446,56 +448,36 @@ static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov,
return acb;
}
-static int connect_to_sdog(const char *addr, const char *port)
-{
- char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
- int fd, ret;
- struct addrinfo hints, *res, *res0;
-
- if (!addr) {
- addr = SD_DEFAULT_ADDR;
- port = SD_DEFAULT_PORT;
- }
+static int set_nodelay(int fd);
- memset(&hints, 0, sizeof(hints));
- hints.ai_socktype = SOCK_STREAM;
+static int connect_to_sdog(const char *host_spec)
+{
+ int fd;
+ const char *path;
+ Error *err = NULL;
- ret = getaddrinfo(addr, port, &hints, &res0);
- if (ret) {
- error_report("unable to get address info %s, %s",
- addr, strerror(errno));
- return -errno;
+ if (host_spec == NULL) {
+ host_spec = SD_DEFAULT_ADDR_AND_PORT;
}
- for (res = res0; res; res = res->ai_next) {
- ret = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
- sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
- if (ret) {
- continue;
- }
-
- fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- if (fd < 0) {
- continue;
- }
+ if (strstart(host_spec, "unix:", &path) && path[0] == '/') {
+ fd = unix_connect(path, &err);
+ } else {
+ fd = inet_connect(host_spec, &err);
- reconnect:
- ret = connect(fd, res->ai_addr, res->ai_addrlen);
- if (ret < 0) {
- if (errno == EINTR) {
- goto reconnect;
+ if (err == NULL) {
+ int ret = set_nodelay(fd);
+ if (ret < 0) {
+ error_report("%s", strerror(errno));
}
- close(fd);
- break;
}
+ }
- dprintf("connected to %s:%s\n", addr, port);
- goto success;
+ if (err != NULL) {
+ qerror_report_err(err);
+ error_free(err);
}
- fd = -errno;
- error_report("failed connect to %s:%s", addr, port);
-success:
- freeaddrinfo(res0);
+
return fd;
}
@@ -796,23 +778,14 @@ static int set_nodelay(int fd)
*/
static int get_sheep_fd(BDRVSheepdogState *s)
{
- int ret, fd;
+ int fd;
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
error_report("%s", strerror(errno));
return fd;
}
- socket_set_nonblock(fd);
-
- ret = set_nodelay(fd);
- if (ret) {
- error_report("%s", strerror(errno));
- closesocket(fd);
- return -errno;
- }
-
qemu_aio_set_fd_handler(fd, co_read_response, NULL, aio_flush_request, s);
return fd;
}
@@ -821,12 +794,10 @@ static int get_sheep_fd(BDRVSheepdogState *s)
* Parse a filename
*
* filename must be one of the following formats:
- * 1. [vdiname]
- * 2. [vdiname]:[snapid]
- * 3. [vdiname]:[tag]
- * 4. [hostname]:[port]:[vdiname]
- * 5. [hostname]:[port]:[vdiname]:[snapid]
- * 6. [hostname]:[port]:[vdiname]:[tag]
+ * - using TCP
+ * [<hostname>:<port>:]<vdiname>[:<snapid or tag>]
+ * - using Unix Domain Socket
+ * unix:<domain-socket>:<vdiname>[:<snapid or tag>]
*
* You can boot from the snapshot images by specifying `snapid` or
* `tag'.
@@ -852,18 +823,15 @@ static int parse_vdiname(BDRVSheepdogState *s, const char *filename,
}
p = q;
- /* use the first two tokens as hostname and port number. */
+ /* use the first two tokens as host_spec. */
if (nr_sep >= 2) {
- s->addr = p;
+ s->host_spec = p;
p = strchr(p, ':');
- *p++ = '\0';
-
- s->port = p;
+ p++;
p = strchr(p, ':');
*p++ = '\0';
} else {
- s->addr = NULL;
- s->port = 0;
+ s->host_spec = NULL;
}
pstrcpy(vdi, SD_MAX_VDI_LEN, p);
@@ -879,7 +847,7 @@ static int parse_vdiname(BDRVSheepdogState *s, const char *filename,
*snapid = CURRENT_VDI_ID; /* search current vdi */
}
- if (s->addr == NULL) {
+ if (s->host_spec == NULL) {
g_free(q);
}
@@ -895,7 +863,7 @@ static int find_vdi_name(BDRVSheepdogState *s, char *filename, uint32_t snapid,
unsigned int wlen, rlen = 0;
char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
return fd;
}
@@ -1118,7 +1086,7 @@ static int sd_open(BlockDriverState *bs, const char *filename, int flags)
}
s->cache_enabled = true;
- s->flush_fd = connect_to_sdog(s->addr, s->port);
+ s->flush_fd = connect_to_sdog(s->host_spec);
if (s->flush_fd < 0) {
error_report("failed to connect");
ret = s->flush_fd;
@@ -1130,7 +1098,7 @@ static int sd_open(BlockDriverState *bs, const char *filename, int flags)
s->is_snapshot = true;
}
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
error_report("failed to connect");
ret = fd;
@@ -1167,7 +1135,7 @@ out:
static int do_sd_create(char *filename, int64_t vdi_size,
uint32_t base_vid, uint32_t *vdi_id, int snapshot,
- const char *addr, const char *port)
+ const char *host_spec)
{
SheepdogVdiReq hdr;
SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
@@ -1175,7 +1143,7 @@ static int do_sd_create(char *filename, int64_t vdi_size,
unsigned int wlen, rlen = 0;
char buf[SD_MAX_VDI_LEN];
- fd = connect_to_sdog(addr, port);
+ fd = connect_to_sdog(host_spec);
if (fd < 0) {
return fd;
}
@@ -1342,7 +1310,7 @@ static int sd_create(const char *filename, QEMUOptionParameter *options)
bdrv_delete(bs);
}
- ret = do_sd_create(vdi, vdi_size, base_vid, &vid, 0, s->addr, s->port);
+ ret = do_sd_create(vdi, vdi_size, base_vid, &vid, 0, s->host_spec);
if (!prealloc || ret) {
goto out;
}
@@ -1363,7 +1331,7 @@ static void sd_close(BlockDriverState *bs)
dprintf("%s\n", s->name);
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
return;
}
@@ -1389,7 +1357,7 @@ static void sd_close(BlockDriverState *bs)
if (s->cache_enabled) {
closesocket(s->flush_fd);
}
- g_free(s->addr);
+ g_free(s->host_spec);
}
static int64_t sd_getlength(BlockDriverState *bs)
@@ -1413,7 +1381,7 @@ static int sd_truncate(BlockDriverState *bs, int64_t offset)
return -EINVAL;
}
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
return fd;
}
@@ -1490,14 +1458,14 @@ static int sd_create_branch(BDRVSheepdogState *s)
buf = g_malloc(SD_INODE_SIZE);
ret = do_sd_create(s->name, s->inode.vdi_size, s->inode.vdi_id, &vid, 1,
- s->addr, s->port);
+ s->host_spec);
if (ret) {
goto out;
}
dprintf("%" PRIx32 " is created.\n", vid);
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
error_report("failed to connect");
ret = fd;
@@ -1766,7 +1734,7 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
/* refresh inode. */
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
ret = fd;
goto cleanup;
@@ -1780,7 +1748,7 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
}
ret = do_sd_create(s->name, s->inode.vdi_size, s->inode.vdi_id, &new_vid, 1,
- s->addr, s->port);
+ s->host_spec);
if (ret < 0) {
error_report("failed to create inode for snapshot. %s",
strerror(errno));
@@ -1835,7 +1803,7 @@ static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
goto out;
}
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
error_report("failed to connect");
ret = fd;
@@ -1899,7 +1867,7 @@ static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
vdi_inuse = g_malloc(max);
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
ret = fd;
goto out;
@@ -1926,7 +1894,7 @@ static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT);
start_nr = hval & (SD_NR_VDIS - 1);
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
error_report("failed to connect");
ret = fd;
@@ -1985,7 +1953,7 @@ static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data,
uint32_t vdi_index;
uint64_t offset;
- fd = connect_to_sdog(s->addr, s->port);
+ fd = connect_to_sdog(s->host_spec);
if (fd < 0) {
return fd;
}
diff --git a/qemu-options.hx b/qemu-options.hx
index 6a37f54..b9c85eb 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -2034,17 +2034,15 @@ devices.
Syntax for specifying a sheepdog device
@table @list
-``sheepdog:<vdiname>''
-
-``sheepdog:<vdiname>:<snapid>''
-
-``sheepdog:<vdiname>:<tag>''
-
-``sheepdog:<host>:<port>:<vdiname>''
-
-``sheepdog:<host>:<port>:<vdiname>:<snapid>''
+using TCP:
+ at example
+sheepdog:[<hostname>:<port>:]<vdiname>[:<snapid or tag>]
+ at end example
-``sheepdog:<host>:<port>:<vdiname>:<tag>''
+using Unix Domain Socket:
+ at example
+sheepdog:unix:<domain-socket>:<vdiname>[:<snapid or tag>]
+ at end example
@end table
Example
--
1.7.2.5
More information about the sheepdog
mailing list