net: Improve error reporting
Subsystems (2)
v2 cover net: Improve error reporting
v2:
- Touch up a few commit messages [Eric]
- Touch up a an error message in PATCH 03+15 [Eric]
- Don't report possibly bogus errno in PATCH 12 [Eric]
Markus Armbruster (15):
net: Improve error message for -net hubport a bit
net: Permit incremental conversion of init functions to Error
net: Improve -net nic error reporting
net/dump: Improve -net/host_net_add dump error reporting
tap: net_tap_fd_init() can't fail, drop dead error handling
tap: Improve -netdev/netdev_add/-net/... bridge error reporting
tap: Convert tap_set_sndbuf() to Error
tap: Convert net_init_tap_one() to Error
tap: Convert launch_script() to Error
tap: Permit incremental conversion of tap_open() to Error
tap-linux: Convert tap_open() to Error
tap-bsd: Convert tap_open() to Error
tap-solaris: Convert tap_open() to Error
tap: Finish conversion of tap_open() to Error
tap: Improve -netdev/netdev_add/-net/... tap error reporting
net/clients.h | 20 +++--- net/dump.c | 13 ++-- net/hub.c | 7 +- net/l2tpv3.c | 5 +- net/net.c | 30 ++++++--- net/netmap.c | 3 +- net/slirp.c | 3 +- net/socket.c | 3 +- net/tap-aix.c | 7 +- net/tap-bsd.c | 38 +++++------ net/tap-haiku.c | 7 +- net/tap-linux.c | 24 +++---- net/tap-solaris.c | 63 ++++++++--------- net/tap-win32.c | 3 +- net/tap.c | 198 ++++++++++++++++++++++++++++++------------------------ net/tap_int.h | 4 +- net/vde.c | 3 +- net/vhost-user.c | 3 +- 18 files changed, 231 insertions(+), 203 deletions(-)
Signature · 1 line
1.9.3
PATCH v2 1/15 net: Improve error message for -net hubport a bit
Type "hubport" is valid only with -netdev. Unfortunately, that's detected late and the error message doesn't explain why:
$ qemu-system-i386 -net hubport,id=foo,hubid=0
qemu-system-i386: -net hubport,id=foo,hubid=0: Device 'hubport' could not be initializedImprove the error message to "Parameter 'type' expects a net type".
Not fixed: -net hubport without the parameters required by -netdev hubport still asks for those parameters:
$ qemu-system-i386 -net hubport
qemu-system-i386: -net hubport: Parameter 'hubid' is missing--- net/hub.c | 5 +---- net/net.c | 5 +++++ 2 files changed, 6 insertions(+), 4 deletions(-)
net/hub.c +1 -4
@@ -286,12 +286,9 @@ int net_init_hubport(const NetClientOptions *opts, const char *name,const NetdevHubPortOptions *hubport;assert (opts->kind ==NET_CLIENT_OPTIONS_KIND_HUBPORT ); +assert (!peer); hubport = opts->hubport; -if (peer) { -return -EINVAL ; - } -net_hub_add_port (hubport->hubid, name);return 0 ; }
net/net.c +5 -0
@@ -882,6 +882,11 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp) }else { u.net = object; opts = u.net->opts; +if (opts->kind ==NET_CLIENT_OPTIONS_KIND_HUBPORT ) { +error_set (errp,QERR_INVALID_PARAMETER_VALUE ,"type" , +"a net type" ); +return -1 ; + }/* missing optional values have been initialized to "all bits zero" */ name = u.net->has_id ? u.net->id : u.net->name; }
Signature · 1 line
1.9.3
PATCH v2 5/15 tap: net_tap_fd_init() can't fail, drop dead error handling
In reply to Markus Armbruster--- net/tap.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)
net/tap.c +1 -13
@@ -536,7 +536,6 @@ int net_init_bridge(const NetClientOptions *opts, const char *name,/* FIXME error_setg(errp, ...) on failure */ const NetdevBridgeOptions *bridge;const char *helper, *br; -TAPState *s;int fd, vnet_hdr;
@@ -552,14 +551,8 @@ int net_init_bridge(const NetClientOptions *opts, const char *name, }fcntl (fd,F_SETFL ,O_NONBLOCK ); - vnet_hdr =tap_probe_vnet_hdr (fd); - s =net_tap_fd_init (peer,"bridge" , name, fd, vnet_hdr); -if (!s) { -close (fd); -return -1 ; - }snprintf (s->nc.info_str,sizeof (s->nc.info_str),"helper=%s,br=%s" , helper, br);
@@ -607,14 +600,9 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, int vnet_hdr, int fd) {Error *err =NULL ; -TAPState *s; +TAPState *s =net_tap_fd_init (peer, model, name, fd, vnet_hdr);int vhostfd; - s =net_tap_fd_init (peer, model, name, fd, vnet_hdr); -if (!s) { -return -1 ; - } -if (tap_set_sndbuf (s->fd, tap) <0 ) {return -1 ; }
Signature · 1 line
1.9.3
PATCH v2 2/15 net: Permit incremental conversion of init functions to Error
In reply to Markus ArmbrusterError reporting for netdev_add is broken: the net_client_init_fun[] report the actual errors with (at best) error_report(), and their caller net_client_init1() makes up a generic error on top.
For command line and HMP, this produces an mildly ugly error cascade.
In QMP, the actual errors go to stderr, and the generic error becomes the command's error reply.
To fix this, we need to convert the net_client_init_fun[] to Error.
To permit fixing them one by one, add an Error ** parameter to the net_client_init_fun[]. If the call fails without returning an Error, make up the same generic Error as before. But if it returns one, use that instead. Since none of them does so far, no functional change.
--- net/clients.h | 20 ++++++++++---------- net/dump.c | 3 ++- net/hub.c | 2 +- net/l2tpv3.c | 5 ++--- net/net.c | 15 +++++++++------ net/netmap.c | 3 ++- net/slirp.c | 3 ++- net/socket.c | 3 ++- net/tap-win32.c | 3 ++- net/tap.c | 6 ++++-- net/vde.c | 3 ++- net/vhost-user.c | 3 ++- 12 files changed, 40 insertions(+), 29 deletions(-)
net/clients.h +10 -10
@@ -28,38 +28,38 @@#include "qapi-types.h" int net_init_dump (const NetClientOptions *opts,const char *name, -NetClientState *peer); +NetClientState *peer,Error **errp);#ifdef CONFIG_SLIRP int net_init_slirp (const NetClientOptions *opts,const char *name, -NetClientState *peer); +NetClientState *peer,Error **errp);#endif int net_init_hubport (const NetClientOptions *opts,const char *name, -NetClientState *peer); +NetClientState *peer,Error **errp);int net_init_socket (const NetClientOptions *opts,const char *name, -NetClientState *peer); +NetClientState *peer,Error **errp);int net_init_tap (const NetClientOptions *opts,const char *name, -NetClientState *peer); +NetClientState *peer,Error **errp);int net_init_bridge (const NetClientOptions *opts,const char *name, -NetClientState *peer); +NetClientState *peer,Error **errp);int net_init_l2tpv3 (const NetClientOptions *opts,const char *name, -NetClientState *peer); +NetClientState *peer,Error **errp);#ifdef CONFIG_VDE int net_init_vde (const NetClientOptions *opts,const char *name, -NetClientState *peer); +NetClientState *peer,Error **errp);#endif #ifdef CONFIG_NETMAP int net_init_netmap (const NetClientOptions *opts,const char *name, -NetClientState *peer); +NetClientState *peer,Error **errp);#endif int net_init_vhost_user (const NetClientOptions *opts,const char *name, -NetClientState *peer); +NetClientState *peer,Error **errp);#endif /* QEMU_NET_CLIENTS_H */
net/dump.c +2 -1
@@ -146,8 +146,9 @@ static int net_dump_init(NetClientState *peer, const char *device, }int net_init_dump (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ int len;const char *file;char def_file[128 ];
net/hub.c +1 -1
@@ -281,7 +281,7 @@ int net_hub_id_for_client(NetClientState *nc, int *id) }int net_init_hubport (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) {const NetdevHubPortOptions *hubport;
net/l2tpv3.c +2 -3
@@ -536,10 +536,9 @@ static NetClientInfo net_l2tpv3_info = {int net_init_l2tpv3 (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) { - - +/* FIXME error_setg(errp, ...) on failure */ const NetdevL2TPv3Options *l2tpv3;NetL2TPV3State *s;NetClientState *nc;
net/net.c +9 -6
@@ -740,8 +740,9 @@ int qemu_find_nic_model(NICInfo *nd, const char * const *models, }static int net_init_nic (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ int idx;NICInfo *nd;const NetLegacyNicOptions *nic;
@@ -809,7 +810,7 @@ static int net_init_nic(const NetClientOptions *opts, const char *name,static int (*const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX ])(const NetClientOptions *opts,const char *name, -NetClientState *peer) = { +NetClientState *peer,Error **errp) = { [NET_CLIENT_OPTIONS_KIND_NIC ] = net_init_nic, #ifdef CONFIG_SLIRP [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
@@ -902,10 +903,12 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp) peer =net_hub_add_port (u.net->has_vlan ? u.net->vlan :0 ,NULL ); } -if (net_client_init_fun[opts->kind](opts, name, peer) <0 ) { -/* TODO push error reporting into init() methods */ -error_set (errp,QERR_DEVICE_INIT_FAILED , - NetClientOptionsKind_lookup[opts->kind]); +if (net_client_init_fun[opts->kind](opts, name, peer, errp) <0 ) { +/* FIXME drop when all init functions store an Error */ +if (errp && !*errp) { +error_set (errp,QERR_DEVICE_INIT_FAILED , + NetClientOptionsKind_lookup[opts->kind]); + }return -1 ; } }
net/netmap.c +2 -1
@@ -446,8 +446,9 @@ static NetClientInfo net_netmap_info = {* ... -net netmap,ifname="..." */ int net_init_netmap (const NetClientOptions *opts, -const char *name,NetClientState *peer) +const char *name,NetClientState *peer,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ const NetdevNetmapOptions *netmap_opts = opts->netmap;NetClientState *nc;NetmapPriv me;
net/slirp.c +2 -1
@@ -737,8 +737,9 @@ static const char **slirp_dnssearch(const StringList *dnsname) }int net_init_slirp (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ struct slirp_config_str *config;char *vnet;int ret;
net/socket.c +2 -1
@@ -693,8 +693,9 @@ static int net_socket_udp_init(NetClientState *peer, }int net_init_socket (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ Error *err =NULL ;const NetdevSocketOptions *sock;
net/tap-win32.c +2 -1
@@ -752,8 +752,9 @@ static int tap_win32_init(NetClientState *peer, const char *model, }int net_init_tap (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ const NetdevTapOptions *tap;assert (opts->kind ==NET_CLIENT_OPTIONS_KIND_TAP );
net/tap.c +4 -2
@@ -531,8 +531,9 @@ static int net_bridge_run_helper(const char *helper, const char *bridge) }int net_init_bridge (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ const NetdevBridgeOptions *bridge;const char *helper, *br;
@@ -699,8 +700,9 @@ static int get_fds(char *str, char *fds[], int max) }int net_init_tap (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ const NetdevTapOptions *tap;int fd, vnet_hdr =0 , i =0 , queues;/* for the no-fd, no-helper case */
net/vde.c +2 -1
@@ -110,8 +110,9 @@ static int net_vde_init(NetClientState *peer, const char *model, }int net_init_vde (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ const NetdevVdeOptions *vde;assert (opts->kind ==NET_CLIENT_OPTIONS_KIND_VDE );
net/vhost-user.c +2 -1
@@ -223,8 +223,9 @@ static int net_vhost_check_net(QemuOpts *opts, void *opaque) }int net_init_vhost_user (const NetClientOptions *opts,const char *name, -NetClientState *peer) +NetClientState *peer,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ const NetdevVhostUserOptions *vhost_user_opts;CharDriverState *chr;
Signature · 1 line
1.9.3
PATCH v2 3/15 net: Improve -net nic error reporting
In reply to Markus ArmbrusterWhen -net nic fails, it first reports a specific error, then a generic one, like this:
$ qemu-system-x86_64 -net nic,netdev=nonexistent
qemu-system-x86_64: -net nic,netdev=nonexistent: netdev 'nonexistent' not found
qemu-system-x86_64: -net nic,netdev=nonexistent: Device 'nic' could not be initializedConvert net_init_nic() to Error to get rid of the unwanted second error message.
While there, tidy up an Overcapitalized Error Message.
--- net/net.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
net/net.c +6 -6
@@ -742,7 +742,6 @@ int qemu_find_nic_model(NICInfo *nd, const char * const *models,static int net_init_nic (const NetClientOptions *opts,const char *name,NetClientState *peer,Error **errp) { -/* FIXME error_setg(errp, ...) on failure */ int idx;NICInfo *nd;const NetLegacyNicOptions *nic;
@@ -752,7 +751,7 @@ static int net_init_nic(const NetClientOptions *opts, const char *name, idx =nic_get_free_idx ();if (idx ==-1 || nb_nics >=MAX_NICS ) { -error_report ("Too Many NICs" ); +error_setg (errp,"too many NICs" );return -1 ; }
@@ -763,7 +762,7 @@ static int net_init_nic(const NetClientOptions *opts, const char *name,if (nic->has_netdev) { nd->netdev =qemu_find_netdev (nic->netdev);if (!nd->netdev) { -error_report ("netdev '%s' not found" , nic->netdev); +error_setg (errp,"netdev '%s' not found" , nic->netdev);return -1 ; } }else {
@@ -780,19 +779,20 @@ static int net_init_nic(const NetClientOptions *opts, const char *name,if (nic->has_macaddr &&net_parse_macaddr (nd->macaddr.a, nic->macaddr) <0 ) { -error_report ("invalid syntax for ethernet address" ); +error_setg (errp,"invalid syntax for ethernet address" );return -1 ; }if (nic->has_macaddr &&is_multicast_ether_addr (nd->macaddr.a)) { -error_report ("NIC cannot have multicast MAC address (odd 1st byte)" ); +error_setg (errp, +"NIC cannot have multicast MAC address (odd 1st byte)" );return -1 ; }qemu_macaddr_default_if_unset (&nd->macaddr);if (nic->has_vectors) {if (nic->vectors >0x7ffffff ) { -error_report ("invalid # of vectors: %" PRIu32, nic->vectors); +error_setg (errp,"invalid # of vectors: %" PRIu32, nic->vectors);return -1 ; } nd->nvectors = nic->vectors;
Signature · 1 line
1.9.3
PATCH v2 4/15 net/dump: Improve -net/host_net_add dump error reporting
In reply to Markus ArmbrusterWhen -net dump fails, it first reports a specific error, then a generic one, like this:
$ qemu-system-x86_64 -net dump,id=foo,file=/eperm
qemu-system-x86_64: -net dump,id=foo,file=/eperm: -net dump: can't open /eperm
qemu-system-x86_64: -net dump,id=foo,file=/eperm: Device 'dump' could not be initializedConvert net_init_tap() to Error. This suppresses the unwanted second message.
Improve the error messages to include strerror(errno) where appropriate.
--- net/dump.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
net/dump.c +6 -6
@@ -101,7 +101,8 @@ static NetClientInfo net_dump_info = { };static int net_dump_init (NetClientState *peer,const char *device, -const char *name,const char *filename,int len) +const char *name,const char *filename,int len, +Error **errp) {struct pcap_file_hdr hdr;NetClientState *nc;
@@ -111,7 +112,7 @@ static int net_dump_init(NetClientState *peer, const char *device, fd =open (filename,O_CREAT |O_TRUNC |O_WRONLY |O_BINARY ,0644 );if (fd <0 ) { -error_report ("-net dump: can't open %s" , filename); +error_setg_errno (errp, errno,"-net dump: can't open %s" , filename);return -1 ; }
@@ -124,7 +125,7 @@ static int net_dump_init(NetClientState *peer, const char *device, hdr.linktype =1 ;if (write (fd, &hdr,sizeof (hdr)) <sizeof (hdr)) { -error_report ("-net dump write error: %s" ,strerror (errno)); +error_setg_errno (errp, errno,"-net dump write error" );close (fd);return -1 ; }
@@ -148,7 +149,6 @@ static int net_dump_init(NetClientState *peer, const char *device,int net_init_dump (const NetClientOptions *opts,const char *name,NetClientState *peer,Error **errp) { -/* FIXME error_setg(errp, ...) on failure */ int len;const char *file;char def_file[128 ];
@@ -174,7 +174,7 @@ int net_init_dump(const NetClientOptions *opts, const char *name,if (dump->has_len) {if (dump->len >INT_MAX ) { -error_report ("invalid length: %" PRIu64, dump->len); +error_setg (errp,"invalid length: %" PRIu64, dump->len);return -1 ; } len = dump->len;
@@ -182,5 +182,5 @@ int net_init_dump(const NetClientOptions *opts, const char *name, len =65536 ; } -return net_dump_init (peer,"dump" , name, file, len); +return net_dump_init (peer,"dump" , name, file, len, errp); }
Signature · 1 line
1.9.3
PATCH v2 6/15 tap: Improve -netdev/netdev_add/-net/... bridge error reporting
In reply to Markus ArmbrusterWhen -netdev bridge fails, it first reports a specific error, then a generic one, like this:
$ qemu-system-x86_64 -netdev bridge,id=foo
failed to launch bridge helper
qemu-system-x86_64: -netdev bridge,id=foo: Device 'bridge' could not be initializedThe first message goes to stderr. Wrong for HMP, because errors need to go to the monitor there.
The second message goes to stderr for -netdev, to the monitor for HMP netdev_add, and becomes the error reply for QMP netdev_add.
Convert net_bridge_run_helper() to Error, and propagate its errors through net_init_bridge(). This ensures the error gets reported where the user is, and suppresses the unwanted second message.
While there, improve the error messages a bit.
The above example becomes:
$ qemu-system-x86_64 -netdev bridge,id=foo
qemu-system-x86_64: -netdev bridge,id=foo: bridge helper failednet_init_tap() also uses net_bridge_run_helper(). Propagate its errors there as well. Improves reporting these errors with -netdev tap & friends.
--- net/tap.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
net/tap.c +19 -11
@@ -437,7 +437,8 @@ static int recv_fd(int c)return len; } -static int net_bridge_run_helper (const char *helper,const char *bridge) +static int net_bridge_run_helper (const char *helper,const char *bridge, +Error **errp) {sigset_t oldmask, mask;int pid, status;
@@ -450,11 +451,16 @@ static int net_bridge_run_helper(const char *helper, const char *bridge)sigprocmask (SIG_BLOCK , &mask, &oldmask);if (socketpair (PF_UNIX ,SOCK_STREAM ,0 , sv) ==-1 ) { +error_setg_errno (errp, errno,"socketpair() failed" );return -1 ; }/* try to launch bridge helper */ pid =fork (); +if (pid <0 ) { +error_setg_errno (errp, errno,"Can't fork bridge helper" ); +return -1 ; + }if (pid ==0 ) {int open_max =sysconf (_SC_OPEN_MAX), i;char fd_buf[6 +10 ];
@@ -502,14 +508,16 @@ static int net_bridge_run_helper(const char *helper, const char *bridge) }_exit (1 ); - }else if (pid >0 ) { + }else {int fd; +int saved_errno;close (sv[1 ]);do { fd =recv_fd (sv[0 ]); }while (fd ==-1 && errno ==EINTR ); + saved_errno = errno;close (sv[0 ]);
@@ -518,22 +526,21 @@ static int net_bridge_run_helper(const char *helper, const char *bridge) }sigprocmask (SIG_SETMASK , &oldmask,NULL );if (fd <0 ) { -fprintf (stderr,"failed to recv file descriptor\n" ); +error_setg_errno (errp, saved_errno, +"failed to recv file descriptor" );return -1 ; } - -if (WIFEXITED (status) &&WEXITSTATUS (status) ==0 ) { -return fd; +if (!WIFEXITED (status) ||WEXITSTATUS (status) !=0 ) { +error_setg (errp,"bridge helper failed" ); +return -1 ; } +return fd; } -fprintf (stderr,"failed to launch bridge helper\n" ); -return -1 ; }int net_init_bridge (const NetClientOptions *opts,const char *name,NetClientState *peer,Error **errp) { -/* FIXME error_setg(errp, ...) on failure */ const NetdevBridgeOptions *bridge;const char *helper, *br;TAPState *s;
@@ -545,7 +552,7 @@ int net_init_bridge(const NetClientOptions *opts, const char *name, helper = bridge->has_helper ? bridge->helper :DEFAULT_BRIDGE_HELPER ; br = bridge->has_br ? bridge->br :DEFAULT_BRIDGE_INTERFACE ; - fd =net_bridge_run_helper (helper, br); + fd =net_bridge_run_helper (helper, br, errp);if (fd ==-1 ) {return -1 ; }
@@ -792,7 +799,8 @@ int net_init_tap(const NetClientOptions *opts, const char *name,return -1 ; } - fd =net_bridge_run_helper (tap->helper,DEFAULT_BRIDGE_INTERFACE ); + fd =net_bridge_run_helper (tap->helper,DEFAULT_BRIDGE_INTERFACE , + errp);if (fd ==-1 ) {return -1 ; }
Signature · 1 line
1.9.3
PATCH v2 7/15 tap: Convert tap_set_sndbuf() to Error
In reply to Markus Armbruster--- net/tap-aix.c | 3 +-- net/tap-bsd.c | 3 +-- net/tap-haiku.c | 3 +-- net/tap-linux.c | 6 ++---- net/tap-solaris.c | 3 +-- net/tap.c | 4 +++- net/tap_int.h | 2 +- 7 files changed, 10 insertions(+), 14 deletions(-)
net/tap-aix.c +1 -2
@@ -32,9 +32,8 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,return -1 ; } -int tap_set_sndbuf (int fd,const NetdevTapOptions *tap) +void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { -return 0 ; }int tap_probe_vnet_hdr (int fd)
net/tap-bsd.c +1 -2
@@ -176,9 +176,8 @@ error: }#endif /* __FreeBSD__ */ -int tap_set_sndbuf (int fd,const NetdevTapOptions *tap) +void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { -return 0 ; }int tap_probe_vnet_hdr (int fd)
net/tap-haiku.c +1 -2
@@ -32,9 +32,8 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,return -1 ; } -int tap_set_sndbuf (int fd,const NetdevTapOptions *tap) +void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { -return 0 ; }int tap_probe_vnet_hdr (int fd)
net/tap-linux.c +2 -4
@@ -126,7 +126,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,*/ #define TAP_DEFAULT_SNDBUF 0 -int tap_set_sndbuf (int fd,const NetdevTapOptions *tap) +void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) {int sndbuf;
@@ -139,10 +139,8 @@ int tap_set_sndbuf(int fd, const NetdevTapOptions *tap) }if (ioctl (fd,TUNSETSNDBUF , &sndbuf) ==-1 && tap->has_sndbuf) { -error_report ("TUNSETSNDBUF ioctl failed: %s" ,strerror (errno)); -return -1 ; +error_setg_errno (errp, errno,"TUNSETSNDBUF ioctl failed" ); } -return 0 ; }int tap_probe_vnet_hdr (int fd)
net/tap-solaris.c +1 -2
@@ -198,9 +198,8 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,return fd; } -int tap_set_sndbuf (int fd,const NetdevTapOptions *tap) +void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { -return 0 ; }int tap_probe_vnet_hdr (int fd)
net/tap.c +3 -1
@@ -610,7 +610,9 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,TAPState *s =net_tap_fd_init (peer, model, name, fd, vnet_hdr);int vhostfd; -if (tap_set_sndbuf (s->fd, tap) <0 ) { +tap_set_sndbuf (s->fd, tap, &err); +if (err) { +error_report_err (err);return -1 ; }
net/tap_int.h +1 -1
@@ -34,7 +34,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,ssize_t tap_read_packet (int tapfd,uint8_t *buf,int maxlen); -int tap_set_sndbuf (int fd,const NetdevTapOptions *tap); +void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp);int tap_probe_vnet_hdr (int fd);int tap_probe_vnet_hdr_len (int fd,int len);int tap_probe_has_ufo (int fd);
Signature · 1 line
1.9.3
PATCH v2 9/15 tap: Convert launch_script() to Error
In reply to Markus ArmbrusterFixes inappropriate use of stderr in monitor command handler.
While there, improve the messages some.
--- net/tap.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-)
net/tap.c +28 -12
@@ -59,7 +59,8 @@ typedef struct TAPState {unsigned host_vnet_hdr_len; } TAPState; -static int launch_script (const char *setup_script,const char *ifname,int fd); +static void launch_script (const char *setup_script,const char *ifname, +int fd,Error **errp);static int tap_can_send (void *opaque);static void tap_send (void *opaque);
@@ -288,6 +289,7 @@ static void tap_set_offload(NetClientState *nc, int csum, int tso4,static void tap_cleanup (NetClientState *nc) {TAPState *s =DO_UPCAST (TAPState, nc, nc); +Error **err =NULL ;if (s->vhost_net) {vhost_net_cleanup (s->vhost_net);
@@ -296,8 +298,12 @@ static void tap_cleanup(NetClientState *nc)qemu_purge_queued_packets (nc); -if (s->down_script[0 ]) -launch_script (s->down_script, s->down_script_arg, s->fd); +if (s->down_script[0 ]) { +launch_script (s->down_script, s->down_script_arg, s->fd, &err); +if (err) { +error_report_err (err); + } + }tap_read_poll (s, false);tap_write_poll (s, false);
@@ -368,7 +374,8 @@ static TAPState *net_tap_fd_init(NetClientState *peer,return s; } -static int launch_script (const char *setup_script,const char *ifname,int fd) +static void launch_script (const char *setup_script,const char *ifname, +int fd,Error **errp) {int pid, status;char *args[3 ];
@@ -376,6 +383,11 @@ static int launch_script(const char *setup_script, const char *ifname, int fd)/* try to launch network script */ pid =fork (); +if (pid <0 ) { +error_setg_errno (errp, errno,"could not launch network script %s" , + setup_script); +return ; + }if (pid ==0 ) {int open_max =sysconf (_SC_OPEN_MAX), i;
@@ -390,17 +402,17 @@ static int launch_script(const char *setup_script, const char *ifname, int fd) *parg =NULL ;execv (setup_script, args);_exit (1 ); - }else if (pid >0 ) { + }else {while (waitpid (pid, &status,0 ) != pid) {/* loop */ }if (WIFEXITED (status) &&WEXITSTATUS (status) ==0 ) { -return 0 ; +return ; } +error_setg (errp,"network script %s failed with status %d" , + setup_script, status); } -fprintf (stderr,"%s: could not launch network script\n" , setup_script); -return -1 ; }static int recv_fd (int c)
@@ -571,6 +583,7 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, const char *setup_script, char *ifname, size_t ifname_sz, int mq_required) { + Error **err = NULL;int fd, vnet_hdr_required;if (tap->has_vnet_hdr) {
@@ -589,10 +602,13 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,if (setup_script && setup_script[0 ] !='\0' && -strcmp (setup_script,"no" ) !=0 && -launch_script (setup_script, ifname, fd)) { -close (fd); -return -1 ; +strcmp (setup_script,"no" ) !=0 ) { +launch_script (setup_script, ifname, fd, &err); +if (err) { +error_report_err (err); +close (fd); +return -1 ; + } }return fd;
Signature · 1 line
1.9.3
PATCH v2 11/15 tap-linux: Convert tap_open() to Error
In reply to Markus Armbruster--- net/tap-linux.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
net/tap-linux.c +9 -8
@@ -39,7 +39,6 @@int tap_open (char *ifname,int ifname_size,int *vnet_hdr,int vnet_hdr_required,int mq_required,Error **errp) { -/* FIXME error_setg(errp, ...) on failure */ struct ifreq ifr;int fd, ret;int len =sizeof (struct virtio_net_hdr );
@@ -47,7 +46,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,TFR (fd =open (PATH_NET_TUN ,O_RDWR ));if (fd <0 ) { -error_report ("could not open %s: %m" ,PATH_NET_TUN ); +error_setg_errno (errp, errno,"could not open %s" ,PATH_NET_TUN );return -1 ; }memset (&ifr,0 ,sizeof (ifr));
@@ -71,8 +70,8 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, }if (vnet_hdr_required && !*vnet_hdr) { -error_report ("vnet_hdr=1 requested, but no kernel " -"support for IFF_VNET_HDR available" ); +error_setg (errp,"vnet_hdr=1 requested, but no kernel " +"support for IFF_VNET_HDR available" );close (fd);return -1 ; }
@@ -87,8 +86,8 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,if (mq_required) {if (!(features &IFF_MULTI_QUEUE )) { -error_report ("multiqueue required, but no kernel " -"support for IFF_MULTI_QUEUE available" ); +error_setg (errp,"multiqueue required, but no kernel " +"support for IFF_MULTI_QUEUE available" );close (fd);return -1 ; }else {
@@ -103,9 +102,11 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, ret =ioctl (fd,TUNSETIFF , (void *) &ifr);if (ret !=0 ) {if (ifname[0 ] !='\0' ) { -error_report ("could not configure %s (%s): %m" ,PATH_NET_TUN , ifr.ifr_name); +error_setg_errno (errp, errno,"could not configure %s (%s)" , +PATH_NET_TUN , ifr.ifr_name); }else { -error_report ("could not configure %s: %m" ,PATH_NET_TUN ); +error_setg_errno (errp, errno,"could not configure %s" , +PATH_NET_TUN ); }close (fd);return -1 ;
Signature · 1 line
1.9.3
PATCH v2 12/15 tap-bsd: Convert tap_open() to Error
In reply to Markus ArmbrusterFixes inappropriate use of stderr in monitor command handler.
While there, improve some of the messages a bit.
--- net/tap-bsd.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-)
net/tap-bsd.c +14 -19
@@ -37,7 +37,6 @@int tap_open (char *ifname,int ifname_size,int *vnet_hdr,int vnet_hdr_required,int mq_required,Error **errp) { -/* FIXME error_setg(errp, ...) on failure */ int fd;#ifdef TAPGIFNAME struct ifreq ifr;
@@ -72,23 +71,19 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, } }if (fd <0 ) { -error_report ("warning: could not open %s (%s): no virtual network emulation" , - dname,strerror (errno)); +error_setg_errno (errp, errno,"could not open %s" , dname);return -1 ; }#ifdef TAPGIFNAME if (ioctl (fd,TAPGIFNAME , (void *)&ifr) <0 ) { -fprintf (stderr,"warning: could not get tap name: %s\n" , -strerror (errno)); +error_setg_errno (errp, errno,"could not get tap name" );return -1 ; }pstrcpy (ifname, ifname_size, ifr.ifr_name);#else if (fstat (fd, &s) <0 ) { -fprintf (stderr, -"warning: could not stat /dev/tap: no virtual network emulation: %s\n" , -strerror (errno)); +error_setg_errno (errp, errno,"could not stat %s" , dname);return -1 ; } dev =devname (s.st_rdev,S_IFCHR );
@@ -100,8 +95,8 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, *vnet_hdr =0 ;if (vnet_hdr_required && !*vnet_hdr) { -error_report ("vnet_hdr=1 requested, but no kernel " -"support for IFF_VNET_HDR available" ); +error_setg (errp,"vnet_hdr=1 requested, but no kernel " +"support for IFF_VNET_HDR available" );close (fd);return -1 ; }
@@ -117,13 +112,12 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,int tap_open (char *ifname,int ifname_size,int *vnet_hdr,int vnet_hdr_required,int mq_required,Error **errp) { -/* FIXME error_setg(errp, ...) on failure */ int fd, s, ret;struct ifreq ifr;TFR (fd =open (PATH_NET_TAP ,O_RDWR ));if (fd <0 ) { -error_report ("could not open %s: %s" ,PATH_NET_TAP ,strerror (errno)); +error_setg_errno (errp, errno,"could not open %s" ,PATH_NET_TAP );return -1 ; }
@@ -131,7 +125,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, ret =ioctl (fd,TAPGIFNAME , (void *)&ifr);if (ret <0 ) { -error_report ("could not get tap interface name" ); +error_setg_errno (errp, errno,"could not get tap interface name" ); goto error; }
@@ -139,14 +133,15 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,/* User requested the interface to have a specific name */ s =socket (AF_LOCAL ,SOCK_DGRAM ,0 );if (s <0 ) { -error_report ("could not open socket to set interface name" ); +error_setg_errno (errp, errno, +"could not open socket to set interface name" ); goto error; } ifr.ifr_data = ifname; ret =ioctl (s,SIOCSIFNAME , (void *)&ifr);close (s);if (ret <0 ) { -error_report ("could not set tap interface name" ); +error_setg (errp,"could not set tap interface name" ); goto error; } }else {
@@ -158,14 +153,14 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, *vnet_hdr =0 ;if (vnet_hdr_required && !*vnet_hdr) { -error_report ("vnet_hdr=1 requested, but no kernel " -"support for IFF_VNET_HDR available" ); +error_setg (errp,"vnet_hdr=1 requested, but no kernel " +"support for IFF_VNET_HDR available" ); goto error; } }if (mq_required) { -error_report ("mq_required requested, but not kernel support" -"for IFF_MULTI_QUEUE available" ); +error_setg (errp,"mq_required requested, but no kernel support" +" for IFF_MULTI_QUEUE available" ); goto error; }
Signature · 1 line
1.9.3
PATCH v2 14/15 tap: Finish conversion of tap_open() to Error
In reply to Markus Armbruster--- net/tap.c | 4 ---- 1 file changed, 4 deletions(-)
net/tap.c +0 -4
@@ -597,10 +597,6 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,TFR (fd =tap_open (ifname, ifname_sz, vnet_hdr, vnet_hdr_required, mq_required, errp));if (fd <0 ) { -/* FIXME drop when all tap_open() store an Error */ -if (errp && !*errp) { -error_setg (errp,"can't open tap device" ); - }return -1 ; }
Signature · 1 line
1.9.3
PATCH v2 8/15 tap: Convert net_init_tap_one() to Error
In reply to Markus Armbruster--- net/tap.c | 70 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 32 deletions(-)
net/tap.c +38 -32
@@ -600,11 +600,11 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,#define MAX_TAP_QUEUES 1024 -static int net_init_tap_one (const NetdevTapOptions *tap,NetClientState *peer, -const char *model,const char *name, -const char *ifname,const char *script, -const char *downscript,const char *vhostfdname, -int vnet_hdr,int fd) +static void net_init_tap_one (const NetdevTapOptions *tap,NetClientState *peer, +const char *model,const char *name, +const char *ifname,const char *script, +const char *downscript,const char *vhostfdname, +int vnet_hdr,int fd,Error **errp) {Error *err =NULL ;TAPState *s =net_tap_fd_init (peer, model, name, fd, vnet_hdr);
@@ -612,8 +612,8 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,tap_set_sndbuf (s->fd, tap, &err);if (err) { -error_report_err (err); -return -1 ; +error_propagate (errp, err); +return ; }if (tap->has_fd || tap->has_fds) {
@@ -644,30 +644,28 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,if (tap->has_vhostfd || tap->has_vhostfds) { vhostfd =monitor_fd_param (cur_mon, vhostfdname, &err);if (vhostfd ==-1 ) { -error_report_err (err); -return -1 ; +error_propagate (errp, err); +return ; } }else { vhostfd =open ("/dev/vhost-net" ,O_RDWR );if (vhostfd <0 ) { -error_report ("tap: open vhost char device failed: %s" , -strerror (errno)); -return -1 ; +error_setg_errno (errp, errno, +"tap: open vhost char device failed: %s" ); +return ; } } options.opaque = (void *)(uintptr_t )vhostfd; s->vhost_net =vhost_net_init (&options);if (!s->vhost_net) { -error_report ("vhost-net requested but could not be initialized" ); -return -1 ; +error_setg (errp, +"vhost-net requested but could not be initialized" ); +return ; } }else if (tap->has_vhostfd || tap->has_vhostfds) { -error_report ("vhostfd= is not valid without vhost" ); -return -1 ; +error_setg (errp,"vhostfd= is not valid without vhost" ); } - -return 0 ; }static int get_fds (char *str,char *fds[],int max)
@@ -741,9 +739,11 @@ int net_init_tap(const NetClientOptions *opts, const char *name, vnet_hdr =tap_probe_vnet_hdr (fd); -if (net_init_tap_one (tap, peer,"tap" , name,NULL , - script, downscript, - vhostfdname, vnet_hdr, fd)) { +net_init_tap_one (tap, peer,"tap" , name,NULL , + script, downscript, + vhostfdname, vnet_hdr, fd, &err); +if (err) { +error_report_err (err);return -1 ; } }else if (tap->has_fds) {
@@ -786,10 +786,12 @@ int net_init_tap(const NetClientOptions *opts, const char *name,return -1 ; } -if (net_init_tap_one (tap, peer,"tap" , name, ifname, - script, downscript, - tap->has_vhostfds ? vhost_fds[i] :NULL , - vnet_hdr, fd)) { +net_init_tap_one (tap, peer,"tap" , name, ifname, + script, downscript, + tap->has_vhostfds ? vhost_fds[i] :NULL , + vnet_hdr, fd, &err); +if (err) { +error_report_err (err);return -1 ; } }
@@ -810,9 +812,11 @@ int net_init_tap(const NetClientOptions *opts, const char *name,fcntl (fd,F_SETFL ,O_NONBLOCK ); vnet_hdr =tap_probe_vnet_hdr (fd); -if (net_init_tap_one (tap, peer,"bridge" , name, ifname, - script, downscript, vhostfdname, - vnet_hdr, fd)) { +net_init_tap_one (tap, peer,"bridge" , name, ifname, + script, downscript, vhostfdname, + vnet_hdr, fd, &err); +if (err) { +error_report_err (err);close (fd);return -1 ; }
@@ -846,10 +850,12 @@ int net_init_tap(const NetClientOptions *opts, const char *name, } } -if (net_init_tap_one (tap, peer,"tap" , name, ifname, - i >=1 ?"no" : script, - i >=1 ?"no" : downscript, - vhostfdname, vnet_hdr, fd)) { +net_init_tap_one (tap, peer,"tap" , name, ifname, + i >=1 ?"no" : script, + i >=1 ?"no" : downscript, + vhostfdname, vnet_hdr, fd, &err); +if (err) { +error_report_err (err);close (fd);return -1 ; }
Signature · 1 line
1.9.3
PATCH v2 10/15 tap: Permit incremental conversion of tap_open() to Error
In reply to Markus ArmbrusterConvert the trivial ones immediately: tap-aix and tap-haiku.
--- net/tap-aix.c | 4 ++-- net/tap-bsd.c | 6 ++++-- net/tap-haiku.c | 4 ++-- net/tap-linux.c | 3 ++- net/tap-solaris.c | 3 ++- net/tap.c | 13 +++++++++---- net/tap_int.h | 2 +- 7 files changed, 22 insertions(+), 13 deletions(-)
net/tap-aix.c +2 -2
@@ -26,9 +26,9 @@#include <stdio.h> int tap_open (char *ifname,int ifname_size,int *vnet_hdr, -int vnet_hdr_required,int mq_required) +int vnet_hdr_required,int mq_required,Error **errp) { -fprintf (stderr,"no tap on AIX\n" ); +error_setg (errp,"no tap on AIX" );return -1 ; }
net/tap-bsd.c +4 -2
@@ -35,8 +35,9 @@#ifndef __FreeBSD__int tap_open (char *ifname,int ifname_size,int *vnet_hdr, -int vnet_hdr_required,int mq_required) +int vnet_hdr_required,int mq_required,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ int fd;#ifdef TAPGIFNAME struct ifreq ifr;
@@ -114,8 +115,9 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,#define PATH_NET_TAP "/dev/tap"int tap_open (char *ifname,int ifname_size,int *vnet_hdr, -int vnet_hdr_required,int mq_required) +int vnet_hdr_required,int mq_required,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ int fd, s, ret;struct ifreq ifr;
net/tap-haiku.c +2 -2
@@ -26,9 +26,9 @@#include <stdio.h> int tap_open (char *ifname,int ifname_size,int *vnet_hdr, -int vnet_hdr_required,int mq_required) +int vnet_hdr_required,int mq_required,Error **errp) { -fprintf (stderr,"no tap on Haiku\n" ); +error_setg (errp,"no tap on Haiku" );return -1 ; }
net/tap-linux.c +2 -1
@@ -37,8 +37,9 @@#define PATH_NET_TUN "/dev/net/tun"int tap_open (char *ifname,int ifname_size,int *vnet_hdr, -int vnet_hdr_required,int mq_required) +int vnet_hdr_required,int mq_required,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ struct ifreq ifr;int fd, ret;int len =sizeof (struct virtio_net_hdr );
net/tap-solaris.c +2 -1
@@ -174,8 +174,9 @@ static int tap_alloc(char *dev, size_t dev_size) }int tap_open (char *ifname,int ifname_size,int *vnet_hdr, -int vnet_hdr_required,int mq_required) +int vnet_hdr_required,int mq_required,Error **errp) { +/* FIXME error_setg(errp, ...) on failure */ char dev[10 ]="" ;int fd;if ( (fd =tap_alloc (dev,sizeof (dev))) <0 ){
net/tap.c +9 -4
@@ -581,7 +581,7 @@ int net_init_bridge(const NetClientOptions *opts, const char *name,static int net_tap_init (const NetdevTapOptions *tap,int *vnet_hdr,const char *setup_script,char *ifname, -size_t ifname_sz,int mq_required) +size_t ifname_sz,int mq_required,Error **errp) {Error **err =NULL ;int fd, vnet_hdr_required;
@@ -595,8 +595,12 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, }TFR (fd =tap_open (ifname, ifname_sz, vnet_hdr, vnet_hdr_required, - mq_required)); + mq_required, errp));if (fd <0 ) { +/* FIXME drop when all tap_open() store an Error */ +if (errp && !*errp) { +error_setg (errp,"can't open tap device" ); + }return -1 ; }
@@ -605,7 +609,7 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, strcmp(setup_script, "no") != 0) {launch_script (setup_script, ifname, fd, &err);if (err) { -error_report_err (err); +error_propagate (errp, err);close (fd);return -1 ; }
@@ -853,8 +857,9 @@ int net_init_tap(const NetClientOptions *opts, const char *name,for (i =0 ; i < queues; i++) { fd =net_tap_init (tap, &vnet_hdr, i >=1 ?"no" : script, - ifname,sizeof ifname, queues >1 ); + ifname,sizeof ifname, queues >1 , &err);if (fd ==-1 ) { +error_report_err (err);return -1 ; }
net/tap_int.h +1 -1
@@ -30,7 +30,7 @@#include "qapi-types.h" int tap_open (char *ifname,int ifname_size,int *vnet_hdr, -int vnet_hdr_required,int mq_required); +int vnet_hdr_required,int mq_required,Error **errp);ssize_t tap_read_packet (int tapfd,uint8_t *buf,int maxlen);
Signature · 1 line
1.9.3
PATCH v2 13/15 tap-solaris: Convert tap_open() to Error
In reply to Markus ArmbrusterFixes inappropriate use of syslog().
Not fixed: leaks on error paths, suspicious non-fatal errors. FIXMEs added instead.
--- net/tap-solaris.c | 59 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 29 deletions(-)
net/tap-solaris.c +30 -29
@@ -36,7 +36,6 @@#include <netinet/udp.h> #include <netinet/tcp.h> #include <net/if.h> -#include <syslog.h> #include <stropts.h> #include "qemu/error-report.h"
@@ -56,8 +55,10 @@ ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)* Allocate TAP device, returns opened fd. * Stores dev name in the first arg(must be large enough). */ -static int tap_alloc (char *dev,size_t dev_size) +static int tap_alloc (char *dev,size_t dev_size,Error **errp) { +/* FIXME leaks like a sieve on error paths */ +/* FIXME suspicious: many errors are reported, then ignored */ int tap_fd, if_fd, ppa =-1 ;static int ip_fd =0 ;char *ptr;
@@ -83,14 +84,14 @@ static int tap_alloc(char *dev, size_t dev_size)TFR (ip_fd =open ("/dev/udp" ,O_RDWR ,0 ));if (ip_fd <0 ) { -syslog (LOG_ERR ,"Can't open /dev/ip (actually /dev/udp)" ); -return -1 ; +error_setg (errp,"Can't open /dev/ip (actually /dev/udp)" ); +return -1 ; }TFR (tap_fd =open ("/dev/tap" ,O_RDWR ,0 ));if (tap_fd <0 ) { -syslog (LOG_ERR ,"Can't open /dev/tap" ); -return -1 ; +error_setg (errp,"Can't open /dev/tap" ); +return -1 ; }/* Assign a new PPA and get its unit number. */
@@ -99,20 +100,20 @@ static int tap_alloc(char *dev, size_t dev_size) strioc_ppa.ic_len =sizeof (ppa); strioc_ppa.ic_dp = (char *)&ppa;if ((ppa =ioctl (tap_fd,I_STR , &strioc_ppa)) <0 ) -syslog (LOG_ERR ,"Can't assign new interface" ); +error_report ("Can't assign new interface" );TFR (if_fd =open ("/dev/tap" ,O_RDWR ,0 ));if (if_fd <0 ) { -syslog (LOG_ERR ,"Can't open /dev/tap (2)" ); -return -1 ; +error_setg (errp,"Can't open /dev/tap (2)" ); +return -1 ; }if (ioctl (if_fd,I_PUSH ,"ip" ) <0 ){ -syslog (LOG_ERR ,"Can't push IP module" ); -return -1 ; +error_setg (errp,"Can't push IP module" ); +return -1 ; }if (ioctl (if_fd,SIOCGLIFFLAGS , &ifr) <0 ) -syslog (LOG_ERR ,"Can't get flags\n" ); +error_report ("Can't get flags" );snprintf (actual_name,32 ,"tap%d" , ppa);pstrcpy (ifr.lifr_name,sizeof (ifr.lifr_name), actual_name);
@@ -121,22 +122,22 @@ static int tap_alloc(char *dev, size_t dev_size)/* Assign ppa according to the unit number returned by tun device */ if (ioctl (if_fd,SIOCSLIFNAME , &ifr) <0 ) -syslog (LOG_ERR ,"Can't set PPA %d" , ppa); +error_report ("Can't set PPA %d" , ppa);if (ioctl (if_fd,SIOCGLIFFLAGS , &ifr) <0 ) -syslog (LOG_ERR ,"Can't get flags\n" ); +error_report ("Can't get flags" );/* Push arp module to if_fd */ if (ioctl (if_fd,I_PUSH ,"arp" ) <0 ) -syslog (LOG_ERR ,"Can't push ARP module (2)" ); +error_report ("Can't push ARP module (2)" );/* Push arp module to ip_fd */ if (ioctl (ip_fd,I_POP ,NULL ) <0 ) -syslog (LOG_ERR ,"I_POP failed\n" ); +error_report ("I_POP failed" );if (ioctl (ip_fd,I_PUSH ,"arp" ) <0 ) -syslog (LOG_ERR ,"Can't push ARP module (3)\n" ); +error_report ("Can't push ARP module (3)" );/* Open arp_fd */ TFR (arp_fd =open ("/dev/tap" ,O_RDWR ,0 ));if (arp_fd <0 ) -syslog (LOG_ERR ,"Can't open %s\n" ,"/dev/tap" ); +error_report ("Can't open %s" ,"/dev/tap" );/* Set ifname to arp */ strioc_if.ic_cmd =SIOCSLIFNAME ;
@@ -144,16 +145,16 @@ static int tap_alloc(char *dev, size_t dev_size) strioc_if.ic_len =sizeof (ifr); strioc_if.ic_dp = (char *)𝔦if (ioctl (arp_fd,I_STR , &strioc_if) <0 ){ -syslog (LOG_ERR ,"Can't set ifname to arp\n" ); +error_report ("Can't set ifname to arp" ); }if ((ip_muxid =ioctl (ip_fd,I_LINK , if_fd)) <0 ){ -syslog (LOG_ERR ,"Can't link TAP device to IP" ); -return -1 ; +error_setg (errp,"Can't link TAP device to IP" ); +return -1 ; }if ((arp_muxid =ioctl (ip_fd, link_type, arp_fd)) <0 ) -syslog (LOG_ERR ,"Can't link TAP device to ARP" ); +error_report ("Can't link TAP device to ARP" );close (if_fd);
@@ -166,7 +167,7 @@ static int tap_alloc(char *dev, size_t dev_size) {ioctl (ip_fd,I_PUNLINK , arp_muxid);ioctl (ip_fd,I_PUNLINK , ip_muxid); -syslog (LOG_ERR ,"Can't set multiplexor id" ); +error_report ("Can't set multiplexor id" ); }snprintf (dev, dev_size,"tap%d" , ppa);
@@ -176,12 +177,12 @@ static int tap_alloc(char *dev, size_t dev_size)int tap_open (char *ifname,int ifname_size,int *vnet_hdr,int vnet_hdr_required,int mq_required,Error **errp) { -/* FIXME error_setg(errp, ...) on failure */ char dev[10 ]="" ;int fd; -if ( (fd =tap_alloc (dev,sizeof (dev))) <0 ){ -fprintf (stderr,"Cannot allocate TAP device\n" ); -return -1 ; + + fd =tap_alloc (dev,sizeof (dev), errp); +if (fd <0 ) { +return -1 ; }pstrcpy (ifname, ifname_size, dev);if (*vnet_hdr) {
@@ -189,8 +190,8 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, *vnet_hdr =0 ;if (vnet_hdr_required && !*vnet_hdr) { -error_report ("vnet_hdr=1 requested, but no kernel " -"support for IFF_VNET_HDR available" ); +error_setg (errp,"vnet_hdr=1 requested, but no kernel " +"support for IFF_VNET_HDR available" );close (fd);return -1 ; }
Signature · 1 line
1.9.3
PATCH v2 15/15 tap: Improve -netdev/netdev_add/-net/... tap error reporting
In reply to Markus ArmbrusterWhen -netdev tap fails, it first reports a specific error, then a generic one, like this:
$ qemu-system-x86_64 -netdev tap,id=foo
qemu-system-x86_64: -netdev tap,id=foo: could not configure /dev/net/tun: Operation not permitted
qemu-system-x86_64: -netdev tap,id=foo: Device 'tap' could not be initializedWith the command line, the messages go to stderr. In HMP, they go to the monitor. In QMP, the second one becomes the error reply, and the first one goes to stderr.
Convert net_init_tap() to Error. This suppresses the unwanted second message, and makes the specific error the QMP error reply.
--- net/tap.c | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-)
net/tap.c +22 -23
@@ -713,7 +713,6 @@ static int get_fds(char *str, char *fds[], int max)int net_init_tap (const NetClientOptions *opts,const char *name,NetClientState *peer,Error **errp) { -/* FIXME error_setg(errp, ...) on failure */ const NetdevTapOptions *tap;int fd, vnet_hdr =0 , i =0 , queues;/* for the no-fd, no-helper case */
@@ -731,7 +730,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name,/* QEMU vlans does not support multiqueue tap, in this case peer is set. * For -netdev, peer is always NULL. */ if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) { -error_report ("Multiqueue tap cannot be used with QEMU vlans" ); +error_setg (errp,"Multiqueue tap cannot be used with QEMU vlans" );return -1 ; }
@@ -739,15 +738,15 @@ int net_init_tap(const NetClientOptions *opts, const char *name,if (tap->has_ifname || tap->has_script || tap->has_downscript || tap->has_vnet_hdr || tap->has_helper || tap->has_queues || tap->has_fds || tap->has_vhostfds) { -error_report ("ifname=, script=, downscript=, vnet_hdr=, " -"helper=, queues=, fds=, and vhostfds= " -"are invalid with fd=" ); +error_setg (errp,"ifname=, script=, downscript=, vnet_hdr=, " +"helper=, queues=, fds=, and vhostfds= " +"are invalid with fd=" );return -1 ; } fd =monitor_fd_param (cur_mon, tap->fd, &err);if (fd ==-1 ) { -error_report_err (err); +error_propagate (errp, err);return -1 ; }
@@ -759,7 +758,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name, script, downscript, vhostfdname, vnet_hdr, fd, &err);if (err) { -error_report_err (err); +error_propagate (errp, err);return -1 ; } }else if (tap->has_fds) {
@@ -770,9 +769,9 @@ int net_init_tap(const NetClientOptions *opts, const char *name,if (tap->has_ifname || tap->has_script || tap->has_downscript || tap->has_vnet_hdr || tap->has_helper || tap->has_queues || tap->has_vhostfd) { -error_report ("ifname=, script=, downscript=, vnet_hdr=, " -"helper=, queues=, and vhostfd= " -"are invalid with fds=" ); +error_setg (errp,"ifname=, script=, downscript=, vnet_hdr=, " +"helper=, queues=, and vhostfd= " +"are invalid with fds=" );return -1 ; }
@@ -780,8 +779,8 @@ int net_init_tap(const NetClientOptions *opts, const char *name,if (tap->has_vhostfds) { nvhosts =get_fds (tap->vhostfds, vhost_fds,MAX_TAP_QUEUES );if (nfds != nvhosts) { -error_report ("The number of fds passed does not match the " -"number of vhostfds passed" ); +error_setg (errp,"The number of fds passed does not match " +"the number of vhostfds passed" );return -1 ; } }
@@ -789,7 +788,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name,for (i =0 ; i < nfds; i++) { fd =monitor_fd_param (cur_mon, fds[i], &err);if (fd ==-1 ) { -error_report_err (err); +error_propagate (errp, err);return -1 ; }
@@ -798,7 +797,8 @@ int net_init_tap(const NetClientOptions *opts, const char *name,if (i ==0 ) { vnet_hdr =tap_probe_vnet_hdr (fd); }else if (vnet_hdr !=tap_probe_vnet_hdr (fd)) { -error_report ("vnet_hdr not consistent across given tap fds" ); +error_setg (errp, +"vnet_hdr not consistent across given tap fds" );return -1 ; }
@@ -807,15 +807,15 @@ int net_init_tap(const NetClientOptions *opts, const char *name, tap->has_vhostfds ? vhost_fds[i] :NULL , vnet_hdr, fd, &err);if (err) { -error_report_err (err); +error_propagate (errp, err);return -1 ; } } }else if (tap->has_helper) {if (tap->has_ifname || tap->has_script || tap->has_downscript || tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) { -error_report ("ifname=, script=, downscript=, and vnet_hdr= " -"queues=, and vhostfds= are invalid with helper=" ); +error_setg (errp,"ifname=, script=, downscript=, and vnet_hdr=, " +"queues=, and vhostfds= are invalid with helper=" );return -1 ; }
@@ -832,13 +832,13 @@ int net_init_tap(const NetClientOptions *opts, const char *name, script, downscript, vhostfdname, vnet_hdr, fd, &err);if (err) { -error_report_err (err); +error_propagate (errp, err);close (fd);return -1 ; } }else {if (tap->has_vhostfds) { -error_report ("vhostfds= is invalid if fds= wasn't specified" ); +error_setg (errp,"vhostfds= is invalid if fds= wasn't specified" );return -1 ; } script = tap->has_script ? tap->script :DEFAULT_NETWORK_SCRIPT ;
@@ -853,15 +853,14 @@ int net_init_tap(const NetClientOptions *opts, const char *name,for (i =0 ; i < queues; i++) { fd =net_tap_init (tap, &vnet_hdr, i >=1 ?"no" : script, - ifname,sizeof ifname, queues >1 , &err); + ifname,sizeof ifname, queues >1 , errp);if (fd ==-1 ) { -error_report_err (err);return -1 ; }if (queues >1 && i ==0 && !tap->has_ifname) {if (tap_fd_get_ifname (fd, ifname)) { -error_report ("Fail to get ifname" ); +error_setg (errp,"Fail to get ifname" );close (fd);return -1 ; }
@@ -872,7 +871,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name, i >=1 ?"no" : downscript, vhostfdname, vnet_hdr, fd, &err);if (err) { -error_report_err (err); +error_propagate (errp, err);close (fd);return -1 ; }
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v2 12/15] tap-bsd: Convert tap_open() to Error
In reply to Markus Armbruster[series has a typo in my email address, for anyone doing reply-all - but at least I spotted it on list]
Quoted 9 lines · Markus Armbruster
Fixes inappropriate use of stderr in monitor command handler.
While there, improve some of the messages a bit.
Signed-off-by Markus Armbruster <[email protected]>--- net/tap-bsd.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v2 15/15] tap: Improve -netdev/netdev_add/-net/... tap error reporting
In reply to Markus ArmbrusterQuoted 27 lines · Markus Armbruster
When -netdev tap fails, it first reports a specific error, then a generic one, like this:
$ qemu-system-x86_64 -netdev tap,id=foo qemu-system-x86_64: -netdev tap,id=foo: could not configure /dev/net/tun: Operation not permitted qemu-system-x86_64: -netdev tap,id=foo: Device 'tap' could not be initializedWith the command line, the messages go to stderr. In HMP, they go to the monitor. In QMP, the second one becomes the error reply, and the first one goes to stderr.
Convert
net_init_tap()to Error. This suppresses the unwanted second message, and makes the specific error the QMP error reply.Signed-off-by Markus Armbruster <[email protected]>Reviewed-by Eric Blake <[email protected]>--- net/tap.c | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-)net/tap.c +1
-1@@ -807,15 +807,15 @@ int net_init_tap(const NetClientOptions *opts, const char *name, tap->has_vhostfds ? vhost_fds[i] :NULL , vnet_hdr, fd, &err);if (err) { -error_report_err (err); +error_propagate (errp, err);
net/tap.c int net_init_tap(const NetClientOptions *opts, const char *name, · line 818
Quoted 10 lines
net/tap.c +2
-2return -1 ; } } }else if (tap->has_helper) {if (tap->has_ifname || tap->has_script || tap->has_downscript || tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) { -error_report ("ifname=, script=, downscript=, and vnet_hdr= " -"queues=, and vhostfds= are invalid with helper=" ); +error_setg (errp,"ifname=, script=, downscript=, and vnet_hdr=, " +"queues=, and vhostfds= are invalid with helper=" );
Still might be worth deleting the first 'and'; committer could do that when preparing the pull request. But my R-b stands either way.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v2 08/15] tap: Convert net_init_tap_one() to Error
In reply to Markus ArmbrusterQuoted 8 lines · Markus Armbruster
net/tap.c +2
-2@@ -644,30 +644,28 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,if (tap->has_vhostfd || tap->has_vhostfds) { vhostfd =monitor_fd_param (cur_mon, vhostfdname, &err);if (vhostfd ==-1 ) { -error_report_err (err); -return -1 ; +error_propagate (errp, err); +return ;
net/tap.c static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, · line 655
Quoted 10 lines
net/tap.c +3
-3} }else { vhostfd =open ("/dev/vhost-net" ,O_RDWR );if (vhostfd <0 ) { -error_report ("tap: open vhost char device failed: %s" , -strerror (errno)); -return -1 ; +error_setg_errno (errp, errno, +"tap: open vhost char device failed: %s" ); +return ;
%s must be removed since error_setg_errno() already prints the error string and we have no format string argument.
If there are no other issues I'll do this when merging the patch.
Stefan Hajnoczi
AppliedQuoted 33 lines · Markus Armbruster
v2:
- Touch up a few commit messages [Eric]
- Touch up a an error message in PATCH 03+15 [Eric]
- Don't report possibly bogus errno in PATCH 12 [Eric]Markus Armbruster (15):
net: Improve error message for -net hubport a bit
net: Permit incremental conversion of init functions to Error
net: Improve -net nic error reporting
net/dump: Improve -net/host_net_add dump error reporting
tap:net_tap_fd_init()can't fail, drop dead error handling
tap: Improve -netdev/netdev_add/-net/... bridge error reporting
tap: Converttap_set_sndbuf()to Error
tap: Convertnet_init_tap_one()to Error
tap: Convertlaunch_script()to Error
tap: Permit incremental conversion oftap_open()to Error
tap-linux: Converttap_open()to Error
tap-bsd: Converttap_open()to Error
tap-solaris: Converttap_open()to Error
tap: Finish conversion oftap_open()to Error
tap: Improve -netdev/netdev_add/-net/... tap error reportingnet/clients.h | 20 +++--- net/dump.c | 13 ++-- net/hub.c | 7 +- net/l2tpv3.c | 5 +- net/net.c | 30 ++++++--- net/netmap.c | 3 +- net/slirp.c | 3 +- net/socket.c | 3 +- net/tap-aix.c | 7 +- net/tap-bsd.c | 38 +++++------ net/tap-haiku.c | 7 +-
Quoted 13 lines
net/tap-linux.c | 24 +++---- net/tap-solaris.c | 63 ++++++++--------- net/tap-win32.c | 3 +- net/tap.c | 198 ++++++++++++++++++++++++++++++------------------------ net/tap_int.h | 4 +- net/vde.c | 3 +- net/vhost-user.c | 3 +- 18 files changed, 231 insertions(+), 203 deletions(-)Signature · 1 line
1.9.3
Thanks, applied to my net tree:
https://github.com/stefanha/qemu/commits/net
Stefan