All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: Mao Zhongyi <maozy.fnst@cn.fujitsu.com>
Cc: qemu-devel@nongnu.org, jasowang@redhat.com, armbru@redhat.com
Subject: Re: [Qemu-devel] [PATCH v6 4/4] net/socket: Improve -net socket error reporting
Date: Wed, 28 Jun 2017 14:27:45 +0100	[thread overview]
Message-ID: <20170628132745.GK29134@redhat.com> (raw)
In-Reply-To: <f268a317086c90e4d8e2547a084ec9b477fc6a04.1498654240.git.maozy.fnst@cn.fujitsu.com>

On Wed, Jun 28, 2017 at 09:08:50PM +0800, Mao Zhongyi wrote:
> When -net socket fails, it first reports a specific error, then
> a generic one, like this:
> 
>     $ qemu-system-x86_64 -net socket,
>     qemu-system-x86_64: -net socket: exactly one of fd=, listen=, connect=, mcast= or udp= is required
>     qemu-system-x86_64: -net socket: Device 'socket' could not be initialized

This second line of error message comes from net/net.c in the
net_client_init1 method:

        /* FIXME drop when all init functions store an Error */
        if (errp && !*errp) {
            error_setg(errp, QERR_DEVICE_INIT_FAILED,
                       NetClientDriver_lookup[netdev->type]);
        }


hopefully your patch could drop this code too ?

In fact this is the only use of QERR_DEVICE_INIT_FAILED in the
whole tree, so the QERR constant could possibly be killed too.


