diff mbox

[5/5] virtio-net: unbreak any layout

Message ID 1436766411-29144-5-git-send-email-jasowang@redhat.com
State New
Headers show

Commit Message

Jason Wang July 13, 2015, 5:46 a.m. UTC
Commit 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header") breaks any layout by
requiring out_sg[0].iov_len >= n->guest_hdr_len. Fixing this by
copying header to temporary buffer and copying it back after byteswap.

Fixes 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header")
Cc: qemu-stable@nongnu.org
Cc: clg@fr.ibm.com
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 hw/net/virtio-net.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Comments

Paolo Bonzini July 13, 2015, 6:50 a.m. UTC | #1
On 13/07/2015 07:46, Jason Wang wrote:
> -            if (out_sg[0].iov_len < n->guest_hdr_len) {
> +            s = iov_to_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
> +            if (s != sizeof(hdr)) {
>                  error_report("virtio-net header incorrect");
>                  exit(1);
>              }
> -            virtio_net_hdr_swap(vdev, (void *) out_sg[0].iov_base);
> +            virtio_net_hdr_swap(vdev, (void *) &hdr);
> +            s = iov_from_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
> +            assert(s == sizeof(hdr));
>          }

Are the copies necessary in the common case of no swap?  In that case
you can just use iov_size.

