[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 18/27] sheepdog: Use SocketAddress and socket_connect
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PULL 18/27] sheepdog: Use SocketAddress and socket_connect() |
Date: |
Tue, 7 Mar 2017 16:40:42 +0100 |
From: Markus Armbruster <address@hidden>
sd_parse_uri() builds a string from host and port parts for
inet_connect(). inet_connect() parses it into host, port and options.
Whether this gets exactly the same host, port and no options for all
inputs is not obvious.
Cut out the string middleman and build a SocketAddress for
socket_connect() instead.
Signed-off-by: Markus Armbruster <address@hidden>
Reviewed-by: Kevin Wolf <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block/sheepdog.c | 53 ++++++++++++++++++++++++++++++-----------------------
1 file changed, 30 insertions(+), 23 deletions(-)
diff --git a/block/sheepdog.c b/block/sheepdog.c
index 161932d..9b1e121 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -374,8 +374,7 @@ struct BDRVSheepdogState {
uint32_t cache_flags;
bool discard_supported;
- char *host_spec;
- bool is_unix;
+ SocketAddress *addr;
int fd;
CoMutex lock;
@@ -532,16 +531,12 @@ static int connect_to_sdog(BDRVSheepdogState *s, Error
**errp)
{
int fd;
- if (s->is_unix) {
- fd = unix_connect(s->host_spec, errp);
- } else {
- fd = inet_connect(s->host_spec, errp);
+ fd = socket_connect(s->addr, errp, NULL, NULL);
- if (fd >= 0) {
- int ret = socket_set_nodelay(fd);
- if (ret < 0) {
- error_report("%s", strerror(errno));
- }
+ if (s->addr->type == SOCKET_ADDRESS_KIND_INET && fd >= 0) {
+ int ret = socket_set_nodelay(fd);
+ if (ret < 0) {
+ error_report("%s", strerror(errno));
}
}
@@ -820,8 +815,7 @@ static void coroutine_fn aio_read_response(void *opaque)
case AIOCB_DISCARD_OBJ:
switch (rsp.result) {
case SD_RES_INVALID_PARMS:
- error_report("sheep(%s) doesn't support discard command",
- s->host_spec);
+ error_report("server doesn't support discard command");
rsp.result = SD_RES_SUCCESS;
s->discard_supported = false;
break;
@@ -962,8 +956,10 @@ static void sd_parse_uri(BDRVSheepdogState *s, const char
*filename,
Error **errp)
{
Error *err = NULL;
- URI *uri;
QueryParams *qp = NULL;
+ SocketAddress *addr = NULL;
+ bool is_unix;
+ URI *uri;
uri = uri_parse(filename);
if (!uri) {
@@ -973,11 +969,11 @@ static void sd_parse_uri(BDRVSheepdogState *s, const char
*filename,
/* transport */
if (!strcmp(uri->scheme, "sheepdog")) {
- s->is_unix = false;
+ is_unix = false;
} else if (!strcmp(uri->scheme, "sheepdog+tcp")) {
- s->is_unix = false;
+ is_unix = false;
} else if (!strcmp(uri->scheme, "sheepdog+unix")) {
- s->is_unix = true;
+ is_unix = true;
} else {
error_setg(&err, "URI scheme must be 'sheepdog', 'sheepdog+tcp',"
" or 'sheepdog+unix'");
@@ -994,8 +990,9 @@ static void sd_parse_uri(BDRVSheepdogState *s, const char
*filename,
}
qp = query_params_parse(uri->query);
+ addr = g_new0(SocketAddress, 1);
- if (s->is_unix) {
+ if (is_unix) {
/* sheepdog+unix:///vdiname?socket=path */
if (uri->server || uri->port) {
error_setg(&err, "URI scheme %s doesn't accept a server address",
@@ -1012,15 +1009,20 @@ static void sd_parse_uri(BDRVSheepdogState *s, const
char *filename,
error_setg(&err, "unexpected query parameters");
goto out;
}
- s->host_spec = g_strdup(qp->p[0].value);
+ addr->type = SOCKET_ADDRESS_KIND_UNIX;
+ addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
+ addr->u.q_unix.data->path = g_strdup(qp->p[0].value);
} else {
/* sheepdog[+tcp]://[host:port]/vdiname */
if (qp->n) {
error_setg(&err, "unexpected query parameters");
goto out;
}
- s->host_spec = g_strdup_printf("%s:%d", uri->server ?: SD_DEFAULT_ADDR,
- uri->port ?: SD_DEFAULT_PORT);
+ addr->type = SOCKET_ADDRESS_KIND_INET;
+ addr->u.inet.data = g_new0(InetSocketAddress, 1);
+ addr->u.inet.data->host = g_strdup(uri->server ?: SD_DEFAULT_ADDR);
+ addr->u.inet.data->port = g_strdup_printf("%d",
+ uri->port ?: SD_DEFAULT_PORT);
}
/* snapshot tag */
@@ -1035,7 +1037,12 @@ static void sd_parse_uri(BDRVSheepdogState *s, const
char *filename,
}
out:
- error_propagate(errp, err);
+ if (err) {
+ error_propagate(errp, err);
+ qapi_free_SocketAddress(addr);
+ } else {
+ s->addr = addr;
+ }
if (qp) {
query_params_free(qp);
}
@@ -1998,7 +2005,7 @@ static void sd_close(BlockDriverState *bs)
aio_set_fd_handler(bdrv_get_aio_context(bs), s->fd,
false, NULL, NULL, NULL, NULL);
closesocket(s->fd);
- g_free(s->host_spec);
+ qapi_free_SocketAddress(s->addr);
}
static int64_t sd_getlength(BlockDriverState *bs)
--
1.8.3.1
- [Qemu-devel] [PULL 07/27] block: Factor out bdrv_replace_child_noperm(), (continued)
- [Qemu-devel] [PULL 07/27] block: Factor out bdrv_replace_child_noperm(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 08/27] block: Ignore multiple children in bdrv_check_update_perm(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 09/27] block: Handle permission errors in change_parent_backing_link(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 10/27] block: Fix error handling in bdrv_replace_in_backing_chain(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 12/27] sheepdog: Fix error handling in sd_snapshot_delete(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 11/27] sheepdog: Defuse time bomb in sd_open() error handling, Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 14/27] sheepdog: Mark sd_snapshot_delete() lossage FIXME, Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 13/27] sheepdog: Fix error handling sd_create(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 15/27] sheepdog: Fix snapshot ID parsing in _open(), _create, _goto(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 16/27] sheepdog: Don't truncate long VDI name in _open(), _create(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 18/27] sheepdog: Use SocketAddress and socket_connect(),
Kevin Wolf <=
- [Qemu-devel] [PULL 20/27] gluster: Drop assumptions on SocketTransport names, Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 17/27] sheepdog: Report errors in pseudo-filename more usefully, Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 19/27] sheepdog: Implement bdrv_parse_filename(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 25/27] sheepdog: Support blockdev-add, Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 22/27] gluster: Plug memory leaks in qemu_gluster_parse_json(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 23/27] qapi-schema: Rename GlusterServer to SocketAddressFlat, Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 21/27] gluster: Don't duplicate qapi-util.c's qapi_enum_parse(), Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 24/27] qapi-schema: Rename SocketAddressFlat's variant tcp to inet, Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 26/27] block: Don't use error_abort in blk_new_open, Kevin Wolf, 2017/03/07
- [Qemu-devel] [PULL 27/27] commit: Don't use error_abort in commit_start, Kevin Wolf, 2017/03/07