diff options
author | William Henderson <william.henderson@nutanix.com> | 2023-08-11 09:23:36 +0000 |
---|---|---|
committer | John Levon <john.levon@nutanix.com> | 2023-09-15 13:05:01 +0100 |
commit | ac959deb725bfd74f25720d8c8ae24bf5f6befbe (patch) | |
tree | 5b4f247eaa0692e7fe7f480d2b0933527927e4ee /samples/server.c | |
parent | f9d93f78c6a505a85f1245e654729f9e65b8a6ce (diff) | |
download | libvfio-user-ac959deb725bfd74f25720d8c8ae24bf5f6befbe.zip libvfio-user-ac959deb725bfd74f25720d8c8ae24bf5f6befbe.tar.gz libvfio-user-ac959deb725bfd74f25720d8c8ae24bf5f6befbe.tar.bz2 |
fix: minor changes to samples according to John's review
Signed-off-by: William Henderson <william.henderson@nutanix.com>
Diffstat (limited to 'samples/server.c')
-rw-r--r-- | samples/server.c | 68 |
1 files changed, 31 insertions, 37 deletions
diff --git a/samples/server.c b/samples/server.c index 3a02246..fdce13a 100644 --- a/samples/server.c +++ b/samples/server.c @@ -62,7 +62,7 @@ struct server_data { size_t bar1_size; struct dma_regions regions[NR_DMA_REGIONS]; struct { - uint64_t pending_bytes; + uint64_t bytes_transferred; vfu_migr_state_t state; } migration; }; @@ -132,10 +132,6 @@ bar1_access(vfu_ctx_t *vfu_ctx, char * const buf, } if (is_write) { - if (server_data->migration.state == VFU_MIGR_STATE_PRE_COPY) { - /* dirty the whole thing */ - server_data->migration.pending_bytes = server_data->bar1_size; - } memcpy(server_data->bar1 + offset, buf, count); } else { memcpy(buf, server_data->bar1, count); @@ -274,20 +270,24 @@ migration_device_state_transition(vfu_ctx_t *vfu_ctx, vfu_migr_state_t state) if (setitimer(ITIMER_REAL, &new, NULL) != 0) { err(EXIT_FAILURE, "failed to disable timer"); } - server_data->migration.pending_bytes = server_data->bar1_size + sizeof(time_t); /* FIXME BAR0 region size */ + server_data->migration.bytes_transferred = 0; break; case VFU_MIGR_STATE_PRE_COPY: - server_data->migration.pending_bytes = server_data->bar1_size; + server_data->migration.bytes_transferred = 0; break; case VFU_MIGR_STATE_STOP: /* FIXME should gracefully fail */ - assert(server_data->migration.pending_bytes == 0); + if (server_data->migration.state == VFU_MIGR_STATE_STOP_AND_COPY) { + assert(server_data->migration.bytes_transferred == + server_data->bar1_size + sizeof(time_t)); + } break; case VFU_MIGR_STATE_RESUME: - server_data->migration.pending_bytes = server_data->bar1_size + sizeof(time_t); + server_data->migration.bytes_transferred = 0; break; case VFU_MIGR_STATE_RUNNING: - assert(server_data->migration.pending_bytes == 0); + assert(server_data->migration.bytes_transferred == + server_data->bar1_size + sizeof(time_t)); ret = arm_timer(vfu_ctx, server_data->bar0); if (ret < 0) { return ret; @@ -312,18 +312,18 @@ migration_read_data(vfu_ctx_t *vfu_ctx, void *buf, uint64_t size) * more complex state tracking which exceeds the scope of this sample. */ - if (server_data->migration.pending_bytes == 0 || size == 0) { - vfu_log(vfu_ctx, LOG_DEBUG, "no data left to read"); - return 0; - } - uint32_t total_read = server_data->bar1_size; if (server_data->migration.state == VFU_MIGR_STATE_STOP_AND_COPY) { total_read += sizeof(server_data->bar0); } - uint32_t read_start = total_read - server_data->migration.pending_bytes; + if (server_data->migration.bytes_transferred == total_read || size == 0) { + vfu_log(vfu_ctx, LOG_DEBUG, "no data left to read"); + return 0; + } + + uint32_t read_start = server_data->migration.bytes_transferred; uint32_t read_end = MIN(read_start + size, total_read); // exclusive assert(read_end > read_start); @@ -357,7 +357,7 @@ migration_read_data(vfu_ctx_t *vfu_ctx, void *buf, uint64_t size) memcpy(buf, &server_data->bar0 + read_start, bytes_read); } - server_data->migration.pending_bytes -= bytes_read; + server_data->migration.bytes_transferred += bytes_read; return bytes_read; } @@ -371,13 +371,13 @@ migration_write_data(vfu_ctx_t *vfu_ctx, void *data, uint64_t size) assert(server_data != NULL); assert(data != NULL); - if (server_data->migration.pending_bytes == 0 || size == 0) { + uint32_t total_write = server_data->bar1_size + sizeof(server_data->bar0); + + if (server_data->migration.bytes_transferred == total_write || size == 0) { return 0; } - uint32_t total_write = server_data->bar1_size + sizeof(server_data->bar0); - - uint32_t write_start = total_write - server_data->migration.pending_bytes; + uint32_t write_start = server_data->migration.bytes_transferred; uint32_t write_end = MIN(write_start + size, total_write); // exclusive assert(write_end > write_start); @@ -385,15 +385,17 @@ migration_write_data(vfu_ctx_t *vfu_ctx, void *data, uint64_t size) if (write_end <= server_data->bar1_size) { // case 1: entire write lies within bar1 - // TODO check the following is always allowed memcpy(server_data->bar1 + write_start, buf, bytes_written); - } else if ( - write_start < server_data->bar1_size // starts in bar1 - && write_end > server_data->bar1_size // ends in bar0 - ) { - // case 2: part of the write in bar1 and part of the write in bar0 - // TODO check the following is always allowed + } else if (write_start >= server_data->bar1_size) { + // case 2: entire write lies within bar0 + + write_start -= server_data->bar1_size; + write_end -= server_data->bar1_size; + + memcpy(&server_data->bar0 + write_start, buf, bytes_written); + } else { + // case 3: part of the write in bar1 and part of the write in bar0 uint32_t length_in_bar1 = server_data->bar1_size - write_start; uint32_t length_in_bar0 = write_end - server_data->bar1_size; @@ -401,17 +403,9 @@ migration_write_data(vfu_ctx_t *vfu_ctx, void *data, uint64_t size) memcpy(server_data->bar1 + write_start, buf, length_in_bar1); memcpy(&server_data->bar0, buf + length_in_bar1, length_in_bar0); - } else if (write_start >= server_data->bar1_size) { - // case 3: entire write lies within bar0 - // TODO check the following is always allowed - - write_start -= server_data->bar1_size; - write_end -= server_data->bar1_size; - - memcpy(&server_data->bar0 + write_start, buf, bytes_written); } - server_data->migration.pending_bytes -= bytes_written; + server_data->migration.bytes_transferred += bytes_written; return bytes_written; } |