Skip to contentSkip to conversation

Clean up non-blocking error reporting

PATCH In review [email protected] 4 participants 8 messages Oct 8, 2014 – Oct 9, 2014

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/3 qemu-error: Add error_vreport()

In reply to [email protected]

From: Corey Minyard <[email protected]>

Needed to nicely print socket error reports.

---
 include/qemu/error-report.h |  1 +
 util/qemu-error.c           | 23 ++++++++++++++++++-----
 2 files changed, 19 insertions(+), 5 deletions(-)
include/qemu/error-report.h +1 -0
index 000eae3..7ab2355 100644
@@ -38,6 +38,7 @@ void error_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
 void error_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 void error_printf_unless_qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 void error_set_progname(const char *argv0);
+void error_vreport(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
 void error_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 const char *error_get_progname(void);
 extern bool enable_timestamp_msg;
util/qemu-error.c +18 -5
index 7b167fd..9bba5f5 100644
@@ -199,14 +199,13 @@ static void error_print_loc(void)
 bool enable_timestamp_msg;
 /*
  * Print an error message to current monitor if we have one, else to stderr.
- * Format arguments like sprintf().  The result should not contain
+ * Format arguments like vsprintf().  The result should not contain
  * newlines.
  * Prepend the current location and append a newline.
  * It's wrong to call this in a QMP monitor.  Use qerror_report() there.
  */
-void error_report(const char *fmt, ...)
+void error_vreport(const char *fmt, va_list ap)
 {
-    va_list ap;
     GTimeVal tv;
     gchar *timestr;
 
@@ -218,8 +217,22 @@ void error_report(const char *fmt, ...)
     }
 
     error_print_loc();
-    va_start(ap, fmt);
     error_vprintf(fmt, ap);
-    va_end(ap);
     error_printf("\n");
 }
+
+/*
+ * Print an error message to current monitor if we have one, else to stderr.
+ * Format arguments like sprintf().  The result should not contain
+ * newlines.
+ * Prepend the current location and append a newline.
+ * It's wrong to call this in a QMP monitor.  Use qerror_report() there.
+ */
+void error_report(const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    error_vreport(fmt, ap);
+    va_end(ap);
+}
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v2 3/3 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            |  6 +++---
 util/qemu-sockets.c    | 19 ++++++++++++++-----
 5 files changed, 22 insertions(+), 13 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 +3 -3
index 83ff458..8f3af06 100644
@@ -3061,14 +3061,14 @@ 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 socket %s",
-                                   chr->label);
+        check_report_connect_error(chr, "Unable to connect to socket %s: %s",
+                                   chr->label, error_get_pretty(err));
         return;
     }
 
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
#0 lore raw

v2 cover Clean up non-blocking error reporting

This version makes the error message reporting more sane and adds a error_vreport() to make that easier.

It also cleans up the bool handling.

This depends on the previos non-blocking socket changes, which should hopefully be in qemu soon.

#3 lore raw

PATCH v2 2/3 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 | 57 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 42 insertions(+), 15 deletions(-)
qemu-char.c +42 -15
index 62af0ef..83ff458 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,24 @@ static void qemu_chr_socket_restart_timer(CharDriverState *chr)
                                                socket_reconnect_timeout, chr);
 }
 
+static void check_report_connect_error(CharDriverState *chr,
+                                       const char *fmt, ...) GCC_FMT_ATTR(2, 3);
+
+static void check_report_connect_error(CharDriverState *chr,
+                                       const char *fmt, ...)
+{
+    TCPCharDriver *s = chr->opaque;
+
+    if (!s->connect_err_reported) {
+        va_list ap;
+        va_start(ap, fmt);
+        error_vreport(fmt, ap);
+        va_end(ap);
+        s->connect_err_reported = true;
+    }
+    qemu_chr_socket_restart_timer(chr);
+}
+
 static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
 
 #ifndef _WIN32