Paolo
Michael S. Tsirkin July 13, 2015, 7:24 a.m. UTC | #2
On Mon, Jul 13, 2015 at 01:46:51PM +0800, Jason Wang wrote:
> Commit 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
> ("virtio-net: byteswap virtio-net header") breaks any layout by
> requiring out_sg[0].iov_len >= n->guest_hdr_len. Fixing this by
> copying header to temporary buffer and copying it back after byteswap.
> 
> Fixes 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
> ("virtio-net: byteswap virtio-net header")
> Cc: qemu-stable@nongnu.org
> Cc: clg@fr.ibm.com
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  hw/net/virtio-net.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> index e3c2db3..b42af8f 100644
> --- a/hw/net/virtio-net.c
> +++ b/hw/net/virtio-net.c
> @@ -1139,10 +1139,11 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
>      }
>  
>      while (virtqueue_pop(q->tx_vq, &elem)) {
> -        ssize_t ret, len;
> +        ssize_t ret, len, s;
>          unsigned int out_num = elem.out_num;
>          struct iovec *out_sg = &elem.out_sg[0];
>          struct iovec sg[VIRTQUEUE_MAX_SIZE];
> +        struct virtio_net_hdr hdr;
>  
>          if (out_num < 1) {
>              error_report("virtio-net header not in first element");
> @@ -1150,11 +1151,14 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
>          }
>  
>          if (n->has_vnet_hdr) {
> -            if (out_sg[0].iov_len < n->guest_hdr_len) {
> +            s = iov_to_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
> +            if (s != sizeof(hdr)) {
>                  error_report("virtio-net header incorrect");
>                  exit(1);
>              }
> -            virtio_net_hdr_swap(vdev, (void *) out_sg[0].iov_base);
> +            virtio_net_hdr_swap(vdev, (void *) &hdr);
> +            s = iov_from_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
> +            assert(s == sizeof(hdr));
>          }
>  
>          /*

Looks like this will write into out_sg - that's violating the virtio spec.

> -- 
> 2.1.4
Michael S. Tsirkin July 13, 2015, 8:22 a.m. UTC | #3
On Mon, Jul 13, 2015 at 10:24:59AM +0300, Michael S. Tsirkin wrote:
> On Mon, Jul 13, 2015 at 01:46:51PM +0800, Jason Wang wrote:
> > Commit 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
> > ("virtio-net: byteswap virtio-net header") breaks any layout by
> > requiring out_sg[0].iov_len >= n->guest_hdr_len. Fixing this by
> > copying header to temporary buffer and copying it back after byteswap.
> > 
> > Fixes 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
> > ("virtio-net: byteswap virtio-net header")
> > Cc: qemu-stable@nongnu.org
> > Cc: clg@fr.ibm.com
> > Signed-off-by: Jason Wang <jasowang@redhat.com>
> > ---
> >  hw/net/virtio-net.c | 10 +++++++---
> >  1 file changed, 7 insertions(+), 3 deletions(-)
> > 
> > diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> > index e3c2db3..b42af8f 100644
> > --- a/hw/net/virtio-net.c
> > +++ b/hw/net/virtio-net.c
> > @@ -1139,10 +1139,11 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
> >      }
> >  
> >      while (virtqueue_pop(q->tx_vq, &elem)) {
> > -        ssize_t ret, len;
> > +        ssize_t ret, len, s;
> >          unsigned int out_num = elem.out_num;
> >          struct iovec *out_sg = &elem.out_sg[0];
> >          struct iovec sg[VIRTQUEUE_MAX_SIZE];
> > +        struct virtio_net_hdr hdr;
> >  
> >          if (out_num < 1) {
> >              error_report("virtio-net header not in first element");
> > @@ -1150,11 +1151,14 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
> >          }
> >  
> >          if (n->has_vnet_hdr) {
> > -            if (out_sg[0].iov_len < n->guest_hdr_len) {
> > +            s = iov_to_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
> > +            if (s != sizeof(hdr)) {
> >                  error_report("virtio-net header incorrect");
> >                  exit(1);
> >              }
> > -            virtio_net_hdr_swap(vdev, (void *) out_sg[0].iov_base);
> > +            virtio_net_hdr_swap(vdev, (void *) &hdr);
> > +            s = iov_from_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
> > +            assert(s == sizeof(hdr));
> >          }
> >  
> >          /*
> 
> Looks like this will write into out_sg - that's violating the virtio spec.

And the fault lies with the original code upstream - not with these
patches. In my opinion the thing to do here is to completely
avoid using vnet header for cross endian unless host supports ioctls
to set endian-ness.

> > -- 
> > 2.1.4
Jason Wang July 13, 2015, 8:30 a.m. UTC | #4
On 07/13/2015 02:50 PM, Paolo Bonzini wrote:
>
> On 13/07/2015 07:46, Jason Wang wrote:
>> -            if (out_sg[0].iov_len < n->guest_hdr_len) {
>> +            s = iov_to_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
>> +            if (s != sizeof(hdr)) {
>>                  error_report("virtio-net header incorrect");
>>                  exit(1);
>>              }
>> -            virtio_net_hdr_swap(vdev, (void *) out_sg[0].iov_base);
>> +            virtio_net_hdr_swap(vdev, (void *) &hdr);
>> +            s = iov_from_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
>> +            assert(s == sizeof(hdr));
>>          }
> Are the copies necessary in the common case of no swap?  In that case
> you can just use iov_size.
>
> Paolo
>

Not necessary, will fix this in V2.

Thanks
Jason Wang July 13, 2015, 8:30 a.m. UTC | #5
On 07/13/2015 03:24 PM, Michael S. Tsirkin wrote:
> On Mon, Jul 13, 2015 at 01:46:51PM +0800, Jason Wang wrote:
>> Commit 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
>> ("virtio-net: byteswap virtio-net header") breaks any layout by
>> requiring out_sg[0].iov_len >= n->guest_hdr_len. Fixing this by
>> copying header to temporary buffer and copying it back after byteswap.
>>
>> Fixes 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
>> ("virtio-net: byteswap virtio-net header")
>> Cc: qemu-stable@nongnu.org
>> Cc: clg@fr.ibm.com
>> Signed-off-by: Jason Wang <jasowang@redhat.com>
>> ---
>>  hw/net/virtio-net.c | 10 +++++++---
>>  1 file changed, 7 insertions(+), 3 deletions(-)
>>
>> diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
>> index e3c2db3..b42af8f 100644
>> --- a/hw/net/virtio-net.c
>> +++ b/hw/net/virtio-net.c
>> @@ -1139,10 +1139,11 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
>>      }
>>  
>>      while (virtqueue_pop(q->tx_vq, &elem)) {
>> -        ssize_t ret, len;
>> +        ssize_t ret, len, s;
>>          unsigned int out_num = elem.out_num;
>>          struct iovec *out_sg = &elem.out_sg[0];
>>          struct iovec sg[VIRTQUEUE_MAX_SIZE];
>> +        struct virtio_net_hdr hdr;
>>  
>>          if (out_num < 1) {
>>              error_report("virtio-net header not in first element");
>> @@ -1150,11 +1151,14 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
>>          }
>>  
>>          if (n->has_vnet_hdr) {
>> -            if (out_sg[0].iov_len < n->guest_hdr_len) {
>> +            s = iov_to_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
>> +            if (s != sizeof(hdr)) {
>>                  error_report("virtio-net header incorrect");
>>                  exit(1);
>>              }
>> -            virtio_net_hdr_swap(vdev, (void *) out_sg[0].iov_base);
>> +            virtio_net_hdr_swap(vdev, (void *) &hdr);
>> +            s = iov_from_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
>> +            assert(s == sizeof(hdr));
>>          }
>>  
>>          /*
> Looks like this will write into out_sg - that's violating the virtio spec.

Right, will fix this in V2.

Thanks
>
>> -- 
>> 2.1.4
Greg Kurz July 13, 2015, 10:54 a.m. UTC | #6
On Mon, 13 Jul 2015 11:22:09 +0300
"Michael S. Tsirkin" <mst@redhat.com> wrote:
> On Mon, Jul 13, 2015 at 10:24:59AM +0300, Michael S. Tsirkin wrote:
> > On Mon, Jul 13, 2015 at 01:46:51PM +0800, Jason Wang wrote:
> > > Commit 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
> > > ("virtio-net: byteswap virtio-net header") breaks any layout by
> > > requiring out_sg[0].iov_len >= n->guest_hdr_len. Fixing this by
> > > copying header to temporary buffer and copying it back after byteswap.
> > > 
> > > Fixes 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
> > > ("virtio-net: byteswap virtio-net header")
> > > Cc: qemu-stable@nongnu.org
> > > Cc: clg@fr.ibm.com
> > > Signed-off-by: Jason Wang <jasowang@redhat.com>
> > > ---
> > >  hw/net/virtio-net.c | 10 +++++++---
> > >  1 file changed, 7 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> > > index e3c2db3..b42af8f 100644
> > > --- a/hw/net/virtio-net.c
> > > +++ b/hw/net/virtio-net.c
> > > @@ -1139,10 +1139,11 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
> > >      }
> > >  
> > >      while (virtqueue_pop(q->tx_vq, &elem)) {
> > > -        ssize_t ret, len;
> > > +        ssize_t ret, len, s;
> > >          unsigned int out_num = elem.out_num;
> > >          struct iovec *out_sg = &elem.out_sg[0];
> > >          struct iovec sg[VIRTQUEUE_MAX_SIZE];
> > > +        struct virtio_net_hdr hdr;
> > >  
> > >          if (out_num < 1) {
> > >              error_report("virtio-net header not in first element");
> > > @@ -1150,11 +1151,14 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
> > >          }
> > >  
> > >          if (n->has_vnet_hdr) {
> > > -            if (out_sg[0].iov_len < n->guest_hdr_len) {
> > > +            s = iov_to_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
> > > +            if (s != sizeof(hdr)) {
> > >                  error_report("virtio-net header incorrect");
> > >                  exit(1);
> > >              }
> > > -            virtio_net_hdr_swap(vdev, (void *) out_sg[0].iov_base);
> > > +            virtio_net_hdr_swap(vdev, (void *) &hdr);
> > > +            s = iov_from_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
> > > +            assert(s == sizeof(hdr));
> > >          }
> > >  
> > >          /*
> > 
> > Looks like this will write into out_sg - that's violating the virtio spec.
> 
> And the fault lies with the original code upstream - not with these
> patches. In my opinion the thing to do here is to completely

I fully agree: it is a hack.

> avoid using vnet header for cross endian unless host supports ioctls
> to set endian-ness.
> 

Makes sense. I had an *experimental* patch that would just deactivate
the cross-endian hack if the host kernel provides the TUNSETVNETLE/BE
ioctls. It worked but the patch was really ugly and I did not care to
post at the time... I'll try to work something out before I go on
vacation.

> > > -- 
> > > 2.1.4
> 

Cheers.

--
Greg
Michael S. Tsirkin July 13, 2015, 11:13 a.m. UTC | #7
On Mon, Jul 13, 2015 at 12:54:02PM +0200, Greg Kurz wrote:
> On Mon, 13 Jul 2015 11:22:09 +0300
> "Michael S. Tsirkin" <mst@redhat.com> wrote:
> > On Mon, Jul 13, 2015 at 10:24:59AM +0300, Michael S. Tsirkin wrote:
> > > On Mon, Jul 13, 2015 at 01:46:51PM +0800, Jason Wang wrote:
> > > > Commit 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
> > > > ("virtio-net: byteswap virtio-net header") breaks any layout by
> > > > requiring out_sg[0].iov_len >= n->guest_hdr_len. Fixing this by
> > > > copying header to temporary buffer and copying it back after byteswap.
> > > > 
> > > > Fixes 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
> > > > ("virtio-net: byteswap virtio-net header")
> > > > Cc: qemu-stable@nongnu.org
> > > > Cc: clg@fr.ibm.com
> > > > Signed-off-by: Jason Wang <jasowang@redhat.com>
> > > > ---
> > > >  hw/net/virtio-net.c | 10 +++++++---
> > > >  1 file changed, 7 insertions(+), 3 deletions(-)
> > > > 
> > > > diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> > > > index e3c2db3..b42af8f 100644
> > > > --- a/hw/net/virtio-net.c
> > > > +++ b/hw/net/virtio-net.c
> > > > @@ -1139,10 +1139,11 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
> > > >      }
> > > >  
> > > >      while (virtqueue_pop(q->tx_vq, &elem)) {
> > > > -        ssize_t ret, len;
> > > > +        ssize_t ret, len, s;
> > > >          unsigned int out_num = elem.out_num;
> > > >          struct iovec *out_sg = &elem.out_sg[0];
> > > >          struct iovec sg[VIRTQUEUE_MAX_SIZE];
> > > > +        struct virtio_net_hdr hdr;
> > > >  
> > > >          if (out_num < 1) {
> > > >              error_report("virtio-net header not in first element");
> > > > @@ -1150,11 +1151,14 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
> > > >          }
> > > >  
> > > >          if (n->has_vnet_hdr) {
> > > > -            if (out_sg[0].iov_len < n->guest_hdr_len) {
> > > > +            s = iov_to_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
> > > > +            if (s != sizeof(hdr)) {
> > > >                  error_report("virtio-net header incorrect");
> > > >                  exit(1);
> > > >              }
> > > > -            virtio_net_hdr_swap(vdev, (void *) out_sg[0].iov_base);
> > > > +            virtio_net_hdr_swap(vdev, (void *) &hdr);
> > > > +            s = iov_from_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
> > > > +            assert(s == sizeof(hdr));
> > > >          }
> > > >  
> > > >          /*
> > > 
> > > Looks like this will write into out_sg - that's violating the virtio spec.
> > 
> > And the fault lies with the original code upstream - not with these
> > patches. In my opinion the thing to do here is to completely
> 
> I fully agree: it is a hack.
> 
> > avoid using vnet header for cross endian unless host supports ioctls
> > to set endian-ness.
> > 
> 
> Makes sense. I had an *experimental* patch that would just deactivate
> the cross-endian hack if the host kernel provides the TUNSETVNETLE/BE
> ioctls. It worked but the patch was really ugly and I did not care to
> post at the time...

So my proposal would be to basically drop the hack completely:
if we are cross-endian and TUNSETVNETLE/BE are unavailable,
treat it as if has_vnet_hdr was not set.


> I'll try to work something out before I go on
> vacation.
> 
> > > > -- 
> > > > 2.1.4
> > 
> 
> Cheers.
> 
> --
> Greg
diff mbox

Patch

diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index e3c2db3..b42af8f 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -1139,10 +1139,11 @@  static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
     }
 
     while (virtqueue_pop(q->tx_vq, &elem)) {
-        ssize_t ret, len;
+        ssize_t ret, len, s;
         unsigned int out_num = elem.out_num;
         struct iovec *out_sg = &elem.out_sg[0];
         struct iovec sg[VIRTQUEUE_MAX_SIZE];
+        struct virtio_net_hdr hdr;
 
         if (out_num < 1) {
             error_report("virtio-net header not in first element");
@@ -1150,11 +1151,14 @@  static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
         }
 
         if (n->has_vnet_hdr) {
-            if (out_sg[0].iov_len < n->guest_hdr_len) {
+            s = iov_to_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
+            if (s != sizeof(hdr)) {
                 error_report("virtio-net header incorrect");
                 exit(1);
             }
-            virtio_net_hdr_swap(vdev, (void *) out_sg[0].iov_base);
+            virtio_net_hdr_swap(vdev, (void *) &hdr);
+            s = iov_from_buf(out_sg, out_num, 0, &hdr, sizeof(hdr));
+            assert(s == sizeof(hdr));
         }
 
         /*