Skip to contentSkip to conversation

tools/libxl: Improvements to libxl__datacopier

PATCH Superseded Andrew Cooper 3 participants 16 messages Mar 6, 2015 – Mar 16, 2015

lore
Share & tools
lore
#0 lore raw

v2 cover tools/libxl: Improvements to libxl__datacopier

To support migration v2, we need far more flexibility out of the datacopier.

This series adds the ability to read from an fd into a local buffer and to copy a specific number of bytes rather than to EOF. It also contains bugfixes related to writing from a local buffer, and POLLHUP handling.

Changes in v2:
* Rewrite most commit message (but no code change)
* Rework the POLLHUP fix from scratch

Andrew Cooper (2):
tools/libxl: Introduce min and max macros
tools/libxl: Fix datacopier POLLHUP handling to not always be fatal

Ross Lagerwall (3):
tools/libxl: Allow adding larger amounts of prefixdata to datacopier tools/libxl: Allow limiting amount copied by datacopier tools/libxl: Extend datacopier to support reading into a buffer

Wen Congyang (1):
tools/libxl: Update datacopier to support sending data only

 tools/libxl/libxl_aoutils.c    |  121 +++++++++++++++++++++++++---------------
 tools/libxl/libxl_bootloader.c |   24 +++++++-
 tools/libxl/libxl_dom.c        |    1 +
 tools/libxl/libxl_internal.h   |   35 ++++++++++--
 4 files changed, 129 insertions(+), 52 deletions(-)
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v2 1/6 tools/libxl: Introduce min and max macros

This is the same set used by libxc.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>

---
v2: Don't use reserved identifiers in min_t/max_t

---
 tools/libxl/libxl_internal.h |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/libxl/libxl_internal.h +16 -0
index 934465a..fcbec7f 100644
@@ -108,6 +108,22 @@
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 
+#define min(X, Y) ({                             \
+            const typeof (X) _x = (X);           \
+            const typeof (Y) _y = (Y);           \
+            (void) (&_x == &_y);                 \
+            (_x < _y) ? _x : _y; })
+#define max(X, Y) ({                             \
+            const typeof (X) _x = (X);           \
+            const typeof (Y) _y = (Y);           \
+            (void) (&_x == &_y);                 \
+            (_x > _y) ? _x : _y; })
+
+#define min_t(type, x, y)                                               \
+    ({ const type _x = (x); const type _y = (y); _x < _y ? _x: _y; })
+#define max_t(type, x, y)                                               \
+    ({ const type _x = (x); const type _y = (y); _x > _y ? _x: _y; })
+
 #define LIBXL__LOGGING_ENABLED
 
 #ifdef LIBXL__LOGGING_ENABLED
Signature · 1 line
1.7.10.4
Acked-by on v2 1/6
#2 lore raw

Re: [PATCH v2 1/6] tools/libxl: Introduce min and max macros

Quoted 18 lines · Andrew Cooper

This is the same set used by libxc.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>

---
v2: Don't use reserved identifiers in min_t/max_t

---
 tools/libxl/libxl_internal.h |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/libxl/libxl_internal.h +0 -0
index 934465a..fcbec7f 100644

tools/libxl/libxl_internal.h line 113

Quoted 6 lines
tools/libxl/libxl_internal.h +3 -0
@@ -108,6 +108,22 @@
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 
+#define min(X, Y) ({                             \
+            const typeof (X) _x = (X);           \
+            const typeof (Y) _y = (Y);           \

You've avoided the reserved-by-C names here, but IIRC a single underscore is reserved by POSIX.

However since you've just copied from libxc, I think we can live with this until someone find the energy to fix them all:

Ian.

#3 lore raw

PATCH v2 2/6 tools/libxl: Update datacopier to support sending data only

In reply to Andrew Cooper

From: Wen Congyang <[email protected]>

Currently, starting a datacopier requires a valid read and write fd, but this is a problem when purely sending data from a local buffer to a writable fd.