@@ -3045,12 +3064,15 @@ 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 socket %s",
+                                   chr->label);
         return;
     }
 
+    s->connect_err_reported = false;
     qemu_chr_finish_socket_connection(chr, fd);
 }
 
@@ -4066,11 +4088,21 @@ 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 connect to socket %s: %s",
+                                   chr->label, error_get_pretty(err));
+    }
+}
+
 static gboolean socket_reconnect_timeout(gpointer opaque)
 {
     CharDriverState *chr = opaque;
     TCPCharDriver *s = chr->opaque;
-    Error *err;
 
     s->reconnect_timer = 0;
 
@@ -4078,10 +4110,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 +4162,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
#7 lore raw
In reply to [email protected]

Il 08/10/2014 14:11, [email protected] ha scritto:

Quoted 7 lines

This version makes the error message reporting more sane and adds a error_vreport() to make that easier.

It also cleans up the bool handling.

This depends on the previos non-blocking socket changes, which should hopefully be in qemu soon.

Looks good, but let's unify the error message like this:

+static void check_report_connect_error(CharDriverState *chr,
+ Error *err)
+{
+ TCPCharDriver *s = chr->opaque;
+
+ if (!s->connect_err_reported) {
+ error_report("Unable to connect character device %s: %s",
+ chr->label, error_get_pretty(err));
+ s->connect_err_reported = true;
+ }
+ qemu_chr_socket_restart_timer(chr);
+}
+

This is an error on connecting the character device to the backend. The error on the socket is already detailed after the colon, thanks to your patch 2.

I'll send a pull request today or tomorrow.

Thanks,

Paolo

Reviewed-by on v2 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] qemu-error: Add error_vreport()

In reply to [email protected]
Quoted 9 lines · On 10/08/2014 06:11 AM, [email protected] wrote:

From: Corey Minyard <[email protected]>

Needed to nicely print socket error reports.

---
 include/qemu/error-report.h |  1 +
 util/qemu-error.c           | 23 ++++++++++++++++++-----
 2 files changed, 19 insertions(+), 5 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw

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

In reply to [email protected]

Il 08/10/2014 14:11, [email protected] ha scritto:

Quoted 100 lines

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            |  6 +++---
 util/qemu-sockets.c    | 19 ++++++++++++++-----
 5 files changed, 22 insertions(+), 13 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 +3 -3
index 83ff458..8f3af06 100644
@@ -3061,14 +3061,14 @@ 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 socket %s",
-                                   chr->label);
+        check_report_connect_error(chr, "Unable to connect to socket %s: %s",
+                                   chr->label, error_get_pretty(err));
         return;
     }
 
util/qemu-sockets.c +1 -0
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);
 

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

Quoted 7 lines
util/qemu-sockets.c +1 -0
@@ -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;
     }

This is missing above this hunk:

util/qemu-sockets.c +1 -0
index e6a9644..a76bb3c 100644
@@ -245,6 +245,7 @@ static void wait_for_connect(void *opaque)
     /* update rc to contain error */
     if (!rc && val) {
         rc = -1;
+        errno = val;
     }
 
     /* connect error */
Quoted 57 lines
util/qemu-sockets.c +12 -5
@@ -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;
 
#6 lore raw

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

Quoted 12 lines · Paolo Bonzini
Quoted 4 lines

+ error_setg_errno(&err, errno, "Error connecting to socket");

Quoted 3 lines
         closesocket(s->fd);
         s->fd = rc;
     }

This is missing above this hunk:

util/qemu-sockets.c +0 -0
index e6a9644..a76bb3c 100644

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

Quoted 7 lines
util/qemu-sockets.c +1 -0
@@ -245,6 +245,7 @@ static void wait_for_connect(void *opaque)
     /* update rc to contain error */
     if (!rc && val) {
         rc = -1;
+        errno = val;
     }
 
     /* connect error */

Yes, it is. I see that everything went in, thanks for your work on this.

Now on to IPMI...

-corey