[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v20210616 06/36] tools: add readv_exact to libxenctrl


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx
  • From: Olaf Hering <olaf@xxxxxxxxx>
  • Date: Wed, 16 Jun 2021 14:50:59 +0200
  • Arc-authentication-results: i=1; strato.com; dkim=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1623847896; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=2reXKEzUM07y7ty9jLpkdpjD7wjbIcJf0B0ZkXQLZnc=; b=noUc6A2hE/RWnNf1+XuBctR6/Mytab/IRCwImeUw18mDKqqnrgPeu+7MzVAvTB03hN rEG688W0NlVcQRBaYy6Lx/ih5DbKkOm7LcTXbawrnGuboA2OvUYtJiSNv8rfA3773O3Q FG+lDUuyEyReBBaa4Wx0tQNf6HoKwYZRl5ftbwll1RvFF45WGuXOLWNDA7sDV0eOtB/1 Nb3Ghmo2E8Hnz9q8F66AKHYAHzqT+ZyQbr3Rkh+UI31AP5Iwez/4yF/tGli4H5B66v0d uR4yhi3ZQNL0J6rwVlPstvmlnkuQr7yD9d1V0DbaGaPdXlwd1gR9PhLIhC23bd+t/b0c 7qKw==
  • Arc-seal: i=1; a=rsa-sha256; t=1623847896; cv=none; d=strato.com; s=strato-dkim-0002; b=gg+/47bZpm4YB6I1hv7GH0kgAMdek5epu5Z7Cwt0HbMxD0mv2NJDtrvIS/eJUdH9Qd ZiybM/Yt17xfoQGSa6wYbBmqLbvB/pxNUvndEP6KTGsfIsS2ZG79Dk4RxrTmHibYjcg+ 8H/ZbGhHgEgAfQpexEViICbi8NheNQKlRTo6bnXMe5tL99sE109F2PIuLdKZ9tnBeqSi wMxb2h9te3iLP/zkRNGQmdCKG1H/zGt/P4NOCkz+VmpUTa83mfwcJ5HTYBZGmm7MaM2F qgOy+5DoQ4CjvBjEszbAb5COq8CDl7PDPyEsrSTM/78MGURuE2fdHQlt6M+Gshkh1Gn5 o0RQ==
  • Authentication-results: strato.com; dkim=none
  • Cc: Olaf Hering <olaf@xxxxxxxxx>, Ian Jackson <iwj@xxxxxxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, Juergen Gross <jgross@xxxxxxxx>
  • Delivery-date: Wed, 16 Jun 2021 12:51:44 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

Read a batch of iovec's.

Short reads are the common case, finish the trailing iov with read_exact.

Signed-off-by: Olaf Hering <olaf@xxxxxxxxx>

v2:
- add comment to short-read handling
---
 tools/libs/ctrl/xc_private.c | 57 +++++++++++++++++++++++++++++++++++-
 tools/libs/ctrl/xc_private.h |  1 +
 2 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/tools/libs/ctrl/xc_private.c b/tools/libs/ctrl/xc_private.c
index d94f846686..da58c3d9ba 100644
--- a/tools/libs/ctrl/xc_private.c
+++ b/tools/libs/ctrl/xc_private.c
@@ -659,8 +659,23 @@ int write_exact(int fd, const void *data, size_t size)
 
 #if defined(__MINIOS__)
 /*
- * MiniOS's libc doesn't know about writev(). Implement it as multiple 
write()s.
+ * MiniOS's libc doesn't know about readv/writev().
+ * Implement it as multiple read/write()s.
  */
+int readv_exact(int fd, const struct iovec *iov, int iovcnt)
+{
+    int rc, i;
+
+    for ( i = 0; i < iovcnt; ++i )
+    {
+        rc = read_exact(fd, iov[i].iov_base, iov[i].iov_len);
+        if ( rc )
+            return rc;
+    }
+
+    return 0;
+}
+
 int writev_exact(int fd, const struct iovec *iov, int iovcnt)
 {
     int rc, i;
@@ -675,6 +690,46 @@ int writev_exact(int fd, const struct iovec *iov, int 
iovcnt)
     return 0;
 }
 #else
+int readv_exact(int fd, const struct iovec *iov, int iovcnt)
+{
+    int rc = 0, idx = 0;
+    ssize_t len;
+
+    while ( idx < iovcnt )
+    {
+        len = readv(fd, &iov[idx], min(iovcnt - idx, IOV_MAX));
+        if ( len == -1 && errno == EINTR )
+            continue;
+        if ( len <= 0 )
+        {
+            rc = -1;
+            goto out;
+        }
+
+        /* Finish a potential short read in the last iov */
+        while ( len > 0 && idx < iovcnt )
+        {
+            if ( len >= iov[idx].iov_len )
+            {
+                len -= iov[idx].iov_len;
+            }
+            else
+            {
+                void *p = iov[idx].iov_base + len;
+                size_t l = iov[idx].iov_len - len;
+
+                rc = read_exact(fd, p, l);
+                if ( rc )
+                    goto out;
+                len = 0;
+            }
+            idx++;
+        }
+    }
+out:
+    return rc;
+}
+
 int writev_exact(int fd, const struct iovec *iov, int iovcnt)
 {
     struct iovec *local_iov = NULL;
diff --git a/tools/libs/ctrl/xc_private.h b/tools/libs/ctrl/xc_private.h
index 3e299b943f..66086ef19f 100644
--- a/tools/libs/ctrl/xc_private.h
+++ b/tools/libs/ctrl/xc_private.h
@@ -410,6 +410,7 @@ int xc_flush_mmu_updates(xc_interface *xch, struct xc_mmu 
*mmu);
 
 /* Return 0 on success; -1 on error setting errno. */
 int read_exact(int fd, void *data, size_t size); /* EOF => -1, errno=0 */
+int readv_exact(int fd, const struct iovec *iov, int iovcnt);
 int write_exact(int fd, const void *data, size_t size);
 int writev_exact(int fd, const struct iovec *iov, int iovcnt);
 



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.