Skip to contentSkip to conversation

qemu-char: Clean up socket reporting

PATCH In review [email protected] 5 participants 8 messages Oct 6, 2014 – Oct 8, 2014

lore
Share & tools
lore
#0 lore raw

cover qemu-char: Clean up socket reporting

The first patch fixes some reporting issues for reconnect sockets that fail to connect. It prevents a constant stream of error messages, and reports the error immediately instead of delaying it. This required a small restructure, but nothing big.

The second adds an error to non-blocking connect reporting. The error was available, it just wasn't being passed. Hopefully this makes the error easier to diagnose.

-corey

#1 lore raw

PATCH 1/2 qemu-char: Fix reconnect socket error reporting

From: Corey Minyard <[email protected]>

If reconnect was set, errors wouldn't always be reported. Fix that and also only report a connect error once until a connection has been made.

The primary purpose of this is to tell the user that a connection failed so they can know they need to figure out what went wrong. So we don't want to spew too much out here, just enough so they know.

---
 qemu-char.c | 47 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 15 deletions(-)
qemu-char.c +32 -15
index 62af0ef..fb895c7 100644
@@ -2509,6 +2509,7 @@ typedef struct {
 
     guint reconnect_timer;
     int64_t reconnect_time;
+    bool connect_err_reported;
 } TCPCharDriver;
 
 static gboolean socket_reconnect_timeout(gpointer opaque);
@@ -2521,6 +2522,17 @@ static void qemu_chr_socket_restart_timer(CharDriverState *chr)
                                                socket_reconnect_timeout, chr);
 }
 
+static void check_report_connect_error(CharDriverState *chr, const char *str)
+{
+    TCPCharDriver *s = chr->opaque;
+
+    if (!s->connect_err_reported) {
+        error_report("%s char device %s\n", str, chr->label);
+        s->connect_err_reported = 1;
+    }
+    qemu_chr_socket_restart_timer(chr);
+}
+
 static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
 
 #ifndef _WIN32
@@ -3045,12 +3057,14 @@ static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
 static void qemu_chr_socket_connected(int fd, void *opaque)
 {
     CharDriverState *chr = opaque;
+    TCPCharDriver *s = chr->opaque;
 
     if (fd < 0) {
-        qemu_chr_socket_restart_timer(chr);
+        check_report_connect_error(chr, "Unable to connect to");
         return;
     }
 
+    s->connect_err_reported = 0;
     qemu_chr_finish_socket_connection(chr, fd);
 }
 
@@ -4066,11 +4080,19 @@ static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
 
 #endif /* WIN32 */
 
+static void socket_try_connect(CharDriverState *chr)
+{
+    Error *err = NULL;
+
+    if (!qemu_chr_open_socket_fd(chr, &err)) {
+        check_report_connect_error(chr, "Unable to start connection to");
+    }
+}
+
 static gboolean socket_reconnect_timeout(gpointer opaque)
 {
     CharDriverState *chr = opaque;
     TCPCharDriver *s = chr->opaque;
-    Error *err;
 
     s->reconnect_timer = 0;
 
@@ -4078,10 +4100,7 @@ static gboolean socket_reconnect_timeout(gpointer opaque)
         return false;
     }
 
-    if (!qemu_chr_open_socket_fd(chr, &err)) {
-        error_report("Unable to connect to char device %s\n", chr->label);
-        qemu_chr_socket_restart_timer(chr);
-    }
+    socket_try_connect(chr);
 
     return false;
 }
@@ -4133,15 +4152,13 @@ static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
         s->reconnect_time = reconnect;
     }
 
-    if (!qemu_chr_open_socket_fd(chr, errp)) {
-        if (s->reconnect_time) {
-            qemu_chr_socket_restart_timer(chr);
-        } else {
-            g_free(s);
-            g_free(chr->filename);
-            g_free(chr);
-            return NULL;
-        }
+    if (s->reconnect_time) {
+        socket_try_connect(chr);
+    } else if (!qemu_chr_open_socket_fd(chr, errp)) {
+        g_free(s);
+        g_free(chr->filename);
+        g_free(chr);
+        return NULL;
     }
 
     if (is_listen && is_waitconnect) {
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] qemu-char: Fix reconnect socket error reporting

Quoted 21 lines · On 10/06/2014 11:59 AM, [email protected] wrote:

From: Corey Minyard <[email protected]>

If reconnect was set, errors wouldn't always be reported. Fix that and also only report a connect error once until a connection has been made.

The primary purpose of this is to tell the user that a connection failed so they can know they need to figure out what went wrong. So we don't want to spew too much out here, just enough so they know.

---
 qemu-char.c | 47 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 15 deletions(-)
qemu-char.c +1 -0
+    bool connect_err_reported;
 } TCPCharDriver;
 
 static gboolean socket_reconnect_timeout(gpointer opaque);

qemu-char.c static void qemu_chr_socket_restart_timer(CharDriverState *chr) · line 2530

Quoted 9 lines
qemu-char.c +6 -0
@@ -2521,6 +2522,17 @@ static void qemu_chr_socket_restart_timer(CharDriverState *chr)
                                                socket_reconnect_timeout, chr);
 }
 
+static void check_report_connect_error(CharDriverState *chr, const char *str)
+{
+    TCPCharDriver *s = chr->opaque;
+
+    if (!s->connect_err_reported) {
+        error_report("%s char device %s\n", str, chr->label);

No \n at the end of an error_report() message.

qemu-char.c static void qemu_chr_socket_restart_timer(CharDriverState *chr) · line 2531

Quoted 1 line
qemu-char.c +1 -0
+        s->connect_err_reported = 1;

s/1/true/ since you typed it as bool.

Quoted 8 lines
qemu-char.c +4 -0
+    }
+    qemu_chr_socket_restart_timer(chr);
+}
+
 static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
 
 #ifndef _WIN32

qemu-char.c static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd) · line 3063

Quoted 8 lines
qemu-char.c +2 -1
@@ -3045,12 +3057,14 @@ static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
 static void qemu_chr_socket_connected(int fd, void *opaque)
 {
     CharDriverState *chr = opaque;
+    TCPCharDriver *s = chr->opaque;
 
     if (fd < 0) {
-        qemu_chr_socket_restart_timer(chr);
+        check_report_connect_error(chr, "Unable to connect to");

Works in English, but if there is ever translation of the message printed in check_report_connect_error, you are probably doing translators a disservice by splitting a sentence into two parts separated by a number of lines of code. (Spoken by one who has never contributed a translation, so take with a grain of salt)

qemu-char.c static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd) · line 3067

Quoted 4 lines
qemu-char.c +1 -0
         return;
     }
 
+    s->connect_err_reported = 0;

s/0/false/

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] qemu-char: Fix reconnect socket error reporting

Quoted 24 lines · Eric Blake
Quoted 23 lines · On 10/06/2014 11:59 AM, [email protected] wrote:

From: Corey Minyard <[email protected]>

If reconnect was set, errors wouldn't always be reported. Fix that and also only report a connect error once until a connection has been made.

The primary purpose of this is to tell the user that a connection failed so they can know they need to figure out what went wrong. So we don't want to spew too much out here, just enough so they know.

---
 qemu-char.c | 47 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 15 deletions(-)
qemu-char.c +1 -0
+    bool connect_err_reported;
 } TCPCharDriver;
 
 static gboolean socket_reconnect_timeout(gpointer opaque);
@@ -2521,6 +2522,17 @@ static void qemu_chr_socket_restart_timer(CharDriverState *chr)
                                                socket_reconnect_timeout, chr);
 }
 
Quoted 7 lines

qemu-char.c static void qemu_chr_socket_restart_timer(CharDriverState *chr) · line 2530

