Net patches
Subsystems (2)
[PULL 01/13] net/tap-linux.c: avoid abort when setting invalid fd
In reply to Jason WangFrom: "Houqi (Nick) Zuo" <[email protected]>
This patch removes abort() call in the tap_fd_set_vnet_hdr_len() function. If the fd is found to be in a bad state (e.g., EBADFD
or ENODEV), the function will print an error message.
When QEMU creates a tap device automatically and the tap device is manually removed from the host while the guest is running, the tap device file descriptor becomes invalid. Later, when the guest executes shutdown, the tap_fd_set_vnet_hdr_len() function may be called and abort QEMU with a core dump when attempting to use the invalid fd. The expected behavior for this negative test case is that QEMU should report an error but continue running rather than aborting.
Testing:
- Start QEMU with automatically created tap device
- Manually remove the tap device on the host
- Execute shutdown in the guest
- Verify QEMU reports an error but does not abort
--- net/tap-linux.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
net/tap-linux.c +5 -4
@@ -214,10 +214,11 @@ bool tap_probe_has_tunnel(int fd)void tap_fd_set_vnet_hdr_len (int fd,int len) { -if (ioctl (fd,TUNSETVNETHDRSZ , &len) ==-1 ) { -fprintf (stderr,"TUNSETVNETHDRSZ ioctl() failed: %s. Exiting.\n" , -strerror (errno)); -abort (); +int ret; + + ret =ioctl (fd,TUNSETVNETHDRSZ , &len); +if (ret !=0 ) { +error_report ("TUNSETVNETHDRSZ ioctl() failed: %s." ,strerror (errno)); } }
Signature · 1 line
2.42.0
[PULL 12/13] qtest: add a test to test redirector status change
In reply to Jason WangThis patch adds a qtest to test the status change of filter-redirector. Two subtests were added:
- test_redirector_status: tests dynamic on/off switching at runtime using qom-set QMP command
- test_redirector_init_status_off: tests creating filter-redirector with status=off from the start via command line
Both tests verify that:
1. When status is off, data from indev chardev is not received
2. When status is switched to on, data is received correctly
--- tests/qtest/test-filter-redirector.c | 192 +++++++++++++++++++++++++++ 1 file changed, 192 insertions(+)
tests/qtest/test-filter-redirector.c +192 -0
@@ -196,10 +196,202 @@ static void test_redirector_rx(void)qtest_quit (qts); } +/* +* Test filter-redirector status on/off switching. +* +* This test verifies that: +* 1. When status is set to "off", the filter stops receiving data from indev +* 2. When status is set back to "on", the filter resumes receiving data +*/ +static void test_redirector_status (void ) +{ +int backend_sock[2 ], send_sock; +uint32_t ret =0 , len =0 ; +char send_buf[] ="Hello!!" ; +char sock_path0[] ="filter-redirector0.XXXXXX" ; +char *recv_buf; +uint32_t size =sizeof (send_buf); + size =htonl (size); +QTestState *qts; +struct timeval tv; +fd_set rfds; + + ret =socketpair (PF_UNIX ,SOCK_STREAM ,0 , backend_sock); + g_assert_cmpint(ret, !=, -1); + + ret =mkstemp (sock_path0); + g_assert_cmpint(ret, !=, -1); + +/* +* Setup a simple rx path: +* chardev (sock_path0) -> filter-redirector -> socket backend +*/ + qts =qtest_initf ( +"-nic socket,id=qtest-bn0,fd=%d " +"-chardev socket,id=redirector0,path=%s,server=on,wait=off " +"-object filter-redirector,id=qtest-f0,netdev=qtest-bn0," +"queue=rx,indev=redirector0 " , + backend_sock[1 ], sock_path0); + + send_sock =unix_connect (sock_path0,NULL ); + g_assert_cmpint(send_sock, !=, -1); + +/* send a qmp command to guarantee that 'connected' is setting to true. */ +qtest_qmp_assert_success (qts,"{ 'execute' : 'query-status'}" ); + +struct iovec iov[] = { + { + .iov_base = &size, + .iov_len =sizeof (size), + }, { + .iov_base = send_buf, + .iov_len =sizeof (send_buf), + }, + }; + +/* +* Test 1: Set status to "off" and verify data is not received +*/ +qtest_qmp_assert_success (qts, +"{ 'execute': 'qom-set', 'arguments': " +"{ 'path': '/objects/qtest-f0', 'property': 'status', 'value': 'off' }}" ); + + ret =iov_send (send_sock, iov,2 ,0 ,sizeof (size) +sizeof (send_buf)); + g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size)); + +/* +* Use select with timeout to check if data arrives. +* When status is off, no data should arrive. +*/ +FD_ZERO (&rfds); +FD_SET (backend_sock[0 ], &rfds); + tv.tv_sec =0 ; + tv.tv_usec =500000 ;/* 500ms timeout */ + ret =select (backend_sock[0 ] +1 , &rfds,NULL ,NULL , &tv); + g_assert_cmpint(ret, ==, 0); /* Should timeout, no data */ + +/* +* Test 2: Set status back to "on" and verify data is received +*/ +qtest_qmp_assert_success (qts, +"{ 'execute': 'qom-set', 'arguments': " +"{ 'path': '/objects/qtest-f0', 'property': 'status', 'value': 'on' }}" ); + + ret =iov_send (send_sock, iov,2 ,0 ,sizeof (size) +sizeof (send_buf)); + g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size)); + + ret =recv (backend_sock[0 ], &len,sizeof (len),0 ); + g_assert_cmpint(ret, ==, sizeof(len)); + len =ntohl (len); + + g_assert_cmpint(len, ==, sizeof(send_buf)); + recv_buf =g_malloc (len); + ret =recv (backend_sock[0 ], recv_buf, len,0 ); + g_assert_cmpint(ret, ==, len); + g_assert_cmpstr(recv_buf, ==, send_buf); + +g_free (recv_buf); +close (send_sock); +unlink (sock_path0); +qtest_quit (qts); +} + +/* +* Test filter-redirector created with status=off. +* +* This test verifies that when a filter-redirector is created with +* status=off, it does not receive data until status is set to on. +*/ +static void test_redirector_init_status_off (void ) +{ +int backend_sock[2 ], send_sock; +uint32_t ret =0 , len =0 ; +char send_buf[] ="Hello!!" ; +char sock_path0[] ="filter-redirector0.XXXXXX" ; +char *recv_buf; +uint32_t size =sizeof (send_buf); + size =htonl (size); +QTestState *qts; +struct timeval tv; +fd_set rfds; + + ret =socketpair (PF_UNIX ,SOCK_STREAM ,0 , backend_sock); + g_assert_cmpint(ret, !=, -1); + + ret =mkstemp (sock_path0); + g_assert_cmpint(ret, !=, -1); + +/* +* Create filter-redirector with status=off from the start +*/ + qts =qtest_initf ( +"-nic socket,id=qtest-bn0,fd=%d " +"-chardev socket,id=redirector0,path=%s,server=on,wait=off " +"-object filter-redirector,id=qtest-f0,netdev=qtest-bn0," +"queue=rx,indev=redirector0,status=off " , + backend_sock[1 ], sock_path0); + + send_sock =unix_connect (sock_path0,NULL ); + g_assert_cmpint(send_sock, !=, -1); + +qtest_qmp_assert_success (qts,"{ 'execute' : 'query-status'}" ); + +struct iovec iov[] = { + { + .iov_base = &size, + .iov_len =sizeof (size), + }, { + .iov_base = send_buf, + .iov_len =sizeof (send_buf), + }, + }; + +/* +* Test 1: Filter was created with status=off, data should not be received +*/ + ret =iov_send (send_sock, iov,2 ,0 ,sizeof (size) +sizeof (send_buf)); + g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size)); + +FD_ZERO (&rfds); +FD_SET (backend_sock[0 ], &rfds); + tv.tv_sec =0 ; + tv.tv_usec =500000 ; + ret =select (backend_sock[0 ] +1 , &rfds,NULL ,NULL , &tv); + g_assert_cmpint(ret, ==, 0); /* Should timeout, no data */ + +/* +* Test 2: Set status to "on" and verify data is received +*/ +qtest_qmp_assert_success (qts, +"{ 'execute': 'qom-set', 'arguments': " +"{ 'path': '/objects/qtest-f0', 'property': 'status', 'value': 'on' }}" ); + + ret =iov_send (send_sock, iov,2 ,0 ,sizeof (size) +sizeof (send_buf)); + g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size)); + + ret =recv (backend_sock[0 ], &len,sizeof (len),0 ); + g_assert_cmpint(ret, ==, sizeof(len)); + len =ntohl (len); + + g_assert_cmpint(len, ==, sizeof(send_buf)); + recv_buf =g_malloc (len); + ret =recv (backend_sock[0 ], recv_buf, len,0 ); + g_assert_cmpint(ret, ==, len); + g_assert_cmpstr(recv_buf, ==, send_buf); + +g_free (recv_buf); +close (send_sock); +unlink (sock_path0); +qtest_quit (qts); +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );qtest_add_func ("/netfilter/redirector_tx" , test_redirector_tx);qtest_add_func ("/netfilter/redirector_rx" , test_redirector_rx); +qtest_add_func ("/netfilter/redirector_status" , test_redirector_status); +qtest_add_func ("/netfilter/redirector_init_status_off" , + test_redirector_init_status_off);return g_test_run (); }
Signature · 1 line
2.42.0
[PULL 05/13] net/tap: rework scripts handling
In reply to Jason WangFrom: Vladimir Sementsov-Ogievskiy <[email protected]>
Simplify 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);
@@ -970,23 +983,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 {
@@ -994,7 +1000,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 ;
@@ -1009,8 +1015,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.42.0
[PULL 08/13] net/tap: rework tap_set_sndbuf()
In reply to Jason WangFrom: Vladimir Sementsov-Ogievskiy <[email protected]>
Keep 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.42.0
[PULL 03/13] net/tap: net_init_tap_one(): move parameter checking earlier
In reply to Jason WangFrom: Vladimir Sementsov-Ogievskiy <[email protected]>
Let'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
@@ -767,9 +767,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 ;
@@ -831,6 +828,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.42.0
[PULL 07/13] net/tap: tap_set_sndbuf(): add return value
In reply to Jason WangFrom: Vladimir Sementsov-Ogievskiy <[email protected]>
Follow 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.42.0
[PULL 10/13] tests/qtest: add test for filter-buffer interval change
In reply to Jason WangAdd test_change_interval_timer to verify that modifying the 'interval' property of filter-buffer at runtime takes effect immediately.
The test uses socket backend and filter-redirector to verify timer behavior:
- Creates filter-buffer with a very long interval (1000 seconds)
- Sends a packet which gets buffered
- Advances virtual clock by 1 second, verifies packet is still buffered
- Changes interval to 1ms via qom-set (timer should be rescheduled)
- Advances virtual clock by 2ms, verifies packet is now released
- This proves the timer was rescheduled immediately when interval changed
The test uses filter-redirector to observe when packets are released by filter-buffer, providing end-to-end verification of the timer rescheduling behavior.
--- tests/qtest/meson.build | 1 + tests/qtest/test-filter-buffer.c | 169 +++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 tests/qtest/test-filter-buffer.c
tests/qtest/meson.build +1 -0
@@ -46,6 +46,7 @@ qtests_cxl = \ # for the availability of the default NICs in the tests qtests_filter = \ (get_option('default_devices') and slirp.found() ? ['test-netfilter'] : []) + \ + (get_option('default_devices') and host_os != 'windows' ? ['test-filter-buffer'] : []) + \ (get_option('default_devices') and host_os != 'windows' ? ['test-filter-mirror'] : []) + \ (get_option('default_devices') and host_os != 'windows' ? ['test-filter-redirector'] : [])
tests/qtest/test-filter-buffer.c new file +169 -0
169 lines not on this page · raw message
Signature · 1 line
2.42.0
The following changes since commit 2339d0a1cfac6ecc667e6e062a593865c1541c35:
Merge tag 'hw-misc-20260120' of https://github.com/philmd/qemu into staging (2026-01-21 07:39:57 +1100)
are available in the Git repository at:
https://github.com/jasowang/qemu.git tags/net-pull-request
for you to fetch changes up to 014c06435f96c82d0e0be67832d40be43133dc71:
tests/qtest: Add test for filter-redirector rx event opened (2026-01-23 14:47:24 +0800)
----------------------------------------------------------------
-----BEGIN PGP SIGNATURE-----
iQEzBAABCAAdFiEEIV1G9IJGaJ7HfzVi7wSWWzmNYhEFAmlzZqsACgkQ7wSWWzmN YhGITAf+I46cGYha4dE7Gepbqnk+/eHxURNhToX2yZwWsRBkn4LEWHxavWzYGhTk acaVL7zPiHG7S33xSUT7Ie3yrLvbpATAlBsa5xbEKS26KAVIzVtsmMJA6jHyKXUX RKBoX2zUkveMZCDtU0XSPjf/wzf7LyeFEDk/o9Agl5zzqfU3mfe58Zk+9MkpFJ9Y HEGgocbW4Kuu65RJzesejbrBw0f3PMq8cfktUJ8rj0o5v5MX58hrijBAbE5JLrxG Z2u1GvMFR4ZA3e+Mmgu5zg2/AZ4/ZrN9c8moxB9DWLVX8Psz4fJwyYm2Hx0ldhf4 4ETQ326nrAZ5REiUTea1FPACBSK7dw==
=TV7k
-----END PGP SIGNATURE-----
----------------------------------------------------------------
Houqi (Nick) Zuo (1):
net/tap-linux.c: avoid abort when setting invalid fd
Jason Wang (5):
net/filter-buffer: make interval change take effect immediately tests/qtest: add test for filter-buffer interval change net/filter-redirector: add support for dynamic status on/off switching
qtest: add a test to test redirector status change tests/qtest: Add test for filter-redirector rx event opened
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/filter-buffer.c | 6 + net/filter-mirror.c | 38 ++++- net/tap-bsd.c | 3 +- net/tap-linux.c | 28 ++-- net/tap-solaris.c | 3 +- net/tap-stub.c | 3 +- net/tap.c | 94 ++++++------ net/tap_int.h | 3 +- tests/qtest/meson.build | 1 + tests/qtest/test-filter-buffer.c | 169 ++++++++++++++++++++ tests/qtest/test-filter-redirector.c | 288 +++++++++++++++++++++++++++++++++++ 11 files changed, 564 insertions(+), 72 deletions(-) create mode 100644 tests/qtest/test-filter-buffer.c
[PULL 06/13] net/tap: setup exit notifier only when needed
From: Vladimir Sementsov-Ogievskiy <[email protected]>
No 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.42.0
[PULL 09/13] net/filter-buffer: make interval change take effect immediately
In reply to Jason WangPreviously, when the 'interval' property was modified at runtime via QMP, the new value would only take effect after the current timer period elapsed. This could lead to unexpected behavior when users expect immediate changes.
Fix this by checking if the timer is already running when setting the interval property. If so, reschedule the timer with the new interval value immediately.
--- net/filter-buffer.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/filter-buffer.c +6 -0
@@ -159,6 +159,7 @@ static void filter_buffer_set_interval(Object *obj, Visitor *v, Error **errp) {FilterBufferState *s =FILTER_BUFFER (obj); +NetFilterState *nf =NETFILTER (obj);uint32_t value;if (!visit_type_uint32 (v, name, &value, errp)) {
@@ -170,6 +171,11 @@ static void filter_buffer_set_interval(Object *obj, Visitor *v,return ; } s->interval = value; + +if (nf->netdev && nf->on) { +timer_mod (&s->release_timer, +qemu_clock_get_us (QEMU_CLOCK_VIRTUAL ) + s->interval); + } }static void filter_buffer_class_init (ObjectClass *oc,const void *data)
Signature · 1 line
2.42.0
[PULL 02/13] net/tap: net_init_tap_one(): drop extra error propagation
In reply to Jason WangFrom: Vladimir Sementsov-Ogievskiy <[email protected]>
--- 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.42.0
[PULL 04/13] net/tap: pass NULL to net_init_tap_one() in cases when scripts are NULL
In reply to Jason WangFrom: Vladimir Sementsov-Ogievskiy <[email protected]>
Directly 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
@@ -807,8 +807,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 ];
@@ -818,8 +816,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. */
@@ -860,7 +856,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);
@@ -921,7 +917,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) {
@@ -966,7 +962,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);
@@ -974,6 +970,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.42.0
[PULL 11/13] net/filter-redirector: add support for dynamic status on/off switching
In reply to Jason WangCurrently, filter-redirector does not implement the status_changed callback, which means the 'status' property cannot be used to dynamically enable/disable the filter at runtime. When status is set to 'off' via QMP/HMP, the filter still receives data from the indev chardev because the chardev handlers remain registered.
This patch adds proper support for the 'status' property:
1. Implement filter_redirector_status_changed() callback:
- When status changes to 'off': remove chardev read handlers
- When status changes to 'on': re-register chardev handlers (only if chardev is already open)
2. Update filter_redirector_setup() to respect initial status:
- If filter is created with status=off, do not register handlers
- This allows creating disabled filters via command line or QMP
3. Handle chardev OPENED/CLOSED events to re-arm handlers on reconnect:
- Keep the chr_event callback installed on CLOSE so a later OPENED
can re-register the read handlers when nf->on
- Use qemu_chr_fe_set_handlers_full(..., set_open=false, sync_state=false) instead of qemu_chr_fe_set_handlers() because the latter forces sync_state=true and may emit CHR_EVENT_OPENED for an already-open backend. Doing that from inside the chr_event callback would cause recursive/re-entrant OPENED handling.
--- net/filter-mirror.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-)
net/filter-mirror.c +33 -5
@@ -179,9 +179,16 @@ static void redirector_chr_event(void *opaque, QEMUChrEvent event)MirrorState *s =FILTER_REDIRECTOR (nf);switch (event) { +case CHR_EVENT_OPENED : +if (nf->on) { +qemu_chr_fe_set_handlers_full (&s->chr_in, redirector_chr_can_read, + redirector_chr_read, redirector_chr_event, +NULL , nf,NULL , false, false); + } +break ;case CHR_EVENT_CLOSED : -qemu_chr_fe_set_handlers (&s->chr_in,NULL ,NULL ,NULL , -NULL ,NULL ,NULL , true); +qemu_chr_fe_set_handlers_full (&s->chr_in,NULL ,NULL , redirector_chr_event, +NULL , nf,NULL , false, false);break ;default :break ;
@@ -306,9 +313,11 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)return ; } -qemu_chr_fe_set_handlers (&s->chr_in, redirector_chr_can_read, - redirector_chr_read, redirector_chr_event, -NULL , nf,NULL , true); +if (nf->on) { +qemu_chr_fe_set_handlers (&s->chr_in, redirector_chr_can_read, + redirector_chr_read, redirector_chr_event, +NULL , nf,NULL , true); + } }if (s->outdev) {
@@ -324,6 +333,24 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp) } } +static void filter_redirector_status_changed (NetFilterState *nf,Error **errp) +{ +MirrorState *s =FILTER_REDIRECTOR (nf); + +if (!s->indev) { +return ; + } + +if (nf->on) { +qemu_chr_fe_set_handlers (&s->chr_in, redirector_chr_can_read, + redirector_chr_read, redirector_chr_event, +NULL , nf,NULL , true); + }else { +qemu_chr_fe_set_handlers (&s->chr_in,NULL ,NULL ,NULL , +NULL ,NULL ,NULL , true); + } +} +static char *filter_redirector_get_indev (Object *obj,Error **errp) {MirrorState *s =FILTER_REDIRECTOR (obj);
@@ -440,6 +467,7 @@ static void filter_redirector_class_init(ObjectClass *oc, const void *data) nfc->setup = filter_redirector_setup; nfc->cleanup = filter_redirector_cleanup; nfc->receive_iov = filter_redirector_receive_iov; + nfc->status_changed = filter_redirector_status_changed; }static void filter_mirror_init (Object *obj)
Signature · 1 line
2.42.0
[PULL 13/13] tests/qtest: Add test for filter-redirector rx event opened
In reply to Jason WangAdd a new test case 'test_redirector_rx_event_opened' to verify the handling of the CHR_EVENT_OPENED event in filter-redirector.
The test simulates a scenario where the backend character device (socket) is disconnected and then reconnected. It works by:
1. Connecting to the redirector's socket (triggers CHR_EVENT_OPENED).
2. Sending a packet to verify initial connectivity.
3. Disconnecting (triggers CHR_EVENT_CLOSED).
4. Reconnecting (triggers CHR_EVENT_OPENED again).
5. Sending another packet to verify that the redirector correctly re-registers its handlers and resumes passing traffic.
This ensures that the filter-redirector can recover and function correctly after a backend reconnection.
--- tests/qtest/test-filter-redirector.c | 96 ++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+)
tests/qtest/test-filter-redirector.c +96 -0
@@ -385,6 +385,100 @@ static void test_redirector_init_status_off(void)qtest_quit (qts); } +static void test_redirector_rx_event_opened (void ) +{ +int backend_sock[2 ], send_sock; +uint32_t ret =0 , len =0 ; +char send_buf[] ="Hello!!" ; +char send_buf2[] ="Hello2!!" ; +char sock_path0[] ="filter-redirector0.XXXXXX" ; +char *recv_buf; +uint32_t size =sizeof (send_buf); +uint32_t size2 =sizeof (send_buf2); + size =htonl (size); + size2 =htonl (size2); +QTestState *qts; + + ret =socketpair (PF_UNIX ,SOCK_STREAM ,0 , backend_sock); + g_assert_cmpint(ret, !=, -1); + + ret =mkstemp (sock_path0); + g_assert_cmpint(ret, !=, -1); + + qts =qtest_initf ( +"-nic socket,id=qtest-bn0,fd=%d " +"-chardev socket,id=redirector0,path=%s,server=on,wait=off " +"-object filter-redirector,id=qtest-f0,netdev=qtest-bn0," +"queue=rx,indev=redirector0 " , + backend_sock[1 ], sock_path0); + +struct iovec iov[] = { + { + .iov_base = &size, + .iov_len =sizeof (size), + }, { + .iov_base = send_buf, + .iov_len =sizeof (send_buf), + }, + }; + +struct iovec iov2[] = { + { + .iov_base = &size2, + .iov_len =sizeof (size2), + }, { + .iov_base = send_buf2, + .iov_len =sizeof (send_buf2), + }, + }; + +/* First connection */ + send_sock =unix_connect (sock_path0,NULL ); + g_assert_cmpint(send_sock, !=, -1); +qtest_qmp_assert_success (qts,"{ 'execute' : 'query-status'}" ); + + ret =iov_send (send_sock, iov,2 ,0 ,sizeof (size) +sizeof (send_buf)); + g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size)); + + ret =recv (backend_sock[0 ], &len,sizeof (len),0 ); + g_assert_cmpint(ret, ==, sizeof(len)); + len =ntohl (len); + g_assert_cmpint(len, ==, sizeof(send_buf)); + recv_buf =g_malloc (len); + ret =recv (backend_sock[0 ], recv_buf, len,0 ); + g_assert_cmpint(ret, ==, len); + g_assert_cmpstr(recv_buf, ==, send_buf); +g_free (recv_buf); + +close (send_sock); + +/* Verify disconnection handling if needed, but mainly we want to test Reconnection */ +qtest_qmp_assert_success (qts,"{ 'execute' : 'query-status'}" ); + +/* Second connection */ + send_sock =unix_connect (sock_path0,NULL ); + g_assert_cmpint(send_sock, !=, -1); +qtest_qmp_assert_success (qts,"{ 'execute' : 'query-status'}" ); + + ret =iov_send (send_sock, iov2,2 ,0 ,sizeof (size2) +sizeof (send_buf2)); + g_assert_cmpint(ret, ==, sizeof(send_buf2) + sizeof(size2)); + + ret =recv (backend_sock[0 ], &len,sizeof (len),0 ); + g_assert_cmpint(ret, ==, sizeof(len)); + len =ntohl (len); + g_assert_cmpint(len, ==, sizeof(send_buf2)); + recv_buf =g_malloc (len); + ret =recv (backend_sock[0 ], recv_buf, len,0 ); + g_assert_cmpint(ret, ==, len); + g_assert_cmpstr(recv_buf, ==, send_buf2); +g_free (recv_buf); + +close (send_sock); +unlink (sock_path0); +qtest_quit (qts); +close (backend_sock[0 ]); +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );
@@ -393,5 +487,7 @@ int main(int argc, char **argv)qtest_add_func ("/netfilter/redirector_status" , test_redirector_status);qtest_add_func ("/netfilter/redirector_init_status_off" , test_redirector_init_status_off); +qtest_add_func ("/netfilter/redirector_rx_event_opened" , + test_redirector_rx_event_opened);return g_test_run (); }
Signature · 1 line
2.42.0
Richard Henderson
AppliedQuoted 11 lines · Jason Wang
The following changes since commit
2339d0a1cfac6ecc667e6e062a593865c1541c35:Merge tag 'hw-misc-20260120' ofhttps://github.com/philmd/qemu into staging (2026-01-21 07:39:57 +1100)are available in the Git repository at:
https://github.com/jasowang/qemu.git tags/net-pull-request
for you to fetch changes up to
014c06435f96c82d0e0be67832d40be43133dc71:tests/qtest: Add test for filter-redirector rx event opened (2026-01-23 14:47:24 +0800)
Applied, thanks. Please update https://wiki.qemu.org/ChangeLog/11.0 as appropriate.
r~