io: add new qio_channel_{readv, writev, read, write}_all functions

These functions wait until they are able to read / write the full
requested data buffer(s).

Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2017-08-30 14:53:59 +01:00
parent 50ea44f077
commit d4622e5588
3 changed files with 193 additions and 93 deletions

View file

@ -21,6 +21,7 @@
#include "qemu/osdep.h"
#include "io-channel-helpers.h"
#include "qapi/error.h"
#include "qemu/iov.h"
struct QIOChannelTest {
QIOChannel *src;
@ -37,77 +38,17 @@ struct QIOChannelTest {
};
static void test_skip_iovec(struct iovec **iov,
size_t *niov,
size_t skip,
struct iovec *old)
{
size_t offset = 0;
size_t i;
for (i = 0; i < *niov; i++) {
if (skip < (*iov)[i].iov_len) {
old->iov_len = (*iov)[i].iov_len;
old->iov_base = (*iov)[i].iov_base;
(*iov)[i].iov_len -= skip;
(*iov)[i].iov_base += skip;
break;
} else {
skip -= (*iov)[i].iov_len;
if (i == 0 && old->iov_base) {
(*iov)[i].iov_len = old->iov_len;
(*iov)[i].iov_base = old->iov_base;
old->iov_len = 0;
old->iov_base = NULL;
}
offset++;
}
}
*iov = *iov + offset;
*niov -= offset;
}
/* This thread sends all data using iovecs */
static gpointer test_io_thread_writer(gpointer opaque)
{
QIOChannelTest *data = opaque;
struct iovec *iov = data->inputv;
size_t niov = data->niov;
struct iovec old = { 0 };
qio_channel_set_blocking(data->src, data->blocking, NULL);
while (niov) {
ssize_t ret;
ret = qio_channel_writev(data->src,
iov,
niov,
&data->writeerr);
if (ret == QIO_CHANNEL_ERR_BLOCK) {
if (data->blocking) {
error_setg(&data->writeerr,
"Unexpected I/O blocking");
break;
} else {
qio_channel_wait(data->src,
G_IO_OUT);
continue;
}
} else if (ret < 0) {
break;
} else if (ret == 0) {
error_setg(&data->writeerr,
"Unexpected zero length write");
break;
}
test_skip_iovec(&iov, &niov, ret, &old);
}
qio_channel_writev_all(data->src,
data->inputv,
data->niov,
&data->writeerr);
return NULL;
}
@ -117,38 +58,13 @@ static gpointer test_io_thread_writer(gpointer opaque)
static gpointer test_io_thread_reader(gpointer opaque)
{
QIOChannelTest *data = opaque;
struct iovec *iov = data->outputv;
size_t niov = data->niov;
struct iovec old = { 0 };
qio_channel_set_blocking(data->dst, data->blocking, NULL);
while (niov) {
ssize_t ret;
ret = qio_channel_readv(data->dst,
iov,
niov,
&data->readerr);
if (ret == QIO_CHANNEL_ERR_BLOCK) {
if (data->blocking) {
error_setg(&data->readerr,
"Unexpected I/O blocking");
break;
} else {
qio_channel_wait(data->dst,
G_IO_IN);
continue;
}
} else if (ret < 0) {
break;
} else if (ret == 0) {
break;
}
test_skip_iovec(&iov, &niov, ret, &old);
}
qio_channel_readv_all(data->dst,
data->outputv,
data->niov,
&data->readerr);
return NULL;
}