net/tap: simple refactoring
v9 cover net/tap: simple refactoring
Hi all!
These are some refactoring patches, extracted from
[PATCH v8 00/19] virtio-net: live-TAP local migration
These patches are good in general, even not considered as preparation to the feature. I hope, they may be queued in advance, to simplify further work on the rest of the series.
The (reworked) rest of the series is coming soon and will be based on this one.
v9: Mostly unchanged, so keep r-bs. Still, drop t-bs, as there were still some conflicts due to commits reordering.
Vladimir Sementsov-Ogievskiy (7):
net/tap: net_init_tap_one(): drop extra error propagation net/tap: net_init_tap_one(): move parameter checking earlier net/tap: pass NULL to net_init_tap_one() in cases when scripts are
NULL
net/tap: rework scripts handling
net/tap: setup exit notifier only when needed
net/tap: tap_set_sndbuf(): add return value
net/tap: rework tap_set_sndbuf()
net/tap-bsd.c | 3 +- net/tap-linux.c | 19 +++------- net/tap-solaris.c | 3 +- net/tap-stub.c | 3 +- net/tap.c | 94 +++++++++++++++++++++++++---------------------- net/tap_int.h | 3 +- 6 files changed, 62 insertions(+), 63 deletions(-)
Signature · 1 line
2.48.1
PATCH v9 1/7 net/tap: net_init_tap_one(): drop extra error propagation
--- net/tap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/tap.c +1 -2
@@ -736,9 +736,8 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, }if (vhostfdname) { - vhostfd =monitor_fd_param (monitor_cur (), vhostfdname, &err); + vhostfd =monitor_fd_param (monitor_cur (), vhostfdname, errp);if (vhostfd ==-1 ) { -error_propagate (errp, err); goto failed; }if (!qemu_set_blocking (vhostfd, false, errp)) {
Signature · 1 line
2.48.1
PATCH v9 2/7 net/tap: net_init_tap_one(): move parameter checking earlier
In reply to Vladimir Sementsov-OgievskiyLet's keep all similar argument checking in net_init_tap() function.
--- net/tap.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
net/tap.c +5 -3
@@ -768,9 +768,6 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, "vhost-net requested but could not be initialized"); goto failed; } - }else if (vhostfdname) { -error_setg (errp,"vhostfd(s)= is not valid without vhost" ); - goto failed; }return ;
@@ -832,6 +829,11 @@ int net_init_tap(const Netdev *netdev, const char *name,return -1 ; } +if (tap->has_vhost && !tap->vhost && (tap->vhostfds || tap->vhostfd)) { +error_setg (errp,"vhostfd(s)= is not valid without vhost" ); +return -1 ; + } +if (tap->fd) { if (tap->ifname || tap->script || tap->downscript || tap->has_vnet_hdr || tap->helper || tap->has_queues ||
Signature · 1 line
2.48.1
PATCH v9 3/7 net/tap: pass NULL to net_init_tap_one() in cases when scripts are NULL
In reply to Vladimir Sementsov-OgievskiyDirectly pass NULL in cases where we report an error if script or downscript are set.
--- net/tap.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
net/tap.c +5 -7
@@ -808,8 +808,6 @@ int net_init_tap(const Netdev *netdev, const char *name,const NetdevTapOptions *tap;int fd, vnet_hdr =0 , i =0 , queues;/* for the no-fd, no-helper case */ -const char *script; -const char *downscript;Error *err =NULL ;const char *vhostfdname;char ifname[128 ];
@@ -819,8 +817,6 @@ int net_init_tap(const Netdev *netdev, const char *name, tap = &netdev->u.tap; queues = tap->has_queues ? tap->queues :1 ; vhostfdname = tap->vhostfd; - script = tap->script; - downscript = tap->downscript;/* QEMU hubs do not support multiqueue tap, in this case peer is set. * For -netdev, peer is always NULL. */
@@ -861,7 +857,7 @@ int net_init_tap(const Netdev *netdev, const char *name, }net_init_tap_one (tap, peer,"tap" , name,NULL , - script, downscript, +NULL ,NULL , vhostfdname, vnet_hdr, fd, &err);if (err) {error_propagate (errp, err);
@@ -922,7 +918,7 @@ int net_init_tap(const Netdev *netdev, const char *name, }net_init_tap_one (tap, peer,"tap" , name, ifname, - script, downscript, +NULL ,NULL , tap->vhostfds ? vhost_fds[i] :NULL , vnet_hdr, fd, &err);if (err) {
@@ -967,7 +963,7 @@ free_fail: }net_init_tap_one (tap, peer,"bridge" , name, ifname, - script, downscript, vhostfdname, +NULL ,NULL , vhostfdname, vnet_hdr, fd, &err);if (err) {error_propagate (errp, err);
@@ -975,6 +971,8 @@ free_fail:return -1 ; } }else { +const char *script = tap->script; +const char *downscript = tap->downscript; g_autofree char *default_script = NULL; g_autofree char *default_downscript = NULL;if (tap->vhostfds) {
Signature · 1 line
2.48.1
PATCH v9 4/7 net/tap: rework scripts handling
In reply to Vladimir Sementsov-OgievskiySimplify handling scripts: parse all these "no" and '\0' once, and then keep simpler logic for net_tap_open() and net_init_tap_one(): NULL means no script to run, otherwise run script.
--- net/tap.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-)
net/tap.c +26 -20
@@ -91,6 +91,21 @@ static void launch_script(const char *setup_script, const char *ifname,static void tap_send (void *opaque);static void tap_writable (void *opaque); +static char *tap_parse_script (const char *script_arg,const char *default_path) +{ + g_autofree char *res = g_strdup(script_arg); + +if (!res) { + res =get_relocated_path (default_path); + } + +if (res[0 ] =='\0' ||strcmp (res,"no" ) ==0 ) { +return NULL ; + } + +return g_steal_pointer (&res); +} +static void tap_update_fd_handler (TAPState *s) { qemu_set_fd_handler(s->fd,
@@ -676,9 +691,7 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,return -1 ; } -if (setup_script && - setup_script[0 ] !='\0' && -strcmp (setup_script,"no" ) !=0 ) { +if (setup_script) {launch_script (setup_script, ifname, fd, &err);if (err) {error_propagate (errp, err);
@@ -714,9 +727,9 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,qemu_set_info_str (&s->nc,"helper=%s" , tap->helper); }else {qemu_set_info_str (&s->nc,"ifname=%s,script=%s,downscript=%s" , ifname, - script, downscript); + script ?:"no" , downscript ?:"no" ); -if (strcmp (downscript,"no" ) !=0 ) { +if (downscript) {snprintf (s->down_script,sizeof (s->down_script),"%s" , downscript);snprintf (s->down_script_arg,sizeof (s->down_script_arg),"%s" , ifname);
@@ -971,23 +984,16 @@ free_fail:return -1 ; } }else { -const char *script = tap->script; -const char *downscript = tap->downscript; - g_autofree char *default_script = NULL; - g_autofree char *default_downscript = NULL; + g_autofree char *script = +tap_parse_script (tap->script,DEFAULT_NETWORK_SCRIPT ); + g_autofree char *downscript = +tap_parse_script (tap->downscript,DEFAULT_NETWORK_DOWN_SCRIPT ); +if (tap->vhostfds) {error_setg (errp,"vhostfds= is invalid if fds= wasn't specified" );return -1 ; } -if (!script) { - script = default_script =get_relocated_path (DEFAULT_NETWORK_SCRIPT ); - } -if (!downscript) { - downscript = default_downscript = -get_relocated_path (DEFAULT_NETWORK_DOWN_SCRIPT ); - } -if (tap->ifname) {pstrcpy (ifname,sizeof ifname, tap->ifname); }else {
@@ -995,7 +1001,7 @@ free_fail: }for (i =0 ; i < queues; i++) { - fd =net_tap_init (tap, &vnet_hdr, i >=1 ?"no" : script, + fd =net_tap_init (tap, &vnet_hdr, i >=1 ?NULL : script, ifname,sizeof ifname, queues >1 , errp);if (fd ==-1 ) {return -1 ;
@@ -1010,8 +1016,8 @@ free_fail: }net_init_tap_one (tap, peer,"tap" , name, ifname, - i >=1 ?"no" : script, - i >=1 ?"no" : downscript, + i >=1 ?NULL : script, + i >=1 ?NULL : downscript, vhostfdname, vnet_hdr, fd, &err);if (err) {error_propagate (errp, err);
Signature · 1 line
2.48.1
PATCH v9 5/7 net/tap: setup exit notifier only when needed
In reply to Vladimir Sementsov-OgievskiyNo reason to setup notifier on each queue of multique tap, when we actually want to run downscript only once. As well, let's not setup notifier, when downscript is not enabled (downsciprt="no").
--- net/tap.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
net/tap.c +10 -10
@@ -326,11 +326,9 @@ static void tap_exit_notify(Notifier *notifier, void *data)TAPState *s =container_of (notifier, TAPState, exit);Error *err =NULL ; -if (s->down_script[0 ]) { -launch_script (s->down_script, s->down_script_arg, s->fd, &err); -if (err) { -error_report_err (err); - } +launch_script (s->down_script, s->down_script_arg, s->fd, &err); +if (err) { +error_report_err (err); } }
@@ -346,8 +344,11 @@ static void tap_cleanup(NetClientState *nc)qemu_purge_queued_packets (nc); -tap_exit_notify (&s->exit,NULL ); -qemu_remove_exit_notifier (&s->exit); +if (s->exit.notify) { +tap_exit_notify (&s->exit,NULL ); +qemu_remove_exit_notifier (&s->exit); + s->exit.notify =NULL ; + }tap_read_poll (s, false);tap_write_poll (s, false);
@@ -443,9 +444,6 @@ static TAPState *net_tap_fd_init(NetClientState *peer,tap_read_poll (s, true); s->vhost_net =NULL ; - s->exit.notify = tap_exit_notify; -qemu_add_exit_notifier (&s->exit); -return s; }
@@ -733,6 +731,8 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,snprintf (s->down_script,sizeof (s->down_script),"%s" , downscript);snprintf (s->down_script_arg,sizeof (s->down_script_arg),"%s" , ifname); + s->exit.notify = tap_exit_notify; +qemu_add_exit_notifier (&s->exit); } }
Signature · 1 line
2.48.1
PATCH v9 6/7 net/tap: tap_set_sndbuf(): add return value
In reply to Vladimir Sementsov-OgievskiyFollow common recommendations in include/qapi/error.h of having a return value together with errp. This allows to avoid error propagation.
--- net/tap-bsd.c | 3 ++- net/tap-linux.c | 5 ++++- net/tap-solaris.c | 3 ++- net/tap-stub.c | 3 ++- net/tap.c | 5 +---- net/tap_int.h | 2 +- 6 files changed, 12 insertions(+), 9 deletions(-)
net/tap-bsd.c +2 -1
@@ -206,8 +206,9 @@ error: }#endif /* __FreeBSD__ */ -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +bool tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { +return true; }int tap_probe_vnet_hdr (int fd,Error **errp)
net/tap-linux.c +4 -1
@@ -145,7 +145,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) +bool tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) {int sndbuf;
@@ -159,7 +159,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 false; } + +return true; }int tap_probe_vnet_hdr (int fd,Error **errp)
net/tap-solaris.c +2 -1
@@ -208,8 +208,9 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,return fd; } -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +bool tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { +return true; }int tap_probe_vnet_hdr (int fd,Error **errp)
net/tap-stub.c +2 -1
@@ -33,8 +33,9 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,return -1 ; } -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +bool tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) { +return true; }int tap_probe_vnet_hdr (int fd,Error **errp)
net/tap.c +1 -4
@@ -709,13 +709,10 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, 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);int vhostfd; -tap_set_sndbuf (s->fd, tap, &err); -if (err) { -error_propagate (errp, err); +if (!tap_set_sndbuf (s->fd, tap, errp)) { goto failed; }
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); -void tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp); +bool tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp);int tap_probe_vnet_hdr (int fd,Error **errp);int tap_probe_has_ufo (int fd);int tap_probe_has_uso (int fd);
Signature · 1 line
2.48.1
PATCH v9 7/7 net/tap: rework tap_set_sndbuf()
In reply to Vladimir Sementsov-OgievskiyKeep NetdevTapOptions related logic in tap.c, and make tap_set_sndbuf a simple system call wrapper, more like other functions in tap-linux.c
--- net/tap-bsd.c | 2 +- net/tap-linux.c | 16 ++-------------- net/tap-solaris.c | 2 +- net/tap-stub.c | 2 +- net/tap.c | 6 +++++- net/tap_int.h | 3 +-- 6 files changed, 11 insertions(+), 20 deletions(-)
net/tap-bsd.c +1 -1
@@ -206,7 +206,7 @@ error: }#endif /* __FreeBSD__ */ -bool tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +bool tap_set_sndbuf (int fd,int sndbuf,Error **errp) {return true; }
net/tap-linux.c +2 -14
@@ -143,21 +143,9 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,* Ethernet NICs generally have txqueuelen=1000, so 1Mb is * a good value, given a 1500 byte MTU. */ -#define TAP_DEFAULT_SNDBUF 0 - -bool tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +bool tap_set_sndbuf (int fd,int sndbuf,Error **errp) { -int sndbuf; - - sndbuf = !tap->has_sndbuf ?TAP_DEFAULT_SNDBUF : - tap->sndbuf >INT_MAX ?INT_MAX : - tap->sndbuf; - -if (!sndbuf) { - sndbuf =INT_MAX ; - } - -if (ioctl (fd,TUNSETSNDBUF , &sndbuf) ==-1 && tap->has_sndbuf) { +if (ioctl (fd,TUNSETSNDBUF , &sndbuf) ==-1 ) {error_setg_errno (errp, errno,"TUNSETSNDBUF ioctl failed" );return false; }
net/tap-solaris.c +1 -1
@@ -208,7 +208,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,return fd; } -bool tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +bool tap_set_sndbuf (int fd,int sndbuf,Error **errp) {return true; }
net/tap-stub.c +1 -1
@@ -33,7 +33,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,return -1 ; } -bool tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp) +bool tap_set_sndbuf (int fd,int sndbuf,Error **errp) {return true; }
net/tap.c +5 -1
@@ -711,8 +711,12 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, {TAPState *s =net_tap_fd_init (peer, model, name, fd, vnet_hdr);int vhostfd; +bool sndbuf_required = tap->has_sndbuf; +int sndbuf = + (tap->has_sndbuf && tap->sndbuf) ?MIN (tap->sndbuf,INT_MAX ) :INT_MAX ; -if (!tap_set_sndbuf (s->fd, tap, errp)) { +if (!tap_set_sndbuf (fd, sndbuf, sndbuf_required ? errp :NULL ) && + sndbuf_required) { goto failed; }
net/tap_int.h +1 -2
@@ -26,7 +26,6 @@#ifndef NET_TAP_INT_H #define NET_TAP_INT_H -#include "qapi/qapi-types-net.h" #include "net/net.h" int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
@@ -34,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); -bool tap_set_sndbuf (int fd,const NetdevTapOptions *tap,Error **errp); +bool tap_set_sndbuf (int fd,int sndbuf,Error **errp);int tap_probe_vnet_hdr (int fd,Error **errp);int tap_probe_has_ufo (int fd);int tap_probe_has_uso (int fd);
Signature · 1 line
2.48.1
Tested this series of patches with virtio-net regression tests, everything works fine.
Quoted 39 lines · Vladimir Sementsov-Ogievskiy
Hi all!
These are some refactoring patches, extracted from
[PATCH v8 00/19] virtio-net: live-TAP local migration
These patches are good in general, even not considered as preparation to the feature. I hope, they may be queued in advance, to simplify further work on the rest of the series.
The (reworked) rest of the series is coming soon and will be based on this one.
v9: Mostly unchanged, so keep r-bs. Still, drop t-bs, as there were still some conflicts due to commits reordering.
Vladimir Sementsov-Ogievskiy (7):
net/tap:net_init_tap_one(): drop extra error propagation net/tap:net_init_tap_one(): move parameter checking earlier net/tap: pass NULL tonet_init_tap_one()in cases when scripts are
NULL
net/tap: rework scripts handling
net/tap: setup exit notifier only when needed
net/tap:tap_set_sndbuf(): add return value
net/tap: reworktap_set_sndbuf()net/tap-bsd.c | 3 +- net/tap-linux.c | 19 +++------- net/tap-solaris.c | 3 +- net/tap-stub.c | 3 +- net/tap.c | 94 +++++++++++++++++++++++++---------------------- net/tap_int.h | 3 +- 6 files changed, 62 insertions(+), 63 deletions(-)--
2.48.1
ping
(now as 11.0 material)
Quoted 35 lines · Vladimir Sementsov-Ogievskiy
Hi all!
These are some refactoring patches, extracted from
[PATCH v8 00/19] virtio-net: live-TAP local migration
These patches are good in general, even not considered as preparation to the feature. I hope, they may be queued in advance, to simplify further work on the rest of the series.
The (reworked) rest of the series is coming soon and will be based on this one.
v9: Mostly unchanged, so keep r-bs. Still, drop t-bs, as there were still some conflicts due to commits reordering.
Vladimir Sementsov-Ogievskiy (7):
net/tap:net_init_tap_one(): drop extra error propagation net/tap:net_init_tap_one(): move parameter checking earlier net/tap: pass NULL tonet_init_tap_one()in cases when scripts are
NULL
net/tap: rework scripts handling
net/tap: setup exit notifier only when needed
net/tap:tap_set_sndbuf(): add return value
net/tap: reworktap_set_sndbuf()net/tap-bsd.c | 3 +- net/tap-linux.c | 19 +++------- net/tap-solaris.c | 3 +- net/tap-stub.c | 3 +- net/tap.c | 94 +++++++++++++++++++++++++---------------------- net/tap_int.h | 3 +- 6 files changed, 62 insertions(+), 63 deletions(-)
Signature · 2 lines
Best regards, Vladimir
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
ping
(now as 11.0 material)
I've queued this.
Thanks
Quoted 10 lines · Jason Wang
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
ping
(now as 11.0 material)
I've queued this.
Thanks!
Signature · 2 lines
Best regards, Vladimir
Quoted 12 lines · Jason Wang
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
ping
(now as 11.0 material)
I've queued this.
Thanks
Hi! Gently remind. Aren't these patches forgotten?
Signature · 2 lines
Best regards, Vladimir
Quoted 10 lines · Vladimir Sementsov-Ogievskiy
Quoted 8 lines · Jason Wang
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
ping
(now as 11.0 material)
Quoted 6 lines
Quoted 4 lines
I've queued this.
Thanks
Hi! Gently remind. Aren't these patches forgotten?
Thanks for the reminder. It's not, I plan to send the pull request no later than the end of this week, it should include this series.
Thanks
Quoted 5 lines
--
Best regards,
Vladimir