The prefixdata mechanism already exists and works for inserting data from a local buffer ahead of reading from the read fd.

Make the lack of a read fd non-fatal. A datacopier with no read fd, but some prefixdata will write the prefixdata to the write fd and complete successfully.

[Rewrite commit message]
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>

---
v2: Rewrite commit message. No code change

---
 tools/libxl/libxl_aoutils.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/libxl/libxl_aoutils.c +5 -3
index b10d2e1..3e0c0ae 100644
@@ -309,9 +309,11 @@ int libxl__datacopier_start(libxl__datacopier_state *dc)
 
     libxl__datacopier_init(dc);
 
-    rc = libxl__ev_fd_register(gc, &dc->toread, datacopier_readable,
-                               dc->readfd, POLLIN);
-    if (rc) goto out;
+    if (dc->readfd >= 0) {
+        rc = libxl__ev_fd_register(gc, &dc->toread, datacopier_readable,
+                                   dc->readfd, POLLIN);
+        if (rc) goto out;
+    }
 
     rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
                                dc->writefd, POLLOUT);
Signature · 1 line
1.7.10.4
Acked-by on v2 2/6
#4 lore raw

Re: [PATCH v2 2/6] tools/libxl: Update datacopier to support sending data only

Quoted 11 lines · Andrew Cooper

From: Wen Congyang <[email protected]>

Currently, starting a datacopier requires a valid read and write fd, but this is a problem when purely sending data from a local buffer to a writable fd.

The prefixdata mechanism already exists and works for inserting data from a local buffer ahead of reading from the read fd.

Make the lack of a read fd non-fatal. A datacopier with no read fd, but some prefixdata will write the prefixdata to the write fd and complete successfully.

Quoted 6 lines
[Rewrite commit message]
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
#5 lore raw

PATCH v2 3/6 tools/libxl: Avoid overrunning static buffer with prefixdata

In reply to Andrew Cooper

From: Ross Lagerwall <[email protected]>

An individual datacopier_buf contains a static buffer of 1000 bytes. Attempting to add prefixdata of more than 1000 bytes would overrun the buffer and cause heap corruption.

Instead, split the prefixdata and chain together multiple datacopier buffers. This allows for an arbitrary quantity of prefixdata to be added to a datacopier.

---
v2: Rewrite commit message. No code change

---
 tools/libxl/libxl_aoutils.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/libxl/libxl_aoutils.c +9 -5
