From patchwork Thu Mar 21 09:09:20 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orit Wasserman X-Patchwork-Id: 229598 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 87C052C00DE for ; Thu, 21 Mar 2013 20:28:30 +1100 (EST) Received: from localhost ([::1]:33559 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UIbWU-0000nn-Kt for incoming@patchwork.ozlabs.org; Thu, 21 Mar 2013 05:10:58 -0400 Received: from eggs.gnu.org ([208.118.235.92]:59635) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UIbUO-0006Xc-Fl for qemu-devel@nongnu.org; Thu, 21 Mar 2013 05:08:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UIbUN-0005Xg-1H for qemu-devel@nongnu.org; Thu, 21 Mar 2013 05:08:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:2514) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UIbUM-0005Xa-IN for qemu-devel@nongnu.org; Thu, 21 Mar 2013 05:08:46 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r2L98idl004160 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 21 Mar 2013 05:08:44 -0400 Received: from dhcp-1-120.tlv.redhat.com (vpn-200-38.tlv.redhat.com [10.35.200.38]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r2L98Z4D016557; Thu, 21 Mar 2013 05:08:41 -0400 From: Orit Wasserman To: qemu-devel@nongnu.org Date: Thu, 21 Mar 2013 11:09:20 +0200 Message-Id: <1363856971-4601-2-git-send-email-owasserm@redhat.com> In-Reply-To: <1363856971-4601-1-git-send-email-owasserm@redhat.com> References: <1363856971-4601-1-git-send-email-owasserm@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Orit Wasserman , pbonzini@redhat.com, mst@redhat.com, chegu_vinod@hp.com, quintela@redhat.com Subject: [Qemu-devel] [RFC 01/12] Add iov_writev to use writev to send iovec (also for files) X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org We use writev because sendmsg can only be used on socket fds. Signed-off-by: Orit Wasserman --- include/qemu/iov.h | 12 ++++++++++++ util/iov.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/include/qemu/iov.h b/include/qemu/iov.h index 68d25f2..e26b7ba 100644 --- a/include/qemu/iov.h +++ b/include/qemu/iov.h @@ -82,6 +82,18 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, #define iov_send(sockfd, iov, iov_cnt, offset, bytes) \ iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true) +/* + * writes iovcnt buffers of data described by iov to the file associated with + * the file descriptor fd. + * + * @fd - file descriptor + * @iov - iov to write + * @invcnt - number of buffers to write + * + * @return - number of bytes return or -1 on error + */ +ssize_t iov_writev(int fd, struct iovec *iov, unsigned iov_cnt); + /** * Produce a text hexdump of iovec `iov' with `iov_cnt' number of elements * in file `fp', prefixing each line with `prefix' and processing not more diff --git a/util/iov.c b/util/iov.c index 9dae318..bcd6e97 100644 --- a/util/iov.c +++ b/util/iov.c @@ -197,6 +197,42 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, return ret; } +ssize_t iov_writev(int fd, struct iovec *iov, unsigned iov_cnt) +{ +#if defined CONFIG_IOVEC && defined CONFIG_POSIX + ssize_t ret; + + do { + ret = writev(fd, iov, iov_cnt); + } while (ret < 0 && errno == EINTR); + + return ret; +#else + /* else send piece-by-piece */ + /*XXX Note: windows has WSASend() and WSARecv() */ + unsigned i = 0; + ssize_t ret = 0; + while (i < iov_cnt) { + ssize_t r = send(fd, iov[i].iov_base, iov[i].iov_len, 0); + if (r > 0) { + ret += r; + } 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; + } + i++; + } + return ret; +#endif +} void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt, FILE *fp, const char *prefix, size_t limit)