Quoted 6 lines
qemu-char.c +6 -0
+static void check_report_connect_error(CharDriverState *chr, const char *str)
+{
+    TCPCharDriver *s = chr->opaque;
+
+    if (!s->connect_err_reported) {
+        error_report("%s char device %s\n", str, chr->label);

No \n at the end of an error_report() message.

Oops, I read that and forgot to change it.

Quoted 3 lines

qemu-char.c static void qemu_chr_socket_restart_timer(CharDriverState *chr) · line 2531

Quoted 2 lines
qemu-char.c +1 -0
+        s->connect_err_reported = 1;

s/1/true/ since you typed it as bool.

Sigh. Old habits die hard.

Quoted 14 lines
Quoted 14 lines
qemu-char.c +5 -0
+    }
+    qemu_chr_socket_restart_timer(chr);
+}
+
 static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
 
 #ifndef _WIN32
@@ -3045,12 +3057,14 @@ static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
 static void qemu_chr_socket_connected(int fd, void *opaque)
 {
     CharDriverState *chr = opaque;
+    TCPCharDriver *s = chr->opaque;
 
Quoted 8 lines

qemu-char.c static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd) · line 3063

Quoted 3 lines
qemu-char.c +1 -1
     if (fd < 0) {
-        qemu_chr_socket_restart_timer(chr);
+        check_report_connect_error(chr, "Unable to connect to");

Works in English, but if there is ever translation of the message printed in check_report_connect_error, you are probably doing translators a disservice by splitting a sentence into two parts separated by a number of lines of code. (Spoken by one who has never contributed a translation, so take with a grain of salt)

Yeah, I was avoiding having to add an error_vreport(). I should just add that.

All other comments fixed. Thanks.

-corey

Quoted 8 lines

qemu-char.c static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd) · line 3067

Quoted 4 lines
qemu-char.c +1 -0
         return;
     }
 
+    s->connect_err_reported = 0;

s/0/false/

#4 lore raw

Re: [Qemu-devel] [PATCH 1/2] qemu-char: Fix reconnect socket error reporting

In reply to Eric Blake

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Il 06/10/2014 20:36, Eric Blake ha scritto:

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

- qemu_chr_socket_restart_timer(chr); +
check_report_connect_error(chr, "Unable to connect to");

Works in English, but if there is ever translation of the message printed in check_report_connect_error, you are probably doing translators a disservice by splitting a sentence into two parts separated by a number of lines of code. (Spoken by one who has never contributed a translation, so take with a grain of salt)

I agree, I think it's simpler to unify the two messages.

Paolo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJUMv1IAAoJEBRUblpOawnXtFoH/1QIYr/yZXZQhw7zqDxiyhgH Gto3UiDjd/eLNC/iFwdY3J2pnuHIu8yVmBAO68cALdK6KotZ6X/TGVpsGUmPeoPk rPtkbxAvBvxdPImCUi798YSXbkOsVA/5+pMOOxNLeMwzA2ClFDFBqzgqgBbL/PwU O25XgCNucwFpk6An5yTOblHbS5Ji6oSnfJfHkXwuZlzjE7ql2nbr0Gr9lEulgHk3 3+hDoyesCD55V0r1qO/CwGJsuyJ1PXJGxV65ML1ja6F+yiMjlCgaTmzLAtcdWu4A oD6wR5auS9ZTWogS/JBxCx9oFnZVs19LgnSPSvQVHb3TaQJcVSTZuQjkb3/m8bU= =ctwR
-----END PGP SIGNATURE-----

#5 lore raw

Re: [Qemu-devel] [PATCH 1/2] qemu-char: Fix reconnect socket error reporting

In reply to [email protected]
Quoted 21 lines · [email protected] writes:

From: Corey Minyard <[email protected]>

If reconnect was set, errors wouldn't always be reported. Fix that and also only report a connect error once until a connection has been made.

The primary purpose of this is to tell the user that a connection failed so they can know they need to figure out what went wrong. So we don't want to spew too much out here, just enough so they know.

---
 qemu-char.c | 47 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 15 deletions(-)
qemu-char.c +0 -0
index 62af0ef..fb895c7 100644