index 3e0c0ae..6882ca3 100644
@@ -160,6 +160,8 @@ void libxl__datacopier_prefixdata(libxl__egc *egc, libxl__datacopier_state *dc,
 {
     EGC_GC;
     libxl__datacopier_buf *buf;
+    const uint8_t *ptr;
+
     /*
      * It is safe for this to be called immediately after _start, as
      * is documented in the public comment.  _start's caller must have
@@ -170,12 +172,14 @@ void libxl__datacopier_prefixdata(libxl__egc *egc, libxl__datacopier_state *dc,
 
     assert(len < dc->maxsz - dc->used);
 
-    buf = libxl__zalloc(NOGC, sizeof(*buf));
-    buf->used = len;
-    memcpy(buf->buf, data, len);
+    for (ptr = data; len; len -= buf->used, ptr += buf->used) {
+        buf = libxl__malloc(NOGC, sizeof(*buf));
+        buf->used = min(len, sizeof(buf->buf));
+        memcpy(buf->buf, ptr, buf->used);
 
-    dc->used += len;
-    LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+        dc->used += buf->used;
+        LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+    }
 }
 
 static int datacopier_pollhup_handled(libxl__egc *egc,
Signature · 1 line
1.7.10.4
Acked-by on v2 3/6
#6 lore raw

Re: [PATCH v2 3/6] tools/libxl: Avoid overrunning static buffer with prefixdata

Quoted 10 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

An individual datacopier_buf contains a static buffer of 1000 bytes. Attempting to add prefixdata of more than 1000 bytes would overrun the buffer and cause heap corruption.

Instead, split the prefixdata and chain together multiple datacopier buffers. This allows for an arbitrary quantity of prefixdata to be added to a datacopier.

Quoted 5 lines
#7 lore raw

PATCH v2 4/6 tools/libxl: Allow limiting amount copied by datacopier

In reply to Andrew Cooper

From: Ross Lagerwall <[email protected]>

Currently, a datacopier will unconditionally read until EOF on its read fd.

For migration v2, libxl needs to read records of a specific length out of the migration stream, without reading any further data.

Introduce a parameter, maxread, which may be used to stop the datacopier ahead of reaching EOF.

[Rewrite commit message]
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>

---
v2: Rewrite commit message. No code change

---
 tools/libxl/libxl_aoutils.c    |   11 +++++++----
 tools/libxl/libxl_bootloader.c |    2 ++
 tools/libxl/libxl_dom.c        |    1 +
 tools/libxl/libxl_internal.h   |    1 +
 4 files changed, 11 insertions(+), 4 deletions(-)
tools/libxl/libxl_aoutils.c +7 -4
index 6882ca3..037244e 100644
@@ -145,7 +145,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
                 return;
             }
         }
-    } else if (!libxl__ev_fd_isregistered(&dc->toread)) {
+    } else if (!libxl__ev_fd_isregistered(&dc->toread) || dc->maxread == 0) {
         /* we have had eof */
         datacopier_callback(egc, dc, 0, 0);
         return;
@@ -231,9 +231,8 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
             buf->used = 0;
             LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
         }
-        int r = read(ev->fd,
-                     buf->buf + buf->used,
-                     sizeof(buf->buf) - buf->used);
+        int r = read(ev->fd, buf->buf + buf->used,
+                     min_t(size_t, sizeof(buf->buf) - buf->used, dc->maxread));
         if (r < 0) {
             if (errno == EINTR) continue;
             if (errno == EWOULDBLOCK) break;
@@ -258,7 +257,11 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
         }
         buf->used += r;
         dc->used += r;
+        dc->maxread -= r;
         assert(buf->used <= sizeof(buf->buf));
+        assert(dc->maxread >= 0);
+        if (dc->maxread == 0)
+            break;
     }
     datacopier_check_state(egc, dc);
 }
tools/libxl/libxl_bootloader.c +2 -0
index 79947d4..1503101 100644
@@ -516,6 +516,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
 
     bl->keystrokes.ao = ao;
     bl->keystrokes.maxsz = BOOTLOADER_BUF_OUT;
+    bl->keystrokes.maxread = INT_MAX;
     bl->keystrokes.copywhat =
         GCSPRINTF("bootloader input for domain %"PRIu32, bl->domid);
     bl->keystrokes.callback =         bootloader_keystrokes_copyfail;
@@ -527,6 +528,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
 
     bl->display.ao = ao;
     bl->display.maxsz = BOOTLOADER_BUF_IN;
+    bl->display.maxread = INT_MAX;
     bl->display.copywhat =
         GCSPRINTF("bootloader output for domain %"PRIu32, bl->domid);
     bl->display.callback =         bootloader_display_copyfail;
