net/tap: some fixes and refactorings
Subsystems (2)
PATCH 2/5 net/tap: drop duplicated tap stubs
In reply to Vladimir Sementsov-Ogievskiytap-solaris and tap-bsd realize only tap_open() interface and all other functions are duplicated with tap-stub. Let's split tap-stub so that we can reuse all stubs except for tap_open() for solaris and bsd.
--- net/tap-bsd.c | 53 --------------------------------------------- net/tap-open-stub.c | 34 +++++++++++++++++++++++++++++ net/tap-solaris.c | 53 --------------------------------------------- net/tap-stub.c | 7 ------ net/meson.build | 3 +++ 5 files changed, 37 insertions(+), 113 deletions(-) create mode 100644 net/tap-open-stub.c
net/tap-bsd.c +0 -53
@@ -206,56 +206,3 @@ error: return -1; }#endif /* __FreeBSD__ */ - -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) -{ -} - -int tap_probe_vnet_hdr (int fd,Error **errp) -{ -return 0 ; -} - -int tap_probe_has_ufo (int fd) -{ -return 0 ; -} - -int tap_probe_vnet_hdr_len (int fd,int len) -{ -return 0 ; -} - -void tap_fd_set_vnet_hdr_len (int fd,int len) -{ -} - -int tap_fd_set_vnet_le (int fd,int is_le) -{ -return -EINVAL ; -} - -int tap_fd_set_vnet_be (int fd,int is_be) -{ -return -EINVAL ; -} - -void tap_fd_set_offload (int fd,int csum,int tso4, -int tso6,int ecn,int ufo) -{ -} - -int tap_fd_enable (int fd) -{ -return -1 ; -} - -int tap_fd_disable (int fd) -{ -return -1 ; -} - -int tap_fd_get_ifname (int fd,char *ifname) -{ -return -1 ; -}
net/tap-open-stub.c new file +34 -0
@@ -0,0 +1,34 @@ +/* +* QEMU System Emulator +* +* Copyright (c) 2003-2008 Fabrice Bellard +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "tap_int.h" + +int tap_open (char *ifname,int ifname_size,int *vnet_hdr, +int vnet_hdr_required,int mq_required,Error **errp) +{ +error_setg (errp,"tap is not supported in this build" ); +return -1 ; +}
net/tap-solaris.c +0 -53
56 lines not on this page · raw message
net/tap-stub.c +0 -7
@@ -26,13 +26,6 @@#include "qapi/error.h" #include "tap_int.h" -int tap_open (char *ifname,int ifname_size,int *vnet_hdr, -int vnet_hdr_required,int mq_required,Error **errp) -{ -error_setg (errp,"tap is not supported in this build" ); -return -1 ; -} -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { }
net/meson.build +3 -0
@@ -32,6 +32,9 @@ softmmu_ss.add(when: 'CONFIG_BSD', if_true: files('tap-bsd.c')) softmmu_ss.add(when: 'CONFIG_SOLARIS', if_true: files('tap-solaris.c')) tap_posix = ['tap.c'] if not config_host.has_key('CONFIG_LINUX') and not config_host.has_key('CONFIG_BSD') and not config_host.has_key('CONFIG_SOLARIS') + tap_posix += 'tap-open-stub.c' +endif +if not config_host.has_key('CONFIG_LINUX') tap_posix += 'tap-stub.c' endif softmmu_ss.add(when: 'CONFIG_POSIX', if_true: files(tap_posix))
Signature · 1 line
2.28.0
PATCH 1/5 net/tap: fix net_init_tap(): set ret on failure path
In reply to Vladimir Sementsov-OgievskiyWe should set ret to -1 on this failure path.
--- net/tap.c | 1 + 1 file changed, 1 insertion(+)
net/tap.c +1 -0
@@ -880,6 +880,7 @@ int net_init_tap(const Netdev *netdev, const char *name,if (i ==0 ) { vnet_hdr =tap_probe_vnet_hdr (fd, errp);if (vnet_hdr <0 ) { + ret =-1 ; goto free_fail; } }else if (vnet_hdr !=tap_probe_vnet_hdr (fd,NULL )) {
Signature · 1 line
2.28.0
PATCH 4/5 net/tap: refactor and improve net_init_tap_one()
In reply to Vladimir Sementsov-OgievskiyFirst, 46d4d36d0bf2b24 changed the logic of net_init_tap_one() so that failure of vhost initialization is ignored (still, warning printed) when vhostforce is not set.
Then 894022e616016fe8 updated net_init_tap_one() to use qemu_try_set_nonblock() instead of qemu_set_nonblock(), to not crash, but it didn't consider the failure-ignoring logic and used wrong fd for error message.
Let's refactor the function net_init_tap_one() splitting out vhost-net initialization and fixing all the issues.
Error message change: we need to fix fd anyway. Also, don't want to pass name to the new function only for error path, so, drop it. As a pay, add more information on vhost fd itself.
--- include/net/vhost_net.h | 3 -- net/tap.c | 103 +++++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 53 deletions(-)
include/net/vhost_net.h +0 -3
@@ -4,9 +4,6 @@#include "net/net.h" #include "hw/virtio/vhost-backend.h" -#define VHOST_NET_INIT_FAILED \ - "vhost-net requested but could not be initialized" -struct vhost_net ;typedef struct vhost_net VHostNetState ;
net/tap.c +53 -50
126 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH 3/5 net/tap: tap_set_sndbuf(): return status
In reply to Vladimir Sementsov-OgievskiyIt's recommended to return a value indicating success / failure for functions with errp parameter (see include/qapi/error.h). Let's update tap_set_sndbuf().
--- net/tap_int.h | 2 +- net/tap-linux.c | 5 ++++- net/tap-stub.c | 2 +- net/tap.c | 6 +++--- 4 files changed, 9 insertions(+), 6 deletions(-)
net/tap_int.h +1 -1
@@ -33,7 +33,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,ssize_t tap_read_packet (int tapfd,uint8_t *buf,int maxlen); -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp); +int tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp);int tap_probe_vnet_hdr (int fd,Error **errp);int tap_probe_vnet_hdr_len (int fd,int len);int tap_probe_has_ufo (int fd);
net/tap-linux.c +4 -1
@@ -130,7 +130,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,*/ #define TAP_DEFAULT_SNDBUF 0 -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +int tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) {int sndbuf;
@@ -144,7 +144,10 @@ void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)if (ioctl (fd,TUNSETSNDBUF , &sndbuf) ==-1 && tap->has_sndbuf) {error_setg_errno (errp, errno,"TUNSETSNDBUF ioctl failed" ); +return -1 ; } + +return 0 ; }int tap_probe_vnet_hdr (int fd,Error **errp)
net/tap-stub.c +1 -1
@@ -26,7 +26,7 @@#include "qapi/error.h" #include "tap_int.h" -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +int tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { }
net/tap.c +3 -3
@@ -661,10 +661,10 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,Error *err =NULL ;TAPState *s =net_tap_fd_init (peer, model, name, fd, vnet_hdr);int vhostfd; +int ret; -tap_set_sndbuf (s->fd, tap, &err); -if (err) { -error_propagate (errp, err); + ret =tap_set_sndbuf (s->fd, tap, errp); +if (ret <0 ) {return ; }
Signature · 1 line
2.28.0
PATCH 5/5 net/tap: net_init_tap_one(): fix net-client leak on failure path
In reply to Vladimir Sementsov-Ogievskiynet_tap_fd_init() allocates new NetClientState through qemu_new_net_client(). We should free it on failure path.
---
Attention: it's an intuitive patch.
I see, that net-client is leaked. May be it's still freed some tricky way? And I don't understand the whole logic of qemu_del_net_client(), it's just the only public interface to free net-client I found.
net/tap.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
net/tap.c +8 -1
@@ -711,7 +711,7 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, ret =tap_set_sndbuf (s->fd, tap, errp);if (ret <0 ) { -return ; + goto fail; }if (tap->has_fd || tap->has_fds) {
@@ -739,13 +739,20 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,if (ret <0 ) {if (tap->has_vhostforce && tap->vhostforce) {error_propagate (errp, err); + goto fail; }else {warn_report_err (err); } } }else if (vhostfdname) {error_setg (errp,"vhostfd(s)= is not valid without vhost" ); + goto fail; } + +return ; + +fail: +qemu_del_net_client (&s->nc); }static int get_fds (char *str,char *fds[],int max)
Signature · 1 line
2.28.0
cover net/tap: some fixes and refactorings
Hi all. I have a work in progress around net/tap (and not sure, will it be done or we go another way), but some fixes and good refactoring I'd like to post anyway:
Vladimir Sementsov-Ogievskiy (5):
net/tap: fix net_init_tap(): set ret on failure path net/tap: drop duplicated tap stubs
net/tap: tap_set_sndbuf(): return status
net/tap: refactor and improve net_init_tap_one() net/tap: net_init_tap_one(): fix net-client leak on failure path
include/net/vhost_net.h | 3 - net/tap_int.h | 2 +- net/tap-bsd.c | 53 ------------------ net/tap-linux.c | 5 +- net/tap-open-stub.c | 34 ++++++++++++ net/tap-solaris.c | 53 ------------------ net/tap-stub.c | 9 +-- net/tap.c | 119 ++++++++++++++++++++++------------------ net/meson.build | 3 + 9 files changed, 108 insertions(+), 173 deletions(-) create mode 100644 net/tap-open-stub.c
Signature · 1 line
2.28.0
Re: [PATCH 3/5] net/tap: tap_set_sndbuf(): return status
In reply to Vladimir Sementsov-OgievskiyQuoted 3 lines · Vladimir Sementsov-Ogievskiy
It's recommended to return a value indicating success / failure for functions with errp parameter (see
include/qapi/error.h). Let's updatetap_set_sndbuf().
For simple "success/failure" a bool type is enough.
Preferably using bool type:
Quoted 78 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- net/tap_int.h | 2 +- net/tap-linux.c | 5 ++++- net/tap-stub.c | 2 +- net/tap.c | 6 +++--- 4 files changed, 9 insertions(+), 6 deletions(-)net/tap_int.h +1
-1@@ -33,7 +33,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,ssize_t tap_read_packet (int tapfd,uint8_t *buf,int maxlen); -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp); +int tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp);int tap_probe_vnet_hdr (int fd,Error **errp);int tap_probe_vnet_hdr_len (int fd,int len);int tap_probe_has_ufo (int fd);net/tap-linux.c +4
-1@@ -130,7 +130,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,*/ #define TAP_DEFAULT_SNDBUF 0 -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +int tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) {int sndbuf;@@ -144,7 +144,10 @@ void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)if (ioctl (fd,TUNSETSNDBUF , &sndbuf) ==-1 && tap->has_sndbuf) {error_setg_errno (errp, errno,"TUNSETSNDBUF ioctl failed" ); +return -1 ; } + +return 0 ; }int tap_probe_vnet_hdr (int fd,Error **errp)net/tap-stub.c +1
-1@@ -26,7 +26,7 @@#include "qapi/error.h" #include "tap_int.h" -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +int tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { }net/tap.c +3
-3@@ -661,10 +661,10 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,Error *err =NULL ;TAPState *s =net_tap_fd_init (peer, model, name, fd, vnet_hdr);int vhostfd; +int ret; -tap_set_sndbuf (s->fd, tap, &err); -if (err) { -error_propagate (errp, err); + ret =tap_set_sndbuf (s->fd, tap, errp); +if (ret <0 ) {return ; }
Re: [PATCH 3/5] net/tap: tap_set_sndbuf(): return status
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Vladimir Sementsov-Ogievskiy
It's recommended to return a value indicating success / failure for functions with errp parameter (see
include/qapi/error.h). Let's updatetap_set_sndbuf().For simple "success/failure" a bool type is enough.
Preferably using bool type:
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
0/-1 return status is highly used in net/tap, so I decided to follow it.
Quoted 80 lines
Quoted 79 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- net/tap_int.h | 2 +- net/tap-linux.c | 5 ++++- net/tap-stub.c | 2 +- net/tap.c | 6 +++--- 4 files changed, 9 insertions(+), 6 deletions(-)net/tap_int.h +1
-1@@ -33,7 +33,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,ssize_t tap_read_packet (int tapfd,uint8_t *buf,int maxlen); -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp); +int tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp);int tap_probe_vnet_hdr (int fd,Error **errp);int tap_probe_vnet_hdr_len (int fd,int len);int tap_probe_has_ufo (int fd);net/tap-linux.c +4
-1@@ -130,7 +130,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,*/ #define TAP_DEFAULT_SNDBUF 0 -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +int tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) {int sndbuf;@@ -144,7 +144,10 @@ void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)if (ioctl (fd,TUNSETSNDBUF , &sndbuf) ==-1 && tap->has_sndbuf) {error_setg_errno (errp, errno,"TUNSETSNDBUF ioctl failed" ); +return -1 ; } + +return 0 ; }int tap_probe_vnet_hdr (int fd,Error **errp)net/tap-stub.c +1
-1@@ -26,7 +26,7 @@#include "qapi/error.h" #include "tap_int.h" -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +int tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { }net/tap.c +3
-3@@ -661,10 +661,10 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,Error *err =NULL ;TAPState *s =net_tap_fd_init (peer, model, name, fd, vnet_hdr);int vhostfd; +int ret; -tap_set_sndbuf (s->fd, tap, &err); -if (err) { -error_propagate (errp, err); + ret =tap_set_sndbuf (s->fd, tap, errp); +if (ret <0 ) {return ; }
Signature · 2 lines
Best regards, Vladimir
ping :)
Quoted 23 lines · Vladimir Sementsov-Ogievskiy
Hi all. I have a work in progress around net/tap (and not sure, will it be done or we go another way), but some fixes and good refactoring I'd like to post anyway:
Vladimir Sementsov-Ogievskiy (5):
net/tap: fixnet_init_tap(): set ret on failure path net/tap: drop duplicated tap stubs
net/tap:tap_set_sndbuf(): return status
net/tap: refactor and improvenet_init_tap_one()net/tap:net_init_tap_one(): fix net-client leak on failure pathinclude/net/vhost_net.h | 3 - net/tap_int.h | 2 +- net/tap-bsd.c | 53 ------------------ net/tap-linux.c | 5 +- net/tap-open-stub.c | 34 ++++++++++++ net/tap-solaris.c | 53 ------------------ net/tap-stub.c | 9 +-- net/tap.c | 119 ++++++++++++++++++++++------------------ net/meson.build | 3 + 9 files changed, 108 insertions(+), 173 deletions(-)create mode 100644
net/tap-open-stub.c
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 5/5] net/tap: net_init_tap_one(): fix net-client leak on failure path
In reply to Vladimir Sementsov-OgievskiyQuoted 11 lines · Vladimir Sementsov-Ogievskiy
net_tap_fd_init()allocates new NetClientState throughqemu_new_net_client(). We should free it on failure path.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
Attention: it's an intuitive patch.
I see, that net-client is leaked. May be it's still freed some tricky way? And I don't understand the whole logic of
qemu_del_net_client(), it's just the only public interface to free net-client I found.
Your patch looks correct and it's indeed a leak.
I wonder whether it's better to do the cleanup in the free_fail label in net_init_tap(). The reason is that we need deal with case of multiqueue. Though qemu_del_net_client() can handle this but it's not clear if we do it in net_init_tap_one().
Thanks
Quoted 38 lines
net/tap.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)net/tap.c +8
-1@@ -711,7 +711,7 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, ret =tap_set_sndbuf (s->fd, tap, errp);if (ret <0 ) { -return ; + goto fail; }if (tap->has_fd || tap->has_fds) {@@ -739,13 +739,20 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,if (ret <0 ) {if (tap->has_vhostforce && tap->vhostforce) {error_propagate (errp, err); + goto fail; }else {warn_report_err (err); } } }else if (vhostfdname) {error_setg (errp,"vhostfd(s)= is not valid without vhost" ); + goto fail; } + +return ; + +fail: +qemu_del_net_client (&s->nc); }static int get_fds (char *str,char *fds[],int max)
Re: [PATCH 5/5] net/tap: net_init_tap_one(): fix net-client leak on failure path
Quoted 10 lines · Jason Wang
Quoted 8 lines · Vladimir Sementsov-Ogievskiy
net_tap_fd_init()allocates new NetClientState throughqemu_new_net_client(). We should free it on failure path.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
Attention: it's an intuitive patch.
Quoted 9 lines
Quoted 3 lines
I see, that net-client is leaked. May be it's still freed some tricky way? And I don't understand the whole logic of
qemu_del_net_client(), it's just the only public interface to free net-client I found.Your patch looks correct and it's indeed a leak.
I wonder whether it's better to do the cleanup in the
free_faillabel innet_init_tap(). The reason is that we need deal with case of multiqueue. Thoughqemu_del_net_client()can handle this but it's not clear if we do it innet_init_tap_one().
Sorry for so long delay :(
Now I'm thinking about reviving this series. But I don't understand what you mean about multiqueue.. I think, if some function allocates a resource, we should release the resource on failure path in this function, not in the caller. Good functions tries to roll-back any visible changes on failure.. What am I missing?
Quoted 36 lines
Quoted 35 lines
net/tap.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)net/tap.c +8
-1@@ -711,7 +711,7 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, ret = tap_set_sndbuf(s->fd, tap, errp); if (ret < 0) { - return; + goto fail; } if (tap->has_fd || tap->has_fds) {@@ -739,13 +739,20 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, if (ret < 0) { if (tap->has_vhostforce && tap->vhostforce) { error_propagate(errp, err); + goto fail; } else { warn_report_err(err); } } } else if (vhostfdname) { error_setg(errp, "vhostfd(s)= is not valid without vhost"); + goto fail; } + + return; + +fail: + qemu_del_net_client(&s->nc); } static int get_fds(char *str, char *fds[], int max)
Signature · 2 lines
Best regards, Vladimir