Consolidate qemu_iovec_memset{, _skip}() into single, simplified function
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/5 Consolidate qemu_iovec_memset{, _skip}() into single, simplified function
This patch combines two functions into one, simplifies the implementation and adds some assert()s into place.
--- block/qcow2.c | 4 +- block/qed.c | 4 +- cutils.c | 54 +++++++++++++++------------------------------------ linux-aio.c | 2 +- posix-aio-compat.c | 2 +- qemu-common.h | 4 +-- 6 files changed, 23 insertions(+), 47 deletions(-)
block/qcow2.c +2 -2
@@ -406,7 +406,7 @@ int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,else n1 = bs->total_sectors - sector_num; -qemu_iovec_memset_skip (qiov,0 ,512 * (nb_sectors - n1),512 * n1); +qemu_iovec_memset (qiov,0 ,512 * (nb_sectors - n1),512 * n1);return n1; }
@@ -466,7 +466,7 @@ static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num, } }else {/* Note: in this case, no need to wait */ -qemu_iovec_memset (&hd_qiov,0 ,512 * cur_nr_sectors); +qemu_iovec_memset (&hd_qiov,0 ,512 * cur_nr_sectors,0 ); } }else if (cluster_offset &QCOW_OFLAG_COMPRESSED ) {/* add AIO support for compressed blocks ? */
block/qed.c +2 -2
@@ -738,7 +738,7 @@ static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,/* Zero all sectors if reading beyond the end of the backing file */ if (pos >= backing_length || pos + qiov->size > backing_length) { -qemu_iovec_memset (qiov,0 , qiov->size); +qemu_iovec_memset (qiov,0 , qiov->size,0 ); }/* Complete now if there are no backing file sectors to read */
@@ -1253,7 +1253,7 @@ static void qed_aio_read_data(void *opaque, int ret,/* Handle zero cluster and backing file reads */ if (ret ==QED_CLUSTER_ZERO ) { -qemu_iovec_memset (&acb->cur_qiov,0 , acb->cur_qiov.size); +qemu_iovec_memset (&acb->cur_qiov,0 , acb->cur_qiov.size,0 );qed_aio_next_io (acb,0 );return ; }else if (ret !=QED_CLUSTER_FOUND ) {
cutils.c +16 -38
62 lines not on this page · raw message
linux-aio.c +1 -1
@@ -64,7 +64,7 @@ static void qemu_laio_process_completion(struct qemu_laio_state *s, }else if (ret >=0 ) {/* Short reads mean EOF, pad with zeros. */ if (laiocb->is_read) { -qemu_iovec_memset_skip (laiocb->qiov,0 , +qemu_iovec_memset (laiocb->qiov,0 , laiocb->qiov->size - ret, ret); }else { ret = -EINVAL ;
posix-aio-compat.c +1 -1
@@ -355,7 +355,7 @@ static void *aio_thread(void *unused)qemu_iovec_init_external (&qiov, aiocb->aio_iov, aiocb->aio_niov); -qemu_iovec_memset_skip (&qiov,0 , aiocb->aio_nbytes - ret, ret); +qemu_iovec_memset (&qiov,0 , aiocb->aio_nbytes - ret, ret); ret = aiocb->aio_nbytes; }
qemu-common.h +1 -3
@@ -340,9 +340,7 @@ void qemu_iovec_destroy(QEMUIOVector *qiov);void qemu_iovec_reset (QEMUIOVector *qiov);void qemu_iovec_to_buffer (QEMUIOVector *qiov,void *buf);void qemu_iovec_from_buffer (QEMUIOVector *qiov,const void *buf,size_t count); -void qemu_iovec_memset (QEMUIOVector *qiov,int c,size_t count); -void qemu_iovec_memset_skip (QEMUIOVector *qiov,int c,size_t count, -size_t skip); +void qemu_iovec_memset (QEMUIOVector *qiov,int c,size_t bytes,size_t offset);bool buffer_is_zero (const void *buf,size_t len);
Signature · 1 line
1.7.9.1
PATCH 2/5 change prototypes of qemu_sendv() and qemu_recvv()
In reply to a message not in the archiveRename arguments and use size_t for sizes instead of int
-qemu_sendv(int sockfd, struct iovec *iov, int len, int iov_offset) +qemu_sendv(int sockfd, struct iovec *iov, size_t bytes, size_t offset)
The main motivation was to make it clear that length and offset are in _bytes_, not in iov elements: it was very confusing before, because all standard functions which deals with iovecs expects number of iovs, not bytes, even the fact that struct iovec has iov_len and iov_ prefix does not help. With "bytes" and "offset", especially since they're now size_t, it is much more explicit.
All callers of qemu_sendv() and qemu_recvv() and related, like qemu_co_sendv() and qemu_co_recvv(), were checked to verify that it is safe to use unsigned datatype instead of int.
While at it, clean up misleading comment near do_sendv_recvv().
--- cutils.c | 34 ++++++++++++---------------------- qemu-common.h | 4 ++-- 2 files changed, 14 insertions(+), 24 deletions(-)
cutils.c +12 -22
@@ -424,38 +424,28 @@ int qemu_parse_fd(const char *param)* * This function send/recv data from/to the iovec buffer directly. * The first `offset' bytes in the iovec buffer are skipped and next -* `len' bytes are used. -* -* For example, -* -* do_sendv_recvv(sockfd, iov, len, offset, 1); -* -* is equal to -* -* char *buf = malloc(size); -* iov_to_buf(iov, iovcnt, buf, offset, size); -* send(sockfd, buf, size, 0); -* free(buf); +* `bytes' bytes are used. */ -static int do_sendv_recvv (int sockfd,struct iovec *iov,int len,int offset, +static int do_sendv_recvv (int sockfd,struct iovec *iov,size_t bytes,size_t offset,int do_sendv) { -int ret, diff, iovlen; +int ret, iovlen; +size_t diff;struct iovec *last_iov;/* last_iov is inclusive, so count from one. */ iovlen =1 ; last_iov = iov; - len += offset; + bytes += offset; -while (last_iov->iov_len < len) { - len -= last_iov->iov_len; +while (last_iov->iov_len < bytes) { + bytes -= last_iov->iov_len; last_iov++; iovlen++; } - diff = last_iov->iov_len - len; + diff = last_iov->iov_len - bytes; last_iov->iov_len -= diff;while (iov->iov_len <= offset) {
@@ -517,13 +507,13 @@ static int do_sendv_recvv(int sockfd, struct iovec *iov, int len, int offset,return ret; } -int qemu_recvv (int sockfd,struct iovec *iov,int len,int iov_offset) +int qemu_recvv (int sockfd,struct iovec *iov,size_t bytes,size_t offset) { -return do_sendv_recvv (sockfd, iov, len, iov_offset,0 ); +return do_sendv_recvv (sockfd, iov, bytes, offset,0 ); } -int qemu_sendv (int sockfd,struct iovec *iov,int len,int iov_offset) +int qemu_sendv (int sockfd,struct iovec *iov,size_t bytes,size_t offset) { -return do_sendv_recvv (sockfd, iov, len, iov_offset,1 ); +return do_sendv_recvv (sockfd, iov, bytes, offset,1 ); }
qemu-common.h +2 -2
@@ -199,8 +199,8 @@ int qemu_pipe(int pipefd[2]);#define qemu_recv (sockfd, buf, len, flags) recv(sockfd, buf, len, flags)#endif -int qemu_recvv (int sockfd,struct iovec *iov,int len,int iov_offset); -int qemu_sendv (int sockfd,struct iovec *iov,int len,int iov_offset); +int qemu_recvv (int sockfd,struct iovec *iov,size_t bytes,size_t offset); +int qemu_sendv (int sockfd,struct iovec *iov,size_t bytes,size_t offset);/* Error handling. */
Signature · 1 line
1.7.9.1
PATCH 3/5 Export qemu_sendv_recvv() and use it in (inlined) qemu_sendv() and qemu_recvv()
In reply to a message not in the archiveRename do_sendv_recvv() to qemu_sendv_recvv(), change its last arg (do_send) from int to bool, export it in qemu-common.h, and made the two callers of it (qemu_sendv() and qemu_recvv()) to be trivial #defines just adding 5th arg.
qemu_sendv_recvv() will be used later.
--- cutils.c | 15 ++------------- qemu-common.h | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 15 deletions(-)
cutils.c +2 -13
@@ -426,8 +426,8 @@ int qemu_parse_fd(const char *param)* The first `offset' bytes in the iovec buffer are skipped and next * `bytes' bytes are used. */ -static int do_sendv_recvv (int sockfd,struct iovec *iov,size_t bytes,size_t offset, -int do_sendv) +int qemu_sendv_recvv (int sockfd,struct iovec *iov,size_t bytes,size_t offset, +bool do_sendv) {int ret, iovlen;size_t diff;
@@ -506,14 +506,3 @@ static int do_sendv_recvv(int sockfd, struct iovec *iov, size_t bytes, size_t of last_iov->iov_len += diff;return ret; } - -int qemu_recvv (int sockfd,struct iovec *iov,size_t bytes,size_t offset) -{ -return do_sendv_recvv (sockfd, iov, bytes, offset,0 ); -} - -int qemu_sendv (int sockfd,struct iovec *iov,size_t bytes,size_t offset) -{ -return do_sendv_recvv (sockfd, iov, bytes, offset,1 ); -} -
qemu-common.h +12 -2
@@ -199,8 +199,18 @@ int qemu_pipe(int pipefd[2]);#define qemu_recv (sockfd, buf, len, flags) recv(sockfd, buf, len, flags)#endif -int qemu_recvv (int sockfd,struct iovec *iov,size_t bytes,size_t offset); -int qemu_sendv (int sockfd,struct iovec *iov,size_t bytes,size_t offset); +/** +* Send or receive data from/to an (optionally partial) iovec. +* Instead of processing whole iovector, this routine can process +* not more than a specified number of bytes, and start not at +* the beginning of iovec but at byte position `offset'. +*/ +int qemu_sendv_recvv (int sockfd,struct iovec *iov, +size_t bytes,size_t offset,bool do_sendv); +#define qemu_recvv (sockfd, iov, bytes, offset) \ + qemu_sendv_recvv(sockfd, iov, bytes, offset, false) +#define qemu_sendv (sockfd, iov, bytes, offset) \ + qemu_sendv_recvv(sockfd, iov, bytes, offset, true)/* Error handling. */
Signature · 1 line
1.7.9.1
PATCH 4/5 cleanup qemu_co_sendv(), qemu_co_recvv() and friends
In reply to a message not in the archiveThe same as for non-coroutine versions in previous patches:
rename arguments to be more obvious, change type of arguments from int to size_t where appropriate, and use common code for send and receive paths (with one extra argument) since these are exactly the same. Use common qemu_sendv_recvv() directly. Also constify buf arg of qemu_co_send().
qemu_co_sendv(), qemu_co_recvv(), and qemu_co_recv() are now trivial #define's merely adding one extra arg. qemu_co_send() is an inline function due to `buf' arg de-constification.
--- qemu-common.h | 37 +++++++++++----------- qemu-coroutine-io.c | 83 +++++++++++++------------------------------------- 2 files changed, 40 insertions(+), 80 deletions(-)
qemu-common.h +18 -19
@@ -306,31 +306,30 @@ struct qemu_work_item {void qemu_init_vcpu (void *env);#endif -/** -* Sends an iovec (or optionally a part of it) down a socket, yielding -* when the socket is full. -*/ -int qemu_co_sendv (int sockfd,struct iovec *iov, -int len,int iov_offset); - -/** -* Receives data into an iovec (or optionally into a part of it) from -* a socket, yielding when there is no data in the socket. -*/ -int qemu_co_recvv (int sockfd,struct iovec *iov, -int len,int iov_offset); -/** -* Sends a buffer down a socket, yielding when the socket is full. +* Sends a (part of) iovec down a socket, yielding when the socket is full, or +* Receives data into a (part of) iovec from a socket, +* yielding when there is no data in the socket. +* XXX should mark these as coroutine_fn */ -int qemu_co_send (int sockfd,void *buf,int len); +int qemu_co_sendv_recvv (int sockfd,struct iovec *iov, +size_t bytes,size_t offset,bool do_send); +#define qemu_co_recvv (sockfd, iov, bytes, offset) \ + qemu_co_sendv_recvv(sockfd, iov, bytes, offset, false) +#define qemu_co_sendv (sockfd, iov, bytes, offset) \ + qemu_co_sendv_recvv(sockfd, iov, bytes, offset, true)/** -* Receives data into a buffer from a socket, yielding when there -* is no data in the socket. +* The same as above, but with just a single buffer */ -int qemu_co_recv (int sockfd,void *buf,int len); +int qemu_co_send_recv (int sockfd,void *buf,size_t bytes,bool do_send); +#define qemu_co_recv (sockfd, buf, bytes) \ + qemu_co_send_recv(sockfd, buf, bytes, false) +static inline int qemu_co_send (int sockfd,const void *buf,size_t bytes) +{ +return qemu_co_send_recv (sockfd, (void *)buf, bytes, true); +} typedef struct QEMUIOVector {
qemu-coroutine-io.c +22 -61
93 lines not on this page · raw message
Signature · 1 line
1.7.9.1
PATCH 5/5 rewrite and comment qemu_sendv_recvv()
In reply to a message not in the archiveMake it much more understandable, and add comments to it. This is in order to prepare similar function for writev_readv().
The new implementation has been extensively tested by splitting a large buffer into many small randomly-sized chunks, sending it over socket to another, slow process and verifying the receiving data is the same.
--- cutils.c | 119 +++++++++++++++++++++++++++++--------------------------- qemu-common.h | 2 +- 2 files changed, 63 insertions(+), 58 deletions(-)
cutils.c +62 -57
@@ -426,83 +426,88 @@ int qemu_parse_fd(const char *param)* The first `offset' bytes in the iovec buffer are skipped and next * `bytes' bytes are used. */ -int qemu_sendv_recvv (int sockfd,struct iovec *iov,size_t bytes,size_t offset, -bool do_sendv) +int qemu_sendv_recvv (int sockfd,struct iovec *iov, +size_t bytes,size_t offset,bool do_send) { -int ret, iovlen; -size_t diff; -struct iovec *last_iov; - -/* last_iov is inclusive, so count from one. */ - iovlen =1 ; - last_iov = iov; - bytes += offset; - -while (last_iov->iov_len < bytes) { - bytes -= last_iov->iov_len; - - last_iov++; - iovlen++; +int ret; +struct iovec *end; + +/* Find the start position, skipping `offset' bytes: +* first, skip all full-sized vector elements, */ +while (offset >= iov->iov_len) { + offset -= iov->iov_len; + ++iov; } - - diff = last_iov->iov_len - bytes; - last_iov->iov_len -= diff; - -while (iov->iov_len <= offset) { - offset -= iov->iov_len; - - iov++; - iovlen--; +if (offset) { +/* second, skip `offset' bytes from the (now) first element, +* undo it on exit */ + iov->iov_base += offset; + iov->iov_len -= offset; + } +/* Find the end position skipping `bytes' bytes: */ +/* first, skip all full-sized elements */ + end = iov; +while (end->iov_len <= bytes) { + bytes -= end->iov_len; + ++end; + } +if (bytes) { +/* second, fixup the last element, and remember +* the length we've cut from the end of it in `bytes' */ +size_t tail = end->iov_len - bytes; + end->iov_len = bytes; + ++end; + bytes = tail; } - - iov->iov_base = (char *) iov->iov_base + offset; - iov->iov_len -= offset; {#if definedCONFIG_IOVEC && definedCONFIG_POSIX struct msghdr msg;memset (&msg,0 ,sizeof (msg)); msg.msg_iov = iov; - msg.msg_iovlen = iovlen; - + msg.msg_iovlen = end - iov;;do { -if (do_sendv) { - ret =sendmsg (sockfd, &msg,0 ); - }else { - ret =recvmsg (sockfd, &msg,0 ); - } - }while (ret ==-1 && errno ==EINTR ); + ret = do_send + ?sendmsg (sockfd, &msg,0 ) + :recvmsg (sockfd, &msg,0 ); + }while (ret <0 && errno ==EINTR );#else -struct iovec *p = iov; +/* else send piece-by-piece */ +/*XXX Note: windows has WSASend() and WSARecv() */ +struct iovec *p = iov; ret =0 ; -while (iovlen >0 ) { -int rc; -if (do_sendv) { - rc =send (sockfd, p->iov_base, p->iov_len,0 ); - }else { - rc =qemu_recv (sockfd, p->iov_base, p->iov_len,0 ); - } -if (rc ==-1 ) { -if (errno ==EINTR ) { -continue ; - } +while (p < end) { +int r = do_send + ?send (sockfd, p->iov_base, p->iov_len,0 ) + :qemu_recv (sockfd, p->iov_base, p->iov_len,0 ); +if (r >0 ) { + ret += r; + ++i; + }else if (!r) { +break ; + }else if (errno ==EINTR ) { +continue ; + }else { +/* else it is some "other" error, +* only return if there was no data processed. */ if (ret ==0 ) { ret =-1 ; }break ; - } -if (rc ==0 ) { -break ; - } - ret += rc; - iovlen--, p++; + } }#endif }/* Undo the changes above */ - iov->iov_base = (char *) iov->iov_base - offset; - iov->iov_len += offset; - last_iov->iov_len += diff; +if (offset) { + iov->iov_base -= offset; + iov->iov_len += offset; + } +if (bytes) { + --end; + end->iov_len += bytes; + } +return ret; }
qemu-common.h +1 -1
@@ -206,7 +206,7 @@ int qemu_pipe(int pipefd[2]);* the beginning of iovec but at byte position `offset'. */ int qemu_sendv_recvv (int sockfd,struct iovec *iov, -size_t bytes,size_t offset,bool do_sendv); +size_t bytes,size_t offset,bool do_send);#define qemu_recvv (sockfd, iov, bytes, offset) \ qemu_sendv_recvv(sockfd, iov, bytes, offset, false)#define qemu_sendv (sockfd, iov, bytes, offset) \
Signature · 1 line
1.7.9.1