KVM Block Device Driver
Hi All,
I'm working with some disk introspection on KVM, and we trying to create
a shadow image of the disk. We've hooked the functions in block.c, in particular bdrv_aio_writev. However we are seeing writes go through, pausing the VM, and the comparing our shadow image with the actual VM image, and they aren't 100% synced up. The first 1-2 sectors appear to be always be correct, however, after that, there are sometimes some discrepancies. I believe we have exhausted most obvious bugs (malloc bugs, incorrect size calculations etc.). Has anyone had any experience with this or have any insights?
Our methodology is as follows:
1. Boot the VM.
2. Pause VM.
3. Copy the disk to our shadow image.
4. Perform very few reads/writes.
5. Pause VM.
6. Compare shadow copy with active vm disk.
And this is where we are seeing discrepancies. Any help is much
appreciated! We are running on Ubuntu 12.04 with a modified Debian build.
- Chad
Signature · 5 lines
Chad S. Spensky MIT Lincoln Laboratory Group 59 (Cyber Systems Assessment) Ph: (781) 981-4173
Quoted 12 lines · On Tue, 08/13 16:13, Spensky, Chad - 0559 - MITLL wrote:
Hi All,
I'm working with some disk introspection on KVM, and we trying to create
a shadow image of the disk. We've hooked the functions inblock.c, in particularbdrv_aio_writev. However we are seeing writes go through, pausing the VM, and the comparing our shadow image with the actual VM image, and they aren't 100% synced up. The first 1-2 sectors appear to be always be correct, however, after that, there are sometimes some discrepancies. I believe we have exhausted most obvious bugs (malloc bugs, incorrect size calculations etc.). Has anyone had any experience with this or have any insights?
Quoted 4 lines
Our methodology is as follows:
1. Boot the VM.
2. Pause VM.
3. Copy the disk to our shadow image.
How do you copy the disk, from guest or host?
Quoted 1 line
4. Perform very few reads/writes.
Did you flush to disk?
Quoted 11 lines
5. Pause VM.
6. Compare shadow copy with active vm disk.And this is where we are seeing discrepancies. Any help is much
appreciated! We are running on Ubuntu 12.04 with a modified Debian build.- Chad
Signature · 1 line
Chad S. Spensky
I think drive-backup command does just what you want, it creates a image and copy-on-write date from guest disk to the target, without pausing VM.
Quoted 32 lines · Fam Zheng
Quoted 12 lines · On Tue, 08/13 16:13, Spensky, Chad - 0559 - MITLL wrote:
Hi All,
I'm working with some disk introspection on KVM, and we trying to create
a shadow image of the disk. We've hooked the functions inblock.c, in particularbdrv_aio_writev. However we are seeing writes go through, pausing the VM, and the comparing our shadow image with the actual VM image, and they aren't 100% synced up. The first 1-2 sectors appear to be always be correct, however, after that, there are sometimes some discrepancies. I believe we have exhausted most obvious bugs (malloc bugs, incorrect size calculations etc.). Has anyone had any experience with this or have any insights?Quoted 4 lines
Our methodology is as follows:
1. Boot the VM.
2. Pause VM.
3. Copy the disk to our shadow image.How do you copy the disk, from guest or host?
Quoted 1 line
4. Perform very few reads/writes.
Did you flush to disk?
Quoted 8 lines
5. Pause VM.
6. Compare shadow copy with active vm disk.And this is where we are seeing discrepancies. Any help is much
appreciated! We are running on Ubuntu 12.04 with a modified Debian build.- Chad
Quoted 7 lines
Quoted 3 lines
Signature · 1 line
Chad S. SpenskyI think drive-backup command does just what you want, it creates a image and copy-on-write date from guest disk to the target, without pausing VM.
Or perhaps drive-mirror.
Maybe Chad can explain what the use case is. There is probably an existing command that does this or that could be extended to do this safely.
Stefan
Stefan, Fam,
We are trying to keep an active shadow copy while the system is running
without any need for pausing. More precisely we want to log every individual access to the drive into a database so that the entire stream of accesses could be replayed at a later time.
- Chad
Signature · 5 lines
Chad S. Spensky MIT Lincoln Laboratory Group 59 (Cyber Systems Assessment) Ph: (781) 981-4173
Quoted 52 lines · Stefan Hajnoczi
Quoted 36 lines · Fam Zheng
Quoted 3 lines · On Tue, 08/13 16:13, Spensky, Chad - 0559 - MITLL wrote:
Hi All,
I'm working with some disk introspection on KVM, and we trying to
create
Quoted 4 lines
a shadow image of the disk. We've hooked the functions in
block.c, in particularbdrv_aio_writev. However we are seeing writes go through, pausing the VM, and the comparing our shadow image with the actual VM image, and they aren't 100% synced up. The first 1-2 sectors appearto be
Quoted 3 lines
always be correct, however, after that, there are sometimes some discrepancies. I believe we have exhausted most obvious bugs (malloc bugs, incorrect size calculations etc.). Has anyone had any
experience
Quoted 6 lines
with this or have any insights?
Our methodology is as follows:
1. Boot the VM.
2. Pause VM.
3. Copy the disk to our shadow image.How do you copy the disk, from guest or host?
Quoted 1 line
4. Perform very few reads/writes.
Did you flush to disk?
Quoted 5 lines
5. Pause VM.
6. Compare shadow copy with active vm disk.And this is where we are seeing discrepancies. Any help is much
appreciated! We are running on Ubuntu 12.04 with a modified Debianbuild.
Quoted 3 lines
- Chad
Quoted 7 lines
Quoted 3 lines
Signature · 1 line
Chad S. SpenskyI think drive-backup command does just what you want, it creates a image and copy-on-write date from guest disk to the target, without pausing VM.
Or perhaps drive-mirror.
Maybe Chad can explain what the use case is. There is probably an existing command that does this or that could be extended to do this safely.
Stefan
Quoted 7 lines · On Wed, 08/14 07:29, Spensky, Chad - 0559 - MITLL wrote:
Stefan, Fam,
We are trying to keep an active shadow copy while the system is running
without any need for pausing. More precisely we want to log every individual access to the drive into a database so that the entire stream of accesses could be replayed at a later time.
There's no IO request log infrastructure in QEMU for now. What drive-mirror can do is to repetitively send changed sectors since last sending point, but it's not in guest request order or operation size, it works just with a dirty bitmap.
In your methodology you didn't mention the hook you worked in block.c, but I think it is necessary to hack block.c to log every r/w access to the drive, I think you synchronous each write to the shadow image, right?
Quoted 56 lines · Stefan Hajnoczi
Quoted 53 lines · Fam Zheng
Quoted 36 lines · On Tue, 08/13 16:13, Spensky, Chad - 0559 - MITLL wrote:
Quoted 3 lines
Hi All,
I'm working with some disk introspection on KVM, and we trying to
create
Quoted 4 lines
a shadow image of the disk. We've hooked the functions in
block.c, in particularbdrv_aio_writev. However we are seeing writes go through, pausing the VM, and the comparing our shadow image with the actual VM image, and they aren't 100% synced up. The first 1-2 sectors appearto be
Quoted 3 lines
always be correct, however, after that, there are sometimes some discrepancies. I believe we have exhausted most obvious bugs (malloc bugs, incorrect size calculations etc.). Has anyone had any
experience
Quoted 6 lines
with this or have any insights?
Our methodology is as follows:
1. Boot the VM.
2. Pause VM.
3. Copy the disk to our shadow image.How do you copy the disk, from guest or host?
Quoted 1 line
4. Perform very few reads/writes.
Did you flush to disk?
Quoted 5 lines
5. Pause VM.
6. Compare shadow copy with active vm disk.And this is where we are seeing discrepancies. Any help is much
appreciated! We are running on Ubuntu 12.04 with a modified Debianbuild.
Quoted 3 lines
- Chad
Quoted 7 lines
Quoted 3 lines
Signature · 1 line
Chad S. SpenskyI think drive-backup command does just what you want, it creates a image and copy-on-write date from guest disk to the target, without pausing VM.
Or perhaps drive-mirror.
Maybe Chad can explain what the use case is. There is probably an existing command that does this or that could be extended to do this safely.
Stefan
Fam,
That's correct, we modified block.c to hook the appropriate functions
and output the information through a unix socket. One of the functions that we hooked is bdrv_aio_writev, however it seems like the data that we are seeing at that point in the callstack is not what actually makes it to the disk image for the guest. The first couple of sectors always seem to be the same, however after sector 2 it's a toss up. I'm guessing there may be some sort of caching going on or something, and we appear to be missing it.
- Chad
Signature · 5 lines
Chad S. Spensky MIT Lincoln Laboratory Group 59 (Cyber Systems Assessment) Ph: (781) 981-4173
Quoted 82 lines · Fam Zheng
Quoted 8 lines · On Wed, 08/14 07:29, Spensky, Chad - 0559 - MITLL wrote:
Stefan, Fam,
We are trying to keep an active shadow copy while the system is
running
without any need for pausing. More precisely we want to log every individual access to the drive into a database so that the entire stream of accesses could be replayed at a later time.There's no IO request log infrastructure in QEMU for now. What drive-mirror can do is to repetitively send changed sectors since last sending point, but it's not in guest request order or operation size, it works just with a dirty bitmap.
In your methodology you didn't mention the hook you worked in
block.c, but I think it is necessary to hackblock.cto log every r/w access to the drive, I think you synchronous each write to the shadow image, right?Quoted 62 lines · Stefan Hajnoczi
Quoted 8 lines · Fam Zheng
Quoted 6 lines · On Tue, 08/13 16:13, Spensky, Chad - 0559 - MITLL wrote:
Quoted 3 lines
Hi All,
I'm working with some disk introspection on KVM, and we trying to
create
Quoted 1 line
a shadow image of the disk. We've hooked the functions in
block.c, inQuoted 1 line
Quoted 1 line
Quoted 1 line
particular
bdrv_aio_writev. However we are seeing writes gothrough,
Quoted 1 line
Quoted 1 line
Quoted 1 line
pausing the VM, and the comparing our shadow image with the actual
VM
Quoted 1 line
Quoted 1 line
Quoted 1 line
image, and they aren't 100% synced up. The first 1-2 sectors
appear
Quoted 3 lines
Quoted 3 lines
to be
Quoted 2 lines
always be correct, however, after that, there are sometimes some discrepancies. I believe we have exhausted most obvious bugs
(malloc
Quoted 22 lines
Quoted 22 lines
Quoted 1 line
bugs, incorrect size calculations etc.). Has anyone had any
experience
Quoted 6 lines
with this or have any insights?
Our methodology is as follows:
1. Boot the VM.
2. Pause VM.
3. Copy the disk to our shadow image.How do you copy the disk, from guest or host?
Quoted 1 line
4. Perform very few reads/writes.
Did you flush to disk?
Quoted 5 lines
5. Pause VM.
6. Compare shadow copy with active vm disk.And this is where we are seeing discrepancies. Any help is much
appreciated! We are running on Ubuntu 12.04 with a modified Debianbuild.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
- Chad
Signature · 1 line
Chad S. SpenskyI think drive-backup command does just what you want, it creates a
image
Quoted 10 lines
Quoted 2 lines
and copy-on-write date from guest disk to the target, without pausing VM.
Or perhaps drive-mirror.
Maybe Chad can explain what the use case is. There is probably an existing command that does this or that could be extended to do this safely.
Stefan
Quoted 11 lines · On Wed, 08/14 08:19, Spensky, Chad - 0559 - MITLL wrote:
Fam,
That's correct, we modified
block.cto hook the appropriate functions
and output the information through a unix socket. One of the functions that we hooked isbdrv_aio_writev, however it seems like the data that we are seeing at that point in the callstack is not what actually makes it to the disk image for the guest. The first couple of sectors always seem to be the same, however after sector 2 it's a toss up. I'm guessing there may be some sort of caching going on or something, and we appear to be missing it.
Your approach sounds right. For you problem, I think it should be possible to debug using gdb: start qemu in gdb, boot with a live CD, so no disturbing disk IO by the OS; attach a image to the guest; insert breakpoint at bdrv_aio_writev, then in guest you can start IO:
# echo "your data" | dd of=/dev/sda oflag=sync seek=XXX
You should now be at the breakpoint in gdb and you can trace the your data to the unix socket and compare with the guest io request.
Fam
Quoted 97 lines
- Chad
Signature · 5 lines
Chad S. Spensky MIT Lincoln Laboratory Group 59 (Cyber Systems Assessment) Ph: (781) 981-4173Quoted 82 lines · Fam Zheng
Quoted 8 lines · On Wed, 08/14 07:29, Spensky, Chad - 0559 - MITLL wrote:
Stefan, Fam,
We are trying to keep an active shadow copy while the system is
running
without any need for pausing. More precisely we want to log every individual access to the drive into a database so that the entire stream of accesses could be replayed at a later time.There's no IO request log infrastructure in QEMU for now. What drive-mirror can do is to repetitively send changed sectors since last sending point, but it's not in guest request order or operation size, it works just with a dirty bitmap.
In your methodology you didn't mention the hook you worked in
block.c, but I think it is necessary to hackblock.cto log every r/w access to the drive, I think you synchronous each write to the shadow image, right?Quoted 62 lines · Stefan Hajnoczi
Quoted 8 lines · Fam Zheng
Quoted 6 lines · On Tue, 08/13 16:13, Spensky, Chad - 0559 - MITLL wrote:
Quoted 3 lines
Hi All,
I'm working with some disk introspection on KVM, and we trying to
create
Quoted 1 line
a shadow image of the disk. We've hooked the functions in
block.c, inQuoted 1 line
Quoted 1 line
Quoted 1 line
particular
bdrv_aio_writev. However we are seeing writes gothrough,
Quoted 1 line
Quoted 1 line
Quoted 1 line
pausing the VM, and the comparing our shadow image with the actual
VM
Quoted 1 line
Quoted 1 line
Quoted 1 line
image, and they aren't 100% synced up. The first 1-2 sectors
appear
Quoted 3 lines
Quoted 3 lines
to be
Quoted 2 lines
always be correct, however, after that, there are sometimes some discrepancies. I believe we have exhausted most obvious bugs
(malloc
Quoted 22 lines
Quoted 22 lines
Quoted 1 line
bugs, incorrect size calculations etc.). Has anyone had any
experience
Quoted 6 lines
with this or have any insights?
Our methodology is as follows:
1. Boot the VM.
2. Pause VM.
3. Copy the disk to our shadow image.How do you copy the disk, from guest or host?
Quoted 1 line
4. Perform very few reads/writes.
Did you flush to disk?
Quoted 5 lines
5. Pause VM.
6. Compare shadow copy with active vm disk.And this is where we are seeing discrepancies. Any help is much
appreciated! We are running on Ubuntu 12.04 with a modified Debianbuild.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
- Chad
Signature · 1 line
Chad S. SpenskyI think drive-backup command does just what you want, it creates a
image
Quoted 10 lines
Quoted 2 lines
and copy-on-write date from guest disk to the target, without pausing VM.
Or perhaps drive-mirror.
Maybe Chad can explain what the use case is. There is probably an existing command that does this or that could be extended to do this safely.
Stefan
Quoted 4 lines · On Wed, Aug 14, 2013 at 07:29:53AM -0400, Spensky, Chad - 0559 - MITLL wrote:
We are trying to keep an active shadow copy while the system is running
without any need for pausing. More precisely we want to log every individual access to the drive into a database so that the entire stream of accesses could be replayed at a later time.
CCing Wolfgang Richter who was previously interested in block I/O tracing:
https://lists.nongnu.org/archive/html/qemu-devel/2013-05/msg01725.html
Stefan
Still interested and back to working on this. I taught a couple classes this summer which killed my time in June - July.
So Chad, are you already logging all accesses? Or do you need something quick to log them?
I have a patch to QEMU mainline (very small) to add block I/O tracing, but it works via the log subsystem and might not fit your environment if you wanted a production solution.
--
Wolf
Quoted 11 lines · Stefan Hajnoczi
Quoted 4 lines · On Wed, Aug 14, 2013 at 07:29:53AM -0400, Spensky, Chad - 0559 - MITLL wrote:
We are trying to keep an active shadow copy while the system is running
without any need for pausing. More precisely we want to log every individual access to the drive into a database so that the entire stream of accesses could be replayed at a later time.CCing Wolfgang Richter who was previously interested in block I/O tracing:
https://lists.nongnu.org/archive/html/qemu-devel/2013-05/msg01725.htmlStefan
Signature · 1 line
Wolf
Wolf,
We're able to get all of the meta data just fine. However it seems that
the actual content of the read/write seems to be wrong some of the time. The first 2 sectors seem to always be correct, however on some writes, the data that we traced does not match up with the data we are actually seeing in the .img file for the guest on disk.
- Chad
Signature · 5 lines
Chad S. Spensky MIT Lincoln Laboratory Group 59 (Cyber Systems Assessment) Ph: (781) 981-4173
Quoted 34 lines · Wolfgang Richter
Still interested and back to working on this. I taught a couple classes this summer which killed my time in June - July.
So Chad, are you already logging all accesses? Or do you need something quick to log them?
I have a patch to QEMU mainline (very small) to add block I/O tracing, but it works via the log subsystem and might not fit your environment if you wanted a production solution.
--
WolfQuoted 14 lines · Stefan Hajnoczi
Quoted 6 lines · On Wed, Aug 14, 2013 at 07:29:53AM -0400, Spensky, Chad - 0559 - MITLL wrote:
We are trying to keep an active shadow copy while the system is
running
without any need for pausing. More precisely we want to log every individual access to the drive into a database so that the entire stream
of accesses could be replayed at a later time.CCing Wolfgang Richter who was previously interested in block I/O tracing:
https://lists.nongnu.org/archive/html/qemu-devel/2013-05/msg01725.htmlStefan
Signature · 1 line
Wolf
Quoted 3 lines · On Wed, Aug 14, 2013 at 10:43 AM, Spensky, Chad - 0559 - MITLL…
Wolf,
We're able to get all of the meta data just fine.
I assume by meta-data you mean (essentially) "function" call parameters within QEMU as to the (1) location of the write on disk, and (2) the amount of data being written out (in bytes).
Quoted 2 lines
However it seems that
the actual content of the read/write seems to be wrong some of the time.
That's very odd, I'm pretty sure I never had that bug (although I might silently have it!).
I did occasionally observe a kernel accidentally writing out kernel buffers that hadn't been cleared to disk (guest kernel) :-)
Quoted 3 lines
The first 2 sectors seem to always be correct, however on some writes, the data that we traced does not match up with the data we are actually seeing in the .img file for the guest on disk.
Are you certain you're getting every write? Sometimes things might be written to rapidly in succession?
How are you obtaining the shadow copy of the write stream (maybe there was earlier stuff in this thread I should read up on)?
Signature · 1 line
Wolf
Read through the kvm thread (I'm not on that mailing just just for reference; thanks for the CC).
I saw you're hooking a different function than me (not sure it matters). I hook bdrv_co_writev and I operate on the passed in iovector datastructure there writing out its contents and a short header to stderr where my introspection tools interpret it. I don't maintain a full shadow copy of the disk, but it conceptually shouldn't matter.
Are you working on the iovec data structure as well? Also, is your disk format raw? I don't think it should matter, but I was just wondering.
--
Wolf
Quoted 31 lines · Wolfgang Richter
Quoted 3 lines · On Wed, Aug 14, 2013 at 10:43 AM, Spensky, Chad - 0559 - MITLL…
Wolf,
We're able to get all of the meta data just fine.
I assume by meta-data you mean (essentially) "function" call parameters within QEMU as to the (1) location of the write on disk, and (2) the amount of data being written out (in bytes).
Quoted 2 lines
However it seems that
the actual content of the read/write seems to be wrong some of the time.That's very odd, I'm pretty sure I never had that bug (although I might silently have it!).
I did occasionally observe a kernel accidentally writing out kernel buffers that hadn't been cleared to disk (guest kernel) :-)
Quoted 3 lines
The first 2 sectors seem to always be correct, however on some writes, the data that we traced does not match up with the data we are actually seeing in the .img file for the guest on disk.
Are you certain you're getting every write? Sometimes things might be written to rapidly in succession?
How are you obtaining the shadow copy of the write stream (maybe there was earlier stuff in this thread I should read up on)?
--
Wolf
Signature · 1 line
Wolf
Wolfgang,
Thanks so much for the response. It turns out that wasn't handling the
QEMUIOVector properly. When I first implemented it, I saw that the iovec was a pointer and assumed that there would only ever be one. Given the lack of documentation and my lack of understanding this went undetected for a while. everything now seems to work just fine. :-) See below for the portion of code that threw me off. Thanks again!
- Chad
Proposed change to qemu-common.h (:
typedef struct QEMUIOVector {
struct iovec *iov;
int niov;
int nalloc;
size_t size;
} QEMUIOVector;changed to:
// Array of I/O vectors
typedef struct QEMUIOVector {
struct iovec iov[];
int niov;
int nalloc;size_t size;
} QEMUIOVector;
Signature · 5 lines
Chad S. Spensky MIT Lincoln Laboratory Group 59 (Cyber Systems Assessment) Ph: (781) 981-4173
Quoted 58 lines · Wolfgang Richter
Read through the kvm thread (I'm not on that mailing just just for reference; thanks for the CC).
I saw you're hooking a different function than me (not sure it matters). I hook
bdrv_co_writevand I operate on the passed in iovector datastructure there writing out its contents and a short header to stderr where my introspection tools interpret it. I don't maintain a full shadow copy of the disk, but it conceptually shouldn't matter.Are you working on the iovec data structure as well? Also, is your disk format raw? I don't think it should matter, but I was just wondering.
--
WolfQuoted 34 lines · Wolfgang Richter
Quoted 3 lines · On Wed, Aug 14, 2013 at 10:43 AM, Spensky, Chad - 0559 - MITLL…
Wolf,
We're able to get all of the meta data just fine.
I assume by meta-data you mean (essentially) "function" call parameters within QEMU as to the (1) location of the write on disk, and (2) the amount of data being written out (in bytes).
Quoted 3 lines
However it seems that
the actual content of the read/write seems to be wrong some of the time.That's very odd, I'm pretty sure I never had that bug (although I might silently have it!).
I did occasionally observe a kernel accidentally writing out kernel buffers that hadn't been cleared to disk (guest kernel) :-)
Quoted 5 lines
The first 2 sectors seem to always be correct, however on some writes, the
data that we traced does not match up with the data we are actually seeing
in the .img file for the guest on disk.Are you certain you're getting every write? Sometimes things might be written to rapidly in succession?
How are you obtaining the shadow copy of the write stream (maybe there was earlier stuff in this thread I should read up on)?
--
WolfSignature · 1 line
Wolf
I'd expect it would be something with QEMUIOvector :-) Glad you found it!
Quoted 8 lines · On Wed, Aug 14, 2013 at 2:44 PM, Spensky, Chad - 0559 - MITLL…
Wolfgang,
Thanks so much for the response. It turns out that wasn't handling the
QEMUIOVector properly. When I first implemented it, I saw that the iovec was a pointer and assumed that there would only ever be one. Given the lack of documentation and my lack of understanding this went undetected for a while. everything now seems to work just fine. :-) See below for the portion of code that threw me off. Thanks again!
Just so you know (possibly to be safer?) the code I use was based on these functions (used to be declared qemu-common.h and iov.h, moved or refactored I think recently?):
void qemu_iovec_to_fd(int fd, QEMUIOVector *qiov);
void iov_to_fd(int fd, const struct iovec *iov, unsigned int iov_cnt);
void qemu_iovec_to_fd(int fd, QEMUIOVector *qiov)
{
iov_to_fd(fd, qiov->iov, qiov->niov);
}void iov_to_fd(int fd, const struct iovec *iov, const unsigned int iov_cnt)
{
unsigned int i;
for (i = 0; i < iov_cnt; i++)
{
assert(qemu_write_full(fd, iov[i].iov_base, iov[i].iov_len) ==
iov[i].iov_len);
}
}Thus, you have to loop through the iovector (as I think you found and just fixed).
Signature · 1 line
Wolf
Paolo Bonzini
maintainerIl 14/08/2013 20:44, Spensky, Chad - 0559 - MITLL ha scritto:
Quoted 23 lines
Wolfgang,
Thanks so much for the response. It turns out that wasn't handling the
QEMUIOVector properly. When I first implemented it, I saw that the iovec was a pointer and assumed that there would only ever be one. Given the lack of documentation and my lack of understanding this went undetected for a while. everything now seems to work just fine. :-) See below for the portion of code that threw me off. Thanks again!- Chad
Proposed change to
qemu-common.h(:typedef struct QEMUIOVector { struct iovec *iov; int niov; int nalloc; size_t size; } QEMUIOVector;changed to:
Quoted 8 lines
// Array of I/O vectors
typedef struct QEMUIOVector {
struct iovec iov[];
int niov;
int nalloc;size_tsize;
} QEMUIOVector;
This wouldn't work. As you wrote it, it wouldn't even compile.
Embedding an array is possible if you place it at the end of the struct, but then it is not possible to resize the array (because when you reallocate it, the containing QEMUIOVector could move in memory and any pointers to the containing QEMUIOVector would become invalid).
In C, a dynamically-allocated array is represented by a pointer to the first item. The size of the array and the size of the dynamically-allocated memory block are stored together with the pointer (in this case, in the niov and nalloc members).
In fact, whenever you see something like "iovec iov[]" in Java, what's being stored in memory is exactly a pointer to the first item, the size of the array and the size of the dynamically-allocated memory block. C just makes that explicit.
Paolo
Also there are utility functions within the QEMU code base that help deal with QEMUIOVectors (as far as I remember) which would help maybe so you don't have to directly code using them.
--
Wolf
Quoted 51 lines · Paolo Bonzini
Il 14/08/2013 20:44, Spensky, Chad - 0559 - MITLL ha scritto:
Quoted 23 lines
Wolfgang,
Thanks so much for the response. It turns out that wasn't handling the
QEMUIOVector properly. When I first implemented it, I saw that the iovec was a pointer and assumed that there would only ever be one. Given the lack of documentation and my lack of understanding this went undetected for a while. everything now seems to work just fine. :-) See below for the portion of code that threw me off. Thanks again!- Chad
Proposed change to
qemu-common.h(:typedef struct QEMUIOVector { struct iovec *iov; int niov; int nalloc; size_t size; } QEMUIOVector;changed to:
Quoted 8 lines
// Array of I/O vectors
typedef struct QEMUIOVector {
struct iovec iov[];
int niov;
int nalloc;size_tsize;
} QEMUIOVector;This wouldn't work. As you wrote it, it wouldn't even compile.
Embedding an array is possible if you place it at the end of the struct, but then it is not possible to resize the array (because when you reallocate it, the containing QEMUIOVector could move in memory and any pointers to the containing QEMUIOVector would become invalid).
In C, a dynamically-allocated array is represented by a pointer to the first item. The size of the array and the size of the dynamically-allocated memory block are stored together with the pointer (in this case, in the niov and nalloc members).
In fact, whenever you see something like "iovec iov[]" in Java, what's being stored in memory is exactly a pointer to the first item, the size of the array and the size of the dynamically-allocated memory block. C just makes that explicit.
Paolo