tools/libxl/libxl_dom.c +1 -0
index a16d4a1..ffa36da 100644
@@ -1922,6 +1922,7 @@ void libxl__domain_save_device_model(libxl__egc *egc,
     dc->readfd = -1;
     dc->writefd = fd;
     dc->maxsz = INT_MAX;
+    dc->maxread = INT_MAX;
     dc->copywhat = GCSPRINTF("qemu save file for domain %"PRIu32, dss->domid);
     dc->writewhat = "save/migration stream";
     dc->callback = save_device_model_datacopier_done;
tools/libxl/libxl_internal.h +1 -0
index fcbec7f..22921c7 100644
@@ -2545,6 +2545,7 @@ struct libxl__datacopier_state {
     libxl__ao *ao;
     int readfd, writefd;
     ssize_t maxsz;
+    ssize_t maxread;
     const char *copywhat, *readwhat, *writewhat; /* for error msgs */
     FILE *log; /* gets a copy of everything */
     libxl__datacopier_callback *callback;
Signature · 1 line
1.7.10.4
#8 lore raw

Re: [PATCH v2 4/6] tools/libxl: Allow limiting amount copied by datacopier

Quoted 9 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Currently, a datacopier will unconditionally read until EOF on its read fd.

For migration v2, libxl needs to read records of a specific length out of the migration stream, without reading any further data.

Introduce a parameter, maxread, which may be used to stop the datacopier ahead of reaching EOF.

maxread is ok as a parameter but not really in the dc.

It's unfortunate that "toread" is already used. Perhaps "remaining" (or remread if you want to get terse)?

tools/libxl/libxl_bootloader.c static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op) · line 519

Quoted 9 lines
tools/libxl/libxl_bootloader.c +1 -0
index 79947d4..1503101 100644
@@ -516,6 +516,7 @@ static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
 
     bl->keystrokes.ao = ao;
     bl->keystrokes.maxsz = BOOTLOADER_BUF_OUT;
+    bl->keystrokes.maxread = INT_MAX;

SSIZE_MAX exists for this I think.

But, doesn't this mean that if the bootloader receives INT_MAX (or SIZE_MAX) keystrokes it will mysteriously stop working? Or do we already have that via maxsz?

#9 lore raw

Re: [PATCH v2 4/6] tools/libxl: Allow limiting amount copied by datacopier

Quoted 8 lines · Ian Campbell
Quoted 7 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Currently, a datacopier will unconditionally read until EOF on its read fd.

For migration v2, libxl needs to read records of a specific length out of the migration stream, without reading any further data.

Quoted 6 lines
Quoted 2 lines

Introduce a parameter, maxread, which may be used to stop the datacopier ahead of reaching EOF.

maxread is ok as a parameter but not really in the dc.

It's unfortunate that "toread" is already used. Perhaps "remaining" (or remread if you want to get terse)?

ssize_t bytes_to_read perhaps? with -1 being a sentinel for "until EOF" ?

~Andrew

#10 lore raw

Re: [PATCH v2 4/6] tools/libxl: Allow limiting amount copied by datacopier

Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Ian Campbell
Quoted 7 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Currently, a datacopier will unconditionally read until EOF on its read fd.

For migration v2, libxl needs to read records of a specific length out of the migration stream, without reading any further data.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Introduce a parameter, maxread, which may be used to stop the datacopier ahead of reaching EOF.

maxread is ok as a parameter but not really in the dc.

It's unfortunate that "toread" is already used. Perhaps "remaining" (or remread if you want to get terse)?

ssize_t bytes_to_read perhaps? with -1 being a sentinel for "until EOF" ?

WFM.

#11 lore raw

PATCH v2 5/6 tools/libxl: Extend datacopier to support reading into a buffer

In reply to Andrew Cooper

From: Ross Lagerwall <[email protected]>

Currently a datacopier may source its data from an fd or local buffer, but its destination must be an fd. For migration v2, libxl needs to read from the migration stream into a local buffer.

Implement a "read into local buffer" mode, invoked when readbuf is set and writefd is -1. On success, the callback passes the number of bytes read.

[Rewrite commit message]
CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>

---
v2: Rewrite commit message. No code change

---
 tools/libxl/libxl_aoutils.c  |   58 +++++++++++++++++++++++++-----------------
 tools/libxl/libxl_internal.h |    4 ++-
 2 files changed, 38 insertions(+), 24 deletions(-)
tools/libxl/libxl_aoutils.c +35 -23
index 037244e..a402e5c 100644
@@ -134,7 +134,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
     STATE_AO_GC(dc->ao);
     int rc;
     
-    if (dc->used) {
+    if (dc->used && !dc->readbuf) {
         if (!libxl__ev_fd_isregistered(&dc->towrite)) {
             rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
                                        dc->writefd, POLLOUT);
@@ -147,7 +147,7 @@ static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state *dc)
         }
     } else if (!libxl__ev_fd_isregistered(&dc->toread) || dc->maxread == 0) {
         /* we have had eof */
-        datacopier_callback(egc, dc, 0, 0);
+        datacopier_callback(egc, dc, 0, dc->readbuf ? dc->used : 0);
         return;
     } else {
         /* nothing buffered, but still reading */
@@ -215,24 +215,30 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
     }
     assert(revents & POLLIN);
     for (;;) {
-        while (dc->used >= dc->maxsz) {
-            libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
-            dc->used -= rm->used;
-            assert(dc->used >= 0);
-            LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
-            free(rm);
-        }
+        libxl__datacopier_buf *buf = NULL;
+        int r;
+
+        if (dc->readbuf) {
+            r = read(ev->fd, dc->readbuf + dc->used, dc->maxread);
+        } else {
+            while (dc->used >= dc->maxsz) {
+                libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs);
+                dc->used -= rm->used;
+                assert(dc->used >= 0);
+                LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry);
+                free(rm);
+            }
 