qemu-char.c typedef struct { · line 2515

Quoted 7 lines
qemu-char.c +1 -0
@@ -2509,6 +2509,7 @@ typedef struct {
 
     guint reconnect_timer;
     int64_t reconnect_time;
+    bool connect_err_reported;
 } TCPCharDriver;
 
 static gboolean socket_reconnect_timeout(gpointer opaque);

Doesn't apply, obviously depends on some other patch. Always state your dependencies explicitly in the cover letter!

[...]

#6 lore raw

PATCH 2/2 qemu-sockets: Add error to non-blocking connect handler

In reply to [email protected]

From: Corey Minyard <[email protected]>

An error value here would be quite handy and more consistent with the rest of the code.

Corey Minyard <[email protected]>

---
 include/qemu/sockets.h |  2 +-
 migration-tcp.c        |  4 ++--
 migration-unix.c       |  4 ++--
 qemu-char.c            | 12 +++++++-----
 util/qemu-sockets.c    | 19 ++++++++++++++-----
 5 files changed, 26 insertions(+), 15 deletions(-)
include/qemu/sockets.h +1 -1
index fdbb196..f47dae6 100644
@@ -47,7 +47,7 @@ int recv_all(int fd, void *buf, int len1, bool single_read);
 /* callback function for nonblocking connect
  * valid fd on success, negative error code on failure
  */
-typedef void NonBlockingConnectHandler(int fd, void *opaque);
+typedef void NonBlockingConnectHandler(int fd, Error *errp, void *opaque);
 
 InetSocketAddress *inet_parse(const char *str, Error **errp);
 int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp);
migration-tcp.c +2 -2
index 2e34517..91c9cf3 100644
@@ -33,12 +33,12 @@
     do { } while (0)
 #endif
 
-static void tcp_wait_for_connect(int fd, void *opaque)
+static void tcp_wait_for_connect(int fd, Error *err, void *opaque)
 {
     MigrationState *s = opaque;
 
     if (fd < 0) {
-        DPRINTF("migrate connect error\n");
+        DPRINTF("migrate connect error: %s\n", error_get_pretty(err));
         s->file = NULL;
         migrate_fd_error(s);
     } else {
migration-unix.c +2 -2
index 0a5f8a1..1cdadfb 100644
@@ -33,12 +33,12 @@
     do { } while (0)
 #endif
 
-static void unix_wait_for_connect(int fd, void *opaque)
+static void unix_wait_for_connect(int fd, Error *err, void *opaque)
 {
     MigrationState *s = opaque;
 
     if (fd < 0) {
-        DPRINTF("migrate connect error\n");
+        DPRINTF("migrate connect error: %s\n", error_get_pretty(err));
         s->file = NULL;
         migrate_fd_error(s);
     } else {
qemu-char.c +7 -5
index fb895c7..22a2eb7 100644
@@ -2522,12 +2522,14 @@ static void qemu_chr_socket_restart_timer(CharDriverState *chr)
                                                socket_reconnect_timeout, chr);
 }
 
-static void check_report_connect_error(CharDriverState *chr, const char *str)
+static void check_report_connect_error(CharDriverState *chr, const char *str,
+                                       Error *err)
 {
     TCPCharDriver *s = chr->opaque;
 
     if (!s->connect_err_reported) {
-        error_report("%s char device %s\n", str, chr->label);
+        error_report("%s char device %s: %s\n", str, chr->label,
+                     error_get_pretty(err));
         s->connect_err_reported = 1;
     }
     qemu_chr_socket_restart_timer(chr);
@@ -3054,13 +3056,13 @@ static void qemu_chr_finish_socket_connection(CharDriverState *chr, int fd)
     }
 }
 
-static void qemu_chr_socket_connected(int fd, void *opaque)
+static void qemu_chr_socket_connected(int fd, Error *err, void *opaque)
 {
     CharDriverState *chr = opaque;
     TCPCharDriver *s = chr->opaque;
 
     if (fd < 0) {
-        check_report_connect_error(chr, "Unable to connect to");
+        check_report_connect_error(chr, "Unable to connect to", err);
         return;
     }
 
@@ -4085,7 +4087,7 @@ static void socket_try_connect(CharDriverState *chr)
     Error *err = NULL;
 
     if (!qemu_chr_open_socket_fd(chr, &err)) {
-        check_report_connect_error(chr, "Unable to start connection to");
+        check_report_connect_error(chr, "Unable to start connection to", err);
     }
 }
 
util/qemu-sockets.c +14 -5
index 1eef590..e6a9644 100644
@@ -234,6 +234,7 @@ static void wait_for_connect(void *opaque)
     int val = 0, rc = 0;
     socklen_t valsize = sizeof(val);
     bool in_progress;
+    Error *err = NULL;
 
     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
 
@@ -248,6 +249,7 @@ static void wait_for_connect(void *opaque)
 
     /* connect error */
     if (rc < 0) {
+        error_setg_errno(&err, errno, "Error connecting to socket");
         closesocket(s->fd);
         s->fd = rc;
     }
@@ -257,9 +259,14 @@ static void wait_for_connect(void *opaque)
         while (s->current_addr->ai_next != NULL && s->fd < 0) {
             s->current_addr = s->current_addr->ai_next;
             s->fd = inet_connect_addr(s->current_addr, &in_progress, s, NULL);
+            if (s->fd < 0) {
+                error_free(err);
+                err = NULL;
+                error_setg_errno(&err, errno, "Unable to start socket connect");
+            }
             /* connect in progress */
             if (in_progress) {
-                return;
+                goto out;
             }
         }
 
@@ -267,9 +274,11 @@ static void wait_for_connect(void *opaque)
     }
 
     if (s->callback) {
-        s->callback(s->fd, s->opaque);
+        s->callback(s->fd, err, s->opaque);
     }
     g_free(s);
+out:
+    error_free(err);
 }
 
 static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
@@ -401,7 +410,7 @@ int inet_connect_opts(QemuOpts *opts, Error **errp,
         return sock;
     } else {
         if (callback) {
-            callback(sock, opaque);
+            callback(sock, NULL, opaque);
         }
     }
     g_free(connect_state);
@@ -769,7 +778,7 @@ int unix_connect_opts(QemuOpts *opts, Error **errp,
     } else if (rc >= 0) {
         /* non blocking socket immediate success, call callback */
         if (callback != NULL) {
-            callback(sock, opaque);
+            callback(sock, NULL, opaque);
         }
     }
 
@@ -919,7 +928,7 @@ int socket_connect(SocketAddress *addr, Error **errp,
         fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
         if (fd >= 0 && callback) {
             qemu_set_nonblock(fd);
-            callback(fd, opaque);
+            callback(fd, NULL, opaque);
         }
         break;
 
Signature · 1 line
1.8.3.1
#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu-sockets: Add error to non-blocking connect handler

Il 06/10/2014 19:59, [email protected] ha scritto:

util/qemu-sockets.c static void wait_for_connect(void *opaque) · line 265

Quoted 12 lines
util/qemu-sockets.c +5 -0
+        error_setg_errno(&err, errno, "Error connecting to socket");
         closesocket(s->fd);
         s->fd = rc;
     }
@@ -257,9 +259,14 @@ static void wait_for_connect(void *opaque)
         while (s->current_addr->ai_next != NULL && s->fd < 0) {
             s->current_addr = s->current_addr->ai_next;
             s->fd = inet_connect_addr(s->current_addr, &in_progress, s, NULL);
+            if (s->fd < 0) {
+                error_free(err);
+                err = NULL;
+                error_setg_errno(&err, errno, "Unable to start socket connect");

So the above snippet is the actual errors that are passed here:

qemu-char.c static void qemu_chr_socket_restart_timer(CharDriverState *chr) · line 2532

Quoted 9 lines
qemu-char.c +4 -1
+static void check_report_connect_error(CharDriverState *chr, const char *str,
+                                       Error *err)
 {
     TCPCharDriver *s = chr->opaque;
 
     if (!s->connect_err_reported) {
-        error_report("%s char device %s\n", str, chr->label);
+        error_report("%s char device %s: %s\n", str, chr->label,
+                     error_get_pretty(err));

If you just make it

error_report("%s: %s", chr->label, error_get_pretty(err));

we still get a good error. It would arguably be better, since there's no duplication, except that it doesn't mention character devices anymore. But something like "serial0: error connecting to socket" is a decent error.

Thanks,

Paolo