virtio-net: Copy received header to buffer

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>
Signed-off-by: Jason Wang <jasowang@redhat.com>
This commit is contained in:
Akihiko Odaki 2024-11-22 14:03:12 +09:00 committed by Jason Wang
parent 17437418c4
commit 7987d2be5a
1 changed files with 46 additions and 39 deletions

View File

@ -1695,41 +1695,44 @@ static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
* cache. * cache.
*/ */
static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 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) + size_t csum_size = ETH_HLEN + sizeof(struct ip_header) +
sizeof(struct udp_header); sizeof(struct udp_header);
buf += *buf_offset;
buf_size -= *buf_offset;
if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 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[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
(buf[23] == 17) && /* ip.protocol == UDP */ (buf[23] == 17) && /* ip.protocol == UDP */
(buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ (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->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, static size_t receive_header(VirtIONet *n, struct virtio_net_hdr *hdr,
const void *buf, size_t size) const void *buf, size_t buf_size,
size_t *buf_offset)
{ {
if (n->has_vnet_hdr) { size_t hdr_len = n->guest_hdr_len;
/* FIXME this cast is evil */
void *wbuf = (void *)buf;
work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
size - n->host_hdr_len);
if (n->needs_vnet_hdr_swap) { memcpy(hdr, buf, sizeof(struct virtio_net_hdr));
virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
} *buf_offset = n->host_hdr_len;
iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); work_around_broken_dhclient(hdr, &hdr_len, buf, buf_size, buf_offset);
} else {
struct virtio_net_hdr hdr = { if (n->needs_vnet_hdr_swap) {
.flags = 0, virtio_net_hdr_swap(VIRTIO_DEVICE(n), hdr);
.gso_type = VIRTIO_NET_HDR_GSO_NONE
};
iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
} }
return hdr_len;
} }
static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
@ -1897,6 +1900,13 @@ static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
return (index == new_index) ? -1 : new_index; 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;
static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
size_t size) size_t size)
{ {
@ -1906,15 +1916,15 @@ static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE]; VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE];
size_t lens[VIRTQUEUE_MAX_SIZE]; size_t lens[VIRTQUEUE_MAX_SIZE];
struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
struct virtio_net_hdr_v1_hash extra_hdr; Header hdr;
unsigned mhdr_cnt = 0; unsigned mhdr_cnt = 0;
size_t offset, i, guest_offset, j; size_t offset, i, guest_offset, j;
ssize_t err; ssize_t err;
memset(&extra_hdr, 0, sizeof(extra_hdr)); memset(&hdr.virtio_net, 0, sizeof(hdr.virtio_net));
if (n->rss_data.enabled && n->rss_data.enabled_software_rss) { if (n->rss_data.enabled && n->rss_data.enabled_software_rss) {
int index = virtio_net_process_rss(nc, buf, size, &extra_hdr); int index = virtio_net_process_rss(nc, buf, size, &hdr.virtio_net);
if (index >= 0) { if (index >= 0) {
nc = qemu_get_subqueue(n->nic, index % n->curr_queue_pairs); nc = qemu_get_subqueue(n->nic, index % n->curr_queue_pairs);
} }
@ -1979,21 +1989,18 @@ static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
if (n->mergeable_rx_bufs) { if (n->mergeable_rx_bufs) {
mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
sg, elem->in_num, sg, elem->in_num,
offsetof(typeof(extra_hdr), hdr.num_buffers), offsetof(typeof(hdr),
sizeof(extra_hdr.hdr.num_buffers)); virtio_net.hdr.num_buffers),
sizeof(hdr.virtio_net.hdr.num_buffers));
} }
receive_header(n, sg, elem->in_num, buf, size); guest_offset = n->has_vnet_hdr ?
if (n->rss_data.populate_hash) { receive_header(n, (struct virtio_net_hdr *)&hdr,
offset = offsetof(typeof(extra_hdr), hash_value); buf, size, &offset) :
iov_from_buf(sg, elem->in_num, offset, n->guest_hdr_len;
(char *)&extra_hdr + offset,
sizeof(extra_hdr.hash_value) + iov_from_buf(sg, elem->in_num, 0, &hdr, guest_offset);
sizeof(extra_hdr.hash_report)); total += guest_offset;
}
offset = n->host_hdr_len;
total += n->guest_hdr_len;
guest_offset = n->guest_hdr_len;
} else { } else {
guest_offset = 0; guest_offset = 0;
} }
@ -2019,11 +2026,11 @@ static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
} }
if (mhdr_cnt) { if (mhdr_cnt) {
virtio_stw_p(vdev, &extra_hdr.hdr.num_buffers, i); virtio_stw_p(vdev, &hdr.virtio_net.hdr.num_buffers, i);
iov_from_buf(mhdr_sg, mhdr_cnt, iov_from_buf(mhdr_sg, mhdr_cnt,
0, 0,
&extra_hdr.hdr.num_buffers, &hdr.virtio_net.hdr.num_buffers,
sizeof extra_hdr.hdr.num_buffers); sizeof hdr.virtio_net.hdr.num_buffers);
} }
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {