nbd: Adapt for dataplane
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 17 lines · Max Reitz
For the NBD server to work with dataplane, it needs to correctly access the exported BDS. It makes the most sense to run both in the same AioContext, therefore this series implements methods for tracking a BDS's AioContext and makes NBD make use of this for keeping the clients connected to that BDS in the same AioContext.
The reason this is an RFC and not a PATCH is my inexperience with AIO, coroutines and the like. Also, I'm not sure about what to do about the coroutines. The NBD server has up to two coroutines per client: One for receiving and one for sending. Theoretically, both have to be "transferred" to the new AioContext if it is changed; however, as far as I see it, coroutines are not really bound to an AioContext, they are simply run in the AioContext entering them. Therefore, I think a transfer is unnecessary. All coroutines are entered from
nbd_read()andnbd_restart_write(), both of which are AIO routines registered viaaio_set_fd_handler2().
Quoted 4 lines
As
bs_aio_detach()unregisters all of these routines, the coroutines can no longer be entered, but only afterbs_aio_attach()is called again. Then, when they are called, they will enter the coroutines in the new AioContext. Therefore, I think an explicit transfer unnecessary.
This reasoning sounds correct.
Quoted 3 lines
However, if
bs_aio_detach()is called from a different thread than the old AioContext is running in, we may still have coroutines running for which we should wait before returning frombs_aio_detach().
The bdrv_attach/detach_aio_context() APIs have rules regarding where these functions are called from:
/**
* bdrv_set_aio_context:
*
* Changes the #AioContext used for fd handlers, timers, and BHs by this
* BlockDriverState and all its children.
*
* This function must be called from the old #AioContext or with a lock held so
* the old #AioContext is not executing.
*/
void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context);
and:
/* Remove fd handlers, timers, and other event loop callbacks so the event
* loop is no longer in use. Called with no in-flight requests and in
* depth-first traversal order with parents before child nodes. */
void (*bdrv_detach_aio_context)(BlockDriverState *bs);
/* Add fd handlers, timers, and other event loop callbacks so I/O requests
* can be processed again. Called with no in-flight requests and in
* depth-first traversal order with child nodes before parent nodes. */
void (*bdrv_attach_aio_context)(BlockDriverState *bs,
AioContext *new_context);
These rules ensure that it's safe to perform these operations. You don't have to support arbitrary callers in NBD either.
Quoted 9 lines
But because of my inexperience with coroutines, I'm not sure. I now have these patches nearly unchanged here for about a week and I'm looking for ways of testing them, but so far I could only test whether the old use cases work, but not whether they will work for what they are intended to
do: With BDS changing their AioContext.So, because I'm not sure what else to do and because I don't know how to test multiple AIO threads (how do I move a BDS into another iothread?) I'm just sending this out as an RFC.
Use a Linux guest with virtio-blk:
qemu -drive if=none,file=test.img,id=drive0 \
-object iothread,id=iothread0 \
-device virtio-blk-pci,drive=drive0,x-iothread=iothread0 \
...Once the guest has booted the virtio-blk device will be in dataplane mode. That means drive0's BlockDriverState ->aio_context will be the IOThread AioContext and not the global qemu_aio_context.
Now you can exercise the run-time NBD server over QMP and check that things still work. For example, try running a few instance of dd if=/dev/vdb of=/dev/null iflag=direct inside the guest to stress guest I/O.
Typically what happens if code is not dataplane-aware is that a deadlock or crash occurs due to race conditions between the QEMU main loop and the IOThread for this virtio-blk device.
For an overview of dataplane programming concepts, see:
https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg01436.html
Stefan
Quoted 30 lines · Stefan Hajnoczi
Quoted 17 lines · Max Reitz
For the NBD server to work with dataplane, it needs to correctly access the exported BDS. It makes the most sense to run both in the same AioContext, therefore this series implements methods for tracking a BDS's AioContext and makes NBD make use of this for keeping the clients connected to that BDS in the same AioContext.
The reason this is an RFC and not a PATCH is my inexperience with AIO, coroutines and the like. Also, I'm not sure about what to do about the coroutines. The NBD server has up to two coroutines per client: One for receiving and one for sending. Theoretically, both have to be "transferred" to the new AioContext if it is changed; however, as far as I see it, coroutines are not really bound to an AioContext, they are simply run in the AioContext entering them. Therefore, I think a transfer is unnecessary. All coroutines are entered from
nbd_read()andnbd_restart_write(), both of which are AIO routines registered viaaio_set_fd_handler2().Quoted 4 lines
As
bs_aio_detach()unregisters all of these routines, the coroutines can no longer be entered, but only afterbs_aio_attach()is called again. Then, when they are called, they will enter the coroutines in the new AioContext. Therefore, I think an explicit transfer unnecessary.This reasoning sounds correct.
Quoted 3 lines
However, if
bs_aio_detach()is called from a different thread than the old AioContext is running in, we may still have coroutines running for which we should wait before returning frombs_aio_detach().The bdrv_attach/detach_aio_context() APIs have rules regarding where these functions are called from:
Quoted 8 lines
/**
*bdrv_set_aio_context:
*
* Changes the #AioContext used for fd handlers, timers, and BHs by this
* BlockDriverState and all its children.
*
* This function must be called from the old #AioContext or with a lock held so
* the old #AioContext is not executing.
Oh, that makes things easier. *g*
Quoted 32 lines
*/void
bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context);and:
/* Remove fd handlers, timers, and other event loop callbacks so the event * loop is no longer in use. Called with no in-flight requests and in * depth-first traversal order with parents before child nodes. */ void (*bdrv_detach_aio_context)(BlockDriverState *bs); /* Add fd handlers, timers, and other event loop callbacks so I/O requests * can be processed again. Called with no in-flight requests and in * depth-first traversal order with child nodes before parent nodes. */ void (*bdrv_attach_aio_context)(BlockDriverState *bs, AioContext *new_context);These rules ensure that it's safe to perform these operations. You don't have to support arbitrary callers in NBD either.
Quoted 9 lines
But because of my inexperience with coroutines, I'm not sure. I now have these patches nearly unchanged here for about a week and I'm looking for ways of testing them, but so far I could only test whether the old use cases work, but not whether they will work for what they are intended to
do: With BDS changing their AioContext.So, because I'm not sure what else to do and because I don't know how to test multiple AIO threads (how do I move a BDS into another iothread?) I'm just sending this out as an RFC.
Use a Linux guest with virtio-blk:
Quoted 8 lines
qemu -drive if=none,file=test.img,id=drive0 \ -object iothread,id=iothread0 \ -device virtio-blk-pci,drive=drive0,x-iothread=iothread0 \ ...Once the guest has booted the virtio-blk device will be in dataplane mode. That means drive0's BlockDriverState ->aio_context will be the IOThread AioContext and not the global
qemu_aio_context.
Ah, thank you.
Quoted 11 lines
Now you can exercise the run-time NBD server over QMP and check that things still work. For example, try running a few instance of dd if=/dev/vdb of=/dev/null iflag=direct inside the guest to stress guest I/O.
Typically what happens if code is not dataplane-aware is that a deadlock or crash occurs due to race conditions between the QEMU main loop and the IOThread for this virtio-blk device.
For an overview of dataplane programming concepts, see:
https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg01436.html
Yes, I took this email as a reference, however it only said how to create a new iothread, but not how to use it. :-)
Max
Quoted 1 line
Stefan
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [Qemu-devel] [RFC 2/5] aio: Add io_read_poll() callback
Il 31/05/2014 20:43, Max Reitz ha scritto:
Review bool aio_pending(AioContext *ctx) · line 127
Quoted 10 lines
(quoted diff) +3
-1@@ -108,7 +120,9 @@ bool aio_pending(AioContext *ctx) int revents; revents = node->pfd.revents & node->pfd.events; - if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) { + if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read && + (!node->io_read_poll || node->io_read_poll(node->opaque))) + { return true; }
This would cause a busy loop whenever io_read_poll returns false. You would need to call io_read_poll when you fill the pollfds array, i.e. in aio_poll and aio_ctx_prepare.
Paolo
Re: [Qemu-devel] [RFC 2/5] aio: Add io_read_poll() callback
Quoted 4 lines · Paolo Bonzini
Il 31/05/2014 20:43, Max Reitz ha scritto:
Quoted 3 lines
(quoted diff) +0
-0@@ -108,7 +120,9 @@ bool aio_pending(AioContext *ctx) int revents;
Quoted 11 lines
Review
Quoted 9 lines
(quoted diff) +1
-1revents = node->pfd.revents & node->pfd.events; - if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) { + if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) &&
node->io_read&&(quoted diff) +2
-0+ (!node->io_read_poll || node->io_read_poll(node->opaque))) + { return true; }This would cause a busy loop whenever
io_read_pollreturns false. You would need to callio_read_pollwhen you fill the pollfds array, i.e. inaio_pollandaio_ctx_prepare.
Thanks; considering Stefan's comment, I'll see whether I can get away without implement io_read_poll().
Max
Quoted 1 line
Paolo
Re: [Qemu-devel] [RFC 2/5] aio: Add io_read_poll() callback
In reply to a message not in the archiveQuoted 16 lines · Max Reitz
Similar to how
qemu_set_fd_handler2()allows defining a function which checks whether the recipient is ready to read, addaio_set_fd_handler2()with a parameter for defining such a callback.Signed-off-by Max Reitz <[email protected]>--- aio-posix.c | 26 ++++++++++++++++++++------ include/block/aio.h | 12 ++++++++++++ include/qemu/main-loop.h | 1 - 3 files changed, 32 insertions(+), 7 deletions(-)aio-posix.c +0
-0
aio-posix.c line 24
Quoted 4 lines
aio-posix.c +1
-0@@ -21,6 +21,7 @@struct AioHandler {GPollFD pfd; +IOCanReadHandler *io_read_poll;
I don't like the io_read_poll() mechanism because it is easy to deadlock. In the network layer these problems are common because it relies on io_read_poll().
Any piece of code that affects the return value of io_read_poll() must remember to call aio_notify() to force a new event loop iteration. Otherwise the thread stays stuck in poll(2) and fails to add the file descriptor back to its fdset.
If you really need io_read_poll() then it's fine. Just wanted to say that code using io_read_poll() can be tricky.
Stefan
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [Qemu-devel] [RFC 1/5] nbd: Correct name comparison for export_set_name()
Quoted 16 lines · Max Reitz
exp->name== name is certainly true if both strings are equal and will work for both of them being NULL (which is important to check here); however, the strings may also be equal without having the same address, in which case there is no need to replace the export's name either. Therefore, add a check for this case.Signed-off-by Max Reitz <[email protected]>--- nbd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)nbd.c +0
-0
nbd.c NBDExport *nbd_export_find(const char *name) · line 837
Quoted 7 lines
nbd.c +1
-1@@ -832,7 +832,7 @@ NBDExport *nbd_export_find(const char *name)void nbd_export_set_name (NBDExport *exp,const char *name) { -if (exp->name == name) { +if (exp->name == name || (exp->name && name && !strcmp (exp->name, name))) {return ; }
It's not clear to me why we even bother. The function is idempotent and there are only 2 call sites in QEMU. This is not a performance-critical function where it helps to bail early.
Can we just drop the if statement completely?
void nbd_export_set_name(NBDExport *exp, const char *name)
{
if (exp->name == name) {
return;
}
nbd_export_get(exp);
if (exp->name != NULL) {
g_free(exp->name);
exp->name = NULL;
QTAILQ_REMOVE(&exports, exp, next);
nbd_export_put(exp);
}
if (name != NULL) {
nbd_export_get(exp);
exp->name = g_strdup(name);
QTAILQ_INSERT_TAIL(&exports, exp, next);
}
nbd_export_put(exp);
}Re: [Qemu-devel] [RFC 1/5] nbd: Correct name comparison for export_set_name()
Quoted 18 lines · Stefan Hajnoczi
Quoted 17 lines · Max Reitz
exp->name== name is certainly true if both strings are equal and will work for both of them being NULL (which is important to check here); however, the strings may also be equal without having the same address, in which case there is no need to replace the export's name either. Therefore, add a check for this case.Signed-off-by Max Reitz <[email protected]>--- nbd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)nbd.c +0
-0@@ -832,7 +832,7 @@ NBDExport *nbd_export_find(const char *name)
Quoted 9 lines
nbd.c NBDExport *nbd_export_find(const char *name) · line 837
Quoted 6 lines
nbd.c +1
-1void nbd_export_set_name (NBDExport *exp,const char *name) { -if (exp->name == name) { +if (exp->name == name || (exp->name && name && !strcmp (exp->name, name))) {return ; }It's not clear to me why we even bother. The function is idempotent and there are only 2 call sites in QEMU. This is not a performance-critical function where it helps to bail early.
You're probably right. I just happened to stumble over this code when looking into NBD.
Quoted 1 line
Can we just drop the if statement completely?
Probably, yes, but then again, I think it's not worth bothering with dropping it, either. ;-)
Max
Quoted 20 lines
void
nbd_export_set_name(NBDExport *exp, const char *name){ if (exp->name == name) { return; } nbd_export_get(exp); if (exp->name != NULL) { g_free(exp->name); exp->name = NULL; QTAILQ_REMOVE(&exports, exp, next); nbd_export_put(exp); } if (name != NULL) { nbd_export_get(exp); exp->name = g_strdup(name); QTAILQ_INSERT_TAIL(&exports, exp, next); } nbd_export_put(exp); }
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [Qemu-devel] [RFC 3/5] nbd: Use aio_set_fd_handler2()
Quoted 2 lines · Max Reitz
Instead of using the main loop function
qemu_set_fd_handler2(), use the AIO function in the context of the exported BDS.
Managing fd handlers shouldn't be necessary at the NBD code level. The current NBD code hasn't been fully converted to coroutines.
This email explains how the NBD code can be fully converted to coroutines. It should simplify the code and reduce the chance of bugs. Whether you want to actually do the conversion is up to you, since it's somewhat orthogonal to the purpose of this patch series.
The point of coroutines is that blocking operations like send/recv on a socket should look like regular blocking calls. Let coroutines handle the event loop housekeeping (registering fd handlers, callbacks). Only use aio explicitly when concurrency is needed.
Here is how I would structure NBD using coroutines:
1 coroutine per connection to receive NBD commands and submit I/O requests:
def nbd_server_receive_co(conn):
while True:
req = nbd_read_req(conn)
if req is None:
break
if req.type == NBD_READ:
bdrv_aio_readv(bs, ...)
elif req.type == NBD_WRITE:
...Notice that bdrv_aio_*() is used since we want concurrent I/O requests.
1 coroutine per connection to send NBD replies:
def nbd_server_send_co(conn):
while True:
while conn.send_queue:
resp = conn.send_queue.pop()
nbd_write_resp(conn, resp)
qemu_coroutine_yield()
And finally the bdrv_aio_*() callback to put responses on to the send queue:
def nbd_server_aio_cb(conn):
resp = NBDResponse(...)
conn.send_queue.push(resp)
conn.send_co.enter()Why is this design cleaner? Because NBD code doesn't have to worry about fd handlers. It uses straightforward coroutine send/recv for socket I/O inside nbd_read_req() and nbd_write_resp(). It's easy to see that only one coroutine receives from the socket and that only one coroutine writes to the socket.
Stefan
Re: [Qemu-devel] [RFC 3/5] nbd: Use aio_set_fd_handler2()
Il 04/06/2014 14:37, Stefan Hajnoczi ha scritto:
Quoted 5 lines
Why is this design cleaner? Because NBD code doesn't have to worry about fd handlers. It uses straightforward coroutine send/recv for socket I/O inside
nbd_read_req()andnbd_write_resp(). It's easy to see that only one coroutine receives from the socket and that only one coroutine writes to the socket.
I don't understand how this would work without managing fd handlers.
- If you don't want to receive anything (because you reached the maximum queue depth), and the client sends something, the read handler will busy wait. The current code solves it with can_read; you could do it by enabling or disabling the read handler as you need, but the idea is the same.
- If you're not sending anything, a socket that has a write handler will have POLLOUT continuously signaled and again you'd busy wait. Since there is no can_write, nbd.c instead enables/disables the write handler around nbd_co_send_reply.
Removing can_read from the NBD server is simpler than Max's patch 2, so that's what I suggest.
The NBD server code uses a coroutine per request. Using one coroutine per direction is nice, but doesn't avert the need to manage fd handlers.
Basically, it boils down to select(2) being level-triggered. If it were edge-triggered, of course you could have other bugs but you probably could write network code without managing fd handlers.
Paolo
Re: [Qemu-devel] [RFC 3/5] nbd: Use aio_set_fd_handler2()
Quoted 8 lines · Paolo Bonzini
Il 04/06/2014 14:37, Stefan Hajnoczi ha scritto:
Quoted 5 lines
Why is this design cleaner? Because NBD code doesn't have to worry about fd handlers. It uses straightforward coroutine send/recv for socket I/O inside
nbd_read_req()andnbd_write_resp(). It's easy to see that only one coroutine receives from the socket and that only one coroutine writes to the socket.I don't understand how this would work without managing fd handlers.
fd handlers still need to be managed, but not by NBD code. They must be managed by coroutine recv/send utility functions. In other words, fd handlers are used locally, not globally.
def co_recv(fd, buf):
while True:
nbytes = recv(fd, buf, len(buf))
if nbytes == -1:
if errno == EINTR:
continue
if errno == EAGAIN or errno == EWOULDBLOCK:
aio_set_fd_read_handler(fd, co_recv_cb)
qemu_coroutine_yield()
aio_set_fd_read_handler(fd, NULL)
continue
return nbytesThe send function is similar.
This does require an extension to the AioContext API. We need to be able to modify the read/write callback independently without clobbering the other callback. This way full duplex I/O is possible.
Quoted 9 lines
- If you don't want to receive anything (because you reached the maximum queue depth), and the client sends something, the read handler will busy wait. The current code solves it with
can_read; you could do it by enabling or disabling the read handler as you need, but the idea is the same.- If you're not sending anything, a socket that has a write handler will have POLLOUT continuously signaled and again you'd busy wait. Since there is no
can_write,nbd.cinstead enables/disables the write handler aroundnbd_co_send_reply.
You only install an fd handler when you want to read/write. This does mean that the request coroutine needs to be woken up by the response coroutine if we were at maximum queue depth.
Stefan
Re: [Qemu-devel] [RFC 3/5] nbd: Use aio_set_fd_handler2()
Il 05/06/2014 10:12, Stefan Hajnoczi ha scritto:
Quoted 27 lines · Paolo Bonzini
Quoted 8 lines
Il 04/06/2014 14:37, Stefan Hajnoczi ha scritto:
Quoted 5 lines
Why is this design cleaner? Because NBD code doesn't have to worry about fd handlers. It uses straightforward coroutine send/recv for socket I/O inside
nbd_read_req()andnbd_write_resp(). It's easy to see that only one coroutine receives from the socket and that only one coroutine writes to the socket.I don't understand how this would work without managing fd handlers.
fd handlers still need to be managed, but not by NBD code. They must be managed by coroutine recv/send utility functions. In other words, fd handlers are used locally, not globally.
def
co_recv(fd, buf):while True: nbytes = recv(fd, buf, len(buf)) if nbytes == -1: if errno == EINTR: continue if errno == EAGAIN or errno == EWOULDBLOCK: aio_set_fd_read_handler(fd, co_recv_cb) qemu_coroutine_yield() aio_set_fd_read_handler(fd, NULL) continue return nbytes
Quoted 1 line
The send function is similar.
I see what you mean now---this however is not how qemu_co_recv and qemu_co_send work. NBD uses them, but they require the caller to set up the handlers manually.
Paolo
Re: [Qemu-devel] [RFC 3/5] nbd: Use aio_set_fd_handler2()
Quoted 28 lines · Paolo Bonzini
Il 05/06/2014 10:12, Stefan Hajnoczi ha scritto:
Quoted 27 lines · Paolo Bonzini
Quoted 8 lines
Il 04/06/2014 14:37, Stefan Hajnoczi ha scritto:
Quoted 5 lines
Why is this design cleaner? Because NBD code doesn't have to worry about fd handlers. It uses straightforward coroutine send/recv for socket I/O inside
nbd_read_req()andnbd_write_resp(). It's easy to see that only one coroutine receives from the socket and that only one coroutine writes to the socket.I don't understand how this would work without managing fd handlers.
fd handlers still need to be managed, but not by NBD code. They must be managed by coroutine recv/send utility functions. In other words, fd handlers are used locally, not globally.
def
co_recv(fd, buf):
while True:nbytes = recv(fd, buf, len(buf)) if nbytes == -1: if errno == EINTR: continue if errno == EAGAIN or errno == EWOULDBLOCK: aio_set_fd_read_handler(fd, co_recv_cb) qemu_coroutine_yield() aio_set_fd_read_handler(fd, NULL) continue return nbytes
Quoted 5 lines
Quoted 1 line
The send function is similar.
I see what you mean now---this however is not how
qemu_co_recvandqemu_co_sendwork. NBD uses them, but they require the caller to set up the handlers manually.
Yep, and I think that's not a clean way to program since it forgoes the advantage of coroutines: functions that look just like their blocking equivalents. I think we should hide the fd handler management so coroutine socket users don't need to worry about it.
Stefan
Re: [Qemu-devel] [RFC 3/5] nbd: Use aio_set_fd_handler2()
In reply to Stefan HajnocziQuoted 50 lines · Stefan Hajnoczi
Quoted 2 lines · Max Reitz
Instead of using the main loop function
qemu_set_fd_handler2(), use the AIO function in the context of the exported BDS.Managing fd handlers shouldn't be necessary at the NBD code level. The current NBD code hasn't been fully converted to coroutines.
This email explains how the NBD code can be fully converted to coroutines. It should simplify the code and reduce the chance of bugs. Whether you want to actually do the conversion is up to you, since it's somewhat orthogonal to the purpose of this patch series.
The point of coroutines is that blocking operations like send/recv on a socket should look like regular blocking calls. Let coroutines handle the event loop housekeeping (registering fd handlers, callbacks). Only use aio explicitly when concurrency is needed.
Here is how I would structure NBD using coroutines:
1 coroutine per connection to receive NBD commands and submit I/O requests:
def
nbd_server_receive_co(conn):while True: req = nbd_read_req(conn) if req is None: break if req.type == NBD_READ: bdrv_aio_readv(bs, ...) elif req.type == NBD_WRITE: ...Notice that bdrv_aio_*() is used since we want concurrent I/O requests.
1 coroutine per connection to send NBD replies:
def
nbd_server_send_co(conn):
while True:
while conn.send_queue:
resp =conn.send_queue.pop()
nbd_write_resp(conn, resp)
qemu_coroutine_yield()And finally the bdrv_aio_*() callback to put responses on to the send queue:
def
nbd_server_aio_cb(conn):resp = NBDResponse(...) conn.send_queue.push(resp) conn.send_co.enter()
Quoted 5 lines
Why is this design cleaner? Because NBD code doesn't have to worry about fd handlers. It uses straightforward coroutine send/recv for socket I/O inside
nbd_read_req()andnbd_write_resp(). It's easy to see that only one coroutine receives from the socket and that only one coroutine writes to the socket.
Yes, this sounds better. I'll take a look into it and see how far I can get.
Max
Re: [Qemu-devel] [RFC 3/5] nbd: Use aio_set_fd_handler2()
Il 05/06/2014 20:18, Max Reitz ha scritto:
Quoted 9 lines
Quoted 6 lines
Why is this design cleaner? Because NBD code doesn't have to worry about fd handlers. It uses straightforward coroutine send/recv for socket I/O inside
nbd_read_req()andnbd_write_resp(). It's easy to see that only one coroutine receives from the socket and that only one coroutine writes to the socket.Yes, this sounds better. I'll take a look into it and see how far I can get.
But it doesn't solve any problem, and requires rethinking the aio_set_fd_handler API. I suggest you just refactor all calls to qemu_set_fd_handler2 into a function like
qemu_set_fd_handler2(fd, NULL, nbd_can_read(client) ? nbd_read : NULL, client->send_coroutine ? nbd_restart_write : NULL, opaque);
and call this function every time the result of nbd_can_read() changes.
Then you can switch to aio_set_fd_handler easily.
Paolo
Re: [Qemu-devel] [RFC 3/5] nbd: Use aio_set_fd_handler2()
Quoted 8 lines · Paolo Bonzini
Il 05/06/2014 20:18, Max Reitz ha scritto:
Quoted 7 lines
Quoted 6 lines
Why is this design cleaner? Because NBD code doesn't have to worry about fd handlers. It uses straightforward coroutine send/recv for socket I/O inside
nbd_read_req()andnbd_write_resp(). It's easy to see that only one coroutine receives from the socket and that only one coroutine writes to the socket.
Quoted 4 lines
Quoted 2 lines
Yes, this sounds better. I'll take a look into it and see how far I can get.
But it doesn't solve any problem, and requires rethinking the
aio_set_fd_handlerAPI.
It might clean things up. :-)
On the other hand, you are right, it's probably too complicated right now.
Quoted 9 lines
I suggest you just refactor all calls to
qemu_set_fd_handler2into a function likeqemu_set_fd_handler2(fd, NULL,
nbd_can_read(client)?nbd_read: NULL,client->send_coroutine?
nbd_restart_write: NULL,
opaque);and call this function every time the result of
nbd_can_read()changes. Then you can switch toaio_set_fd_handlereasily.
Seems good, I'll do that.
Max
Re: [Qemu-devel] [RFC 3/5] nbd: Use aio_set_fd_handler2()
Quoted 29 lines · Max Reitz
Quoted 9 lines · Paolo Bonzini
Il 05/06/2014 20:18, Max Reitz ha scritto:
Quoted 8 lines
Quoted 7 lines
Why is this design cleaner? Because NBD code doesn't have to worry about fd handlers. It uses straightforward coroutine send/recv for socket I/O inside
nbd_read_req()andnbd_write_resp(). It's easy to see
that only one coroutine receives from the socket and that only one coroutine writes to the socket.Quoted 5 lines
Quoted 2 lines
Yes, this sounds better. I'll take a look into it and see how far I can get.
But it doesn't solve any problem, and requires rethinking the
aio_set_fd_handlerAPI.It might clean things up. :-)
On the other hand, you are right, it's probably too complicated right now.
Quoted 9 lines
I suggest you just refactor all calls to
qemu_set_fd_handler2into a function likeqemu_set_fd_handler2(fd, NULL,
nbd_can_read(client)?nbd_read: NULL,client->send_coroutine?
nbd_restart_write: NULL,
opaque);
Quoted 4 lines
Quoted 2 lines
and call this function every time the result of
nbd_can_read()changes. Then you can switch toaio_set_fd_handlereasily.Seems good, I'll do that.
Sounds fine to me. Restructuring NBD can be done later, if desirable.
Stefan
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [Qemu-devel] [RFC 4/5] block: Add AIO followers
Quoted 10 lines · Max Reitz
If a long-running operation on a BDS wants to always remain in the same AIO context, it somehow needs to keep track of the BDS changing its context. This adds a function for registering callbacks on a BDS which are called whenever the BDS is attached or detached from an AIO context.
Signed-off-by Max Reitz <[email protected]>--- block.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ include/block/block_int.h | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+)
Great, we definitely need this mechanism. Block jobs can also use it later.
Usually QEMU calls this a "notifier" rather than a "follower". For example, see include/qemu/notifier.h and bdrv_add_close_notifier().
Stefan
Re: [Qemu-devel] [RFC 4/5] block: Add AIO followers
Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · Max Reitz
If a long-running operation on a BDS wants to always remain in the same AIO context, it somehow needs to keep track of the BDS changing its context. This adds a function for registering callbacks on a BDS which are called whenever the BDS is attached or detached from an AIO context.
Signed-off-by Max Reitz <[email protected]>--- block.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ include/block/block_int.h | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+)
Quoted 5 lines
Great, we definitely need this mechanism. Block jobs can also use it later.
Usually QEMU calls this a "notifier" rather than a "follower". For example, see
include/qemu/notifier.handbdrv_add_close_notifier().
Ah, I see, thank you.
Max
Quoted 1 line
Stefan
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 7 lines · Max Reitz
Quoted 5 lines · Max Reitz
[snip]
However, if
bs_aio_detach()is called from a different thread than the old AioContext is running in, we may still have coroutines running for which we should wait before returning frombs_aio_detach().
Quoted 11 lines
After re-reading Stefan's RFC and the AIO locking code, would it suffice to call
aio_context_acquire()andaio_context_release()on the old AioContext at the end ofbs_aio_detach()to ensure all coroutines are settled? If not, I guess I have to wait until the coroutine variables are set to NULL (which indicates they have completely finished execution; however, this is not actually necessary and might lead to an infinite loop if the block driver keeps yielding due to some condition related to the AioContext switch), or I really have to switch the existing coroutines to the new AioContext while they are running. Doing this from the outside will probably be even messier than it would already be from the inside, so I sure hope to be able to avoid this…
I think you don't need to worry about this at all. bdrv_set_aio_context() is always invoked with the QEMU global mutex held.
Regarding switching a coroutine from one AioContext to another (possibly in another thread), I've implemented it but it's a little involved (complex and slow). Let's avoid it, if possible. I also lost the patch during the great "make distclean twice deletes .git/" fiasco of 2014 so would need to reimplement it :).
Stefan
Quoted 44 lines · Max Reitz
For the NBD server to work with dataplane, it needs to correctly access the exported BDS. It makes the most sense to run both in the same AioContext, therefore this series implements methods for tracking a BDS's AioContext and makes NBD make use of this for keeping the clients connected to that BDS in the same AioContext.
The reason this is an RFC and not a PATCH is my inexperience with AIO, coroutines and the like. Also, I'm not sure about what to do about the coroutines. The NBD server has up to two coroutines per client: One for receiving and one for sending. Theoretically, both have to be "transferred" to the new AioContext if it is changed; however, as far as I see it, coroutines are not really bound to an AioContext, they are simply run in the AioContext entering them. Therefore, I think a transfer is unnecessary. All coroutines are entered from
nbd_read()andnbd_restart_write(), both of which are AIO routines registered viaaio_set_fd_handler2().As
bs_aio_detach()unregisters all of these routines, the coroutines can no longer be entered, but only afterbs_aio_attach()is called again. Then, when they are called, they will enter the coroutines in the new AioContext. Therefore, I think an explicit transfer unnecessary.However, if
bs_aio_detach()is called from a different thread than the old AioContext is running in, we may still have coroutines running for which we should wait before returning frombs_aio_detach().But because of my inexperience with coroutines, I'm not sure. I now have these patches nearly unchanged here for about a week and I'm looking for ways of testing them, but so far I could only test whether the old use cases work, but not whether they will work for what they are intended to
do: With BDS changing their AioContext.So, because I'm not sure what else to do and because I don't know how to test multiple AIO threads (how do I move a BDS into another iothread?) I'm just sending this out as an RFC.
Max Reitz (5):
nbd: Correct name comparison forexport_set_name()
aio: Addio_read_poll()callback
nbd: Useaio_set_fd_handler2()
block: Add AIO followers
nbd: Follow the BDS' AIO context
Quoted 7 lines
aio-posix.c | 26 ++++++++++++++++----- block.c | 55 +++++++++++++++++++++++++++++++++++++++++++ include/block/aio.h | 12 ++++++++++ include/block/block_int.h | 40 ++++++++++++++++++++++++++++++++ include/qemu/main-loop.h | 1 - nbd.c | 59 ++++++++++++++++++++++++++++++++++++++--------- 6 files changed, 175 insertions(+), 18 deletions(-)
Thanks for the RFC. It looks like the concept will work correctly. I left comments, a lot of them are just ideas and you don't have to implement them if you don't want to.
Stefan