qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Akihiko Odaki <akihiko.odaki@daynix.com>
To: Jason Wang <jasowang@redhat.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>, qemu-devel@nongnu.org
Subject: Re: [PATCH 6/7] virtio-net: Copy received header to buffer
Date: Tue, 22 Oct 2024 15:37:18 +0900	[thread overview]
Message-ID: <e5d99f4c-f2d7-42bd-be8d-4cf611c3f425@daynix.com> (raw)
In-Reply-To: <CACGkMEvtNPQk9gU7KPsTN=4ghdiZWyV2feYpEj+_1uAGvrMvKw@mail.gmail.com>

On 2024/10/21 17:22, Jason Wang wrote:
> On Mon, Oct 21, 2024 at 4:21 PM Jason Wang <jasowang@redhat.com> wrote:
>>
>> On Sun, Sep 15, 2024 at 9:07 AM Akihiko Odaki <akihiko.odaki@daynix.com> wrote:
>>>
>>> receive_header() used to cast the const qualifier of the pointer to the
>>> received packet away to modify the header. Avoid this by copying the
>>> received header to buffer.
>>>
>>> Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
>>> ---
>>>   hw/net/virtio-net.c | 85 +++++++++++++++++++++++++++++------------------------
>>>   1 file changed, 46 insertions(+), 39 deletions(-)
>>>
>>> diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
>>> index 3fc1d10cb9e0..ca4e22344f78 100644
>>> --- a/hw/net/virtio-net.c
>>> +++ b/hw/net/virtio-net.c
>>> @@ -1685,41 +1685,44 @@ static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
>>>    * cache.
>>>    */
>>>   static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
>>> -                                        uint8_t *buf, size_t size)
>>> +                                        size_t *hdr_len, const uint8_t *buf,
>>> +                                        size_t buf_size, size_t *buf_offset)
>>>   {
>>>       size_t csum_size = ETH_HLEN + sizeof(struct ip_header) +
>>>                          sizeof(struct udp_header);
>>>
>>> +    buf += *buf_offset;
>>> +    buf_size -= *buf_offset;
>>> +
>>>       if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
>>> -        (size >= csum_size && size < 1500) && /* normal sized MTU */
>>> +        (buf_size >= csum_size && buf_size < 1500) && /* normal sized MTU */
>>>           (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
>>>           (buf[23] == 17) && /* ip.protocol == UDP */
>>>           (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
>>> -        net_checksum_calculate(buf, size, CSUM_UDP);
>>> +        memcpy((uint8_t *)hdr + *hdr_len, buf, csum_size);
>>> +        net_checksum_calculate((uint8_t *)hdr + *hdr_len, csum_size, CSUM_UDP);
>>>           hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
>>> +        *hdr_len += csum_size;
>>> +        *buf_offset += csum_size;
>>>       }
>>>   }
>>>
>>> -static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
>>> -                           const void *buf, size_t size)
>>> +static size_t receive_header(VirtIONet *n, struct virtio_net_hdr *hdr,
>>> +                             const void *buf, size_t buf_size,
>>> +                             size_t *buf_offset)
>>>   {
>>> -    if (n->has_vnet_hdr) {
>>> -        /* FIXME this cast is evil */
>>> -        void *wbuf = (void *)buf;
>>> -        work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
>>> -                                    size - n->host_hdr_len);
>>> +    size_t hdr_len = n->guest_hdr_len;
>>>
>>> -        if (n->needs_vnet_hdr_swap) {
>>> -            virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
>>> -        }
>>> -        iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
>>> -    } else {
>>> -        struct virtio_net_hdr hdr = {
>>> -            .flags = 0,
>>> -            .gso_type = VIRTIO_NET_HDR_GSO_NONE
>>> -        };
>>> -        iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
>>> +    memcpy(hdr, buf, sizeof(struct virtio_net_hdr));
>>> +
>>> +    *buf_offset = n->host_hdr_len;
>>> +    work_around_broken_dhclient(hdr, &hdr_len, buf, buf_size, buf_offset);
>>> +
>>> +    if (n->needs_vnet_hdr_swap) {
>>> +        virtio_net_hdr_swap(VIRTIO_DEVICE(n), hdr);
>>>       }
>>> +
>>> +    return hdr_len;
>>>   }
>>>
>>>   static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
>>> @@ -1887,6 +1890,13 @@ static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
>>>       return (index == new_index) ? -1 : new_index;
>>>   }
>>>
>>> +typedef struct Header {
>>> +    struct virtio_net_hdr_v1_hash virtio_net;
>>> +    struct eth_header eth;
>>> +    struct ip_header ip;
>>> +    struct udp_header udp;
>>> +} Header;
>>
>> I don't see too much value in having this structure especially
>> considering eth/ip/udp is not even used.
>>
>> Any reason we can simply use an array as a buffer in virtio_net_receive_rcu()?

We still need to access virtio_net and it needs to be properly aligned. 
We can still make the latter part an array, but it does not look nicer:

typedef struct Header {
     struct virtio_net_hdr_v1_hash virtio_net;
     uint8_t eth[sizeof(struct eth_header) + sizeof(struct ip_header) + 
sizeof(struct udp_header)];
} Header;

> 
> Btw, since the code was for ancient guests and userspace, I wonder how
> it is tested.

I didn't test with dhclient, but I tested with UDP packets synthesized 
with iperf.

Regards,
Akihiko Odaki


  reply	other threads:[~2024-10-22  6:37 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-15  1:06 [PATCH 0/7] virtio-net fixes Akihiko Odaki
2024-09-15  1:06 ` [PATCH 1/7] net: checksum: Convert data to void * Akihiko Odaki
2024-09-15  1:06 ` [PATCH 2/7] virtio-net: Fix size check in dhclient workaround Akihiko Odaki
2024-09-15  1:06 ` [PATCH 3/7] virtio-net: Do not check for the queue before RSS Akihiko Odaki
2024-10-21  8:23   ` Jason Wang
2024-09-15  1:06 ` [PATCH 4/7] virtio-net: Fix hash reporting when the queue changes Akihiko Odaki
2024-09-15  1:06 ` [PATCH 5/7] virtio-net: Initialize hash reporting values Akihiko Odaki
2024-09-15  1:06 ` [PATCH 6/7] virtio-net: Copy received header to buffer Akihiko Odaki
2024-10-21  8:21   ` Jason Wang
2024-10-21  8:22     ` Jason Wang
2024-10-22  6:37       ` Akihiko Odaki [this message]
2024-09-15  1:06 ` [PATCH 7/7] virtio-net: Fix num_buffers for version 1 Akihiko Odaki
2024-10-15 17:58 ` [PATCH 0/7] virtio-net fixes Michael Tokarev
2024-10-17  6:52   ` Jason Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e5d99f4c-f2d7-42bd-be8d-4cf611c3f425@daynix.com \
    --to=akihiko.odaki@daynix.com \
    --cc=jasowang@redhat.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).