On Mon, Mar 09, 2026 at 10:47:40AM +0100, Laurent Vivier wrote:
Refactor vu_set_vnethdr() to take an iov_tail pointer instead of a direct pointer to the virtio_net_hdr_mrg_rxbuf structure. This makes the function use IOV_PEEK_HEADER() and IOV_PUT_HEADER() to read and write the virtio-net header through the iov_tail abstraction.
Signed-off-by: Laurent Vivier
Reviewed-by: David Gibson
--- tcp_vu.c | 8 +++++--- udp_vu.c | 3 +-- vu_common.c | 21 +++++++++++++++------ vu_common.h | 2 +- 4 files changed, 22 insertions(+), 12 deletions(-)
diff --git a/tcp_vu.c b/tcp_vu.c index 8da2dcfe78d0..688f48905d46 100644 --- a/tcp_vu.c +++ b/tcp_vu.c @@ -94,10 +94,11 @@ int tcp_vu_send_flag(const struct ctx *c, struct tcp_tap_conn *conn, int flags) if (elem_cnt != 1) return -1;
- ASSERT(flags_elem[0].in_sg[0].iov_len >= + payload = IOV_TAIL(&flags_elem[0].in_sg[0], elem_cnt, 0); + ASSERT(iov_tail_size(&payload) >= MAX(hdrlen + sizeof(*opts), ETH_ZLEN + VNET_HLEN));
- vu_set_vnethdr(flags_elem[0].in_sg[0].iov_base, 1); + vu_set_vnethdr(&payload, 1);
eh = vu_eth(flags_elem[0].in_sg[0].iov_base);
@@ -454,6 +455,7 @@ int tcp_vu_data_from_sock(const struct ctx *c, struct tcp_tap_conn *conn) for (i = 0, previous_dlen = -1, check = NULL; i < head_cnt; i++) { struct iovec *iov = &elem[head[i]].in_sg[0]; int buf_cnt = head[i + 1] - head[i]; + struct iov_tail data = IOV_TAIL(iov, buf_cnt, 0); size_t frame_size = iov_size(iov, buf_cnt); bool push = i == head_cnt - 1; ssize_t dlen; @@ -461,7 +463,7 @@ int tcp_vu_data_from_sock(const struct ctx *c, struct tcp_tap_conn *conn) ASSERT(frame_size >= hdrlen);
dlen = frame_size - hdrlen; - vu_set_vnethdr(iov->iov_base, buf_cnt); + vu_set_vnethdr(&data, buf_cnt);
/* The IPv4 header checksum varies only with dlen */ if (previous_dlen != dlen) diff --git a/udp_vu.c b/udp_vu.c index a21a03dbf23e..414750ff742a 100644 --- a/udp_vu.c +++ b/udp_vu.c @@ -233,8 +233,7 @@ void udp_vu_sock_to_tap(const struct ctx *c, int s, int n, flow_sidx_t tosidx) vu_queue_rewind(vq, elem_cnt - elem_used); if (iov_cnt > 0) { struct iov_tail data = IOV_TAIL(iov_vu, iov_cnt, 0); - vu_set_vnethdr(iov_vu[0].iov_base, elem_used); - iov_drop_header(&data, VNET_HLEN); + vu_set_vnethdr(&data, elem_used); udp_vu_prepare(c, &data, toside); if (*c->pcap) { udp_vu_csum(toside, &data); diff --git a/vu_common.c b/vu_common.c index 8afa5199908f..3538e59581b7 100644 --- a/vu_common.c +++ b/vu_common.c @@ -120,17 +120,24 @@ int vu_collect(const struct vu_dev *vdev, struct vu_virtq *vq, }
/** - * vu_set_vnethdr() - set virtio-net headers - * @vnethdr: Address of the header to set + * vu_set_vnethdr() - set virtio-net header + * @data: IOV tail to write header to, updated to + * point after the virtio-net header * @num_buffers: Number of guest buffers of the frame */ -void vu_set_vnethdr(struct virtio_net_hdr_mrg_rxbuf *vnethdr, int num_buffers) +void vu_set_vnethdr(struct iov_tail *data, int num_buffers) { + struct virtio_net_hdr_mrg_rxbuf vnethdr_storage, *vnethdr; + + vnethdr = IOV_PEEK_HEADER(data, vnethdr_storage); + vnethdr->hdr = VU_HEADER; /* Note: if VIRTIO_NET_F_MRG_RXBUF is not negotiated, * num_buffers must be 1 */ vnethdr->num_buffers = htole16(num_buffers); + + IOV_PUT_HEADER(data, vnethdr); }
/** @@ -267,6 +274,7 @@ int vu_send_single(const struct ctx *c, const void *buf, size_t size) struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE]; struct vu_virtq_element elem[VIRTQUEUE_MAX_SIZE]; struct iovec in_sg[VIRTQUEUE_MAX_SIZE]; + struct iov_tail data; size_t total; int elem_cnt; int i; @@ -295,15 +303,16 @@ int vu_send_single(const struct ctx *c, const void *buf, size_t size) } elem_cnt = iov_truncate(in_sg, elem_cnt, size);
- vu_set_vnethdr(in_sg[0].iov_base, elem_cnt); + data = IOV_TAIL(&in_sg[0], elem_cnt, 0); + vu_set_vnethdr(&data, elem_cnt);
size -= VNET_HLEN;
/* copy data from the buffer to the iovec */ - iov_from_buf(in_sg, elem_cnt, VNET_HLEN, buf, size); + iov_from_buf(in_sg, elem_cnt, data.off, buf, size);
if (*c->pcap) - pcap_iov(in_sg, elem_cnt, VNET_HLEN); + pcap_iov(data.iov, data.cnt, data.off);
vu_flush(vdev, vq, elem, elem_cnt);
diff --git a/vu_common.h b/vu_common.h index 5de0c987b936..d068c61695f8 100644 --- a/vu_common.h +++ b/vu_common.h @@ -55,7 +55,7 @@ void vu_init_elem(struct vu_virtq_element *elem, struct iovec *iov, int vu_collect(const struct vu_dev *vdev, struct vu_virtq *vq, struct vu_virtq_element *elem, int max_elem, size_t size, size_t *collected); -void vu_set_vnethdr(struct virtio_net_hdr_mrg_rxbuf *vnethdr, int num_buffers); +void vu_set_vnethdr(struct iov_tail *data, int num_buffers); void vu_flush(const struct vu_dev *vdev, struct vu_virtq *vq, struct vu_virtq_element *elem, int elem_cnt); void vu_kick_cb(struct vu_dev *vdev, union epoll_ref ref, -- 2.53.0
-- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson