Skip to content

Commit

Permalink
net: handle QIOTask completion to report useful error message
Browse files Browse the repository at this point in the history
The network stream backend uses the async QIO socket APIs for listening
and connecting sockets. It does not check the task object completion
status, however, instead just looking at whether the socket FD is -1
or not.

By checking the task completion, we can set a useful error message for
users instead of the non-actionable "connection error" string.

eg so users will see:

(qemu) info network
net: index=0,type=stream,error: Failed to connect to '/foo.unix': No such file or directory

Signed-off-by: "Daniel P. Berrangé" <berrange@redhat.com>
Message-ID: <20240104162942.211458-6-berrange@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
  • Loading branch information
berrange authored and huth committed Jan 11, 2024
1 parent cc91ca6 commit 9cd67f0
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions net/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ static gboolean net_stream_send(QIOChannel *ioc,
s->ioc_write_tag = 0;
}
if (s->listener) {
qemu_set_info_str(&s->nc, "listening");
qio_net_listener_set_client_func(s->listener, net_stream_listen,
s, NULL);
}
Expand All @@ -173,7 +174,6 @@ static gboolean net_stream_send(QIOChannel *ioc,

net_socket_rs_init(&s->rs, net_stream_rs_finalize, false);
s->nc.link_down = true;
qemu_set_info_str(&s->nc, "listening");

qapi_event_send_netdev_stream_disconnected(s->nc.name);
net_stream_arm_reconnect(s);
Expand Down Expand Up @@ -272,9 +272,11 @@ static void net_stream_server_listening(QIOTask *task, gpointer opaque)
QIOChannelSocket *listen_sioc = QIO_CHANNEL_SOCKET(s->listen_ioc);
SocketAddress *addr;
int ret;
Error *err = NULL;

if (listen_sioc->fd < 0) {
qemu_set_info_str(&s->nc, "connection error");
if (qio_task_propagate_error(task, &err)) {
qemu_set_info_str(&s->nc, "error: %s", error_get_pretty(err));
error_free(err);
return;
}

Expand Down Expand Up @@ -327,9 +329,11 @@ static void net_stream_client_connected(QIOTask *task, gpointer opaque)
SocketAddress *addr;
gchar *uri;
int ret;
Error *err = NULL;

if (sioc->fd < 0) {
qemu_set_info_str(&s->nc, "connection error");
if (qio_task_propagate_error(task, &err)) {
qemu_set_info_str(&s->nc, "error: %s", error_get_pretty(err));
error_free(err);
goto error;
}

Expand Down Expand Up @@ -384,6 +388,7 @@ static gboolean net_stream_reconnect(gpointer data)
static void net_stream_arm_reconnect(NetStreamState *s)
{
if (s->reconnect && s->timer_tag == 0) {
qemu_set_info_str(&s->nc, "connecting");
s->timer_tag = g_timeout_add_seconds(s->reconnect,
net_stream_reconnect, s);
}
Expand Down

0 comments on commit 9cd67f0

Please sign in to comment.