-        libxl__datacopier_buf *buf =
-            LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
-        if (!buf || buf->used >= sizeof(buf->buf)) {
-            buf = malloc(sizeof(*buf));
-            if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
-            buf->used = 0;
-            LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
-        }
-        int r = read(ev->fd, buf->buf + buf->used,
+            buf = LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs);
+            if (!buf || buf->used >= sizeof(buf->buf)) {
+                buf = malloc(sizeof(*buf));
+                if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf));
+                buf->used = 0;
+                LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry);
+            }
+            r = read(ev->fd, buf->buf + buf->used,
                      min_t(size_t, sizeof(buf->buf) - buf->used, dc->maxread));
+        }
         if (r < 0) {
             if (errno == EINTR) continue;
             if (errno == EWOULDBLOCK) break;
@@ -255,10 +261,12 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
                 return;
             }
         }
-        buf->used += r;
+        if (!dc->readbuf) {
+            buf->used += r;
+            assert(buf->used <= sizeof(buf->buf));
+        }
         dc->used += r;
         dc->maxread -= r;
-        assert(buf->used <= sizeof(buf->buf));
         assert(dc->maxread >= 0);
         if (dc->maxread == 0)
             break;
@@ -316,15 +324,19 @@ int libxl__datacopier_start(libxl__datacopier_state *dc)
 
     libxl__datacopier_init(dc);
 
+    assert(dc->readfd >= 0 || dc->writefd >= 0);
+
     if (dc->readfd >= 0) {
         rc = libxl__ev_fd_register(gc, &dc->toread, datacopier_readable,
                                    dc->readfd, POLLIN);
         if (rc) goto out;
     }
 
-    rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
-                               dc->writefd, POLLOUT);
-    if (rc) goto out;
+    if (dc->writefd >= 0) {
+        rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable,
+                                   dc->writefd, POLLOUT);
+        if (rc) goto out;
+    }
 
     return 0;
 
tools/libxl/libxl_internal.h +3 -1
index 22921c7..3e1d78a 100644
@@ -2524,7 +2524,8 @@ _hidden void libxl__device_disk_local_initiate_detach(libxl__egc *egc,
 
 /* onwrite==1 means failure happened when writing, logged, errnoval is valid
  * onwrite==0 means failure happened when reading
- *     errnoval==0 means we got eof and all data was written
+ *     errnoval>=0 means we got eof and all data was written or number of bytes
+ *                 written when in read mode
  *     errnoval!=0 means we had a read error, logged
  * onwrite==-1 means some other internal failure, errnoval not valid, logged
  * If we get POLLHUP, we call callback_pollhup(..., onwrite, -1);
@@ -2553,6 +2554,7 @@ struct libxl__datacopier_state {
     /* remaining fields are private to datacopier */
     libxl__ev_fd toread, towrite;
     ssize_t used;
+    void *readbuf;
     LIBXL_TAILQ_HEAD(libxl__datacopier_bufs, libxl__datacopier_buf) bufs;
 };
 
