Make local migration with TAP network device possible
cover Make local migration with TAP network device possible
Hi
These couple patches aims to make possible local migration (within one host) on the same TAP device used by source and destination QEMU
The scenario looks like this
1. Create TAP devices and pass file descriptors to source QEMU
2. Launch destination QEMU (-incoming defer) and pass same descriptors to it.
3. Start migration
Regarding the first patch: It makes possible to receive file descriptor in non-blocking state. But I probably didn't cover all FD users which might need to set blocking state after the patch. So I'm hopping for the hints where else, besides fd_start_incoming_migration() I need to put qemu_socket_set_block() calls.
Andrey Ryabinin (2):
chardev: don't set O_NONBLOCK on SCM_RIGHTS file descriptors.
tap: initialize TAPState->enabled according to the actual state of
queue
chardev/char-socket.c | 3 --- io/channel-socket.c | 3 --- migration/fd.c | 2 ++ net/tap-bsd.c | 5 +++++ net/tap-linux.c | 12 ++++++++++++ net/tap-solaris.c | 5 +++++ net/tap.c | 2 +- net/tap_int.h | 1 + 8 files changed, 26 insertions(+), 7 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/2 chardev: don't set O_NONBLOCK on SCM_RIGHTS file descriptors.
This reverts commit 9b938c7262e4 ("chardev: clear O_NONBLOCK on SCM_RIGHTS file descriptors"). File descriptor passed to QEMU via 'getfd' QMP command always
changed to blocking mode. Instead of that, change blocking mode by QEMU
file descriptors users when necessary, e.g. like migration.
We need to preserve the state of the file descriptor in case it's still used by an external process and before the QEMU itself started using it.
E.g. our local migration scenario with TAP networking looks like this:
1. Create TAP devices and pass file descriptors to source QEMU
2. Launch destination QEMU (-incoming defer) and pass same descriptors to it.
3. Start migration
In such scenario setting blocking state at stage (2) will hang source QEMU since TAP fd suddenly become blocking.
--- chardev/char-socket.c | 3 --- io/channel-socket.c | 3 --- migration/fd.c | 2 ++ 3 files changed, 2 insertions(+), 6 deletions(-)
chardev/char-socket.c +0 -3
@@ -310,9 +310,6 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)continue ; } -/* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ -qemu_socket_set_block (fd); -#ifndef MSG_CMSG_CLOEXEC qemu_set_cloexec (fd);#endif
io/channel-socket.c +0 -3
@@ -479,9 +479,6 @@ static void qio_channel_socket_copy_fds(struct msghdr *msg,continue ; } -/* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ -qemu_socket_set_block (fd); -#ifndef MSG_CMSG_CLOEXEC qemu_set_cloexec (fd);#endif
migration/fd.c +2 -0
@@ -60,6 +60,8 @@ void fd_start_incoming_migration(const char *fdname, Error **errp)return ; } +qemu_socket_set_block (fd); +trace_migration_fd_incoming (fd); ioc =qio_channel_new_fd (fd, errp);
Signature · 1 line
2.35.1
Re: [PATCH 1/2] chardev: don't set O_NONBLOCK on SCM_RIGHTS file descriptors.
Quoted 9 lines · Andrey Ryabinin
This reverts commit
9b938c7262e4("chardev: clearO_NONBLOCKonSCM_RIGHTSfile descriptors"). File descriptor passed to QEMU via 'getfd' QMP command always
changed to blocking mode. Instead of that, change blocking mode by QEMU
file descriptors users when necessary, e.g. like migration.We need to preserve the state of the file descriptor in case it's still used by an external process and before the QEMU itself started using it.
Quoted 7 lines
E.g. our local migration scenario with TAP networking looks like this:
1. Create TAP devices and pass file descriptors to source QEMU
2. Launch destination QEMU (-incoming defer) and pass same descriptors to it.
3. Start migrationIn such scenario setting blocking state at stage (2) will hang source QEMU since TAP fd suddenly become blocking.
Is it possible to add a special flag or API for preserving the O_NONBLOCK open flag? That way the rest of QEMU could continue to safely reset the flag while the tap fd passing code would explicitly ask for the O_NONBLOCK open flag to be preserved. That seems safer but I haven't checked whether it's possible to do this.
Quoted 52 lines
Signed-off-by Andrey Ryabinin <[email protected]>--- chardev/char-socket.c | 3 --- io/channel-socket.c | 3 --- migration/fd.c | 2 ++ 3 files changed, 2 insertions(+), 6 deletions(-)chardev/char-socket.c +0
-3@@ -310,9 +310,6 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)continue ; } -/* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ -qemu_socket_set_block (fd); -#ifndef MSG_CMSG_CLOEXEC qemu_set_cloexec (fd);#endif io/channel-socket.c +0
-3@@ -479,9 +479,6 @@ static void qio_channel_socket_copy_fds(struct msghdr *msg,continue ; } -/* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ -qemu_socket_set_block (fd); -#ifndef MSG_CMSG_CLOEXEC qemu_set_cloexec (fd);#endif migration/fd.c +2
-0@@ -60,6 +60,8 @@ void fd_start_incoming_migration(const char *fdname, Error **errp)return ; } +qemu_socket_set_block (fd); +trace_migration_fd_incoming (fd); ioc =qio_channel_new_fd (fd, errp);Signature · 1 line
2.35.1
Re: [PATCH 1/2] chardev: don't set O_NONBLOCK on SCM_RIGHTS file descriptors.
Quoted 15 lines · Stefan Hajnoczi
Quoted 14 lines · Andrey Ryabinin
This reverts commit
9b938c7262e4("chardev: clearO_NONBLOCKonSCM_RIGHTSfile descriptors"). File descriptor passed to QEMU via 'getfd' QMP command always
changed to blocking mode. Instead of that, change blocking mode by QEMU
file descriptors users when necessary, e.g. like migration.We need to preserve the state of the file descriptor in case it's still used by an external process and before the QEMU itself started using it.
E.g. our local migration scenario with TAP networking looks like this:
1. Create TAP devices and pass file descriptors to source QEMU
2. Launch destination QEMU (-incoming defer) and pass same descriptors to it.
3. Start migration
Quoted 9 lines
Quoted 2 lines
In such scenario setting blocking state at stage (2) will hang source QEMU since TAP fd suddenly become blocking.
Is it possible to add a special flag or API for preserving the
O_NONBLOCKopen flag? That way the rest of QEMU could continue to safely reset the flag while the tap fd passing code would explicitly ask for theO_NONBLOCKopen flag to be preserved. That seems safer but I haven't checked whether it's possible to do this.
The only possibility I see here is embedding some kind 'nonblock' in the message itself along with fds. Not sure if this sensible approach.
Not changing fd state seems like more correct approach to me. E.g. I would expect that sending fd to qemu and executing qmp commands 'getfd' & 'closefd' shouldn't induce any changes in fd state. Which is currently no true.
Re: [PATCH 1/2] chardev: don't set O_NONBLOCK on SCM_RIGHTS file descriptors.
Quoted 28 lines · Andrey Ryabinin
Quoted 15 lines · Stefan Hajnoczi
Quoted 14 lines · Andrey Ryabinin
This reverts commit
9b938c7262e4("chardev: clearO_NONBLOCKonSCM_RIGHTSfile descriptors"). File descriptor passed to QEMU via 'getfd' QMP command always
changed to blocking mode. Instead of that, change blocking mode by QEMU
file descriptors users when necessary, e.g. like migration.We need to preserve the state of the file descriptor in case it's still used by an external process and before the QEMU itself started using it.
E.g. our local migration scenario with TAP networking looks like this:
1. Create TAP devices and pass file descriptors to source QEMU
2. Launch destination QEMU (-incoming defer) and pass same descriptors to it.
3. Start migrationQuoted 9 lines
Quoted 2 lines
In such scenario setting blocking state at stage (2) will hang source QEMU since TAP fd suddenly become blocking.
Is it possible to add a special flag or API for preserving the
O_NONBLOCKopen flag? That way the rest of QEMU could continue to safely reset the flag while the tap fd passing code would explicitly ask for theO_NONBLOCKopen flag to be preserved. That seems safer but I haven't checked whether it's possible to do this.
Quoted 6 lines
The only possibility I see here is embedding some kind 'nonblock' in the message itself along with fds. Not sure if this sensible approach.
Not changing fd state seems like more correct approach to me. E.g. I would expect that sending fd to qemu and executing qmp commands 'getfd' & 'closefd' shouldn't induce any changes in fd state. Which is currently no true.
I think that's a wrong expectation. The contract with 'getfd' is that you are giving the target QEMU ownership of the file. The caller should not expect todo anything with the FD after it has passd it to QEMU, and target QEMU has freedom todo whatever it wants. Admittedly this usage model doesn't fit with what you're trying to make it now do, but those are the historical expectations of getfd.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH 2/2 tap: initialize TAPState->enabled according to the actual state of queue
In reply to Andrey RyabininCurrently TAPState->enabled initialized as true. If fd was passed to qemu in a disabled state it will cause an assert at the attempt to detach queue in virtio_net_set_queues():
virtio_net_set_queues() :
r = peer_detach() -> tap_disable():
if (s->enabled == 0) {
return 0;
} else {
//Will return an error.
ret = tap_fd_disable(s->fd);
...
return ret;
assert(!r);Initialize ->enabled according to the actual state of fd to fix this.
--- net/tap-bsd.c | 5 +++++ net/tap-linux.c | 12 ++++++++++++ net/tap-solaris.c | 5 +++++ net/tap.c | 2 +- net/tap_int.h | 1 + 5 files changed, 24 insertions(+), 1 deletion(-)
net/tap-bsd.c +5 -0
@@ -217,6 +217,11 @@ int tap_probe_vnet_hdr_len(int fd, int len)return 0 ; } +bool tap_probe_enabled (int fd) +{ +return true; +} +void tap_fd_set_vnet_hdr_len (int fd,int len) { }
net/tap-linux.c +12 -0
@@ -193,6 +193,18 @@ int tap_probe_vnet_hdr_len(int fd, int len)return 1 ; } +bool tap_probe_enabled (int fd) +{ +struct ifreq ifr; + +if (ioctl (fd,TUNGETIFF , &ifr) !=0 ) { +error_report ("TUNGETIFF ioctl() failed: %s" , +strerror (errno)); +return false; + } +return !(ifr.ifr_flags &IFF_DETACH_QUEUE ); +} +void tap_fd_set_vnet_hdr_len (int fd,int len) {if (ioctl (fd,TUNSETVNETHDRSZ , &len) ==-1 ) {
net/tap-solaris.c +5 -0
@@ -221,6 +221,11 @@ int tap_probe_vnet_hdr_len(int fd, int len)return 0 ; } +bool tap_probe_enabled (int fd) +{ +return true; +} +void tap_fd_set_vnet_hdr_len (int fd,int len) { }
net/tap.c +1 -1
@@ -399,7 +399,7 @@ static TAPState *net_tap_fd_init(NetClientState *peer, s->host_vnet_hdr_len = vnet_hdr ?sizeof (struct virtio_net_hdr ) :0 ; s->using_vnet_hdr = false; s->has_ufo =tap_probe_has_ufo (s->fd); - s->enabled = true; + s->enabled =tap_probe_enabled (s->fd);tap_set_offload (&s->nc,0 ,0 ,0 ,0 ,0 );/* * Make sure host header length is set correctly in tap:
net/tap_int.h +1 -0
@@ -37,6 +37,7 @@ void 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); +bool tap_probe_enabled (int fd);void tap_fd_set_offload (int fd,int csum,int tso4,int tso6,int ecn,int ufo);void tap_fd_set_vnet_hdr_len (int fd,int len);int tap_fd_set_vnet_le (int fd,int vnet_is_le);
Signature · 1 line
2.35.1
Re: [PATCH 2/2] tap: initialize TAPState->enabled according to the actual state of queue
Quoted 50 lines · Andrey Ryabinin
Currently
TAPState->enabledinitialized as true. If fd was passed to qemu in a disabled state it will cause an assert at the attempt to detach queue invirtio_net_set_queues():
virtio_net_set_queues():r = peer_detach() -> tap_disable(): if (s->enabled == 0) { return 0; } else { //Will return an error. ret = tap_fd_disable(s->fd); ... return ret; assert(!r);Initialize ->enabled according to the actual state of fd to fix this.
Signed-off-by Andrey Ryabinin <[email protected]>--- net/tap-bsd.c | 5 +++++ net/tap-linux.c | 12 ++++++++++++ net/tap-solaris.c | 5 +++++ net/tap.c | 2 +- net/tap_int.h | 1 + 5 files changed, 24 insertions(+), 1 deletion(-)net/tap-bsd.c +5
-0@@ -217,6 +217,11 @@ int tap_probe_vnet_hdr_len(int fd, int len)return 0 ; } +bool tap_probe_enabled (int fd) +{ +return true; +} +void tap_fd_set_vnet_hdr_len (int fd,int len) { }net/tap-linux.c +0
-0@@ -193,6 +193,18 @@ int tap_probe_vnet_hdr_len(int fd, int len)return 1 ; }
net/tap-linux.c int tap_probe_vnet_hdr_len(int fd, int len) · line 205
Quoted 10 lines
net/tap-linux.c +10
-0+bool tap_probe_enabled (int fd) +{ +struct ifreq ifr; + +if (ioctl (fd,TUNGETIFF , &ifr) !=0 ) { +error_report ("TUNGETIFF ioctl() failed: %s" , +strerror (errno)); +return false; + } +return !(ifr.ifr_flags &IFF_DETACH_QUEUE );
Is it better to check IFF_MULT_QUEUE before this to unbreak the old host?
Thanks
Quoted 46 lines
net/tap-linux.c +2
-0+} +void tap_fd_set_vnet_hdr_len (int fd,int len) {if (ioctl (fd,TUNSETVNETHDRSZ , &len) ==-1 ) {net/tap-solaris.c +5
-0@@ -221,6 +221,11 @@ int tap_probe_vnet_hdr_len(int fd, int len)return 0 ; } +bool tap_probe_enabled (int fd) +{ +return true; +} +void tap_fd_set_vnet_hdr_len (int fd,int len) { }net/tap.c +1
-1@@ -399,7 +399,7 @@ static TAPState *net_tap_fd_init(NetClientState *peer, s->host_vnet_hdr_len = vnet_hdr ?sizeof (struct virtio_net_hdr ) :0 ; s->using_vnet_hdr = false; s->has_ufo =tap_probe_has_ufo (s->fd); - s->enabled = true; + s->enabled =tap_probe_enabled (s->fd);tap_set_offload (&s->nc,0 ,0 ,0 ,0 ,0 );/* * Make sure host header length is set correctly in tap: net/tap_int.h +1
-0@@ -37,6 +37,7 @@ void 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); +bool tap_probe_enabled (int fd);void tap_fd_set_offload (int fd,int csum,int tso4,int tso6,int ecn,int ufo);void tap_fd_set_vnet_hdr_len (int fd,int len);int tap_fd_set_vnet_le (int fd,int vnet_is_le);
Quoted 11 lines · Andrey Ryabinin
Hi
These couple patches aims to make possible local migration (within one host) on the same TAP device used by source and destination QEMU
The scenario looks like this
1. Create TAP devices and pass file descriptors to source QEMU
2. Launch destination QEMU (-incoming defer) and pass same descriptors to it.
3. Start migration
Quoted 4 lines
Regarding the first patch: It makes possible to receive file descriptor in non-blocking state. But I probably didn't cover all FD users which might need to set blocking state after the patch. So I'm hopping for the hints where else, besides
fd_start_incoming_migration()I need to putqemu_socket_set_block()calls.
You'll need to check all callers of
qio_channel_readv_fullqio_channel_readv_full_allqio_channel_readv_full_all_eof
and identify which pass a non-NULL parameter for 'fds'. If the caller does NOT have a qemu_setnonblock call on the FD it gets back, then you have to assume it is expecting it in blocking mode and so need to add qemu_setblock
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 11 lines · Andrey Ryabinin
Hi
These couple patches aims to make possible local migration (within one host) on the same TAP device used by source and destination QEMU
The scenario looks like this
1. Create TAP devices and pass file descriptors to source QEMU
2. Launch destination QEMU (-incoming defer) and pass same descriptors to it.
3. Start migration
Quoted 4 lines
Regarding the first patch: It makes possible to receive file descriptor in non-blocking state. But I probably didn't cover all FD users which might need to set blocking state after the patch. So I'm hopping for the hints where else, besides
fd_start_incoming_migration()I need to putqemu_socket_set_block()calls.
Nice feature. I am worried that these patches are unsafe/incomplete though.
Tap local migration isn't explicitly visible in the code. How will other developers know the feature is there and how to avoid breaking it when modifying the code? Maybe a migration test case, comments that explain the rules about accessing the tap fd, and/or assertions?
How does this interact with hw/net/vhost_net.c, which uses tap_get_fd() to borrow the fd? I guess the idea is that the source VM is paused and no tap activity is expected. Then migration handover happens and the destination VM starts running and is allowed to access the tap fd. However, the source VM still has vhost_net with the tap fd set up. I wonder if there is any issue with interference between the two vhost_net instances?
These kinds of questions should be answered, mostly in the code but also in the cover letter. It should be clear why this approach is correct.
Thanks,
Stefan
Quoted 20 lines
Andrey Ryabinin (2):
chardev: don't setO_NONBLOCKonSCM_RIGHTSfile descriptors.
tap: initializeTAPState->enabledaccording to the actual state of
queuechardev/char-socket.c | 3 --- io/channel-socket.c | 3 --- migration/fd.c | 2 ++ net/tap-bsd.c | 5 +++++ net/tap-linux.c | 12 ++++++++++++ net/tap-solaris.c | 5 +++++ net/tap.c | 2 +- net/tap_int.h | 1 + 8 files changed, 26 insertions(+), 7 deletions(-)Signature · 1 line
2.35.1
Quoted 17 lines · Stefan Hajnoczi
Quoted 11 lines · Andrey Ryabinin
Hi
These couple patches aims to make possible local migration (within one host) on the same TAP device used by source and destination QEMU
The scenario looks like this
1. Create TAP devices and pass file descriptors to source QEMU
2. Launch destination QEMU (-incoming defer) and pass same descriptors to it.
3. Start migrationQuoted 4 lines
Regarding the first patch: It makes possible to receive file descriptor in non-blocking state. But I probably didn't cover all FD users which might need to set blocking state after the patch. So I'm hopping for the hints where else, besides
fd_start_incoming_migration()I need to putqemu_socket_set_block()calls.
Quoted 8 lines
Nice feature. I am worried that these patches are unsafe/incomplete though.
Tap local migration isn't explicitly visible in the code. How will other developers know the feature is there and how to avoid breaking it when modifying the code? Maybe a migration test case, comments that explain the rules about accessing the tap fd, and/or assertions?
Yeah, I'm writing avocado test case, will add it in future next iterations.
Quoted 8 lines
How does this interact with
hw/net/vhost_net.c, which usestap_get_fd()to borrow the fd? I guess the idea is that the source VM is paused and no tap activity is expected. Then migration handover happens and the destination VM starts running and is allowed to access the tap fd. However, the source VM still hasvhost_netwith the tap fd set up. I wonder if there is any issue with interference between the twovhost_netinstances?
I'll try to take a closer look at code, let's see if I can find any possible issues. But I did some brief testing with vhost=on net device, and it find any problems. The test was
1. launch pings from source VM to host
2. Migrate
3. Check at dest VM that there is no lost packets.
Quoted 26 lines
These kinds of questions should be answered, mostly in the code but also in the cover letter. It should be clear why this approach is correct.
Thanks,
StefanQuoted 20 lines
Andrey Ryabinin (2):
chardev: don't setO_NONBLOCKonSCM_RIGHTSfile descriptors.
tap: initializeTAPState->enabledaccording to the actual state of
queuechardev/char-socket.c | 3 --- io/channel-socket.c | 3 --- migration/fd.c | 2 ++ net/tap-bsd.c | 5 +++++ net/tap-linux.c | 12 ++++++++++++ net/tap-solaris.c | 5 +++++ net/tap.c | 2 +- net/tap_int.h | 1 + 8 files changed, 26 insertions(+), 7 deletions(-)Signature · 1 line
2.35.1
Thanks! I will be offline for the next week.
Feel free to progress this series without my review.
Stefan
Quoted 83 lines · On Fri, Jun 24, 2022, 11:53 Andrey Ryabinin <[email protected]> wrote:
Quoted 4 lines · Stefan Hajnoczi
Quoted 3 lines · Andrey Ryabinin
Hi
These couple patches aims to make possible local migration (within one
host)
Quoted 5 lines
Quoted 5 lines
on the same TAP device used by source and destination QEMU
The scenario looks like this
1. Create TAP devices and pass file descriptors to source QEMU
2. Launch destination QEMU (-incoming defer) and pass same descriptorsto it.
Quoted 4 lines
Quoted 4 lines
3. Start migration
Regarding the first patch: It makes possible to receive file descriptor
in non-blocking
Quoted 1 line
Quoted 1 line
state. But I probably didn't cover all FD users which might need to set
blocking state after
Quoted 1 line
Quoted 1 line
the patch. So I'm hopping for the hints where else, besides
fd_start_incoming_migration()Quoted 10 lines
Quoted 1 line
I need to put
qemu_socket_set_block()calls.Nice feature. I am worried that these patches are unsafe/incomplete though.
Tap local migration isn't explicitly visible in the code. How will other developers know the feature is there and how to avoid breaking it when modifying the code? Maybe a migration test case, comments that explain the rules about accessing the tap fd, and/or assertions?
Yeah, I'm writing avocado test case, will add it in future next iterations.
Quoted 8 lines
How does this interact with
hw/net/vhost_net.c, which usestap_get_fd()to borrow the fd? I guess the idea is that the source VM is paused and no tap activity is expected. Then migration handover happens and the destination VM starts running and is allowed to access the tap fd. However, the source VM still hasvhost_netwith the tap fd set up. I wonder if there is any issue with interference between the twovhost_netinstances?I'll try to take a closer look at code, let's see if I can find any possible issues.
But I did some brief testing with vhost=on net device, and it find any problems.
The test was
1. launch pings from source VM to host
2. Migrate
3. Check at dest VM that there is no lost packets.Quoted 26 lines
These kinds of questions should be answered, mostly in the code but also in the cover letter. It should be clear why this approach is correct.
Thanks,
StefanQuoted 20 lines
Andrey Ryabinin (2):
chardev: don't setO_NONBLOCKonSCM_RIGHTSfile descriptors.
tap: initializeTAPState->enabledaccording to the actual state of
queuechardev/char-socket.c | 3 --- io/channel-socket.c | 3 --- migration/fd.c | 2 ++ net/tap-bsd.c | 5 +++++ net/tap-linux.c | 12 ++++++++++++ net/tap-solaris.c | 5 +++++ net/tap.c | 2 +- net/tap_int.h | 1 + 8 files changed, 26 insertions(+), 7 deletions(-)--
2.35.1