> 
> Convert net_socket_*_init() to Error to get rid of the superfluous second
> error message. After the patch, the effect like this:
> 
>     $ qemu-system-x86_64 -net socket,
>     qemu-system-x86_64: -net socket: exactly one of fd=, listen=, connect=, mcast= or udp= is required
> 
> At the same time, add many explicit error handling message when it fails.
> 
> Cc: jasowang@redhat.com
> Cc: armbru@redhat.com
> Signed-off-by: Mao Zhongyi <maozy.fnst@cn.fujitsu.com>
> ---
>  net/socket.c | 92 +++++++++++++++++++++++++++++-------------------------------
>  1 file changed, 44 insertions(+), 48 deletions(-)
> 
> diff --git a/net/socket.c b/net/socket.c
> index 90dd4c0..47e6706 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -494,22 +494,21 @@ static void net_socket_accept(void *opaque)
>  static int net_socket_listen_init(NetClientState *peer,
>                                    const char *model,
>                                    const char *name,
> -                                  const char *host_str)
> +                                  const char *host_str,
> +                                  Error **errp)
>  {
>      NetClientState *nc;
>      NetSocketState *s;
>      struct sockaddr_in saddr;
>      int fd, ret;
> -    Error *err = NULL;
>  
> -    if (parse_host_port(&saddr, host_str, &err) < 0) {
> -        error_report_err(err);
> +    if (parse_host_port(&saddr, host_str, errp) < 0) {
>          return -1;
>      }
>  
>      fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
>      if (fd < 0) {
> -        perror("socket");
> +        error_setg_errno(errp, errno, "failed to create stream socket");
>          return -1;
>      }
>      qemu_set_nonblock(fd);
> @@ -518,13 +517,14 @@ static int net_socket_listen_init(NetClientState *peer,
>  
>      ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
>      if (ret < 0) {
> -        perror("bind");
> +        error_setg_errno(errp, errno, "bind ip=%s to socket failed",
> +                         inet_ntoa(saddr.sin_addr));
>          closesocket(fd);
>          return -1;
>      }
>      ret = listen(fd, 0);
>      if (ret < 0) {
> -        perror("listen");
> +        error_setg_errno(errp, errno, "listen socket failed");
>          closesocket(fd);
>          return -1;
>      }
> @@ -543,21 +543,20 @@ static int net_socket_listen_init(NetClientState *peer,
>  static int net_socket_connect_init(NetClientState *peer,
>                                     const char *model,
>                                     const char *name,
> -                                   const char *host_str)
> +                                   const char *host_str,
> +                                   Error **errp)
>  {
>      NetSocketState *s;
>      int fd, connected, ret;
>      struct sockaddr_in saddr;
> -    Error *err = NULL;
>  
> -    if (parse_host_port(&saddr, host_str, &err) < 0) {
> -        error_report_err(err);
> +    if (parse_host_port(&saddr, host_str, errp) < 0) {
>          return -1;
>      }
>  
>      fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
>      if (fd < 0) {
> -        perror("socket");
> +        error_setg_errno(errp, errno, "failed to create stream socket");
>          return -1;
>      }
>      qemu_set_nonblock(fd);
> @@ -573,7 +572,7 @@ static int net_socket_connect_init(NetClientState *peer,
>                         errno == EINVAL) {
>                  break;
>              } else {
> -                perror("connect");
> +                error_setg_errno(errp, errno, "connection failed");
>                  closesocket(fd);
>                  return -1;
>              }
> @@ -582,9 +581,8 @@ static int net_socket_connect_init(NetClientState *peer,
>              break;
>          }
>      }
> -    s = net_socket_fd_init(peer, model, name, fd, connected, &err);
> +    s = net_socket_fd_init(peer, model, name, fd, connected, errp);
>      if (!s) {
> -        error_report_err(err);
>          return -1;
>      }
>      snprintf(s->nc.info_str, sizeof(s->nc.info_str),
> @@ -597,36 +595,36 @@ static int net_socket_mcast_init(NetClientState *peer,
>                                   const char *model,
>                                   const char *name,
>                                   const char *host_str,
> -                                 const char *localaddr_str)
> +                                 const char *localaddr_str,
> +                                 Error **errp)
>  {
>      NetSocketState *s;
>      int fd;
>      struct sockaddr_in saddr;
>      struct in_addr localaddr, *param_localaddr;
> -    Error *err = NULL;
>  
> -    if (parse_host_port(&saddr, host_str, &err) < 0) {
> -        error_report_err(err);
> +    if (parse_host_port(&saddr, host_str, errp) < 0) {
>          return -1;
>      }
>  
>      if (localaddr_str != NULL) {
> -        if (inet_aton(localaddr_str, &localaddr) == 0)
> +        if (inet_aton(localaddr_str, &localaddr) == 0) {
> +            error_setg(errp, "localaddr '%s' is not a valid IPv4 address",
> +                       localaddr_str);
>              return -1;
> +        }
>          param_localaddr = &localaddr;
>      } else {
>          param_localaddr = NULL;
>      }
>  
> -    fd = net_socket_mcast_create(&saddr, param_localaddr, &err);
> +    fd = net_socket_mcast_create(&saddr, param_localaddr, errp);
>      if (fd < 0) {
> -        error_report_err(err);
>          return -1;
>      }
>  
> -    s = net_socket_fd_init(peer, model, name, fd, 0, &err);
> +    s = net_socket_fd_init(peer, model, name, fd, 0, errp);
>      if (!s) {
> -        error_report_err(err);
>          return -1;
>      }
>  
> @@ -643,45 +641,45 @@ static int net_socket_udp_init(NetClientState *peer,
>                                   const char *model,
>                                   const char *name,
>                                   const char *rhost,
> -                                 const char *lhost)
> +                                 const char *lhost,
> +                                 Error **errp)
>  {
>      NetSocketState *s;
>      int fd, ret;
>      struct sockaddr_in laddr, raddr;
> -    Error *err = NULL;
>  
> -    if (parse_host_port(&laddr, lhost, &err) < 0) {
> -        error_report_err(err);
> +    if (parse_host_port(&laddr, lhost, errp) < 0) {
>          return -1;
>      }
>  
> -    if (parse_host_port(&raddr, rhost, &err) < 0) {
> -        error_report_err(err);
> +    if (parse_host_port(&raddr, rhost, errp) < 0) {
>          return -1;
>      }
>  
>      fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
>      if (fd < 0) {
> -        perror("socket(PF_INET, SOCK_DGRAM)");
> +        error_setg_errno(errp, errno, "failed to create datagram socket");
>          return -1;
>      }
>  
>      ret = socket_set_fast_reuse(fd);
>      if (ret < 0) {
> +        error_setg_errno(errp, errno, "set the socket 'SO_REUSEADDR'"
> +                         " attribute failed");
>          closesocket(fd);
>          return -1;
>      }
>      ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
>      if (ret < 0) {
> -        perror("bind");
> +        error_setg_errno(errp, errno, "bind ip=%s to socket failed",
> +                         inet_ntoa(laddr.sin_addr));
>          closesocket(fd);
>          return -1;
>      }
>      qemu_set_nonblock(fd);
>  
> -    s = net_socket_fd_init(peer, model, name, fd, 0, &err);
> +    s = net_socket_fd_init(peer, model, name, fd, 0, errp);
>      if (!s) {
> -        error_report_err(err);
>          return -1;
>      }
>  
> @@ -696,8 +694,6 @@ static int net_socket_udp_init(NetClientState *peer,
>  int net_init_socket(const Netdev *netdev, const char *name,
>                      NetClientState *peer, Error **errp)
>  {
> -    /* FIXME error_setg(errp, ...) on failure */
> -    Error *err = NULL;
>      const NetdevSocketOptions *sock;
>  
>      assert(netdev->type == NET_CLIENT_DRIVER_SOCKET);
> @@ -705,22 +701,21 @@ int net_init_socket(const Netdev *netdev, const char *name,
>  
>      if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
>          sock->has_udp != 1) {
> -        error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
> +        error_setg(errp, "exactly one of fd=, listen=, connect=, mcast= or udp="
>                       " is required");
>          return -1;
>      }
>  
>      if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
> -        error_report("localaddr= is only valid with mcast= or udp=");
> +        error_setg(errp, "localaddr= is only valid with mcast= or udp=");
>          return -1;
>      }
>  
>      if (sock->has_fd) {
>          int fd;
>  
> -        fd = monitor_fd_param(cur_mon, sock->fd, &err);
> +        fd = monitor_fd_param(cur_mon, sock->fd, errp);
>          if (fd == -1) {
> -            error_report_err(err);
>              return -1;
>          }
>          qemu_set_nonblock(fd);
> @@ -731,15 +726,16 @@ int net_init_socket(const Netdev *netdev, const char *name,
>      }
>  
>      if (sock->has_listen) {
> -        if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
> +        if (net_socket_listen_init(peer, "socket", name,
> +            sock->listen, errp) == -1) {
>              return -1;
>          }
>          return 0;
>      }
>  
>      if (sock->has_connect) {
> -        if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
> -            -1) {
> +        if (net_socket_connect_init(peer, "socket", name,
> +            sock->connect, errp) == -1) {
>              return -1;
>          }
>          return 0;
> @@ -748,8 +744,8 @@ int net_init_socket(const Netdev *netdev, const char *name,
>      if (sock->has_mcast) {
>          /* if sock->localaddr is missing, it has been initialized to "all bits
>           * zero" */
> -        if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
> -            sock->localaddr) == -1) {
> +        if (net_socket_mcast_init(peer, "socket", name,
> +            sock->mcast, sock->localaddr, errp) == -1) {
>              return -1;
>          }
>          return 0;
> @@ -757,11 +753,11 @@ int net_init_socket(const Netdev *netdev, const char *name,
>  
>      assert(sock->has_udp);
>      if (!sock->has_localaddr) {
> -        error_report("localaddr= is mandatory with udp=");
> +        error_setg(errp, "localaddr= is mandatory with udp=");
>          return -1;
>      }
> -    if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) ==
> -        -1) {
> +    if (net_socket_udp_init(peer, "socket", name,
> +        sock->udp, sock->localaddr, errp) == -1) {
>          return -1;
>      }
>      return 0;
> -- 
> 2.9.4
> 
> 
> 
> 

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

  reply	other threads:[~2017-06-28 13:27 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-28 13:08 [Qemu-devel] [PATCH v6 0/4] Improve error reporting Mao Zhongyi
2017-06-28 13:08 ` [Qemu-devel] [PATCH v6 1/4] net/socket: Don't treat odd socket type as SOCK_STREAM Mao Zhongyi
2017-06-28 13:18   ` Daniel P. Berrange
2017-06-28 14:23     ` Eric Blake
2017-06-29  3:24       ` Mao Zhongyi
2017-06-28 13:08 ` [Qemu-devel] [PATCH v6 2/4] net/socket: Convert several helper functions to Error Mao Zhongyi
2017-06-28 13:21   ` Daniel P. Berrange
2017-06-28 13:08 ` [Qemu-devel] [PATCH v6 3/4] net/net: Convert parse_host_port() " Mao Zhongyi
2017-06-28 13:23   ` Daniel P. Berrange
2017-06-28 14:24     ` Eric Blake
2017-06-28 14:28       ` Daniel P. Berrange
2017-06-29  7:29         ` Markus Armbruster
2017-06-29  3:01     ` Mao Zhongyi
2017-06-28 13:08 ` [Qemu-devel] [PATCH v6 4/4] net/socket: Improve -net socket error reporting Mao Zhongyi
2017-06-28 13:27   ` Daniel P. Berrange [this message]
2017-06-29  3:08     ` Mao Zhongyi
2017-06-29  7:31       ` Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170628132745.GK29134@redhat.com \
    --to=berrange@redhat.com \
    --cc=armbru@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=maozy.fnst@cn.fujitsu.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.