Signature · 1 line
1.7.10.4
#12 lore raw

Re: [PATCH v2 5/6] tools/libxl: Extend datacopier to support reading into a buffer

tools/libxl/libxl_aoutils.c static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev, · line 222

Quoted 11 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Currently a datacopier may source its data from an fd or local buffer, but its destination must be an fd. For migration v2, libxl needs to read from the migration stream into a local buffer.

Implement a "read into local buffer" mode, invoked when readbuf is set and writefd is -1. On success, the callback passes the number of bytes read.

tools/libxl/libxl_aoutils.c +2 -0
+        if (dc->readbuf) {
+            r = read(ev->fd, dc->readbuf + dc->used, dc->maxread);

Should the final parameter not be maxread - used?

I assume the caller is expected to make maxread and the readbuf size consistent (which could BTW be mentioned in an update to the comments relating to this interface).

tools/libxl/libxl_internal.h _hidden void libxl__device_disk_local_initiate_detach(libxl__egc *egc, · line 2529

Quoted 12 lines
tools/libxl/libxl_internal.h +2 -1
index 22921c7..3e1d78a 100644
@@ -2524,7 +2524,8 @@ _hidden void libxl__device_disk_local_initiate_detach(libxl__egc *egc,
 
 /* onwrite==1 means failure happened when writing, logged, errnoval is valid
  * onwrite==0 means failure happened when reading
- *     errnoval==0 means we got eof and all data was written
+ *     errnoval>=0 means we got eof and all data was written or number of bytes
+ *                 written when in read mode
  *     errnoval!=0 means we had a read error, logged

Nit: the last should be errnoval < 0.

I'm not sure how I feel about using a field called errnoval to store something other than an errno though. I suppose I can live with it.

Quoted 10 lines
tools/libxl/libxl_internal.h +1 -0
  * onwrite==-1 means some other internal failure, errnoval not valid, logged
  * If we get POLLHUP, we call callback_pollhup(..., onwrite, -1);
@@ -2553,6 +2554,7 @@ struct libxl__datacopier_state {
     /* remaining fields are private to datacopier */
     libxl__ev_fd toread, towrite;
     ssize_t used;
+    void *readbuf;
     LIBXL_TAILQ_HEAD(libxl__datacopier_bufs, libxl__datacopier_buf) bufs;
 };
 
#13 lore raw

Re: [PATCH v2 5/6] tools/libxl: Extend datacopier to support reading into a buffer

Quoted 7 lines · Ian Campbell
Quoted 6 lines · Andrew Cooper

From: Ross Lagerwall <[email protected]>

Currently a datacopier may source its data from an fd or local buffer, but its destination must be an fd. For migration v2, libxl needs to read from the migration stream into a local buffer.

Quoted 7 lines

tools/libxl/libxl_aoutils.c static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev, · line 222

Quoted 5 lines

Implement a "read into local buffer" mode, invoked when readbuf is set and writefd is -1. On success, the callback passes the number of bytes read.

tools/libxl/libxl_aoutils.c +2 -0
+        if (dc->readbuf) {
+            r = read(ev->fd, dc->readbuf + dc->used, dc->maxread);

Should the final parameter not be maxread - used?

No, because maxread is decreased after every read. This makes more sense if it is renamed to bytes_to_read (as it is in V3).

Quoted 4 lines

I assume the caller is expected to make maxread and the readbuf size consistent (which could BTW be mentioned in an update to the comments relating to this interface).

Yes, done in V3.

Quoted 31 lines

tools/libxl/libxl_internal.h _hidden void libxl__device_disk_local_initiate_detach(libxl__egc *egc, · line 2529

Quoted 13 lines
tools/libxl/libxl_internal.h +2 -1
index 22921c7..3e1d78a 100644
@@ -2524,7 +2524,8 @@ _hidden void libxl__device_disk_local_initiate_detach(libxl__egc *egc,
 
  /* onwrite==1 means failure happened when writing, logged, errnoval is valid
   * onwrite==0 means failure happened when reading
- *     errnoval==0 means we got eof and all data was written
+ *     errnoval>=0 means we got eof and all data was written or number of bytes
+ *                 written when in read mode
   *     errnoval!=0 means we had a read error, logged

Nit: the last should be errnoval < 0.

I'm not sure how I feel about using a field called errnoval to store something other than an errno though. I suppose I can live with it.

Quoted 10 lines
tools/libxl/libxl_internal.h +1 -0
   * onwrite==-1 means some other internal failure, errnoval not valid, logged
   * If we get POLLHUP, we call callback_pollhup(..., onwrite, -1);
@@ -2553,6 +2554,7 @@ struct libxl__datacopier_state {
      /* remaining fields are private to datacopier */
      libxl__ev_fd toread, towrite;
      ssize_t used;
+    void *readbuf;
      LIBXL_TAILQ_HEAD(libxl__datacopier_bufs, libxl__datacopier_buf) bufs;
  };
 
Signature · 1 line
Ross Lagerwall
#14 lore raw

PATCH v2 6/6 tools/libxl: Adjust datacopiers POLLHUP handling when the fd is also readable

In reply to Andrew Cooper

POLLHUP|POLLIN is a valid revent to receive when there is readable data in a pipe, but the writable fd has been closed. This occurs in migration v2 when the legacy conversion process (which transforms the data inline) completes and exits successfully.

In the case that there is data to read, suppress the POLLHUP. POSIX states that the hangup state is latched[1], which means it will reoccur on subsequent poll() calls. The datacopier is thus provided the opportunity to read until EOF, if possible.

A POLLHUP on its own is treated exactly as before, indicating a different error with the fd.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>

---
v2: Rework solution from scratch

---
 tools/libxl/libxl_aoutils.c |    3 +++
 1 file changed, 3 insertions(+)
tools/libxl/libxl_aoutils.c +3 -0
index a402e5c..a5ad3e5 100644
@@ -204,6 +204,9 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
     libxl__datacopier_state *dc = CONTAINER_OF(ev, *dc, toread);
     STATE_AO_GC(dc->ao);
 
+    if ((revents & (POLLHUP|POLLIN)) == (POLLHUP|POLLIN))
+        revents &= ~POLLHUP;
+
     if (datacopier_pollhup_handled(egc, dc, revents, 0))
         return;
 
Signature · 1 line
1.7.10.4
#15 lore raw

Re: [PATCH v2 6/6] tools/libxl: Adjust datacopiers POLLHUP handling when the fd is also readable

Quoted 13 lines · Andrew Cooper

POLLHUP|POLLIN is a valid revent to receive when there is readable data in a pipe, but the writable fd has been closed. This occurs in migration v2 when the legacy conversion process (which transforms the data inline) completes and exits successfully.

In the case that there is data to read, suppress the POLLHUP. POSIX states that the hangup state is latched[1], which means it will reoccur on subsequent poll() calls. The datacopier is thus provided the opportunity to read until EOF, if possible.

A POLLHUP on its own is treated exactly as before, indicating a different error with the fd.

Quoted 6 lines

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>

Having looked at the previous thread I'm satisfied that this matches what was discussed there, but I think the final ack needs to come from Ian.

Quoted 21 lines

---
v2: Rework solution from scratch

---
 tools/libxl/libxl_aoutils.c |    3 +++
 1 file changed, 3 insertions(+)
tools/libxl/libxl_aoutils.c +3 -0
index a402e5c..a5ad3e5 100644
@@ -204,6 +204,9 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
     libxl__datacopier_state *dc = CONTAINER_OF(ev, *dc, toread);
     STATE_AO_GC(dc->ao);
 
+    if ((revents & (POLLHUP|POLLIN)) == (POLLHUP|POLLIN))
+        revents &= ~POLLHUP;
+
     if (datacopier_pollhup_handled(egc, dc, revents, 0))
         return;