virtio-net: unbreak any layout
PATCH v2 1/1 virtio-net: unbreak any layout
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 if swap is needed, and then use this buffer as part of out_sg.
Fixes 032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header")
---
Changes from V1:
- avoid header copying if there's no need to do header swap
- don't write the header back
--- hw/net/virtio-net.c | 17 ++++++++++++++--- include/hw/virtio/virtio-access.h | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)
hw/net/virtio-net.c +14 -3
@@ -1142,7 +1142,8 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)ssize_t ret, len;unsigned int out_num = elem.out_num;struct iovec *out_sg = &elem.out_sg[0 ]; -struct iovec sg[VIRTQUEUE_MAX_SIZE ]; +struct iovec sg[VIRTQUEUE_MAX_SIZE ], sg2[VIRTQUEUE_MAX_SIZE ]; +struct virtio_net_hdr hdr;if (out_num <1 ) {error_report ("virtio-net header not in first element" );
@@ -1150,11 +1151,21 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) }if (n->has_vnet_hdr) { -if (out_sg[0 ].iov_len < n->guest_hdr_len) { +if (iov_size (out_sg, out_num) < n->guest_hdr_len) {error_report ("virtio-net header incorrect" );exit (1 ); } -virtio_net_hdr_swap (vdev, (void *) out_sg[0 ].iov_base); +if (virtio_needs_swap (vdev)) { +iov_to_buf (out_sg, out_num,0 , &hdr,sizeof (hdr)); +virtio_net_hdr_swap (vdev, (void *) &hdr); + sg2[0 ].iov_base = &hdr; + sg2[0 ].iov_len =sizeof (hdr); + out_num =iov_copy (&sg2[1 ],ARRAY_SIZE (sg2) -1 , + out_sg, out_num, +sizeof (hdr),-1 ); + out_num +=1 ; + out_sg = sg2; + } }/*
include/hw/virtio/virtio-access.h +9 -0
@@ -143,6 +143,15 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr) } } +static inline bool virtio_needs_swap (VirtIODevice *vdev) +{ +#ifdef HOST_WORDS_BIGENDIAN +return virtio_access_is_big_endian (vdev) ? false : true; +#else +return virtio_access_is_big_endian (vdev) ? true : false; +#endif +} +static inline uint16_t virtio_tswap16 (VirtIODevice *vdev,uint16_t s) {#ifdef HOST_WORDS_BIGENDIAN
Signature · 1 line
2.1.4
Quoted 69 lines · Jason Wang
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 if swap is needed, and then use this buffer as part ofout_sg.Fixes
032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header")---
Changes from V1:
- avoid header copying if there's no need to do header swap
- don't write the header back--- hw/net/virtio-net.c | 17 ++++++++++++++--- include/hw/virtio/virtio-access.h | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)hw/net/virtio-net.c +14
-3@@ -1142,7 +1142,8 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)ssize_t ret, len;unsigned int out_num = elem.out_num;struct iovec *out_sg = &elem.out_sg[0 ]; -struct iovec sg[VIRTQUEUE_MAX_SIZE ]; +struct iovec sg[VIRTQUEUE_MAX_SIZE ], sg2[VIRTQUEUE_MAX_SIZE ]; +struct virtio_net_hdr hdr;if (out_num <1 ) {error_report ("virtio-net header not in first element" );@@ -1150,11 +1151,21 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) }if (n->has_vnet_hdr) { -if (out_sg[0 ].iov_len < n->guest_hdr_len) { +if (iov_size (out_sg, out_num) < n->guest_hdr_len) {error_report ("virtio-net header incorrect" );exit (1 ); } -virtio_net_hdr_swap (vdev, (void *) out_sg[0 ].iov_base); +if (virtio_needs_swap (vdev)) { +iov_to_buf (out_sg, out_num,0 , &hdr,sizeof (hdr)); +virtio_net_hdr_swap (vdev, (void *) &hdr); + sg2[0 ].iov_base = &hdr; + sg2[0 ].iov_len =sizeof (hdr); + out_num =iov_copy (&sg2[1 ],ARRAY_SIZE (sg2) -1 , + out_sg, out_num, +sizeof (hdr),-1 ); + out_num +=1 ; + out_sg = sg2; + } }/* include/hw/virtio/virtio-access.h +2
-0@@ -143,6 +143,15 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr) } } +static inline bool virtio_needs_swap (VirtIODevice *vdev) +{
include/hw/virtio/virtio-access.h static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr) · line 157
Quoted 10 lines
include/hw/virtio/virtio-access.h +7
-0+#ifdef HOST_WORDS_BIGENDIAN +return virtio_access_is_big_endian (vdev) ? false : true; +#else +return virtio_access_is_big_endian (vdev) ? true : false; +#endif +} +static inline uint16_t virtio_tswap16 (VirtIODevice *vdev,uint16_t s) {#ifdef HOST_WORDS_BIGENDIAN
Quoted 48 lines · Jason Wang
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 if swap is needed, and then use this buffer as part ofout_sg.Fixes
032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header")---
Changes from V1:
- avoid header copying if there's no need to do header swap
- don't write the header back--- hw/net/virtio-net.c | 17 ++++++++++++++--- include/hw/virtio/virtio-access.h | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)hw/net/virtio-net.c +7
-3@@ -1142,7 +1142,8 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)ssize_t ret, len;unsigned int out_num = elem.out_num;struct iovec *out_sg = &elem.out_sg[0 ]; -struct iovec sg[VIRTQUEUE_MAX_SIZE ]; +struct iovec sg[VIRTQUEUE_MAX_SIZE ], sg2[VIRTQUEUE_MAX_SIZE ]; +struct virtio_net_hdr hdr;if (out_num <1 ) {error_report ("virtio-net header not in first element" );@@ -1150,11 +1151,21 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) }if (n->has_vnet_hdr) { -if (out_sg[0 ].iov_len < n->guest_hdr_len) { +if (iov_size (out_sg, out_num) < n->guest_hdr_len) {error_report ("virtio-net header incorrect" );exit (1 ); } -virtio_net_hdr_swap (vdev, (void *) out_sg[0 ].iov_base); +if (virtio_needs_swap (vdev)) { +iov_to_buf (out_sg, out_num,0 , &hdr,sizeof (hdr)); +virtio_net_hdr_swap (vdev, (void *) &hdr); + sg2[0 ].iov_base = &hdr;
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 1171
Quoted 10 lines
hw/net/virtio-net.c +7
-0+ sg2[0 ].iov_len =sizeof (hdr); + out_num =iov_copy (&sg2[1 ],ARRAY_SIZE (sg2) -1 , + out_sg, out_num, +sizeof (hdr),-1 ); + out_num +=1 ; + out_sg = sg2; + } }/*
I guess this works but iov cpy is pretty expensive.
- When using the tun backend, we can just set VNETBE/VNETLE correctly and avoid swaps in userspace.
It's easy to probe for - why don't we do this?
- OTOH when not using the tun backend, offloads are generally disabled, so the swap isn't needed since header is going to be all 0s.
- what's left is old kernels when using tun. how about probing for that explicitly?
In other words, virtio_net_needs_swap as opposed to the generic virtio_needs_swap.
Quoted 22 lines
include/hw/virtio/virtio-access.h +9
-0@@ -143,6 +143,15 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr) } } +static inline bool virtio_needs_swap (VirtIODevice *vdev) +{ +#ifdef HOST_WORDS_BIGENDIAN +return virtio_access_is_big_endian (vdev) ? false : true; +#else +return virtio_access_is_big_endian (vdev) ? true : false; +#endif +} +static inline uint16_t virtio_tswap16 (VirtIODevice *vdev,uint16_t s) {#ifdef HOST_WORDS_BIGENDIAN Signature · 1 line
2.1.4
Quoted 58 lines · Michael S. Tsirkin
Quoted 57 lines · Jason Wang
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 if swap is needed, and then use this buffer as part ofout_sg.Fixes
032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header")---
Changes from V1:
- avoid header copying if there's no need to do header swap
- don't write the header back--- hw/net/virtio-net.c | 17 ++++++++++++++--- include/hw/virtio/virtio-access.h | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)hw/net/virtio-net.c +14
-3@@ -1142,7 +1142,8 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)ssize_t ret, len;unsigned int out_num = elem.out_num;struct iovec *out_sg = &elem.out_sg[0 ]; -struct iovec sg[VIRTQUEUE_MAX_SIZE ]; +struct iovec sg[VIRTQUEUE_MAX_SIZE ], sg2[VIRTQUEUE_MAX_SIZE ]; +struct virtio_net_hdr hdr;if (out_num <1 ) {error_report ("virtio-net header not in first element" );@@ -1150,11 +1151,21 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) }if (n->has_vnet_hdr) { -if (out_sg[0 ].iov_len < n->guest_hdr_len) { +if (iov_size (out_sg, out_num) < n->guest_hdr_len) {error_report ("virtio-net header incorrect" );exit (1 ); } -virtio_net_hdr_swap (vdev, (void *) out_sg[0 ].iov_base); +if (virtio_needs_swap (vdev)) { +iov_to_buf (out_sg, out_num,0 , &hdr,sizeof (hdr)); +virtio_net_hdr_swap (vdev, (void *) &hdr); + sg2[0 ].iov_base = &hdr; + sg2[0 ].iov_len =sizeof (hdr); + out_num =iov_copy (&sg2[1 ],ARRAY_SIZE (sg2) -1 , + out_sg, out_num, +sizeof (hdr),-1 ); + out_num +=1 ; + out_sg = sg2; + } }
Quoted 6 lines
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 1171
Quoted 1 line
hw/net/virtio-net.c +0
-0/* I guess this works but iov cpy is pretty expensive.
- When using the tun backend, we can just set VNETBE/VNETLE correctly and avoid swaps in userspace.
It's easy to probe for - why don't we do this?
We need to fix stable branches first. And if we do this for 2.4, if running on a older kernel which does not support VNETBE/VNETLE, do we want to fail the initialization? If yes, it breaks backward compatibility. If no, we still need the swap here.
Quoted 4 lines
- OTOH when not using the tun backend, offloads are generally disabled, so the swap isn't needed since header is going to be all 0s.
I agree it's better to check backend (e.g host_hdr_len) before trying to do the swap.
Quoted 5 lines
- what's left is old kernels when using tun. how about probing for that explicitly?
In other words,virtio_net_needs_swapas opposed to the genericvirtio_needs_swap.
So we still need swap anyway. How about apply this patch first for stable and 2.4 and probe the VNETBE/VNETLE explicitly on top for 2.5?
Quoted 24 lines
Quoted 24 lines
include/hw/virtio/virtio-access.h +9
-0@@ -143,6 +143,15 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr) } } +static inline bool virtio_needs_swap (VirtIODevice *vdev) +{ +#ifdef HOST_WORDS_BIGENDIAN +return virtio_access_is_big_endian (vdev) ? false : true; +#else +return virtio_access_is_big_endian (vdev) ? true : false; +#endif +} +static inline uint16_t virtio_tswap16 (VirtIODevice *vdev,uint16_t s) {#ifdef HOST_WORDS_BIGENDIAN Signature · 1 line
2.1.4
Quoted 82 lines · Jason Wang
Quoted 58 lines · Michael S. Tsirkin
Quoted 57 lines · Jason Wang
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 if swap is needed, and then use this buffer as part ofout_sg.Fixes
032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header")---
Changes from V1:
- avoid header copying if there's no need to do header swap
- don't write the header back--- hw/net/virtio-net.c | 17 ++++++++++++++--- include/hw/virtio/virtio-access.h | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)hw/net/virtio-net.c +14
-3@@ -1142,7 +1142,8 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)ssize_t ret, len;unsigned int out_num = elem.out_num;struct iovec *out_sg = &elem.out_sg[0 ]; -struct iovec sg[VIRTQUEUE_MAX_SIZE ]; +struct iovec sg[VIRTQUEUE_MAX_SIZE ], sg2[VIRTQUEUE_MAX_SIZE ]; +struct virtio_net_hdr hdr;if (out_num <1 ) {error_report ("virtio-net header not in first element" );@@ -1150,11 +1151,21 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) }if (n->has_vnet_hdr) { -if (out_sg[0 ].iov_len < n->guest_hdr_len) { +if (iov_size (out_sg, out_num) < n->guest_hdr_len) {error_report ("virtio-net header incorrect" );exit (1 ); } -virtio_net_hdr_swap (vdev, (void *) out_sg[0 ].iov_base); +if (virtio_needs_swap (vdev)) { +iov_to_buf (out_sg, out_num,0 , &hdr,sizeof (hdr)); +virtio_net_hdr_swap (vdev, (void *) &hdr); + sg2[0 ].iov_base = &hdr; + sg2[0 ].iov_len =sizeof (hdr); + out_num =iov_copy (&sg2[1 ],ARRAY_SIZE (sg2) -1 , + out_sg, out_num, +sizeof (hdr),-1 ); + out_num +=1 ; + out_sg = sg2; + } }Quoted 6 lines
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 1171
Quoted 1 line
hw/net/virtio-net.c +0
-0/* I guess this works but iov cpy is pretty expensive.
- When using the tun backend, we can just set VNETBE/VNETLE correctly and avoid swaps in userspace.
It's easy to probe for - why don't we do this?We need to fix stable branches first. And if we do this for 2.4, if running on a older kernel which does not support VNETBE/VNETLE, do we want to fail the initialization? If yes, it breaks backward compatibility. If no, we still need the swap here.
Quoted 4 lines
- OTOH when not using the tun backend, offloads are generally disabled, so the swap isn't needed since header is going to be all 0s.
I agree it's better to check backend (e.g
host_hdr_len) before trying to do the swap.Quoted 1 line
Quoted 7 lines
Quoted 4 lines
- what's left is old kernels when using tun. how about probing for that explicitly?
In other words,virtio_net_needs_swapas opposed to the genericvirtio_needs_swap.So we still need swap anyway. How about apply this patch first for stable and 2.4 and probe the VNETBE/VNETLE explicitly on top for 2.5?
I'm fine with it being a patch on top, but I think it's preferable to use VNETBE/VNETLE even in 2.4 if that's available.
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
include/hw/virtio/virtio-access.h +9
-0@@ -143,6 +143,15 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr) } } +static inline bool virtio_needs_swap (VirtIODevice *vdev) +{ +#ifdef HOST_WORDS_BIGENDIAN +return virtio_access_is_big_endian (vdev) ? false : true; +#else +return virtio_access_is_big_endian (vdev) ? true : false; +#endif +} +static inline uint16_t virtio_tswap16 (VirtIODevice *vdev,uint16_t s) {#ifdef HOST_WORDS_BIGENDIAN Signature · 1 line
2.1.4
Quoted 35 lines · Jason Wang
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 if swap is needed, and then use this buffer as part ofout_sg.Fixes
032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header")---
Changes from V1:
- avoid header copying if there's no need to do header swap
- don't write the header back--- hw/net/virtio-net.c | 17 ++++++++++++++--- include/hw/virtio/virtio-access.h | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)hw/net/virtio-net.c +2
-1@@ -1142,7 +1142,8 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)ssize_t ret, len;unsigned int out_num = elem.out_num;struct iovec *out_sg = &elem.out_sg[0 ]; -struct iovec sg[VIRTQUEUE_MAX_SIZE ]; +struct iovec sg[VIRTQUEUE_MAX_SIZE ], sg2[VIRTQUEUE_MAX_SIZE ]; +struct virtio_net_hdr hdr;if (out_num <1 ) {error_report ("virtio-net header not in first element" );
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 1157
Quoted 8 lines
hw/net/virtio-net.c +1
-1@@ -1150,11 +1151,21 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) }if (n->has_vnet_hdr) { -if (out_sg[0 ].iov_len < n->guest_hdr_len) { +if (iov_size (out_sg, out_num) < n->guest_hdr_len) {error_report ("virtio-net header incorrect" );exit (1 ); }
this scans the iov unnecessarily. How about checking return code from iov_to_buf instead?
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 1165
Quoted 9 lines
hw/net/virtio-net.c +8
-1-virtio_net_hdr_swap (vdev, (void *) out_sg[0 ].iov_base); +if (virtio_needs_swap (vdev)) { +iov_to_buf (out_sg, out_num,0 , &hdr,sizeof (hdr)); +virtio_net_hdr_swap (vdev, (void *) &hdr); + sg2[0 ].iov_base = &hdr; + sg2[0 ].iov_len =sizeof (hdr); + out_num =iov_copy (&sg2[1 ],ARRAY_SIZE (sg2) -1 , + out_sg, out_num, +sizeof (hdr),-1 );
This might truncate packet if it does not fit in 1024 anymore. It might be better to just drop it.
Quoted 28 lines
hw/net/virtio-net.c +3
-0+ out_num +=1 ; + out_sg = sg2; + } }/* include/hw/virtio/virtio-access.h +9
-0@@ -143,6 +143,15 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr) } } +static inline bool virtio_needs_swap (VirtIODevice *vdev) +{ +#ifdef HOST_WORDS_BIGENDIAN +return virtio_access_is_big_endian (vdev) ? false : true; +#else +return virtio_access_is_big_endian (vdev) ? true : false; +#endif +} +static inline uint16_t virtio_tswap16 (VirtIODevice *vdev,uint16_t s) {#ifdef HOST_WORDS_BIGENDIAN Signature · 1 line
2.1.4
Quoted 38 lines · Michael S. Tsirkin
Quoted 37 lines · Jason Wang
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 if swap is needed, and then use this buffer as part ofout_sg.Fixes
032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header")---
Changes from V1:
- avoid header copying if there's no need to do header swap
- don't write the header back--- hw/net/virtio-net.c | 17 ++++++++++++++--- include/hw/virtio/virtio-access.h | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)hw/net/virtio-net.c +2
-1@@ -1142,7 +1142,8 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)ssize_t ret, len;unsigned int out_num = elem.out_num;struct iovec *out_sg = &elem.out_sg[0 ]; -struct iovec sg[VIRTQUEUE_MAX_SIZE ]; +struct iovec sg[VIRTQUEUE_MAX_SIZE ], sg2[VIRTQUEUE_MAX_SIZE ]; +struct virtio_net_hdr hdr;if (out_num <1 ) {error_report ("virtio-net header not in first element" );@@ -1150,11 +1151,21 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) }
Quoted 8 lines
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 1157
Quoted 6 lines
hw/net/virtio-net.c +1
-1if (n->has_vnet_hdr) { -if (out_sg[0 ].iov_len < n->guest_hdr_len) { +if (iov_size (out_sg, out_num) < n->guest_hdr_len) {error_report ("virtio-net header incorrect" );exit (1 ); }this scans the iov unnecessarily. How about checking return code from
iov_to_bufinstead?
Looks ok since hdr is very short anyway.
Quoted 11 lines
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 1165
Quoted 10 lines
hw/net/virtio-net.c +8
-1-virtio_net_hdr_swap (vdev, (void *) out_sg[0 ].iov_base); +if (virtio_needs_swap (vdev)) { +iov_to_buf (out_sg, out_num,0 , &hdr,sizeof (hdr)); +virtio_net_hdr_swap (vdev, (void *) &hdr); + sg2[0 ].iov_base = &hdr; + sg2[0 ].iov_len =sizeof (hdr); + out_num =iov_copy (&sg2[1 ],ARRAY_SIZE (sg2) -1 , + out_sg, out_num, +sizeof (hdr),-1 );
Quoted 2 lines
This might truncate packet if it does not fit in 1024 anymore. It might be better to just drop it.
Ok, will do this in V3.
Quoted 29 lines
Quoted 29 lines
hw/net/virtio-net.c +3
-0+ out_num +=1 ; + out_sg = sg2; + } }/* include/hw/virtio/virtio-access.h +9
-0@@ -143,6 +143,15 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr) } } +static inline bool virtio_needs_swap (VirtIODevice *vdev) +{ +#ifdef HOST_WORDS_BIGENDIAN +return virtio_access_is_big_endian (vdev) ? false : true; +#else +return virtio_access_is_big_endian (vdev) ? true : false; +#endif +} +static inline uint16_t virtio_tswap16 (VirtIODevice *vdev,uint16_t s) {#ifdef HOST_WORDS_BIGENDIAN Signature · 1 line
2.1.4
Quoted 50 lines · Jason Wang
Quoted 38 lines · Michael S. Tsirkin
Quoted 37 lines · Jason Wang
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 if swap is needed, and then use this buffer as part ofout_sg.Fixes
032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header")---
Changes from V1:
- avoid header copying if there's no need to do header swap
- don't write the header back--- hw/net/virtio-net.c | 17 ++++++++++++++--- include/hw/virtio/virtio-access.h | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)hw/net/virtio-net.c +2
-1@@ -1142,7 +1142,8 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)ssize_t ret, len;unsigned int out_num = elem.out_num;struct iovec *out_sg = &elem.out_sg[0 ]; -struct iovec sg[VIRTQUEUE_MAX_SIZE ]; +struct iovec sg[VIRTQUEUE_MAX_SIZE ], sg2[VIRTQUEUE_MAX_SIZE ]; +struct virtio_net_hdr hdr;if (out_num <1 ) {error_report ("virtio-net header not in first element" );@@ -1150,11 +1151,21 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) }Quoted 8 lines
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 1157
Quoted 6 lines
hw/net/virtio-net.c +1
-1if (n->has_vnet_hdr) { -if (out_sg[0 ].iov_len < n->guest_hdr_len) { +if (iov_size (out_sg, out_num) < n->guest_hdr_len) {error_report ("virtio-net header incorrect" );exit (1 ); }this scans the iov unnecessarily. How about checking return code from
iov_to_bufinstead?
Quoted 1 line
Looks ok since hdr is very short anyway.
but iov_size scans the whole iov, not just the header.
Quoted 45 lines
Quoted 11 lines
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 1165
Quoted 10 lines
hw/net/virtio-net.c +8
-1-virtio_net_hdr_swap (vdev, (void *) out_sg[0 ].iov_base); +if (virtio_needs_swap (vdev)) { +iov_to_buf (out_sg, out_num,0 , &hdr,sizeof (hdr)); +virtio_net_hdr_swap (vdev, (void *) &hdr); + sg2[0 ].iov_base = &hdr; + sg2[0 ].iov_len =sizeof (hdr); + out_num =iov_copy (&sg2[1 ],ARRAY_SIZE (sg2) -1 , + out_sg, out_num, +sizeof (hdr),-1 );Quoted 2 lines
This might truncate packet if it does not fit in 1024 anymore. It might be better to just drop it.
Ok, will do this in V3.
Quoted 29 lines
Quoted 29 lines
hw/net/virtio-net.c +3
-0+ out_num +=1 ; + out_sg = sg2; + } }/* include/hw/virtio/virtio-access.h +9
-0@@ -143,6 +143,15 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr) } } +static inline bool virtio_needs_swap (VirtIODevice *vdev) +{ +#ifdef HOST_WORDS_BIGENDIAN +return virtio_access_is_big_endian (vdev) ? false : true; +#else +return virtio_access_is_big_endian (vdev) ? true : false; +#endif +} +static inline uint16_t virtio_tswap16 (VirtIODevice *vdev,uint16_t s) {#ifdef HOST_WORDS_BIGENDIAN Signature · 1 line
2.1.4
Quoted 51 lines · Michael S. Tsirkin
Quoted 50 lines · Jason Wang
Quoted 50 lines · Michael S. Tsirkin
Quoted 46 lines
Quoted 46 lines · Jason Wang
Quoted 37 lines
Quoted 37 lines
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 if swap is needed, and then use this buffer as part ofout_sg.Fixes
032a74a1c0fcdd5fd1c69e56126b4c857ee36611
("virtio-net: byteswap virtio-net header")---
Changes from V1:
- avoid header copying if there's no need to do header swap
- don't write the header back--- hw/net/virtio-net.c | 17 ++++++++++++++--- include/hw/virtio/virtio-access.h | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)hw/net/virtio-net.c +2
-1@@ -1142,7 +1142,8 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)ssize_t ret, len;unsigned int out_num = elem.out_num;struct iovec *out_sg = &elem.out_sg[0 ]; -struct iovec sg[VIRTQUEUE_MAX_SIZE ]; +struct iovec sg[VIRTQUEUE_MAX_SIZE ], sg2[VIRTQUEUE_MAX_SIZE ]; +struct virtio_net_hdr hdr;if (out_num <1 ) {error_report ("virtio-net header not in first element" );@@ -1150,11 +1151,21 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) }Quoted 6 lines
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 1157
Quoted 6 lines
hw/net/virtio-net.c +1
-1if (n->has_vnet_hdr) { -if (out_sg[0 ].iov_len < n->guest_hdr_len) { +if (iov_size (out_sg, out_num) < n->guest_hdr_len) {error_report ("virtio-net header incorrect" );exit (1 ); }this scans the iov unnecessarily. How about checking return code from
iov_to_bufinstead?
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Looks ok since hdr is very short anyway.
but
iov_sizescans the whole iov, not just the header.
Right.