* [PATCH v7 1/8] packet: replace struct desc by struct iovec
2024-10-09 9:07 [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Laurent Vivier
@ 2024-10-09 9:07 ` Laurent Vivier
2024-10-09 9:07 ` [PATCH v7 2/8] vhost-user: introduce virtio API Laurent Vivier
` (8 subsequent siblings)
9 siblings, 0 replies; 18+ messages in thread
From: Laurent Vivier @ 2024-10-09 9:07 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
To be able to manage buffers inside a shared memory provided
by a VM via a vhost-user interface, we cannot rely on the fact
that buffers are located in a pre-defined memory area and use
a base address and a 32bit offset to address them.
We need a 64bit address, so replace struct desc by struct iovec
and update range checking.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
packet.c | 80 ++++++++++++++++++++++++++++++--------------------------
packet.h | 14 ++--------
2 files changed, 45 insertions(+), 49 deletions(-)
diff --git a/packet.c b/packet.c
index ccfc84607709..37489961a37e 100644
--- a/packet.c
+++ b/packet.c
@@ -22,6 +22,35 @@
#include "util.h"
#include "log.h"
+/**
+ * packet_check_range() - Check if a packet memory range is valid
+ * @p: Packet pool
+ * @offset: Offset of data range in packet descriptor
+ * @len: Length of desired data range
+ * @start: Start of the packet descriptor
+ * @func: For tracing: name of calling function
+ * @line: For tracing: caller line of function call
+ *
+ * Return: 0 if the range is valid, -1 otherwise
+ */
+static int packet_check_range(const struct pool *p, size_t offset, size_t len,
+ const char *start, const char *func, int line)
+{
+ if (start < p->buf) {
+ trace("packet start %p before buffer start %p, "
+ "%s:%i", (void *)start, (void *)p->buf, func, line);
+ return -1;
+ }
+
+ if (start + len + offset > p->buf + p->buf_size) {
+ trace("packet offset plus length %lu from size %lu, "
+ "%s:%i", start - p->buf + len + offset,
+ p->buf_size, func, line);
+ return -1;
+ }
+
+ return 0;
+}
/**
* packet_add_do() - Add data as packet descriptor to given pool
* @p: Existing pool
@@ -41,34 +70,16 @@ void packet_add_do(struct pool *p, size_t len, const char *start,
return;
}
- if (start < p->buf) {
- trace("add packet start %p before buffer start %p, %s:%i",
- (void *)start, (void *)p->buf, func, line);
+ if (packet_check_range(p, 0, len, start, func, line))
return;
- }
-
- if (start + len > p->buf + p->buf_size) {
- trace("add packet start %p, length: %zu, buffer end %p, %s:%i",
- (void *)start, len, (void *)(p->buf + p->buf_size),
- func, line);
- return;
- }
if (len > UINT16_MAX) {
trace("add packet length %zu, %s:%i", len, func, line);
return;
}
-#if UINTPTR_MAX == UINT64_MAX
- if ((uintptr_t)start - (uintptr_t)p->buf > UINT32_MAX) {
- trace("add packet start %p, buffer start %p, %s:%i",
- (void *)start, (void *)p->buf, func, line);
- return;
- }
-#endif
-
- p->pkt[idx].offset = start - p->buf;
- p->pkt[idx].len = len;
+ p->pkt[idx].iov_base = (void *)start;
+ p->pkt[idx].iov_len = len;
p->count++;
}
@@ -96,36 +107,31 @@ void *packet_get_do(const struct pool *p, size_t idx, size_t offset,
return NULL;
}
- if (len > UINT16_MAX || len + offset > UINT32_MAX) {
+ if (len > UINT16_MAX) {
if (func) {
- trace("packet data length %zu, offset %zu, %s:%i",
- len, offset, func, line);
+ trace("packet data length %zu, %s:%i",
+ len, func, line);
}
return NULL;
}
- if (p->pkt[idx].offset + len + offset > p->buf_size) {
+ if (len + offset > p->pkt[idx].iov_len) {
if (func) {
- trace("packet offset plus length %zu from size %zu, "
- "%s:%i", p->pkt[idx].offset + len + offset,
- p->buf_size, func, line);
+ trace("data length %zu, offset %zu from length %zu, "
+ "%s:%i", len, offset, p->pkt[idx].iov_len,
+ func, line);
}
return NULL;
}
- if (len + offset > p->pkt[idx].len) {
- if (func) {
- trace("data length %zu, offset %zu from length %u, "
- "%s:%i", len, offset, p->pkt[idx].len,
- func, line);
- }
+ if (packet_check_range(p, offset, len, p->pkt[idx].iov_base,
+ func, line))
return NULL;
- }
if (left)
- *left = p->pkt[idx].len - offset - len;
+ *left = p->pkt[idx].iov_len - offset - len;
- return p->buf + p->pkt[idx].offset + offset;
+ return (char *)p->pkt[idx].iov_base + offset;
}
/**
diff --git a/packet.h b/packet.h
index a784b07bbed5..8377dcf678bb 100644
--- a/packet.h
+++ b/packet.h
@@ -6,16 +6,6 @@
#ifndef PACKET_H
#define PACKET_H
-/**
- * struct desc - Generic offset-based descriptor within buffer
- * @offset: Offset of descriptor relative to buffer start, 32-bit limit
- * @len: Length of descriptor, host order, 16-bit limit
- */
-struct desc {
- uint32_t offset;
- uint16_t len;
-};
-
/**
* struct pool - Generic pool of packets stored in a buffer
* @buf: Buffer storing packet descriptors
@@ -29,7 +19,7 @@ struct pool {
size_t buf_size;
size_t size;
size_t count;
- struct desc pkt[1];
+ struct iovec pkt[1];
};
void packet_add_do(struct pool *p, size_t len, const char *start,
@@ -54,7 +44,7 @@ struct _name ## _t { \
size_t buf_size; \
size_t size; \
size_t count; \
- struct desc pkt[_size]; \
+ struct iovec pkt[_size]; \
}
#define PACKET_POOL_INIT_NOCAST(_size, _buf, _buf_size) \
--
@@ -6,16 +6,6 @@
#ifndef PACKET_H
#define PACKET_H
-/**
- * struct desc - Generic offset-based descriptor within buffer
- * @offset: Offset of descriptor relative to buffer start, 32-bit limit
- * @len: Length of descriptor, host order, 16-bit limit
- */
-struct desc {
- uint32_t offset;
- uint16_t len;
-};
-
/**
* struct pool - Generic pool of packets stored in a buffer
* @buf: Buffer storing packet descriptors
@@ -29,7 +19,7 @@ struct pool {
size_t buf_size;
size_t size;
size_t count;
- struct desc pkt[1];
+ struct iovec pkt[1];
};
void packet_add_do(struct pool *p, size_t len, const char *start,
@@ -54,7 +44,7 @@ struct _name ## _t { \
size_t buf_size; \
size_t size; \
size_t count; \
- struct desc pkt[_size]; \
+ struct iovec pkt[_size]; \
}
#define PACKET_POOL_INIT_NOCAST(_size, _buf, _buf_size) \
--
2.46.2
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH v7 2/8] vhost-user: introduce virtio API
2024-10-09 9:07 [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Laurent Vivier
2024-10-09 9:07 ` [PATCH v7 1/8] packet: replace struct desc by struct iovec Laurent Vivier
@ 2024-10-09 9:07 ` Laurent Vivier
2024-10-09 9:07 ` [PATCH v7 3/8] vhost-user: introduce vhost-user API Laurent Vivier
` (7 subsequent siblings)
9 siblings, 0 replies; 18+ messages in thread
From: Laurent Vivier @ 2024-10-09 9:07 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Add virtio.c and virtio.h that define the functions needed
to manage virtqueues.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
Makefile | 4 +-
util.h | 8 +
virtio.c | 665 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
virtio.h | 183 +++++++++++++++
4 files changed, 858 insertions(+), 2 deletions(-)
create mode 100644 virtio.c
create mode 100644 virtio.h
diff --git a/Makefile b/Makefile
index 74a95130d082..a2258891f104 100644
--- a/Makefile
+++ b/Makefile
@@ -54,7 +54,7 @@ FLAGS += -DDUAL_STACK_SOCKETS=$(DUAL_STACK_SOCKETS)
PASST_SRCS = arch.c arp.c checksum.c conf.c dhcp.c dhcpv6.c flow.c fwd.c \
icmp.c igmp.c inany.c iov.c ip.c isolation.c lineread.c log.c mld.c \
ndp.c netlink.c packet.c passt.c pasta.c pcap.c pif.c tap.c tcp.c \
- tcp_buf.c tcp_splice.c udp.c udp_flow.c util.c
+ tcp_buf.c tcp_splice.c udp.c udp_flow.c util.c virtio.c
QRAP_SRCS = qrap.c
SRCS = $(PASST_SRCS) $(QRAP_SRCS)
@@ -64,7 +64,7 @@ PASST_HEADERS = arch.h arp.h checksum.h conf.h dhcp.h dhcpv6.h flow.h fwd.h \
flow_table.h icmp.h icmp_flow.h inany.h iov.h ip.h isolation.h \
lineread.h log.h ndp.h netlink.h packet.h passt.h pasta.h pcap.h pif.h \
siphash.h tap.h tcp.h tcp_buf.h tcp_conn.h tcp_internal.h tcp_splice.h \
- udp.h udp_flow.h util.h
+ udp.h udp_flow.h util.h virtio.h
HEADERS = $(PASST_HEADERS) seccomp.h
C := \#include <linux/tcp.h>\nstruct tcp_info x = { .tcpi_snd_wnd = 0 };
diff --git a/util.h b/util.h
index 2c1e08e0c488..def145c997b7 100644
--- a/util.h
+++ b/util.h
@@ -131,6 +131,14 @@ static inline uint32_t ntohl_unaligned(const void *p)
return ntohl(val);
}
+static inline void barrier(void) { __asm__ __volatile__("" ::: "memory"); }
+#define smp_mb() do { barrier(); __atomic_thread_fence(__ATOMIC_SEQ_CST); } while (0)
+#define smp_mb_release() do { barrier(); __atomic_thread_fence(__ATOMIC_RELEASE); } while (0)
+#define smp_mb_acquire() do { barrier(); __atomic_thread_fence(__ATOMIC_ACQUIRE); } while (0)
+
+#define smp_wmb() smp_mb_release()
+#define smp_rmb() smp_mb_acquire()
+
#define NS_FN_STACK_SIZE (RLIMIT_STACK_VAL * 1024 / 8)
int do_clone(int (*fn)(void *), char *stack_area, size_t stack_size, int flags,
void *arg);
diff --git a/virtio.c b/virtio.c
new file mode 100644
index 000000000000..380590afbca3
--- /dev/null
+++ b/virtio.c
@@ -0,0 +1,665 @@
+// SPDX-License-Identifier: GPL-2.0-or-later AND BSD-3-Clause
+/*
+ * virtio API, vring and virtqueue functions definition
+ *
+ * Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ */
+
+/* Some parts copied from QEMU subprojects/libvhost-user/libvhost-user.c
+ * originally licensed under the following terms:
+ *
+ * --
+ *
+ * Copyright IBM, Corp. 2007
+ * Copyright (c) 2016 Red Hat, Inc.
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.ibm.com>
+ * Marc-André Lureau <mlureau@redhat.com>
+ * Victor Kaplansky <victork@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later. See the COPYING file in the top-level directory.
+ *
+ * Some parts copied from QEMU hw/virtio/virtio.c
+ * licensed under the following terms:
+ *
+ * Copyright IBM, Corp. 2007
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ * --
+ *
+ * virtq_used_event() and virtq_avail_event() from
+ * https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-712000A
+ * licensed under the following terms:
+ *
+ * --
+ *
+ * This header is BSD licensed so anyone can use the definitions
+ * to implement compatible drivers/servers.
+ *
+ * Copyright 2007, 2009, IBM Corporation
+ * Copyright 2011, Red Hat, Inc
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of IBM nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ‘‘AS IS’’ AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stddef.h>
+#include <endian.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/eventfd.h>
+#include <sys/socket.h>
+
+#include "util.h"
+#include "virtio.h"
+
+#define VIRTQUEUE_MAX_SIZE 1024
+
+/**
+ * vu_gpa_to_va() - Translate guest physical address to our virtual address.
+ * @dev: Vhost-user device
+ * @plen: Physical length to map (input), capped to region (output)
+ * @guest_addr: Guest physical address
+ *
+ * Return: virtual address in our address space of the guest physical address
+ */
+static void *vu_gpa_to_va(struct vu_dev *dev, uint64_t *plen, uint64_t guest_addr)
+{
+ unsigned int i;
+
+ if (*plen == 0)
+ return NULL;
+
+ /* Find matching memory region. */
+ for (i = 0; i < dev->nregions; i++) {
+ const struct vu_dev_region *r = &dev->regions[i];
+
+ if ((guest_addr >= r->gpa) &&
+ (guest_addr < (r->gpa + r->size))) {
+ if ((guest_addr + *plen) > (r->gpa + r->size))
+ *plen = r->gpa + r->size - guest_addr;
+ /* NOLINTNEXTLINE(performance-no-int-to-ptr) */
+ return (void *)(guest_addr - r->gpa + r->mmap_addr +
+ r->mmap_offset);
+ }
+ }
+
+ return NULL;
+}
+
+/**
+ * vring_avail_flags() - Read the available ring flags
+ * @vq: Virtqueue
+ *
+ * Return: the available ring descriptor flags of the given virtqueue
+ */
+static inline uint16_t vring_avail_flags(const struct vu_virtq *vq)
+{
+ return le16toh(vq->vring.avail->flags);
+}
+
+/**
+ * vring_avail_idx() - Read the available ring index
+ * @vq: Virtqueue
+ *
+ * Return: the available ring index of the given virtqueue
+ */
+static inline uint16_t vring_avail_idx(struct vu_virtq *vq)
+{
+ vq->shadow_avail_idx = le16toh(vq->vring.avail->idx);
+
+ return vq->shadow_avail_idx;
+}
+
+/**
+ * vring_avail_ring() - Read an available ring entry
+ * @vq: Virtqueue
+ * @i: Index of the entry to read
+ *
+ * Return: the ring entry content (head of the descriptor chain)
+ */
+static inline uint16_t vring_avail_ring(const struct vu_virtq *vq, int i)
+{
+ return le16toh(vq->vring.avail->ring[i]);
+}
+
+/**
+ * virtq_used_event - Get location of used event indices
+ * (only with VIRTIO_F_EVENT_IDX)
+ * @vq Virtqueue
+ *
+ * Return: return the location of the used event index
+ */
+static inline uint16_t *virtq_used_event(const struct vu_virtq *vq)
+{
+ /* For backwards compat, used event index is at *end* of avail ring. */
+ return &vq->vring.avail->ring[vq->vring.num];
+}
+
+/**
+ * vring_get_used_event() - Get the used event from the available ring
+ * @vq Virtqueue
+ *
+ * Return: the used event (available only if VIRTIO_RING_F_EVENT_IDX is set)
+ * used_event is a performant alternative where the driver
+ * specifies how far the device can progress before a notification
+ * is required.
+ */
+static inline uint16_t vring_get_used_event(const struct vu_virtq *vq)
+{
+ return le16toh(*virtq_used_event(vq));
+}
+
+/**
+ * virtqueue_get_head() - Get the head of the descriptor chain for a given
+ * index
+ * @vq: Virtqueue
+ * @idx: Available ring entry index
+ * @head: Head of the descriptor chain
+ */
+static void virtqueue_get_head(const struct vu_virtq *vq,
+ unsigned int idx, unsigned int *head)
+{
+ /* Grab the next descriptor number they're advertising, and increment
+ * the index we've seen.
+ */
+ *head = vring_avail_ring(vq, idx % vq->vring.num);
+
+ /* If their number is silly, that's a fatal mistake. */
+ if (*head >= vq->vring.num)
+ die("vhost-user: Guest says index %u is available", *head);
+}
+
+/**
+ * virtqueue_read_indirect_desc() - Copy virtio ring descriptors from guest
+ * memory
+ * @dev: Vhost-user device
+ * @desc: Destination address to copy the descriptors to
+ * @addr: Guest memory address to copy from
+ * @len: Length of memory to copy
+ *
+ * Return: -1 if there is an error, 0 otherwise
+ */
+static int virtqueue_read_indirect_desc(struct vu_dev *dev, struct vring_desc *desc,
+ uint64_t addr, size_t len)
+{
+ uint64_t read_len;
+
+ if (len > (VIRTQUEUE_MAX_SIZE * sizeof(struct vring_desc)))
+ return -1;
+
+ if (len == 0)
+ return -1;
+
+ while (len) {
+ const struct vring_desc *orig_desc;
+
+ read_len = len;
+ orig_desc = vu_gpa_to_va(dev, &read_len, addr);
+ if (!orig_desc)
+ return -1;
+
+ memcpy(desc, orig_desc, read_len);
+ len -= read_len;
+ addr += read_len;
+ desc += read_len / sizeof(struct vring_desc);
+ }
+
+ return 0;
+}
+
+/**
+ * enum virtqueue_read_desc_state - State in the descriptor chain
+ * @VIRTQUEUE_READ_DESC_ERROR Found an invalid descriptor
+ * @VIRTQUEUE_READ_DESC_DONE No more descriptors in the chain
+ * @VIRTQUEUE_READ_DESC_MORE there are more descriptors in the chain
+ */
+enum virtqueue_read_desc_state {
+ VIRTQUEUE_READ_DESC_ERROR = -1,
+ VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */
+ VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */
+};
+
+/**
+ * virtqueue_read_next_desc() - Read the the next descriptor in the chain
+ * @desc: Virtio ring descriptors
+ * @i: Index of the current descriptor
+ * @max: Maximum value of the descriptor index
+ * @next: Index of the next descriptor in the chain (output value)
+ *
+ * Return: current chain descriptor state (error, next, done)
+ */
+static int virtqueue_read_next_desc(const struct vring_desc *desc,
+ int i, unsigned int max, unsigned int *next)
+{
+ /* If this descriptor says it doesn't chain, we're done. */
+ if (!(le16toh(desc[i].flags) & VRING_DESC_F_NEXT))
+ return VIRTQUEUE_READ_DESC_DONE;
+
+ /* Check they're not leading us off end of descriptors. */
+ *next = le16toh(desc[i].next);
+ /* Make sure compiler knows to grab that: we don't want it changing! */
+ smp_wmb();
+
+ if (*next >= max)
+ return VIRTQUEUE_READ_DESC_ERROR;
+
+ return VIRTQUEUE_READ_DESC_MORE;
+}
+
+/**
+ * vu_queue_empty() - Check if virtqueue is empty
+ * @vq: Virtqueue
+ *
+ * Return: true if the virtqueue is empty, false otherwise
+ */
+bool vu_queue_empty(struct vu_virtq *vq)
+{
+ if (!vq->vring.avail)
+ return true;
+
+ if (vq->shadow_avail_idx != vq->last_avail_idx)
+ return false;
+
+ return vring_avail_idx(vq) == vq->last_avail_idx;
+}
+
+/**
+ * vring_can_notify() - Check if a notification can be sent
+ * @dev: Vhost-user device
+ * @vq: Virtqueue
+ *
+ * Return: true if notification can be sent
+ */
+static bool vring_can_notify(const struct vu_dev *dev, struct vu_virtq *vq)
+{
+ uint16_t old, new;
+ bool v;
+
+ /* We need to expose used array entries before checking used event. */
+ smp_mb();
+
+ /* Always notify when queue is empty (when feature acknowledge) */
+ if (vu_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
+ !vq->inuse && vu_queue_empty(vq))
+ return true;
+
+ if (!vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX))
+ return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
+
+ v = vq->signalled_used_valid;
+ vq->signalled_used_valid = true;
+ old = vq->signalled_used;
+ new = vq->signalled_used = vq->used_idx;
+ return !v || vring_need_event(vring_get_used_event(vq), new, old);
+}
+
+/**
+ * vu_queue_notify() - Send a notification to the given virtqueue
+ * @dev: Vhost-user device
+ * @vq: Virtqueue
+ */
+/* cppcheck-suppress unusedFunction */
+void vu_queue_notify(const struct vu_dev *dev, struct vu_virtq *vq)
+{
+ if (!vq->vring.avail)
+ return;
+
+ if (!vring_can_notify(dev, vq)) {
+ debug("vhost-user: virtqueue can skip notify...");
+ return;
+ }
+
+ if (eventfd_write(vq->call_fd, 1) < 0)
+ die_perror("Error writing vhost-user queue eventfd");
+}
+
+/* virtq_avail_event() - Get location of available event indices
+ * (only with VIRTIO_F_EVENT_IDX)
+ * @vq: Virtqueue
+ *
+ * Return: return the location of the available event index
+ */
+static inline uint16_t *virtq_avail_event(const struct vu_virtq *vq)
+{
+ /* For backwards compat, avail event index is at *end* of used ring. */
+ return (uint16_t *)&vq->vring.used->ring[vq->vring.num];
+}
+
+/**
+ * vring_set_avail_event() - Set avail_event
+ * @vq: Virtqueue
+ * @val: Value to set to avail_event
+ * avail_event is used in the same way the used_event is in the
+ * avail_ring.
+ * avail_event is used to advise the driver that notifications
+ * are unnecessary until the driver writes entry with an index
+ * specified by avail_event into the available ring.
+ */
+static inline void vring_set_avail_event(const struct vu_virtq *vq,
+ uint16_t val)
+{
+ uint16_t val_le = htole16(val);
+
+ if (!vq->notification)
+ return;
+
+ memcpy(virtq_avail_event(vq), &val_le, sizeof(val_le));
+}
+
+/**
+ * virtqueue_map_desc() - Translate descriptor ring physical address into our
+ * virtual address space
+ * @dev: Vhost-user device
+ * @p_num_sg: First iov entry to use (input),
+ * first iov entry not used (output)
+ * @iov: Iov array to use to store buffer virtual addresses
+ * @max_num_sg: Maximum number of iov entries
+ * @pa: Guest physical address of the buffer to map into our virtual
+ * address
+ * @sz: Size of the buffer
+ *
+ * Return: false on error, true otherwise
+ */
+static bool virtqueue_map_desc(struct vu_dev *dev,
+ unsigned int *p_num_sg, struct iovec *iov,
+ unsigned int max_num_sg,
+ uint64_t pa, size_t sz)
+{
+ unsigned int num_sg = *p_num_sg;
+
+ ASSERT(num_sg < max_num_sg);
+ ASSERT(sz);
+
+ while (sz) {
+ uint64_t len = sz;
+
+ iov[num_sg].iov_base = vu_gpa_to_va(dev, &len, pa);
+ if (iov[num_sg].iov_base == NULL)
+ die("vhost-user: invalid address for buffers");
+ iov[num_sg].iov_len = len;
+ num_sg++;
+ sz -= len;
+ pa += len;
+ }
+
+ *p_num_sg = num_sg;
+ return true;
+}
+
+/**
+ * vu_queue_map_desc - Map the virtqueue descriptor ring into our virtual
+ * address space
+ * @dev: Vhost-user device
+ * @vq: Virtqueue
+ * @idx: First descriptor ring entry to map
+ * @elem: Virtqueue element to store descriptor ring iov
+ *
+ * Return: -1 if there is an error, 0 otherwise
+ */
+static int vu_queue_map_desc(struct vu_dev *dev, struct vu_virtq *vq, unsigned int idx,
+ struct vu_virtq_element *elem)
+{
+ const struct vring_desc *desc = vq->vring.desc;
+ struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE];
+ unsigned int out_num = 0, in_num = 0;
+ unsigned int max = vq->vring.num;
+ unsigned int i = idx;
+ uint64_t read_len;
+ int rc;
+
+ if (le16toh(desc[i].flags) & VRING_DESC_F_INDIRECT) {
+ unsigned int desc_len;
+ uint64_t desc_addr;
+
+ if (le32toh(desc[i].len) % sizeof(struct vring_desc))
+ die("vhost-user: Invalid size for indirect buffer table");
+
+ /* loop over the indirect descriptor table */
+ desc_addr = le64toh(desc[i].addr);
+ desc_len = le32toh(desc[i].len);
+ max = desc_len / sizeof(struct vring_desc);
+ read_len = desc_len;
+ desc = vu_gpa_to_va(dev, &read_len, desc_addr);
+ if (desc && read_len != desc_len) {
+ /* Failed to use zero copy */
+ desc = NULL;
+ if (!virtqueue_read_indirect_desc(dev, desc_buf, desc_addr, desc_len))
+ desc = desc_buf;
+ }
+ if (!desc)
+ die("vhost-user: Invalid indirect buffer table");
+ i = 0;
+ }
+
+ /* Collect all the descriptors */
+ do {
+ if (le16toh(desc[i].flags) & VRING_DESC_F_WRITE) {
+ if (!virtqueue_map_desc(dev, &in_num, elem->in_sg,
+ elem->in_num,
+ le64toh(desc[i].addr),
+ le32toh(desc[i].len)))
+ return -1;
+ } else {
+ if (in_num)
+ die("Incorrect order for descriptors");
+ if (!virtqueue_map_desc(dev, &out_num, elem->out_sg,
+ elem->out_num,
+ le64toh(desc[i].addr),
+ le32toh(desc[i].len))) {
+ return -1;
+ }
+ }
+
+ /* If we've got too many, that implies a descriptor loop. */
+ if ((in_num + out_num) > max)
+ die("vhost-user: Loop in queue descriptor list");
+ rc = virtqueue_read_next_desc(desc, i, max, &i);
+ } while (rc == VIRTQUEUE_READ_DESC_MORE);
+
+ if (rc == VIRTQUEUE_READ_DESC_ERROR)
+ die("vhost-user: Failed to read descriptor list");
+
+ elem->index = idx;
+ elem->in_num = in_num;
+ elem->out_num = out_num;
+
+ return 0;
+}
+
+/**
+ * vu_queue_pop() - Pop an entry from the virtqueue
+ * @dev: Vhost-user device
+ * @vq: Virtqueue
+ * @elem: Virtqueue element to file with the entry information
+ *
+ * Return: -1 if there is an error, 0 otherwise
+ */
+/* cppcheck-suppress unusedFunction */
+int vu_queue_pop(struct vu_dev *dev, struct vu_virtq *vq, struct vu_virtq_element *elem)
+{
+ unsigned int head;
+ int ret;
+
+ if (!vq->vring.avail)
+ return -1;
+
+ if (vu_queue_empty(vq))
+ return -1;
+
+ /* Needed after vu_queue_empty(), see comment in
+ * virtqueue_num_heads().
+ */
+ smp_rmb();
+
+ if (vq->inuse >= vq->vring.num)
+ die("vhost-user queue size exceeded");
+
+ virtqueue_get_head(vq, vq->last_avail_idx++, &head);
+
+ if (vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX))
+ vring_set_avail_event(vq, vq->last_avail_idx);
+
+ ret = vu_queue_map_desc(dev, vq, head, elem);
+
+ if (ret < 0)
+ return ret;
+
+ vq->inuse++;
+
+ return 0;
+}
+
+/**
+ * vu_queue_detach_element() - Detach an element from the virqueue
+ * @vq: Virtqueue
+ */
+void vu_queue_detach_element(struct vu_virtq *vq)
+{
+ vq->inuse--;
+ /* unmap, when DMA support is added */
+}
+
+/**
+ * vu_queue_unpop() - Push back the previously popped element from the virqueue
+ * @vq: Virtqueue
+ */
+/* cppcheck-suppress unusedFunction */
+void vu_queue_unpop(struct vu_virtq *vq)
+{
+ vq->last_avail_idx--;
+ vu_queue_detach_element(vq);
+}
+
+/**
+ * vu_queue_rewind() - Push back a given number of popped elements
+ * @vq: Virtqueue
+ * @num: Number of element to unpop
+ */
+/* cppcheck-suppress unusedFunction */
+bool vu_queue_rewind(struct vu_virtq *vq, unsigned int num)
+{
+ if (num > vq->inuse)
+ return false;
+
+ vq->last_avail_idx -= num;
+ vq->inuse -= num;
+ return true;
+}
+
+/**
+ * vring_used_write() - Write an entry in the used ring
+ * @vq: Virtqueue
+ * @uelem: Entry to write
+ * @i: Index of the entry in the used ring
+ */
+static inline void vring_used_write(struct vu_virtq *vq,
+ const struct vring_used_elem *uelem, int i)
+{
+ struct vring_used *used = vq->vring.used;
+
+ used->ring[i] = *uelem;
+}
+
+/**
+ * vu_queue_fill_by_index() - Update information of a descriptor ring entry
+ * in the used ring
+ * @vq: Virtqueue
+ * @index: Descriptor ring index
+ * @len: Size of the element
+ * @idx: Used ring entry index
+ */
+void vu_queue_fill_by_index(struct vu_virtq *vq, unsigned int index,
+ unsigned int len, unsigned int idx)
+{
+ struct vring_used_elem uelem;
+
+ if (!vq->vring.avail)
+ return;
+
+ idx = (idx + vq->used_idx) % vq->vring.num;
+
+ uelem.id = htole32(index);
+ uelem.len = htole32(len);
+ vring_used_write(vq, &uelem, idx);
+}
+
+/**
+ * vu_queue_fill() - Update information of a given element in the used ring
+ * @dev: Vhost-user device
+ * @vq: Virtqueue
+ * @elem: Element information to fill
+ * @len: Size of the element
+ * @idx: Used ring entry index
+ */
+/* cppcheck-suppress unusedFunction */
+void vu_queue_fill(struct vu_virtq *vq, const struct vu_virtq_element *elem,
+ unsigned int len, unsigned int idx)
+{
+ vu_queue_fill_by_index(vq, elem->index, len, idx);
+}
+
+/**
+ * vring_used_idx_set() - Set the descriptor ring current index
+ * @vq: Virtqueue
+ * @val: Value to set in the index
+ */
+static inline void vring_used_idx_set(struct vu_virtq *vq, uint16_t val)
+{
+ vq->vring.used->idx = htole16(val);
+
+ vq->used_idx = val;
+}
+
+/**
+ * vu_queue_flush() - Flush the virtqueue
+ * @vq: Virtqueue
+ * @count: Number of entry to flush
+ */
+/* cppcheck-suppress unusedFunction */
+void vu_queue_flush(struct vu_virtq *vq, unsigned int count)
+{
+ uint16_t old, new;
+
+ if (!vq->vring.avail)
+ return;
+
+ /* Make sure buffer is written before we update index. */
+ smp_wmb();
+
+ old = vq->used_idx;
+ new = old + count;
+ vring_used_idx_set(vq, new);
+ vq->inuse -= count;
+ if ((uint16_t)(new - vq->signalled_used) < (uint16_t)(new - old))
+ vq->signalled_used_valid = false;
+}
diff --git a/virtio.h b/virtio.h
new file mode 100644
index 000000000000..94efeb049fbc
--- /dev/null
+++ b/virtio.h
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * virtio API, vring and virtqueue functions definition
+ *
+ * Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ */
+
+#ifndef VIRTIO_H
+#define VIRTIO_H
+
+#include <stdbool.h>
+#include <linux/vhost_types.h>
+
+/* Maximum size of a virtqueue */
+#define VIRTQUEUE_MAX_SIZE 1024
+
+/**
+ * struct vu_ring - Virtqueue rings
+ * @num: Size of the queue
+ * @desc: Descriptor ring
+ * @avail: Available ring
+ * @used: Used ring
+ * @log_guest_addr: Guest address for logging
+ * @flags: Vring flags
+ * VHOST_VRING_F_LOG is set if log address is valid
+ */
+struct vu_ring {
+ unsigned int num;
+ struct vring_desc *desc;
+ struct vring_avail *avail;
+ struct vring_used *used;
+ uint64_t log_guest_addr;
+ uint32_t flags;
+};
+
+/**
+ * struct vu_virtq - Virtqueue definition
+ * @vring: Virtqueue rings
+ * @last_avail_idx: Next head to pop
+ * @shadow_avail_idx: Last avail_idx read from VQ.
+ * @used_idx: Descriptor ring current index
+ * @signalled_used: Last used index value we have signalled on
+ * @signalled_used_valid: True if signalled_used if valid
+ * @notification: True if the queues notify (via event
+ * index or interrupt)
+ * @inuse: Number of entries in use
+ * @call_fd: The event file descriptor to signal when
+ * buffers are used.
+ * @kick_fd: The event file descriptor for adding
+ * buffers to the vring
+ * @err_fd: The event file descriptor to signal when
+ * error occurs
+ * @enable: True if the virtqueue is enabled
+ * @started: True if the virtqueue is started
+ * @vra: QEMU address of our rings
+ */
+struct vu_virtq {
+ struct vu_ring vring;
+ uint16_t last_avail_idx;
+ uint16_t shadow_avail_idx;
+ uint16_t used_idx;
+ uint16_t signalled_used;
+ bool signalled_used_valid;
+ bool notification;
+ unsigned int inuse;
+ int call_fd;
+ int kick_fd;
+ int err_fd;
+ unsigned int enable;
+ bool started;
+ struct vhost_vring_addr vra;
+};
+
+/**
+ * struct vu_dev_region - guest shared memory region
+ * @gpa: Guest physical address of the region
+ * @size: Memory size in bytes
+ * @qva: QEMU virtual address
+ * @mmap_offset: Offset where the region starts in the mapped memory
+ * @mmap_addr: Address of the mapped memory
+ */
+struct vu_dev_region {
+ uint64_t gpa;
+ uint64_t size;
+ uint64_t qva;
+ uint64_t mmap_offset;
+ uint64_t mmap_addr;
+};
+
+#define VHOST_USER_MAX_QUEUES 2
+
+/*
+ * Set a reasonable maximum number of ram slots, which will be supported by
+ * any architecture.
+ */
+#define VHOST_USER_MAX_RAM_SLOTS 32
+
+/**
+ * struct vu_dev - vhost-user device information
+ * @context: Execution context
+ * @nregions: Number of shared memory regions
+ * @regions: Guest shared memory regions
+ * @features: Vhost-user features
+ * @protocol_features: Vhost-user protocol features
+ */
+struct vu_dev {
+ uint32_t nregions;
+ struct vu_dev_region regions[VHOST_USER_MAX_RAM_SLOTS];
+ struct vu_virtq vq[VHOST_USER_MAX_QUEUES];
+ uint64_t features;
+ uint64_t protocol_features;
+};
+
+/**
+ * struct vu_virtq_element - virtqueue element
+ * @index: Descriptor ring index
+ * @out_num: Number of outgoing iovec buffers
+ * @in_num: Number of incoming iovec buffers
+ * @in_sg: Incoming iovec buffers
+ * @out_sg: Outgoing iovec buffers
+ */
+struct vu_virtq_element {
+ unsigned int index;
+ unsigned int out_num;
+ unsigned int in_num;
+ struct iovec *in_sg;
+ struct iovec *out_sg;
+};
+
+/**
+ * has_feature() - Check a feature bit in a features set
+ * @features: Features set
+ * @fb: Feature bit to check
+ *
+ * Return: True if the feature bit is set
+ */
+static inline bool has_feature(uint64_t features, unsigned int fbit)
+{
+ return !!(features & (1ULL << fbit));
+}
+
+/**
+ * vu_has_feature() - Check if a virtio-net feature is available
+ * @vdev: Vhost-user device
+ * @bit: Feature to check
+ *
+ * Return: True if the feature is available
+ */
+static inline bool vu_has_feature(const struct vu_dev *vdev,
+ unsigned int fbit)
+{
+ return has_feature(vdev->features, fbit);
+}
+
+/**
+ * vu_has_protocol_feature() - Check if a vhost-user feature is available
+ * @vdev: Vhost-user device
+ * @bit: Feature to check
+ *
+ * Return: True if the feature is available
+ */
+/* cppcheck-suppress unusedFunction */
+static inline bool vu_has_protocol_feature(const struct vu_dev *vdev,
+ unsigned int fbit)
+{
+ return has_feature(vdev->protocol_features, fbit);
+}
+
+bool vu_queue_empty(struct vu_virtq *vq);
+void vu_queue_notify(const struct vu_dev *dev, struct vu_virtq *vq);
+int vu_queue_pop(struct vu_dev *dev, struct vu_virtq *vq,
+ struct vu_virtq_element *elem);
+void vu_queue_detach_element(struct vu_virtq *vq);
+void vu_queue_unpop(struct vu_virtq *vq);
+bool vu_queue_rewind(struct vu_virtq *vq, unsigned int num);
+void vu_queue_fill_by_index(struct vu_virtq *vq, unsigned int index,
+ unsigned int len, unsigned int idx);
+void vu_queue_fill(struct vu_virtq *vq,
+ const struct vu_virtq_element *elem, unsigned int len,
+ unsigned int idx);
+void vu_queue_flush(struct vu_virtq *vq, unsigned int count);
+#endif /* VIRTIO_H */
--
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * virtio API, vring and virtqueue functions definition
+ *
+ * Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ */
+
+#ifndef VIRTIO_H
+#define VIRTIO_H
+
+#include <stdbool.h>
+#include <linux/vhost_types.h>
+
+/* Maximum size of a virtqueue */
+#define VIRTQUEUE_MAX_SIZE 1024
+
+/**
+ * struct vu_ring - Virtqueue rings
+ * @num: Size of the queue
+ * @desc: Descriptor ring
+ * @avail: Available ring
+ * @used: Used ring
+ * @log_guest_addr: Guest address for logging
+ * @flags: Vring flags
+ * VHOST_VRING_F_LOG is set if log address is valid
+ */
+struct vu_ring {
+ unsigned int num;
+ struct vring_desc *desc;
+ struct vring_avail *avail;
+ struct vring_used *used;
+ uint64_t log_guest_addr;
+ uint32_t flags;
+};
+
+/**
+ * struct vu_virtq - Virtqueue definition
+ * @vring: Virtqueue rings
+ * @last_avail_idx: Next head to pop
+ * @shadow_avail_idx: Last avail_idx read from VQ.
+ * @used_idx: Descriptor ring current index
+ * @signalled_used: Last used index value we have signalled on
+ * @signalled_used_valid: True if signalled_used if valid
+ * @notification: True if the queues notify (via event
+ * index or interrupt)
+ * @inuse: Number of entries in use
+ * @call_fd: The event file descriptor to signal when
+ * buffers are used.
+ * @kick_fd: The event file descriptor for adding
+ * buffers to the vring
+ * @err_fd: The event file descriptor to signal when
+ * error occurs
+ * @enable: True if the virtqueue is enabled
+ * @started: True if the virtqueue is started
+ * @vra: QEMU address of our rings
+ */
+struct vu_virtq {
+ struct vu_ring vring;
+ uint16_t last_avail_idx;
+ uint16_t shadow_avail_idx;
+ uint16_t used_idx;
+ uint16_t signalled_used;
+ bool signalled_used_valid;
+ bool notification;
+ unsigned int inuse;
+ int call_fd;
+ int kick_fd;
+ int err_fd;
+ unsigned int enable;
+ bool started;
+ struct vhost_vring_addr vra;
+};
+
+/**
+ * struct vu_dev_region - guest shared memory region
+ * @gpa: Guest physical address of the region
+ * @size: Memory size in bytes
+ * @qva: QEMU virtual address
+ * @mmap_offset: Offset where the region starts in the mapped memory
+ * @mmap_addr: Address of the mapped memory
+ */
+struct vu_dev_region {
+ uint64_t gpa;
+ uint64_t size;
+ uint64_t qva;
+ uint64_t mmap_offset;
+ uint64_t mmap_addr;
+};
+
+#define VHOST_USER_MAX_QUEUES 2
+
+/*
+ * Set a reasonable maximum number of ram slots, which will be supported by
+ * any architecture.
+ */
+#define VHOST_USER_MAX_RAM_SLOTS 32
+
+/**
+ * struct vu_dev - vhost-user device information
+ * @context: Execution context
+ * @nregions: Number of shared memory regions
+ * @regions: Guest shared memory regions
+ * @features: Vhost-user features
+ * @protocol_features: Vhost-user protocol features
+ */
+struct vu_dev {
+ uint32_t nregions;
+ struct vu_dev_region regions[VHOST_USER_MAX_RAM_SLOTS];
+ struct vu_virtq vq[VHOST_USER_MAX_QUEUES];
+ uint64_t features;
+ uint64_t protocol_features;
+};
+
+/**
+ * struct vu_virtq_element - virtqueue element
+ * @index: Descriptor ring index
+ * @out_num: Number of outgoing iovec buffers
+ * @in_num: Number of incoming iovec buffers
+ * @in_sg: Incoming iovec buffers
+ * @out_sg: Outgoing iovec buffers
+ */
+struct vu_virtq_element {
+ unsigned int index;
+ unsigned int out_num;
+ unsigned int in_num;
+ struct iovec *in_sg;
+ struct iovec *out_sg;
+};
+
+/**
+ * has_feature() - Check a feature bit in a features set
+ * @features: Features set
+ * @fb: Feature bit to check
+ *
+ * Return: True if the feature bit is set
+ */
+static inline bool has_feature(uint64_t features, unsigned int fbit)
+{
+ return !!(features & (1ULL << fbit));
+}
+
+/**
+ * vu_has_feature() - Check if a virtio-net feature is available
+ * @vdev: Vhost-user device
+ * @bit: Feature to check
+ *
+ * Return: True if the feature is available
+ */
+static inline bool vu_has_feature(const struct vu_dev *vdev,
+ unsigned int fbit)
+{
+ return has_feature(vdev->features, fbit);
+}
+
+/**
+ * vu_has_protocol_feature() - Check if a vhost-user feature is available
+ * @vdev: Vhost-user device
+ * @bit: Feature to check
+ *
+ * Return: True if the feature is available
+ */
+/* cppcheck-suppress unusedFunction */
+static inline bool vu_has_protocol_feature(const struct vu_dev *vdev,
+ unsigned int fbit)
+{
+ return has_feature(vdev->protocol_features, fbit);
+}
+
+bool vu_queue_empty(struct vu_virtq *vq);
+void vu_queue_notify(const struct vu_dev *dev, struct vu_virtq *vq);
+int vu_queue_pop(struct vu_dev *dev, struct vu_virtq *vq,
+ struct vu_virtq_element *elem);
+void vu_queue_detach_element(struct vu_virtq *vq);
+void vu_queue_unpop(struct vu_virtq *vq);
+bool vu_queue_rewind(struct vu_virtq *vq, unsigned int num);
+void vu_queue_fill_by_index(struct vu_virtq *vq, unsigned int index,
+ unsigned int len, unsigned int idx);
+void vu_queue_fill(struct vu_virtq *vq,
+ const struct vu_virtq_element *elem, unsigned int len,
+ unsigned int idx);
+void vu_queue_flush(struct vu_virtq *vq, unsigned int count);
+#endif /* VIRTIO_H */
--
2.46.2
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH v7 3/8] vhost-user: introduce vhost-user API
2024-10-09 9:07 [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Laurent Vivier
2024-10-09 9:07 ` [PATCH v7 1/8] packet: replace struct desc by struct iovec Laurent Vivier
2024-10-09 9:07 ` [PATCH v7 2/8] vhost-user: introduce virtio API Laurent Vivier
@ 2024-10-09 9:07 ` Laurent Vivier
2024-10-09 9:07 ` [PATCH v7 4/8] udp: Prepare udp.c to be shared with vhost-user Laurent Vivier
` (6 subsequent siblings)
9 siblings, 0 replies; 18+ messages in thread
From: Laurent Vivier @ 2024-10-09 9:07 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Add vhost_user.c and vhost_user.h that define the functions needed
to implement vhost-user backend.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
Makefile | 4 +-
vhost_user.c | 999 +++++++++++++++++++++++++++++++++++++++++++++++++++
vhost_user.h | 208 +++++++++++
virtio.h | 1 +
4 files changed, 1210 insertions(+), 2 deletions(-)
create mode 100644 vhost_user.c
create mode 100644 vhost_user.h
diff --git a/Makefile b/Makefile
index a2258891f104..0e8ed60a0da1 100644
--- a/Makefile
+++ b/Makefile
@@ -54,7 +54,7 @@ FLAGS += -DDUAL_STACK_SOCKETS=$(DUAL_STACK_SOCKETS)
PASST_SRCS = arch.c arp.c checksum.c conf.c dhcp.c dhcpv6.c flow.c fwd.c \
icmp.c igmp.c inany.c iov.c ip.c isolation.c lineread.c log.c mld.c \
ndp.c netlink.c packet.c passt.c pasta.c pcap.c pif.c tap.c tcp.c \
- tcp_buf.c tcp_splice.c udp.c udp_flow.c util.c virtio.c
+ tcp_buf.c tcp_splice.c udp.c udp_flow.c util.c vhost_user.c virtio.c
QRAP_SRCS = qrap.c
SRCS = $(PASST_SRCS) $(QRAP_SRCS)
@@ -64,7 +64,7 @@ PASST_HEADERS = arch.h arp.h checksum.h conf.h dhcp.h dhcpv6.h flow.h fwd.h \
flow_table.h icmp.h icmp_flow.h inany.h iov.h ip.h isolation.h \
lineread.h log.h ndp.h netlink.h packet.h passt.h pasta.h pcap.h pif.h \
siphash.h tap.h tcp.h tcp_buf.h tcp_conn.h tcp_internal.h tcp_splice.h \
- udp.h udp_flow.h util.h virtio.h
+ udp.h udp_flow.h util.h vhost_user.h virtio.h
HEADERS = $(PASST_HEADERS) seccomp.h
C := \#include <linux/tcp.h>\nstruct tcp_info x = { .tcpi_snd_wnd = 0 };
diff --git a/vhost_user.c b/vhost_user.c
new file mode 100644
index 000000000000..952e97efaae8
--- /dev/null
+++ b/vhost_user.c
@@ -0,0 +1,999 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * vhost-user API, command management and virtio interface
+ *
+ * Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ *
+ * Some parts from QEMU subprojects/libvhost-user/libvhost-user.c
+ * licensed under the following terms:
+ *
+ * Copyright IBM, Corp. 2007
+ * Copyright (c) 2016 Red Hat, Inc.
+ *
+ * Authors:
+ * Anthony Liguori <aliguori@us.ibm.com>
+ * Marc-André Lureau <mlureau@redhat.com>
+ * Victor Kaplansky <victork@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later. See the COPYING file in the top-level directory.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <assert.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <time.h>
+#include <net/ethernet.h>
+#include <netinet/in.h>
+#include <sys/epoll.h>
+#include <sys/eventfd.h>
+#include <sys/mman.h>
+#include <linux/vhost_types.h>
+#include <linux/virtio_net.h>
+
+#include "util.h"
+#include "passt.h"
+#include "tap.h"
+#include "vhost_user.h"
+#include "pcap.h"
+
+/* vhost-user version we are compatible with */
+#define VHOST_USER_VERSION 1
+
+/**
+ * vu_print_capabilities() - print vhost-user capabilities
+ * this is part of the vhost-user backend
+ * convention.
+ */
+/* cppcheck-suppress unusedFunction */
+void vu_print_capabilities(void)
+{
+ info("{");
+ info(" \"type\": \"net\"");
+ info("}");
+ exit(EXIT_SUCCESS);
+}
+
+/**
+ * vu_request_to_string() - convert a vhost-user request number to its name
+ * @req: request number
+ *
+ * Return: the name of request number
+ */
+static const char *vu_request_to_string(unsigned int req)
+{
+ if (req < VHOST_USER_MAX) {
+#define REQ(req) [req] = #req
+ static const char * const vu_request_str[VHOST_USER_MAX] = {
+ REQ(VHOST_USER_NONE),
+ REQ(VHOST_USER_GET_FEATURES),
+ REQ(VHOST_USER_SET_FEATURES),
+ REQ(VHOST_USER_SET_OWNER),
+ REQ(VHOST_USER_RESET_OWNER),
+ REQ(VHOST_USER_SET_MEM_TABLE),
+ REQ(VHOST_USER_SET_LOG_BASE),
+ REQ(VHOST_USER_SET_LOG_FD),
+ REQ(VHOST_USER_SET_VRING_NUM),
+ REQ(VHOST_USER_SET_VRING_ADDR),
+ REQ(VHOST_USER_SET_VRING_BASE),
+ REQ(VHOST_USER_GET_VRING_BASE),
+ REQ(VHOST_USER_SET_VRING_KICK),
+ REQ(VHOST_USER_SET_VRING_CALL),
+ REQ(VHOST_USER_SET_VRING_ERR),
+ REQ(VHOST_USER_GET_PROTOCOL_FEATURES),
+ REQ(VHOST_USER_SET_PROTOCOL_FEATURES),
+ REQ(VHOST_USER_GET_QUEUE_NUM),
+ REQ(VHOST_USER_SET_VRING_ENABLE),
+ REQ(VHOST_USER_SEND_RARP),
+ REQ(VHOST_USER_NET_SET_MTU),
+ REQ(VHOST_USER_SET_BACKEND_REQ_FD),
+ REQ(VHOST_USER_IOTLB_MSG),
+ REQ(VHOST_USER_SET_VRING_ENDIAN),
+ REQ(VHOST_USER_GET_CONFIG),
+ REQ(VHOST_USER_SET_CONFIG),
+ REQ(VHOST_USER_POSTCOPY_ADVISE),
+ REQ(VHOST_USER_POSTCOPY_LISTEN),
+ REQ(VHOST_USER_POSTCOPY_END),
+ REQ(VHOST_USER_GET_INFLIGHT_FD),
+ REQ(VHOST_USER_SET_INFLIGHT_FD),
+ REQ(VHOST_USER_GPU_SET_SOCKET),
+ REQ(VHOST_USER_VRING_KICK),
+ REQ(VHOST_USER_GET_MAX_MEM_SLOTS),
+ REQ(VHOST_USER_ADD_MEM_REG),
+ REQ(VHOST_USER_REM_MEM_REG),
+ };
+#undef REQ
+ return vu_request_str[req];
+ }
+
+ return "unknown";
+}
+
+/**
+ * qva_to_va() - Translate front-end (QEMU) virtual address to our virtual
+ * address
+ * @dev: vhost-user device
+ * @qemu_addr: front-end userspace address
+ *
+ * Return: the memory address in our process virtual address space.
+ */
+static void *qva_to_va(struct vu_dev *dev, uint64_t qemu_addr)
+{
+ unsigned int i;
+
+ /* Find matching memory region. */
+ for (i = 0; i < dev->nregions; i++) {
+ const struct vu_dev_region *r = &dev->regions[i];
+
+ if ((qemu_addr >= r->qva) && (qemu_addr < (r->qva + r->size))) {
+ /* NOLINTNEXTLINE(performance-no-int-to-ptr) */
+ return (void *)(qemu_addr - r->qva + r->mmap_addr +
+ r->mmap_offset);
+ }
+ }
+
+ return NULL;
+}
+
+/**
+ * vmsg_close_fds() - Close all file descriptors of a given message
+ * @vmsg: vhost-user message with the list of the file descriptors
+ */
+static void vmsg_close_fds(const struct vhost_user_msg *vmsg)
+{
+ int i;
+
+ for (i = 0; i < vmsg->fd_num; i++)
+ close(vmsg->fds[i]);
+}
+
+/**
+ * vu_remove_watch() - Remove a file descriptor from our passt epoll
+ * file descriptor
+ * @vdev: vhost-user device
+ * @fd: file descriptor to remove
+ */
+static void vu_remove_watch(const struct vu_dev *vdev, int fd)
+{
+ /* Placeholder to add passt related code */
+ (void)vdev;
+ (void)fd;
+}
+
+/**
+ * vmsg_set_reply_u64() - Set reply payload.u64 and clear request flags
+ * and fd_num
+ * @vmsg: vhost-user message
+ * @val: 64-bit value to reply
+ */
+static void vmsg_set_reply_u64(struct vhost_user_msg *vmsg, uint64_t val)
+{
+ vmsg->hdr.flags = 0; /* defaults will be set by vu_send_reply() */
+ vmsg->hdr.size = sizeof(vmsg->payload.u64);
+ vmsg->payload.u64 = val;
+ vmsg->fd_num = 0;
+}
+
+/**
+ * vu_message_read_default() - Read incoming vhost-user message from the
+ * front-end
+ * @conn_fd: vhost-user command socket
+ * @vmsg: vhost-user message
+ *
+ * Return: 0 if recvmsg() has been interrupted or if there's no data to read,
+ * 1 if a message has been received
+ */
+static int vu_message_read_default(int conn_fd, struct vhost_user_msg *vmsg)
+{
+ char control[CMSG_SPACE(VHOST_MEMORY_BASELINE_NREGIONS *
+ sizeof(int))] = { 0 };
+ struct iovec iov = {
+ .iov_base = (char *)vmsg,
+ .iov_len = VHOST_USER_HDR_SIZE,
+ };
+ struct msghdr msg = {
+ .msg_iov = &iov,
+ .msg_iovlen = 1,
+ .msg_control = control,
+ .msg_controllen = sizeof(control),
+ };
+ ssize_t ret, sz_payload;
+ struct cmsghdr *cmsg;
+
+ ret = recvmsg(conn_fd, &msg, MSG_DONTWAIT);
+ if (ret < 0) {
+ if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
+ return 0;
+ die_perror("vhost-user message receive (recvmsg)");
+ }
+
+ vmsg->fd_num = 0;
+ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
+ cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+ if (cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_RIGHTS) {
+ size_t fd_size;
+
+ ASSERT(cmsg->cmsg_len >= CMSG_LEN(0));
+ fd_size = cmsg->cmsg_len - CMSG_LEN(0);
+ ASSERT(fd_size <= sizeof(vmsg->fds));
+ vmsg->fd_num = fd_size / sizeof(int);
+ memcpy(vmsg->fds, CMSG_DATA(cmsg), fd_size);
+ break;
+ }
+ }
+
+ sz_payload = vmsg->hdr.size;
+ if ((size_t)sz_payload > sizeof(vmsg->payload)) {
+ die("vhost-user message request too big: %d,"
+ " size: vmsg->size: %zd, "
+ "while sizeof(vmsg->payload) = %zu",
+ vmsg->hdr.request, sz_payload, sizeof(vmsg->payload));
+ }
+
+ if (sz_payload) {
+ do
+ ret = recv(conn_fd, &vmsg->payload, sz_payload, 0);
+ while (ret < 0 && errno == EINTR);
+
+ if (ret < 0)
+ die_perror("vhost-user message receive");
+
+ if (ret == 0)
+ die("EOF on vhost-user message receive");
+
+ if (ret < sz_payload)
+ die("Short-read on vhost-user message receive");
+ }
+
+ return 1;
+}
+
+/**
+ * vu_message_write() - Send a message to the front-end
+ * @conn_fd: vhost-user command socket
+ * @vmsg: vhost-user message
+ *
+ * #syscalls:vu sendmsg
+ */
+static void vu_message_write(int conn_fd, struct vhost_user_msg *vmsg)
+{
+ char control[CMSG_SPACE(VHOST_MEMORY_BASELINE_NREGIONS * sizeof(int))] = { 0 };
+ struct iovec iov = {
+ .iov_base = (char *)vmsg,
+ .iov_len = VHOST_USER_HDR_SIZE + vmsg->hdr.size,
+ };
+ struct msghdr msg = {
+ .msg_iov = &iov,
+ .msg_iovlen = 1,
+ .msg_control = control,
+ };
+ int rc;
+
+ ASSERT(vmsg->fd_num <= VHOST_MEMORY_BASELINE_NREGIONS);
+ if (vmsg->fd_num > 0) {
+ size_t fdsize = vmsg->fd_num * sizeof(int);
+ struct cmsghdr *cmsg;
+
+ msg.msg_controllen = CMSG_SPACE(fdsize);
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_len = CMSG_LEN(fdsize);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ memcpy(CMSG_DATA(cmsg), vmsg->fds, fdsize);
+ }
+
+ do
+ rc = sendmsg(conn_fd, &msg, 0);
+ while (rc < 0 && errno == EINTR);
+
+ if (rc < 0)
+ die_perror("vhost-user message send");
+
+ if ((uint32_t)rc < VHOST_USER_HDR_SIZE + vmsg->hdr.size)
+ die("EOF on vhost-user message send");
+}
+
+/**
+ * vu_send_reply() - Update message flags and send it to front-end
+ * @conn_fd: vhost-user command socket
+ * @vmsg: vhost-user message
+ */
+static void vu_send_reply(int conn_fd, struct vhost_user_msg *msg)
+{
+ msg->hdr.flags &= ~VHOST_USER_VERSION_MASK;
+ msg->hdr.flags |= VHOST_USER_VERSION;
+ msg->hdr.flags |= VHOST_USER_REPLY_MASK;
+
+ vu_message_write(conn_fd, msg);
+}
+
+/**
+ * vu_get_features_exec() - Provide back-end features bitmask to front-end
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: True as a reply is requested
+ */
+static bool vu_get_features_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ uint64_t features =
+ 1ULL << VIRTIO_F_VERSION_1 |
+ 1ULL << VIRTIO_NET_F_MRG_RXBUF |
+ 1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
+
+ (void)vdev;
+
+ vmsg_set_reply_u64(msg, features);
+
+ debug("Sending back to guest u64: 0x%016"PRIx64, msg->payload.u64);
+
+ return true;
+}
+
+/**
+ * vu_set_enable_all_rings() - Enable/disable all the virtqueues
+ * @vdev: vhost-user device
+ * @enable: New virtqueues state
+ */
+static void vu_set_enable_all_rings(struct vu_dev *vdev, bool enable)
+{
+ uint16_t i;
+
+ for (i = 0; i < VHOST_USER_MAX_QUEUES; i++)
+ vdev->vq[i].enable = enable;
+}
+
+/**
+ * vu_set_features_exec() - Enable features of the back-end
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ */
+static bool vu_set_features_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ debug("u64: 0x%016"PRIx64, msg->payload.u64);
+
+ vdev->features = msg->payload.u64;
+ /* We only support devices conforming to VIRTIO 1.0 or
+ * later
+ */
+ if (!vu_has_feature(vdev, VIRTIO_F_VERSION_1))
+ die("virtio legacy devices aren't supported by passt");
+
+ if (!vu_has_feature(vdev, VHOST_USER_F_PROTOCOL_FEATURES))
+ vu_set_enable_all_rings(vdev, true);
+
+ return false;
+}
+
+/**
+ * vu_set_owner_exec() - Session start flag, do nothing in our case
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ */
+static bool vu_set_owner_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ (void)vdev;
+ (void)msg;
+
+ return false;
+}
+
+/**
+ * map_ring() - Convert ring front-end (QEMU) addresses to our process
+ * virtual address space.
+ * @vdev: vhost-user device
+ * @vq: Virtqueue
+ *
+ * Return: True if ring cannot be mapped to our address space
+ */
+static bool map_ring(struct vu_dev *vdev, struct vu_virtq *vq)
+{
+ vq->vring.desc = qva_to_va(vdev, vq->vra.desc_user_addr);
+ vq->vring.used = qva_to_va(vdev, vq->vra.used_user_addr);
+ vq->vring.avail = qva_to_va(vdev, vq->vra.avail_user_addr);
+
+ debug("Setting virtq addresses:");
+ debug(" vring_desc at %p", (void *)vq->vring.desc);
+ debug(" vring_used at %p", (void *)vq->vring.used);
+ debug(" vring_avail at %p", (void *)vq->vring.avail);
+
+ return !(vq->vring.desc && vq->vring.used && vq->vring.avail);
+}
+
+/**
+ * vu_packet_check_range() - Check if a given memory zone is contained in
+ * a mapped guest memory region
+ * @buf: Array of the available memory regions
+ * @offset: Offset of data range in packet descriptor
+ * @size: Length of desired data range
+ * @start: Start of the packet descriptor
+ *
+ * Return: 0 if the zone is in a mapped memory region, -1 otherwise
+ */
+/* cppcheck-suppress unusedFunction */
+int vu_packet_check_range(void *buf, size_t offset, size_t len,
+ const char *start)
+{
+ struct vu_dev_region *dev_region;
+
+ for (dev_region = buf; dev_region->mmap_addr; dev_region++) {
+ /* NOLINTNEXTLINE(performance-no-int-to-ptr) */
+ char *m = (char *)dev_region->mmap_addr;
+
+ if (m <= start &&
+ start + offset + len <= m + dev_region->mmap_offset +
+ dev_region->size)
+ return 0;
+ }
+
+ return -1;
+}
+
+/**
+ * vu_set_mem_table_exec() - Sets the memory map regions to be able to
+ * translate the vring addresses.
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ *
+ * #syscalls:vu mmap munmap
+ */
+static bool vu_set_mem_table_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ struct vhost_user_memory m = msg->payload.memory, *memory = &m;
+ unsigned int i;
+
+ for (i = 0; i < vdev->nregions; i++) {
+ struct vu_dev_region *r = &vdev->regions[i];
+ /* NOLINTNEXTLINE(performance-no-int-to-ptr) */
+ void *mm = (void *)r->mmap_addr;
+
+ if (mm)
+ munmap(mm, r->size + r->mmap_offset);
+ }
+ vdev->nregions = memory->nregions;
+
+ debug("vhost-user nregions: %u", memory->nregions);
+ for (i = 0; i < vdev->nregions; i++) {
+ struct vhost_user_memory_region *msg_region = &memory->regions[i];
+ struct vu_dev_region *dev_region = &vdev->regions[i];
+ void *mmap_addr;
+
+ debug("vhost-user region %d", i);
+ debug(" guest_phys_addr: 0x%016"PRIx64,
+ msg_region->guest_phys_addr);
+ debug(" memory_size: 0x%016"PRIx64,
+ msg_region->memory_size);
+ debug(" userspace_addr 0x%016"PRIx64,
+ msg_region->userspace_addr);
+ debug(" mmap_offset 0x%016"PRIx64,
+ msg_region->mmap_offset);
+
+ dev_region->gpa = msg_region->guest_phys_addr;
+ dev_region->size = msg_region->memory_size;
+ dev_region->qva = msg_region->userspace_addr;
+ dev_region->mmap_offset = msg_region->mmap_offset;
+
+ /* We don't use offset argument of mmap() since the
+ * mapped address has to be page aligned.
+ */
+ mmap_addr = mmap(0, dev_region->size + dev_region->mmap_offset,
+ PROT_READ | PROT_WRITE, MAP_SHARED |
+ MAP_NORESERVE, msg->fds[i], 0);
+
+ if (mmap_addr == MAP_FAILED)
+ die_perror("vhost-user region mmap error");
+
+ dev_region->mmap_addr = (uint64_t)(uintptr_t)mmap_addr;
+ debug(" mmap_addr: 0x%016"PRIx64,
+ dev_region->mmap_addr);
+
+ close(msg->fds[i]);
+ }
+
+ for (i = 0; i < VHOST_USER_MAX_QUEUES; i++) {
+ if (vdev->vq[i].vring.desc) {
+ if (map_ring(vdev, &vdev->vq[i]))
+ die("remapping queue %d during setmemtable", i);
+ }
+ }
+
+ return false;
+}
+
+/**
+ * vu_set_vring_num_exec() - Set the size of the queue (vring size)
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ */
+static bool vu_set_vring_num_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ unsigned int idx = msg->payload.state.index;
+ unsigned int num = msg->payload.state.num;
+
+ debug("State.index: %u", idx);
+ debug("State.num: %u", num);
+ vdev->vq[idx].vring.num = num;
+
+ return false;
+}
+
+/**
+ * vu_set_vring_addr_exec() - Set the addresses of the vring
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ */
+static bool vu_set_vring_addr_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ /* We need to copy the payload to vhost_vring_addr structure
+ * to access index because address of msg->payload.addr
+ * can be unaligned as it is packed.
+ */
+ struct vhost_vring_addr addr = msg->payload.addr;
+ struct vu_virtq *vq = &vdev->vq[addr.index];
+
+ debug("vhost_vring_addr:");
+ debug(" index: %d", addr.index);
+ debug(" flags: %d", addr.flags);
+ debug(" desc_user_addr: 0x%016" PRIx64,
+ (uint64_t)addr.desc_user_addr);
+ debug(" used_user_addr: 0x%016" PRIx64,
+ (uint64_t)addr.used_user_addr);
+ debug(" avail_user_addr: 0x%016" PRIx64,
+ (uint64_t)addr.avail_user_addr);
+ debug(" log_guest_addr: 0x%016" PRIx64,
+ (uint64_t)addr.log_guest_addr);
+
+ vq->vra = msg->payload.addr;
+ vq->vring.flags = addr.flags;
+ vq->vring.log_guest_addr = addr.log_guest_addr;
+
+ if (map_ring(vdev, vq))
+ die("Invalid vring_addr message");
+
+ vq->used_idx = le16toh(vq->vring.used->idx);
+
+ if (vq->last_avail_idx != vq->used_idx) {
+ debug("Last avail index != used index: %u != %u",
+ vq->last_avail_idx, vq->used_idx);
+ }
+
+ return false;
+}
+/**
+ * vu_set_vring_base_exec() - Sets the next index to use for descriptors
+ * in this vring
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ */
+static bool vu_set_vring_base_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ unsigned int idx = msg->payload.state.index;
+ unsigned int num = msg->payload.state.num;
+
+ debug("State.index: %u", idx);
+ debug("State.num: %u", num);
+ vdev->vq[idx].shadow_avail_idx = vdev->vq[idx].last_avail_idx = num;
+
+ return false;
+}
+
+/**
+ * vu_get_vring_base_exec() - Stops the vring and returns the current
+ * descriptor index or indices
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: True as a reply is requested
+ */
+static bool vu_get_vring_base_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ unsigned int idx = msg->payload.state.index;
+
+ debug("State.index: %u", idx);
+ msg->payload.state.num = vdev->vq[idx].last_avail_idx;
+ msg->hdr.size = sizeof(msg->payload.state);
+
+ vdev->vq[idx].started = false;
+
+ if (vdev->vq[idx].call_fd != -1) {
+ close(vdev->vq[idx].call_fd);
+ vdev->vq[idx].call_fd = -1;
+ }
+ if (vdev->vq[idx].kick_fd != -1) {
+ vu_remove_watch(vdev, vdev->vq[idx].kick_fd);
+ close(vdev->vq[idx].kick_fd);
+ vdev->vq[idx].kick_fd = -1;
+ }
+
+ return true;
+}
+
+/**
+ * vu_set_watch() - Add a file descriptor to the passt epoll file descriptor
+ * @vdev: vhost-user device
+ * @fd: file descriptor to add
+ */
+static void vu_set_watch(const struct vu_dev *vdev, int fd)
+{
+ /* Placeholder to add passt related code */
+ (void)vdev;
+ (void)fd;
+}
+
+/**
+ * vu_check_queue_msg_file() - Check if a message is valid,
+ * close fds if NOFD bit is set
+ * @vmsg: vhost-user message
+ */
+static void vu_check_queue_msg_file(struct vhost_user_msg *msg)
+{
+ bool nofd = msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
+ int idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+
+ if (idx >= VHOST_USER_MAX_QUEUES)
+ die("Invalid vhost-user queue index: %u", idx);
+
+ if (nofd) {
+ vmsg_close_fds(msg);
+ return;
+ }
+
+ if (msg->fd_num != 1)
+ die("Invalid fds in vhost-user request: %d", msg->hdr.request);
+}
+
+/**
+ * vu_set_vring_kick_exec() - Set the event file descriptor for adding buffers
+ * to the vring
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ */
+static bool vu_set_vring_kick_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ bool nofd = msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
+ int idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+
+ debug("u64: 0x%016"PRIx64, msg->payload.u64);
+
+ vu_check_queue_msg_file(msg);
+
+ if (vdev->vq[idx].kick_fd != -1) {
+ vu_remove_watch(vdev, vdev->vq[idx].kick_fd);
+ close(vdev->vq[idx].kick_fd);
+ vdev->vq[idx].kick_fd = -1;
+ }
+
+ if (!nofd)
+ vdev->vq[idx].kick_fd = msg->fds[0];
+
+ debug("Got kick_fd: %d for vq: %d", vdev->vq[idx].kick_fd, idx);
+
+ vdev->vq[idx].started = true;
+
+ if (vdev->vq[idx].kick_fd != -1 && VHOST_USER_IS_QUEUE_TX(idx)) {
+ vu_set_watch(vdev, vdev->vq[idx].kick_fd);
+ debug("Waiting for kicks on fd: %d for vq: %d",
+ vdev->vq[idx].kick_fd, idx);
+ }
+
+ return false;
+}
+
+/**
+ * vu_set_vring_call_exec() - Set the event file descriptor to signal when
+ * buffers are used
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ */
+static bool vu_set_vring_call_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ bool nofd = msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
+ int idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+
+ debug("u64: 0x%016"PRIx64, msg->payload.u64);
+
+ vu_check_queue_msg_file(msg);
+
+ if (vdev->vq[idx].call_fd != -1) {
+ close(vdev->vq[idx].call_fd);
+ vdev->vq[idx].call_fd = -1;
+ }
+
+ if (!nofd)
+ vdev->vq[idx].call_fd = msg->fds[0];
+
+ /* in case of I/O hang after reconnecting */
+ if (vdev->vq[idx].call_fd != -1)
+ eventfd_write(msg->fds[0], 1);
+
+ debug("Got call_fd: %d for vq: %d", vdev->vq[idx].call_fd, idx);
+
+ return false;
+}
+
+/**
+ * vu_set_vring_err_exec() - Set the event file descriptor to signal when
+ * error occurs
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ */
+static bool vu_set_vring_err_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ bool nofd = msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
+ int idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+
+ debug("u64: 0x%016"PRIx64, msg->payload.u64);
+
+ vu_check_queue_msg_file(msg);
+
+ if (vdev->vq[idx].err_fd != -1) {
+ close(vdev->vq[idx].err_fd);
+ vdev->vq[idx].err_fd = -1;
+ }
+
+ if (!nofd)
+ vdev->vq[idx].err_fd = msg->fds[0];
+
+ return false;
+}
+
+/**
+ * vu_get_protocol_features_exec() - Provide the protocol (vhost-user) features
+ * to the front-end
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: True as a reply is requested
+ */
+static bool vu_get_protocol_features_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ uint64_t features = 1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK;
+
+ (void)vdev;
+ vmsg_set_reply_u64(msg, features);
+
+ return true;
+}
+
+/**
+ * vu_set_protocol_features_exec() - Enable protocol (vhost-user) features
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ */
+static bool vu_set_protocol_features_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ uint64_t features = msg->payload.u64;
+
+ debug("u64: 0x%016"PRIx64, features);
+
+ vdev->protocol_features = msg->payload.u64;
+
+ return false;
+}
+
+/**
+ * vu_get_queue_num_exec() - Tell how many queues we support
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: True as a reply is requested
+ */
+static bool vu_get_queue_num_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ (void)vdev;
+
+ vmsg_set_reply_u64(msg, VHOST_USER_MAX_QUEUES);
+
+ return true;
+}
+
+/**
+ * vu_set_vring_enable_exec() - Enable or disable corresponding vring
+ * @vdev: vhost-user device
+ * @vmsg: vhost-user message
+ *
+ * Return: False as no reply is requested
+ */
+static bool vu_set_vring_enable_exec(struct vu_dev *vdev,
+ struct vhost_user_msg *msg)
+{
+ unsigned int enable = msg->payload.state.num;
+ unsigned int idx = msg->payload.state.index;
+
+ debug("State.index: %u", idx);
+ debug("State.enable: %u", enable);
+
+ if (idx >= VHOST_USER_MAX_QUEUES)
+ die("Invalid vring_enable index: %u", idx);
+
+ vdev->vq[idx].enable = enable;
+ return false;
+}
+
+/**
+ * vu_init() - Initialize vhost-user device structure
+ * @c: execution context
+ * @vdev: vhost-user device
+ */
+/* cppcheck-suppress unusedFunction */
+void vu_init(struct ctx *c, struct vu_dev *vdev)
+{
+ int i;
+
+ vdev->context = c;
+ for (i = 0; i < VHOST_USER_MAX_QUEUES; i++) {
+ vdev->vq[i] = (struct vu_virtq){
+ .call_fd = -1,
+ .kick_fd = -1,
+ .err_fd = -1,
+ .notification = true,
+ };
+ }
+}
+
+/**
+ * vu_cleanup() - Reset vhost-user device
+ * @vdev: vhost-user device
+ */
+/* cppcheck-suppress unusedFunction */
+void vu_cleanup(struct vu_dev *vdev)
+{
+ unsigned int i;
+
+ for (i = 0; i < VHOST_USER_MAX_QUEUES; i++) {
+ struct vu_virtq *vq = &vdev->vq[i];
+
+ vq->started = false;
+ vq->notification = true;
+
+ if (vq->call_fd != -1) {
+ close(vq->call_fd);
+ vq->call_fd = -1;
+ }
+ if (vq->err_fd != -1) {
+ close(vq->err_fd);
+ vq->err_fd = -1;
+ }
+ if (vq->kick_fd != -1) {
+ vu_remove_watch(vdev, vq->kick_fd);
+ close(vq->kick_fd);
+ vq->kick_fd = -1;
+ }
+
+ vq->vring.desc = 0;
+ vq->vring.used = 0;
+ vq->vring.avail = 0;
+ }
+
+ for (i = 0; i < vdev->nregions; i++) {
+ const struct vu_dev_region *r = &vdev->regions[i];
+ /* NOLINTNEXTLINE(performance-no-int-to-ptr) */
+ void *m = (void *)r->mmap_addr;
+
+ if (m)
+ munmap(m, r->size + r->mmap_offset);
+ }
+ vdev->nregions = 0;
+}
+
+/**
+ * vu_sock_reset() - Reset connection socket
+ * @vdev: vhost-user device
+ */
+static void vu_sock_reset(struct vu_dev *vdev)
+{
+ /* Placeholder to add passt related code */
+ (void)vdev;
+}
+
+static bool (*vu_handle[VHOST_USER_MAX])(struct vu_dev *vdev,
+ struct vhost_user_msg *msg) = {
+ [VHOST_USER_GET_FEATURES] = vu_get_features_exec,
+ [VHOST_USER_SET_FEATURES] = vu_set_features_exec,
+ [VHOST_USER_GET_PROTOCOL_FEATURES] = vu_get_protocol_features_exec,
+ [VHOST_USER_SET_PROTOCOL_FEATURES] = vu_set_protocol_features_exec,
+ [VHOST_USER_GET_QUEUE_NUM] = vu_get_queue_num_exec,
+ [VHOST_USER_SET_OWNER] = vu_set_owner_exec,
+ [VHOST_USER_SET_MEM_TABLE] = vu_set_mem_table_exec,
+ [VHOST_USER_SET_VRING_NUM] = vu_set_vring_num_exec,
+ [VHOST_USER_SET_VRING_ADDR] = vu_set_vring_addr_exec,
+ [VHOST_USER_SET_VRING_BASE] = vu_set_vring_base_exec,
+ [VHOST_USER_GET_VRING_BASE] = vu_get_vring_base_exec,
+ [VHOST_USER_SET_VRING_KICK] = vu_set_vring_kick_exec,
+ [VHOST_USER_SET_VRING_CALL] = vu_set_vring_call_exec,
+ [VHOST_USER_SET_VRING_ERR] = vu_set_vring_err_exec,
+ [VHOST_USER_SET_VRING_ENABLE] = vu_set_vring_enable_exec,
+};
+
+/**
+ * vu_control_handler() - Handle control commands for vhost-user
+ * @vdev: vhost-user device
+ * @fd: vhost-user message socket
+ * @events: epoll events
+ */
+/* cppcheck-suppress unusedFunction */
+void vu_control_handler(struct vu_dev *vdev, int fd, uint32_t events)
+{
+ struct vhost_user_msg msg = { 0 };
+ bool need_reply, reply_requested;
+ int ret;
+
+ if (events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) {
+ vu_sock_reset(vdev);
+ return;
+ }
+
+ ret = vu_message_read_default(fd, &msg);
+ if (ret == 0) {
+ vu_sock_reset(vdev);
+ return;
+ }
+ debug("================ Vhost user message ================");
+ debug("Request: %s (%d)", vu_request_to_string(msg.hdr.request),
+ msg.hdr.request);
+ debug("Flags: 0x%x", msg.hdr.flags);
+ debug("Size: %u", msg.hdr.size);
+
+ need_reply = msg.hdr.flags & VHOST_USER_NEED_REPLY_MASK;
+
+ if (msg.hdr.request >= 0 && msg.hdr.request < VHOST_USER_MAX &&
+ vu_handle[msg.hdr.request])
+ reply_requested = vu_handle[msg.hdr.request](vdev, &msg);
+ else
+ die("Unhandled request: %d", msg.hdr.request);
+
+ /* cppcheck-suppress legacyUninitvar */
+ if (!reply_requested && need_reply) {
+ msg.payload.u64 = 0;
+ msg.hdr.flags = 0;
+ msg.hdr.size = sizeof(msg.payload.u64);
+ msg.fd_num = 0;
+ reply_requested = true;
+ }
+
+ if (reply_requested)
+ vu_send_reply(fd, &msg);
+}
diff --git a/vhost_user.h b/vhost_user.h
new file mode 100644
index 000000000000..5af349ba58b8
--- /dev/null
+++ b/vhost_user.h
@@ -0,0 +1,208 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * vhost-user API, command management and virtio interface
+ *
+ * Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ */
+
+/* some parts from subprojects/libvhost-user/libvhost-user.h */
+
+#ifndef VHOST_USER_H
+#define VHOST_USER_H
+
+#include "virtio.h"
+#include "iov.h"
+
+#define VHOST_USER_F_PROTOCOL_FEATURES 30
+
+#define VHOST_MEMORY_BASELINE_NREGIONS 8
+
+/**
+ * enum vhost_user_protocol_feature - List of available vhost-user features
+ */
+enum vhost_user_protocol_feature {
+ VHOST_USER_PROTOCOL_F_MQ = 0,
+ VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
+ VHOST_USER_PROTOCOL_F_RARP = 2,
+ VHOST_USER_PROTOCOL_F_REPLY_ACK = 3,
+ VHOST_USER_PROTOCOL_F_NET_MTU = 4,
+ VHOST_USER_PROTOCOL_F_BACKEND_REQ = 5,
+ VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6,
+ VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7,
+ VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
+ VHOST_USER_PROTOCOL_F_CONFIG = 9,
+ VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD = 10,
+ VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11,
+ VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD = 12,
+ VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS = 14,
+ VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS = 15,
+
+ VHOST_USER_PROTOCOL_F_MAX
+};
+
+/**
+ * enum vhost_user_request - List of available vhost-user requests
+ */
+enum vhost_user_request {
+ VHOST_USER_NONE = 0,
+ VHOST_USER_GET_FEATURES = 1,
+ VHOST_USER_SET_FEATURES = 2,
+ VHOST_USER_SET_OWNER = 3,
+ VHOST_USER_RESET_OWNER = 4,
+ VHOST_USER_SET_MEM_TABLE = 5,
+ VHOST_USER_SET_LOG_BASE = 6,
+ VHOST_USER_SET_LOG_FD = 7,
+ VHOST_USER_SET_VRING_NUM = 8,
+ VHOST_USER_SET_VRING_ADDR = 9,
+ VHOST_USER_SET_VRING_BASE = 10,
+ VHOST_USER_GET_VRING_BASE = 11,
+ VHOST_USER_SET_VRING_KICK = 12,
+ VHOST_USER_SET_VRING_CALL = 13,
+ VHOST_USER_SET_VRING_ERR = 14,
+ VHOST_USER_GET_PROTOCOL_FEATURES = 15,
+ VHOST_USER_SET_PROTOCOL_FEATURES = 16,
+ VHOST_USER_GET_QUEUE_NUM = 17,
+ VHOST_USER_SET_VRING_ENABLE = 18,
+ VHOST_USER_SEND_RARP = 19,
+ VHOST_USER_NET_SET_MTU = 20,
+ VHOST_USER_SET_BACKEND_REQ_FD = 21,
+ VHOST_USER_IOTLB_MSG = 22,
+ VHOST_USER_SET_VRING_ENDIAN = 23,
+ VHOST_USER_GET_CONFIG = 24,
+ VHOST_USER_SET_CONFIG = 25,
+ VHOST_USER_CREATE_CRYPTO_SESSION = 26,
+ VHOST_USER_CLOSE_CRYPTO_SESSION = 27,
+ VHOST_USER_POSTCOPY_ADVISE = 28,
+ VHOST_USER_POSTCOPY_LISTEN = 29,
+ VHOST_USER_POSTCOPY_END = 30,
+ VHOST_USER_GET_INFLIGHT_FD = 31,
+ VHOST_USER_SET_INFLIGHT_FD = 32,
+ VHOST_USER_GPU_SET_SOCKET = 33,
+ VHOST_USER_VRING_KICK = 35,
+ VHOST_USER_GET_MAX_MEM_SLOTS = 36,
+ VHOST_USER_ADD_MEM_REG = 37,
+ VHOST_USER_REM_MEM_REG = 38,
+ VHOST_USER_MAX
+};
+
+/**
+ * struct vhost_user_header - vhost-user message header
+ * @request: Request type of the message
+ * @flags: Request flags
+ * @size: The following payload size
+ */
+struct vhost_user_header {
+ enum vhost_user_request request;
+
+#define VHOST_USER_VERSION_MASK 0x3
+#define VHOST_USER_REPLY_MASK (0x1 << 2)
+#define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
+ uint32_t flags;
+ uint32_t size;
+} __attribute__ ((__packed__));
+
+/**
+ * struct vhost_user_memory_region - Front-end shared memory region information
+ * @guest_phys_addr: Guest physical address of the region
+ * @memory_size: Memory size
+ * @userspace_addr: front-end (QEMU) userspace address
+ * @mmap_offset: region offset in the shared memory area
+ */
+struct vhost_user_memory_region {
+ uint64_t guest_phys_addr;
+ uint64_t memory_size;
+ uint64_t userspace_addr;
+ uint64_t mmap_offset;
+};
+
+/**
+ * struct vhost_user_memory - List of all the shared memory regions
+ * @nregions: Number of memory regions
+ * @padding: Padding
+ * @regions: Memory regions list
+ */
+struct vhost_user_memory {
+ uint32_t nregions;
+ uint32_t padding;
+ struct vhost_user_memory_region regions[VHOST_MEMORY_BASELINE_NREGIONS];
+};
+
+/**
+ * union vhost_user_payload - vhost-user message payload
+ * @u64: 64-bit payload
+ * @state: vring state payload
+ * @addr: vring addresses payload
+ * vhost_user_memory: Memory regions information payload
+ */
+union vhost_user_payload {
+#define VHOST_USER_VRING_IDX_MASK 0xff
+#define VHOST_USER_VRING_NOFD_MASK (0x1 << 8)
+ uint64_t u64;
+ struct vhost_vring_state state;
+ struct vhost_vring_addr addr;
+ struct vhost_user_memory memory;
+};
+
+/**
+ * struct vhost_user_msg - vhost-use message
+ * @hdr: Message header
+ * @payload: Message payload
+ * @fds: File descriptors associated with the message
+ * in the ancillary data.
+ * (shared memory or event file descriptors)
+ * @fd_num: Number of file descriptors
+ */
+struct vhost_user_msg {
+ struct vhost_user_header hdr;
+ union vhost_user_payload payload;
+
+ int fds[VHOST_MEMORY_BASELINE_NREGIONS];
+ int fd_num;
+} __attribute__ ((__packed__));
+#define VHOST_USER_HDR_SIZE sizeof(struct vhost_user_header)
+
+/* index of the RX virtqueue */
+#define VHOST_USER_RX_QUEUE 0
+/* index of the TX virtqueue */
+#define VHOST_USER_TX_QUEUE 1
+
+/* in case of multiqueue, the RX and TX queues are interleaved */
+#define VHOST_USER_IS_QUEUE_TX(n) (n % 2)
+#define VHOST_USER_IS_QUEUE_RX(n) (!(n % 2))
+
+/* Default virtio-net header for passt */
+#define VU_HEADER ((struct virtio_net_hdr){ \
+ .flags = VIRTIO_NET_HDR_F_DATA_VALID, \
+ .gso_type = VIRTIO_NET_HDR_GSO_NONE, \
+})
+
+/**
+ * vu_queue_enabled - Return state of a virtqueue
+ * @vq: virtqueue to check
+ *
+ * Return: true if the virqueue is enabled, false otherwise
+ */
+/* cppcheck-suppress unusedFunction */
+static inline bool vu_queue_enabled(const struct vu_virtq *vq)
+{
+ return vq->enable;
+}
+
+/**
+ * vu_queue_started - Return state of a virtqueue
+ * @vq: virtqueue to check
+ *
+ * Return: true if the virqueue is started, false otherwise
+ */
+/* cppcheck-suppress unusedFunction */
+static inline bool vu_queue_started(const struct vu_virtq *vq)
+{
+ return vq->started;
+}
+
+void vu_print_capabilities(void);
+void vu_init(struct ctx *c, struct vu_dev *vdev);
+void vu_cleanup(struct vu_dev *vdev);
+void vu_control_handler(struct vu_dev *vdev, int fd, uint32_t events);
+#endif /* VHOST_USER_H */
diff --git a/virtio.h b/virtio.h
index 94efeb049fbc..6410d60f9b3f 100644
--- a/virtio.h
+++ b/virtio.h
@@ -105,6 +105,7 @@ struct vu_dev_region {
* @protocol_features: Vhost-user protocol features
*/
struct vu_dev {
+ struct ctx *context;
uint32_t nregions;
struct vu_dev_region regions[VHOST_USER_MAX_RAM_SLOTS];
struct vu_virtq vq[VHOST_USER_MAX_QUEUES];
--
@@ -105,6 +105,7 @@ struct vu_dev_region {
* @protocol_features: Vhost-user protocol features
*/
struct vu_dev {
+ struct ctx *context;
uint32_t nregions;
struct vu_dev_region regions[VHOST_USER_MAX_RAM_SLOTS];
struct vu_virtq vq[VHOST_USER_MAX_QUEUES];
--
2.46.2
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH v7 4/8] udp: Prepare udp.c to be shared with vhost-user
2024-10-09 9:07 [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Laurent Vivier
` (2 preceding siblings ...)
2024-10-09 9:07 ` [PATCH v7 3/8] vhost-user: introduce vhost-user API Laurent Vivier
@ 2024-10-09 9:07 ` Laurent Vivier
2024-10-09 9:07 ` [PATCH v7 5/8] tcp: Export headers functions Laurent Vivier
` (5 subsequent siblings)
9 siblings, 0 replies; 18+ messages in thread
From: Laurent Vivier @ 2024-10-09 9:07 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Export udp_payload_t, udp_update_hdr4(), udp_update_hdr6() and
udp_sock_errs().
Rename udp_listen_sock_handler() to udp_buf_listen_sock_handler() and
udp_reply_sock_handler to udp_buf_reply_sock_handler().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
udp.c | 74 ++++++++++++++++++++++++++++++--------------------
udp_internal.h | 34 +++++++++++++++++++++++
2 files changed, 79 insertions(+), 29 deletions(-)
create mode 100644 udp_internal.h
diff --git a/udp.c b/udp.c
index 100610f2472e..8fc5d8099310 100644
--- a/udp.c
+++ b/udp.c
@@ -109,8 +109,7 @@
#include "pcap.h"
#include "log.h"
#include "flow_table.h"
-
-#define UDP_MAX_FRAMES 32 /* max # of frames to receive at once */
+#include "udp_internal.h"
/* "Spliced" sockets indexed by bound port (host order) */
static int udp_splice_ns [IP_VERSIONS][NUM_PORTS];
@@ -118,20 +117,8 @@ static int udp_splice_init[IP_VERSIONS][NUM_PORTS];
/* Static buffers */
-/**
- * struct udp_payload_t - UDP header and data for inbound messages
- * @uh: UDP header
- * @data: UDP data
- */
-static struct udp_payload_t {
- struct udphdr uh;
- char data[USHRT_MAX - sizeof(struct udphdr)];
-#ifdef __AVX2__
-} __attribute__ ((packed, aligned(32)))
-#else
-} __attribute__ ((packed, aligned(__alignof__(unsigned int))))
-#endif
-udp_payload[UDP_MAX_FRAMES];
+/* UDP header and data for inbound messages */
+static struct udp_payload_t udp_payload[UDP_MAX_FRAMES];
/* Ethernet header for IPv4 frames */
static struct ethhdr udp4_eth_hdr;
@@ -302,9 +289,9 @@ static void udp_splice_send(const struct ctx *c, size_t start, size_t n,
*
* Return: size of IPv4 payload (UDP header + data)
*/
-static size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
- const struct flowside *toside, size_t dlen,
- bool no_udp_csum)
+size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
+ const struct flowside *toside, size_t dlen,
+ bool no_udp_csum)
{
const struct in_addr *src = inany_v4(&toside->oaddr);
const struct in_addr *dst = inany_v4(&toside->eaddr);
@@ -345,9 +332,9 @@ static size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
*
* Return: size of IPv6 payload (UDP header + data)
*/
-static size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udp_payload_t *bp,
- const struct flowside *toside, size_t dlen,
- bool no_udp_csum)
+size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udp_payload_t *bp,
+ const struct flowside *toside, size_t dlen,
+ bool no_udp_csum)
{
uint16_t l4len = dlen + sizeof(bp->uh);
@@ -477,7 +464,7 @@ static int udp_sock_recverr(int s)
*
* Return: Number of errors handled, or < 0 if we have an unrecoverable error
*/
-static int udp_sock_errs(const struct ctx *c, int s, uint32_t events)
+int udp_sock_errs(const struct ctx *c, int s, uint32_t events)
{
unsigned n_err = 0;
socklen_t errlen;
@@ -554,7 +541,7 @@ static int udp_sock_recv(const struct ctx *c, int s, uint32_t events,
}
/**
- * udp_listen_sock_handler() - Handle new data from socket
+ * udp_buf_listen_sock_handler() - Handle new data from socket
* @c: Execution context
* @ref: epoll reference
* @events: epoll events bitmap
@@ -562,8 +549,9 @@ static int udp_sock_recv(const struct ctx *c, int s, uint32_t events,
*
* #syscalls recvmmsg
*/
-void udp_listen_sock_handler(const struct ctx *c, union epoll_ref ref,
- uint32_t events, const struct timespec *now)
+static void udp_buf_listen_sock_handler(const struct ctx *c,
+ union epoll_ref ref, uint32_t events,
+ const struct timespec *now)
{
const socklen_t sasize = sizeof(udp_meta[0].s_in);
int n, i;
@@ -630,7 +618,21 @@ void udp_listen_sock_handler(const struct ctx *c, union epoll_ref ref,
}
/**
- * udp_reply_sock_handler() - Handle new data from flow specific socket
+ * udp_listen_sock_handler() - Handle new data from socket
+ * @c: Execution context
+ * @ref: epoll reference
+ * @events: epoll events bitmap
+ * @now: Current timestamp
+ */
+void udp_listen_sock_handler(const struct ctx *c,
+ union epoll_ref ref, uint32_t events,
+ const struct timespec *now)
+{
+ udp_buf_listen_sock_handler(c, ref, events, now);
+}
+
+/**
+ * udp_buf_reply_sock_handler() - Handle new data from flow specific socket
* @c: Execution context
* @ref: epoll reference
* @events: epoll events bitmap
@@ -638,8 +640,9 @@ void udp_listen_sock_handler(const struct ctx *c, union epoll_ref ref,
*
* #syscalls recvmmsg
*/
-void udp_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
- uint32_t events, const struct timespec *now)
+static void udp_buf_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
+ uint32_t events,
+ const struct timespec *now)
{
flow_sidx_t tosidx = flow_sidx_opposite(ref.flowside);
const struct flowside *toside = flowside_at_sidx(tosidx);
@@ -684,6 +687,19 @@ void udp_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
}
}
+/**
+ * udp_reply_sock_handler() - Handle new data from flow specific socket
+ * @c: Execution context
+ * @ref: epoll reference
+ * @events: epoll events bitmap
+ * @now: Current timestamp
+ */
+void udp_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
+ uint32_t events, const struct timespec *now)
+{
+ udp_buf_reply_sock_handler(c, ref, events, now);
+}
+
/**
* udp_tap_handler() - Handle packets from tap
* @c: Execution context
diff --git a/udp_internal.h b/udp_internal.h
new file mode 100644
index 000000000000..cc80e3055423
--- /dev/null
+++ b/udp_internal.h
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2021 Red Hat GmbH
+ * Author: Stefano Brivio <sbrivio@redhat.com>
+ */
+
+#ifndef UDP_INTERNAL_H
+#define UDP_INTERNAL_H
+
+#include "tap.h" /* needed by udp_meta_t */
+
+#define UDP_MAX_FRAMES 32 /* max # of frames to receive at once */
+
+/**
+ * struct udp_payload_t - UDP header and data for inbound messages
+ * @uh: UDP header
+ * @data: UDP data
+ */
+struct udp_payload_t {
+ struct udphdr uh;
+ char data[USHRT_MAX - sizeof(struct udphdr)];
+#ifdef __AVX2__
+} __attribute__ ((packed, aligned(32)));
+#else
+} __attribute__ ((packed, aligned(__alignof__(unsigned int))));
+#endif
+
+size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
+ const struct flowside *toside, size_t dlen,
+ bool no_udp_csum);
+size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udp_payload_t *bp,
+ const struct flowside *toside, size_t dlen,
+ bool no_udp_csum);
+int udp_sock_errs(const struct ctx *c, int s, uint32_t events);
+#endif /* UDP_INTERNAL_H */
--
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2021 Red Hat GmbH
+ * Author: Stefano Brivio <sbrivio@redhat.com>
+ */
+
+#ifndef UDP_INTERNAL_H
+#define UDP_INTERNAL_H
+
+#include "tap.h" /* needed by udp_meta_t */
+
+#define UDP_MAX_FRAMES 32 /* max # of frames to receive at once */
+
+/**
+ * struct udp_payload_t - UDP header and data for inbound messages
+ * @uh: UDP header
+ * @data: UDP data
+ */
+struct udp_payload_t {
+ struct udphdr uh;
+ char data[USHRT_MAX - sizeof(struct udphdr)];
+#ifdef __AVX2__
+} __attribute__ ((packed, aligned(32)));
+#else
+} __attribute__ ((packed, aligned(__alignof__(unsigned int))));
+#endif
+
+size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
+ const struct flowside *toside, size_t dlen,
+ bool no_udp_csum);
+size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udp_payload_t *bp,
+ const struct flowside *toside, size_t dlen,
+ bool no_udp_csum);
+int udp_sock_errs(const struct ctx *c, int s, uint32_t events);
+#endif /* UDP_INTERNAL_H */
--
2.46.2
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH v7 5/8] tcp: Export headers functions
2024-10-09 9:07 [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Laurent Vivier
` (3 preceding siblings ...)
2024-10-09 9:07 ` [PATCH v7 4/8] udp: Prepare udp.c to be shared with vhost-user Laurent Vivier
@ 2024-10-09 9:07 ` Laurent Vivier
2024-10-09 9:07 ` [PATCH v7 6/8] passt: rename tap_sock_init() to tap_backend_init() Laurent Vivier
` (4 subsequent siblings)
9 siblings, 0 replies; 18+ messages in thread
From: Laurent Vivier @ 2024-10-09 9:07 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Export tcp_fill_headers[4|6]() and tcp_update_check_tcp[4|6]().
They'll be needed by vhost-user.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
tcp.c | 30 +++++++++++++++---------------
tcp_internal.h | 15 +++++++++++++++
2 files changed, 30 insertions(+), 15 deletions(-)
diff --git a/tcp.c b/tcp.c
index 9617b7ac2404..eae02b1647e3 100644
--- a/tcp.c
+++ b/tcp.c
@@ -761,9 +761,9 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
* @iov_cnt: Length of the array
* @l4offset: IPv4 payload offset in the iovec array
*/
-static void tcp_update_check_tcp4(const struct iphdr *iph,
- const struct iovec *iov, int iov_cnt,
- size_t l4offset)
+void tcp_update_check_tcp4(const struct iphdr *iph,
+ const struct iovec *iov, int iov_cnt,
+ size_t l4offset)
{
uint16_t l4len = ntohs(iph->tot_len) - sizeof(struct iphdr);
struct in_addr saddr = { .s_addr = iph->saddr };
@@ -813,9 +813,9 @@ static void tcp_update_check_tcp4(const struct iphdr *iph,
* @iov_cnt: Length of the array
* @l4offset: IPv6 payload offset in the iovec array
*/
-static void tcp_update_check_tcp6(const struct ipv6hdr *ip6h,
- const struct iovec *iov, int iov_cnt,
- size_t l4offset)
+void tcp_update_check_tcp6(const struct ipv6hdr *ip6h,
+ const struct iovec *iov, int iov_cnt,
+ size_t l4offset)
{
uint16_t l4len = ntohs(ip6h->payload_len);
size_t check_ofs;
@@ -982,11 +982,11 @@ static void tcp_fill_header(struct tcphdr *th,
*
* Return: The IPv4 payload length, host order
*/
-static size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
- struct tap_hdr *taph,
- struct iphdr *iph, struct tcp_payload_t *bp,
- size_t dlen, const uint16_t *check,
- uint32_t seq, bool no_tcp_csum)
+size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
+ struct tap_hdr *taph,
+ struct iphdr *iph, struct tcp_payload_t *bp,
+ size_t dlen, const uint16_t *check,
+ uint32_t seq, bool no_tcp_csum)
{
const struct flowside *tapside = TAPFLOW(conn);
const struct in_addr *src4 = inany_v4(&tapside->oaddr);
@@ -1034,10 +1034,10 @@ static size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
*
* Return: The IPv6 payload length, host order
*/
-static size_t tcp_fill_headers6(const struct tcp_tap_conn *conn,
- struct tap_hdr *taph,
- struct ipv6hdr *ip6h, struct tcp_payload_t *bp,
- size_t dlen, uint32_t seq, bool no_tcp_csum)
+size_t tcp_fill_headers6(const struct tcp_tap_conn *conn,
+ struct tap_hdr *taph,
+ struct ipv6hdr *ip6h, struct tcp_payload_t *bp,
+ size_t dlen, uint32_t seq, bool no_tcp_csum)
{
const struct flowside *tapside = TAPFLOW(conn);
size_t l4len = dlen + sizeof(bp->th);
diff --git a/tcp_internal.h b/tcp_internal.h
index 2f74ffeff8f3..8e87f98b470f 100644
--- a/tcp_internal.h
+++ b/tcp_internal.h
@@ -118,6 +118,21 @@ void tcp_rst_do(const struct ctx *c, struct tcp_tap_conn *conn);
tcp_rst_do(c, conn); \
} while (0)
+void tcp_update_check_tcp4(const struct iphdr *iph,
+ const struct iovec *iov, int iov_cnt,
+ size_t l4offset);
+void tcp_update_check_tcp6(const struct ipv6hdr *ip6h,
+ const struct iovec *iov, int iov_cnt,
+ size_t l4offset);
+size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
+ struct tap_hdr *taph,
+ struct iphdr *iph, struct tcp_payload_t *bp,
+ size_t dlen, const uint16_t *check,
+ uint32_t seq, bool no_tcp_csum);
+size_t tcp_fill_headers6(const struct tcp_tap_conn *conn,
+ struct tap_hdr *taph,
+ struct ipv6hdr *ip6h, struct tcp_payload_t *bp,
+ size_t dlen, uint32_t seq, bool no_tcp_csum);
size_t tcp_l2_buf_fill_headers(const struct tcp_tap_conn *conn,
struct iovec *iov, size_t dlen,
const uint16_t *check, uint32_t seq,
--
@@ -118,6 +118,21 @@ void tcp_rst_do(const struct ctx *c, struct tcp_tap_conn *conn);
tcp_rst_do(c, conn); \
} while (0)
+void tcp_update_check_tcp4(const struct iphdr *iph,
+ const struct iovec *iov, int iov_cnt,
+ size_t l4offset);
+void tcp_update_check_tcp6(const struct ipv6hdr *ip6h,
+ const struct iovec *iov, int iov_cnt,
+ size_t l4offset);
+size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
+ struct tap_hdr *taph,
+ struct iphdr *iph, struct tcp_payload_t *bp,
+ size_t dlen, const uint16_t *check,
+ uint32_t seq, bool no_tcp_csum);
+size_t tcp_fill_headers6(const struct tcp_tap_conn *conn,
+ struct tap_hdr *taph,
+ struct ipv6hdr *ip6h, struct tcp_payload_t *bp,
+ size_t dlen, uint32_t seq, bool no_tcp_csum);
size_t tcp_l2_buf_fill_headers(const struct tcp_tap_conn *conn,
struct iovec *iov, size_t dlen,
const uint16_t *check, uint32_t seq,
--
2.46.2
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH v7 6/8] passt: rename tap_sock_init() to tap_backend_init()
2024-10-09 9:07 [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Laurent Vivier
` (4 preceding siblings ...)
2024-10-09 9:07 ` [PATCH v7 5/8] tcp: Export headers functions Laurent Vivier
@ 2024-10-09 9:07 ` Laurent Vivier
2024-10-09 9:07 ` [PATCH v7 7/8] vhost-user: add vhost-user Laurent Vivier
` (3 subsequent siblings)
9 siblings, 0 replies; 18+ messages in thread
From: Laurent Vivier @ 2024-10-09 9:07 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Extract pool storage initialization loop to tap_sock_update_pool(),
extract QEMU hints to tap_backend_show_hints().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
passt.c | 2 +-
tap.c | 56 +++++++++++++++++++++++++++++++++++++++++---------------
tap.h | 2 +-
3 files changed, 43 insertions(+), 17 deletions(-)
diff --git a/passt.c b/passt.c
index ad6f0bc32df6..79093ee02d62 100644
--- a/passt.c
+++ b/passt.c
@@ -261,7 +261,7 @@ int main(int argc, char **argv)
pasta_netns_quit_init(&c);
- tap_sock_init(&c);
+ tap_backend_init(&c);
secret_init(&c);
diff --git a/tap.c b/tap.c
index c53a39b79e62..4b826fdf7adc 100644
--- a/tap.c
+++ b/tap.c
@@ -1188,11 +1188,31 @@ int tap_sock_unix_open(char *sock_path)
return fd;
}
+/**
+ * tap_backend_show_hints() - Give help information to start QEMU
+ * @c: Execution context
+ */
+static void tap_backend_show_hints(struct ctx *c)
+{
+ switch(c->mode) {
+ case MODE_PASTA:
+ /* No hints */
+ break;
+ case MODE_PASST:
+ info("\nYou can now start qemu (>= 7.2, with commit 13c6be96618c):");
+ info(" kvm ... -device virtio-net-pci,netdev=s -netdev stream,id=s,server=off,addr.type=unix,addr.path=%s",
+ c->sock_path);
+ info("or qrap, for earlier qemu versions:");
+ info(" ./qrap 5 kvm ... -net socket,fd=5 -net nic,model=virtio");
+ break;
+ }
+}
+
/**
* tap_sock_unix_init() - Start listening for connections on AF_UNIX socket
* @c: Execution context
*/
-static void tap_sock_unix_init(struct ctx *c)
+static void tap_sock_unix_init(const struct ctx *c)
{
union epoll_ref ref = { .type = EPOLL_TYPE_TAP_LISTEN };
struct epoll_event ev = { 0 };
@@ -1203,12 +1223,6 @@ static void tap_sock_unix_init(struct ctx *c)
ev.events = EPOLLIN | EPOLLET;
ev.data.u64 = ref.u64;
epoll_ctl(c->epollfd, EPOLL_CTL_ADD, c->fd_tap_listen, &ev);
-
- info("\nYou can now start qemu (>= 7.2, with commit 13c6be96618c):");
- info(" kvm ... -device virtio-net-pci,netdev=s -netdev stream,id=s,server=off,addr.type=unix,addr.path=%s",
- c->sock_path);
- info("or qrap, for earlier qemu versions:");
- info(" ./qrap 5 kvm ... -net socket,fd=5 -net nic,model=virtio");
}
/**
@@ -1321,21 +1335,31 @@ static void tap_sock_tun_init(struct ctx *c)
}
/**
- * tap_sock_init() - Create and set up AF_UNIX socket or tuntap file descriptor
- * @c: Execution context
+ * tap_sock_update_pool() - Set the buffer base and size for the pool of packets
+ * @base: Buffer base
+ * @size Buffer size
*/
-void tap_sock_init(struct ctx *c)
+static void tap_sock_update_pool(void *base, size_t size)
{
- size_t sz = sizeof(pkt_buf);
int i;
- pool_tap4_storage = PACKET_INIT(pool_tap4, TAP_MSGS, pkt_buf, sz);
- pool_tap6_storage = PACKET_INIT(pool_tap6, TAP_MSGS, pkt_buf, sz);
+ pool_tap4_storage = PACKET_INIT(pool_tap4, TAP_MSGS, base, size);
+ pool_tap6_storage = PACKET_INIT(pool_tap6, TAP_MSGS, base, size);
for (i = 0; i < TAP_SEQS; i++) {
- tap4_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, pkt_buf, sz);
- tap6_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, pkt_buf, sz);
+ tap4_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, base, size);
+ tap6_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, base, size);
}
+}
+
+/**
+ * tap_backend_init() - Create and set up AF_UNIX socket or
+ * tuntap file descriptor
+ * @c: Execution context
+ */
+void tap_backend_init(struct ctx *c)
+{
+ tap_sock_update_pool(pkt_buf, sizeof(pkt_buf));
if (c->fd_tap != -1) { /* Passed as --fd */
struct epoll_event ev = { 0 };
@@ -1365,4 +1389,6 @@ void tap_sock_init(struct ctx *c)
*/
memset(&c->guest_mac, 0xff, sizeof(c->guest_mac));
}
+
+ tap_backend_show_hints(c);
}
diff --git a/tap.h b/tap.h
index 85f1e8473711..8728cc5c09c3 100644
--- a/tap.h
+++ b/tap.h
@@ -68,7 +68,7 @@ void tap_handler_pasta(struct ctx *c, uint32_t events,
void tap_handler_passt(struct ctx *c, uint32_t events,
const struct timespec *now);
int tap_sock_unix_open(char *sock_path);
-void tap_sock_init(struct ctx *c);
+void tap_backend_init(struct ctx *c);
void tap_flush_pools(void);
void tap_handler(struct ctx *c, const struct timespec *now);
void tap_add_packet(struct ctx *c, ssize_t l2len, char *p);
--
@@ -68,7 +68,7 @@ void tap_handler_pasta(struct ctx *c, uint32_t events,
void tap_handler_passt(struct ctx *c, uint32_t events,
const struct timespec *now);
int tap_sock_unix_open(char *sock_path);
-void tap_sock_init(struct ctx *c);
+void tap_backend_init(struct ctx *c);
void tap_flush_pools(void);
void tap_handler(struct ctx *c, const struct timespec *now);
void tap_add_packet(struct ctx *c, ssize_t l2len, char *p);
--
2.46.2
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH v7 7/8] vhost-user: add vhost-user
2024-10-09 9:07 [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Laurent Vivier
` (5 preceding siblings ...)
2024-10-09 9:07 ` [PATCH v7 6/8] passt: rename tap_sock_init() to tap_backend_init() Laurent Vivier
@ 2024-10-09 9:07 ` Laurent Vivier
2024-10-09 9:07 ` [PATCH v7 8/8] test: Add tests for passt in vhost-user mode Laurent Vivier
` (2 subsequent siblings)
9 siblings, 0 replies; 18+ messages in thread
From: Laurent Vivier @ 2024-10-09 9:07 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
add virtio and vhost-user functions to connect with QEMU.
$ ./passt --vhost-user
and
# qemu-system-x86_64 ... -m 4G \
-object memory-backend-memfd,id=memfd0,share=on,size=4G \
-numa node,memdev=memfd0 \
-chardev socket,id=chr0,path=/tmp/passt_1.socket \
-netdev vhost-user,id=netdev0,chardev=chr0 \
-device virtio-net,mac=9a:2b:2c:2d:2e:2f,netdev=netdev0 \
...
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
Makefile | 6 +-
conf.c | 21 ++-
epoll_type.h | 4 +
iov.c | 1 -
isolation.c | 15 +-
packet.c | 11 ++
packet.h | 8 +-
passt.1 | 10 +-
passt.c | 9 +
passt.h | 6 +
pcap.c | 1 -
tap.c | 80 +++++++--
tap.h | 5 +-
tcp.c | 7 +
tcp_vu.c | 476 +++++++++++++++++++++++++++++++++++++++++++++++++++
tcp_vu.h | 12 ++
udp.c | 10 ++
udp_vu.c | 332 +++++++++++++++++++++++++++++++++++
udp_vu.h | 13 ++
vhost_user.c | 38 ++--
vhost_user.h | 4 +-
virtio.c | 5 -
vu_common.c | 358 ++++++++++++++++++++++++++++++++++++++
vu_common.h | 47 +++++
24 files changed, 1423 insertions(+), 56 deletions(-)
create mode 100644 tcp_vu.c
create mode 100644 tcp_vu.h
create mode 100644 udp_vu.c
create mode 100644 udp_vu.h
create mode 100644 vu_common.c
create mode 100644 vu_common.h
diff --git a/Makefile b/Makefile
index 0e8ed60a0da1..1e8910dda1f4 100644
--- a/Makefile
+++ b/Makefile
@@ -54,7 +54,8 @@ FLAGS += -DDUAL_STACK_SOCKETS=$(DUAL_STACK_SOCKETS)
PASST_SRCS = arch.c arp.c checksum.c conf.c dhcp.c dhcpv6.c flow.c fwd.c \
icmp.c igmp.c inany.c iov.c ip.c isolation.c lineread.c log.c mld.c \
ndp.c netlink.c packet.c passt.c pasta.c pcap.c pif.c tap.c tcp.c \
- tcp_buf.c tcp_splice.c udp.c udp_flow.c util.c vhost_user.c virtio.c
+ tcp_buf.c tcp_splice.c tcp_vu.c udp.c udp_flow.c udp_vu.c util.c \
+ vhost_user.c virtio.c vu_common.c
QRAP_SRCS = qrap.c
SRCS = $(PASST_SRCS) $(QRAP_SRCS)
@@ -64,7 +65,8 @@ PASST_HEADERS = arch.h arp.h checksum.h conf.h dhcp.h dhcpv6.h flow.h fwd.h \
flow_table.h icmp.h icmp_flow.h inany.h iov.h ip.h isolation.h \
lineread.h log.h ndp.h netlink.h packet.h passt.h pasta.h pcap.h pif.h \
siphash.h tap.h tcp.h tcp_buf.h tcp_conn.h tcp_internal.h tcp_splice.h \
- udp.h udp_flow.h util.h vhost_user.h virtio.h
+ tcp_vu.h udp.h udp_flow.h udp_internal.h udp_vu.h util.h vhost_user.h \
+ virtio.h vu_common.h
HEADERS = $(PASST_HEADERS) seccomp.h
C := \#include <linux/tcp.h>\nstruct tcp_info x = { .tcpi_snd_wnd = 0 };
diff --git a/conf.c b/conf.c
index c63101970155..29d6e41f5770 100644
--- a/conf.c
+++ b/conf.c
@@ -45,6 +45,7 @@
#include "lineread.h"
#include "isolation.h"
#include "log.h"
+#include "vhost_user.h"
/**
* next_chunk - Return the next piece of a string delimited by a character
@@ -762,9 +763,14 @@ static void usage(const char *name, FILE *f, int status)
" default: same interface name as external one\n");
} else {
fprintf(f,
- " -s, --socket PATH UNIX domain socket path\n"
+ " -s, --socket, --socket-path PATH UNIX domain socket path\n"
" default: probe free path starting from "
UNIX_SOCK_PATH "\n", 1);
+ fprintf(f,
+ " --vhost-user Enable vhost-user mode\n"
+ " UNIX domain socket is provided by -s option\n"
+ " --print-capabilities print back-end capabilities in JSON format,\n"
+ " only meaningful for vhost-user mode\n");
}
fprintf(f,
@@ -1290,6 +1296,10 @@ void conf(struct ctx *c, int argc, char **argv)
{"map-host-loopback", required_argument, NULL, 21 },
{"map-guest-addr", required_argument, NULL, 22 },
{"dns-host", required_argument, NULL, 24 },
+ {"vhost-user", no_argument, NULL, 25 },
+ /* vhost-user backend program convention */
+ {"print-capabilities", no_argument, NULL, 26 },
+ {"socket-path", required_argument, NULL, 's' },
{ 0 },
};
const char *logname = (c->mode == MODE_PASTA) ? "pasta" : "passt";
@@ -1478,6 +1488,15 @@ void conf(struct ctx *c, int argc, char **argv)
break;
die("Invalid host nameserver address: %s", optarg);
+ case 25:
+ if (c->mode == MODE_PASTA) {
+ err("--vhost-user is for passt mode only");
+ usage(argv[0], stdout, EXIT_SUCCESS);
+ }
+ c->mode = MODE_VU;
+ break;
+ case 26:
+ vu_print_capabilities();
break;
case 'd':
c->debug = 1;
diff --git a/epoll_type.h b/epoll_type.h
index 0ad1efa0ccec..f3ef41584757 100644
--- a/epoll_type.h
+++ b/epoll_type.h
@@ -36,6 +36,10 @@ enum epoll_type {
EPOLL_TYPE_TAP_PASST,
/* socket listening for qemu socket connections */
EPOLL_TYPE_TAP_LISTEN,
+ /* vhost-user command socket */
+ EPOLL_TYPE_VHOST_CMD,
+ /* vhost-user kick event socket */
+ EPOLL_TYPE_VHOST_KICK,
EPOLL_NUM_TYPES,
};
diff --git a/iov.c b/iov.c
index 3f9e229a305f..3741db21790f 100644
--- a/iov.c
+++ b/iov.c
@@ -68,7 +68,6 @@ size_t iov_skip_bytes(const struct iovec *iov, size_t n,
*
* Returns: The number of bytes successfully copied.
*/
-/* cppcheck-suppress unusedFunction */
size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt,
size_t offset, const void *buf, size_t bytes)
{
diff --git a/isolation.c b/isolation.c
index 45fba1e68b9d..c2a3c7b7911d 100644
--- a/isolation.c
+++ b/isolation.c
@@ -379,12 +379,19 @@ void isolate_postfork(const struct ctx *c)
prctl(PR_SET_DUMPABLE, 0);
- if (c->mode == MODE_PASTA) {
- prog.len = (unsigned short)ARRAY_SIZE(filter_pasta);
- prog.filter = filter_pasta;
- } else {
+ switch (c->mode) {
+ case MODE_PASST:
prog.len = (unsigned short)ARRAY_SIZE(filter_passt);
prog.filter = filter_passt;
+ break;
+ case MODE_PASTA:
+ prog.len = (unsigned short)ARRAY_SIZE(filter_pasta);
+ prog.filter = filter_pasta;
+ break;
+ case MODE_VU:
+ prog.len = (unsigned short)ARRAY_SIZE(filter_vu);
+ prog.filter = filter_vu;
+ break;
}
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) ||
diff --git a/packet.c b/packet.c
index 37489961a37e..e5a78d079231 100644
--- a/packet.c
+++ b/packet.c
@@ -36,6 +36,17 @@
static int packet_check_range(const struct pool *p, size_t offset, size_t len,
const char *start, const char *func, int line)
{
+ if (p->buf_size == 0) {
+ int ret;
+
+ ret = vu_packet_check_range((void *)p->buf, offset, len, start);
+
+ if (ret == -1)
+ trace("cannot find region, %s:%i", func, line);
+
+ return ret;
+ }
+
if (start < p->buf) {
trace("packet start %p before buffer start %p, "
"%s:%i", (void *)start, (void *)p->buf, func, line);
diff --git a/packet.h b/packet.h
index 8377dcf678bb..3f70e949c066 100644
--- a/packet.h
+++ b/packet.h
@@ -8,8 +8,10 @@
/**
* struct pool - Generic pool of packets stored in a buffer
- * @buf: Buffer storing packet descriptors
- * @buf_size: Total size of buffer
+ * @buf: Buffer storing packet descriptors,
+ * a struct vu_dev_region array for passt vhost-user mode
+ * @buf_size: Total size of buffer,
+ * 0 for passt vhost-user mode
* @size: Number of usable descriptors for the pool
* @count: Number of used descriptors for the pool
* @pkt: Descriptors: see macros below
@@ -22,6 +24,8 @@ struct pool {
struct iovec pkt[1];
};
+int vu_packet_check_range(void *buf, size_t offset, size_t len,
+ const char *start);
void packet_add_do(struct pool *p, size_t len, const char *start,
const char *func, int line);
void *packet_get_do(const struct pool *p, const size_t idx,
diff --git a/passt.1 b/passt.1
index ef33267e9cd7..96532dd39aa2 100644
--- a/passt.1
+++ b/passt.1
@@ -397,12 +397,20 @@ interface address are configured on a given host interface.
.SS \fBpasst\fR-only options
.TP
-.BR \-s ", " \-\-socket " " \fIpath
+.BR \-s ", " \-\-socket-path ", " \-\-socket " " \fIpath
Path for UNIX domain socket used by \fBqemu\fR(1) or \fBqrap\fR(1) to connect to
\fBpasst\fR.
Default is to probe a free socket, not accepting connections, starting from
\fI/tmp/passt_1.socket\fR to \fI/tmp/passt_64.socket\fR.
+.TP
+.BR \-\-vhost-user
+Enable vhost-user. The vhost-user command socket is provided by \fB--socket\fR.
+
+.TP
+.BR \-\-print-capabilities
+Print back-end capabilities in JSON format, only meaningful for vhost-user mode.
+
.TP
.BR \-F ", " \-\-fd " " \fIFD
Pass a pre-opened, connected socket to \fBpasst\fR. Usually the socket is opened
diff --git a/passt.c b/passt.c
index 79093ee02d62..2d105e81218d 100644
--- a/passt.c
+++ b/passt.c
@@ -52,6 +52,7 @@
#include "arch.h"
#include "log.h"
#include "tcp_splice.h"
+#include "vu_common.h"
#define EPOLL_EVENTS 8
@@ -74,6 +75,8 @@ char *epoll_type_str[] = {
[EPOLL_TYPE_TAP_PASTA] = "/dev/net/tun device",
[EPOLL_TYPE_TAP_PASST] = "connected qemu socket",
[EPOLL_TYPE_TAP_LISTEN] = "listening qemu socket",
+ [EPOLL_TYPE_VHOST_CMD] = "vhost-user command socket",
+ [EPOLL_TYPE_VHOST_KICK] = "vhost-user kick socket",
};
static_assert(ARRAY_SIZE(epoll_type_str) == EPOLL_NUM_TYPES,
"epoll_type_str[] doesn't match enum epoll_type");
@@ -360,6 +363,12 @@ loop:
case EPOLL_TYPE_PING:
icmp_sock_handler(&c, ref);
break;
+ case EPOLL_TYPE_VHOST_CMD:
+ vu_control_handler(c.vdev, c.fd_tap, eventmask);
+ break;
+ case EPOLL_TYPE_VHOST_KICK:
+ vu_kick_cb(c.vdev, ref, &now);
+ break;
default:
/* Can't happen */
ASSERT(0);
diff --git a/passt.h b/passt.h
index 4908ed937dc8..311482d36257 100644
--- a/passt.h
+++ b/passt.h
@@ -25,6 +25,8 @@ union epoll_ref;
#include "fwd.h"
#include "tcp.h"
#include "udp.h"
+#include "udp_vu.h"
+#include "vhost_user.h"
/* Default address for our end on the tap interface. Bit 0 of byte 0 must be 0
* (unicast) and bit 1 of byte 1 must be 1 (locally administered). Otherwise
@@ -94,6 +96,7 @@ struct fqdn {
enum passt_modes {
MODE_PASST,
MODE_PASTA,
+ MODE_VU,
};
/**
@@ -228,6 +231,7 @@ struct ip6_ctx {
* @freebind: Allow binding of non-local addresses for forwarding
* @low_wmem: Low probed net.core.wmem_max
* @low_rmem: Low probed net.core.rmem_max
+ * @vdev: vhost-user device
*/
struct ctx {
enum passt_modes mode;
@@ -289,6 +293,8 @@ struct ctx {
int low_wmem;
int low_rmem;
+
+ struct vu_dev *vdev;
};
void proto_update_l2_buf(const unsigned char *eth_d,
diff --git a/pcap.c b/pcap.c
index 6ee6cdfd261a..718d6ad61732 100644
--- a/pcap.c
+++ b/pcap.c
@@ -140,7 +140,6 @@ void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n,
* @iovcnt: Number of buffers (@iov entries)
* @offset: Offset of the L2 frame within the full data length
*/
-/* cppcheck-suppress unusedFunction */
void pcap_iov(const struct iovec *iov, size_t iovcnt, size_t offset)
{
struct timespec now;
diff --git a/tap.c b/tap.c
index 4b826fdf7adc..22d19f1833f7 100644
--- a/tap.c
+++ b/tap.c
@@ -58,6 +58,8 @@
#include "packet.h"
#include "tap.h"
#include "log.h"
+#include "vhost_user.h"
+#include "vu_common.h"
/* IPv4 (plus ARP) and IPv6 message batches from tap/guest to IP handlers */
static PACKET_POOL_NOINIT(pool_tap4, TAP_MSGS, pkt_buf);
@@ -78,16 +80,22 @@ void tap_send_single(const struct ctx *c, const void *data, size_t l2len)
struct iovec iov[2];
size_t iovcnt = 0;
- if (c->mode == MODE_PASST) {
+ switch (c->mode) {
+ case MODE_PASST:
iov[iovcnt] = IOV_OF_LVALUE(vnet_len);
iovcnt++;
- }
-
- iov[iovcnt].iov_base = (void *)data;
- iov[iovcnt].iov_len = l2len;
- iovcnt++;
+ /* fall through */
+ case MODE_PASTA:
+ iov[iovcnt].iov_base = (void *)data;
+ iov[iovcnt].iov_len = l2len;
+ iovcnt++;
- tap_send_frames(c, iov, iovcnt, 1);
+ tap_send_frames(c, iov, iovcnt, 1);
+ break;
+ case MODE_VU:
+ vu_send_single(c, data, l2len);
+ break;
+ }
}
/**
@@ -414,10 +422,18 @@ size_t tap_send_frames(const struct ctx *c, const struct iovec *iov,
if (!nframes)
return 0;
- if (c->mode == MODE_PASTA)
+ switch (c->mode) {
+ case MODE_PASTA:
m = tap_send_frames_pasta(c, iov, bufs_per_frame, nframes);
- else
+ break;
+ case MODE_PASST:
m = tap_send_frames_passt(c, iov, bufs_per_frame, nframes);
+ break;
+ case MODE_VU:
+ /* fall through */
+ default:
+ ASSERT(0);
+ }
if (m < nframes)
debug("tap: failed to send %zu frames of %zu",
@@ -976,7 +992,7 @@ void tap_add_packet(struct ctx *c, ssize_t l2len, char *p)
* tap_sock_reset() - Handle closing or failure of connect AF_UNIX socket
* @c: Execution context
*/
-static void tap_sock_reset(struct ctx *c)
+void tap_sock_reset(struct ctx *c)
{
info("Client connection closed%s", c->one_off ? ", exiting" : "");
@@ -987,6 +1003,8 @@ static void tap_sock_reset(struct ctx *c)
epoll_ctl(c->epollfd, EPOLL_CTL_DEL, c->fd_tap, NULL);
close(c->fd_tap);
c->fd_tap = -1;
+ if (c->mode == MODE_VU)
+ vu_cleanup(c->vdev);
}
/**
@@ -1205,6 +1223,11 @@ static void tap_backend_show_hints(struct ctx *c)
info("or qrap, for earlier qemu versions:");
info(" ./qrap 5 kvm ... -net socket,fd=5 -net nic,model=virtio");
break;
+ case MODE_VU:
+ info("You can start qemu with:");
+ info(" kvm ... -chardev socket,id=chr0,path=%s -netdev vhost-user,id=netdev0,chardev=chr0 -device virtio-net,netdev=netdev0 -object memory-backend-memfd,id=memfd0,share=on,size=$RAMSIZE -numa node,memdev=memfd0\n",
+ c->sock_path);
+ break;
}
}
@@ -1232,8 +1255,8 @@ static void tap_sock_unix_init(const struct ctx *c)
*/
void tap_listen_handler(struct ctx *c, uint32_t events)
{
- union epoll_ref ref = { .type = EPOLL_TYPE_TAP_PASST };
struct epoll_event ev = { 0 };
+ union epoll_ref ref = { 0 };
int v = INT_MAX / 2;
struct ucred ucred;
socklen_t len;
@@ -1273,6 +1296,10 @@ void tap_listen_handler(struct ctx *c, uint32_t events)
trace("tap: failed to set SO_SNDBUF to %i", v);
ref.fd = c->fd_tap;
+ if (c->mode == MODE_VU)
+ ref.type = EPOLL_TYPE_VHOST_CMD;
+ else
+ ref.type = EPOLL_TYPE_TAP_PASST;
ev.events = EPOLLIN | EPOLLRDHUP;
ev.data.u64 = ref.u64;
epoll_ctl(c->epollfd, EPOLL_CTL_ADD, c->fd_tap, &ev);
@@ -1339,7 +1366,7 @@ static void tap_sock_tun_init(struct ctx *c)
* @base: Buffer base
* @size Buffer size
*/
-static void tap_sock_update_pool(void *base, size_t size)
+void tap_sock_update_pool(void *base, size_t size)
{
int i;
@@ -1353,13 +1380,15 @@ static void tap_sock_update_pool(void *base, size_t size)
}
/**
- * tap_backend_init() - Create and set up AF_UNIX socket or
- * tuntap file descriptor
+ * tap_sock_init() - Create and set up AF_UNIX socket or tuntap file descriptor
* @c: Execution context
*/
void tap_backend_init(struct ctx *c)
{
- tap_sock_update_pool(pkt_buf, sizeof(pkt_buf));
+ if (c->mode == MODE_VU)
+ tap_sock_update_pool(NULL, 0);
+ else
+ tap_sock_update_pool(pkt_buf, sizeof(pkt_buf));
if (c->fd_tap != -1) { /* Passed as --fd */
struct epoll_event ev = { 0 };
@@ -1367,10 +1396,17 @@ void tap_backend_init(struct ctx *c)
ASSERT(c->one_off);
ref.fd = c->fd_tap;
- if (c->mode == MODE_PASST)
+ switch (c->mode) {
+ case MODE_PASST:
ref.type = EPOLL_TYPE_TAP_PASST;
- else
+ break;
+ case MODE_PASTA:
ref.type = EPOLL_TYPE_TAP_PASTA;
+ break;
+ case MODE_VU:
+ ref.type = EPOLL_TYPE_VHOST_CMD;
+ break;
+ }
ev.events = EPOLLIN | EPOLLRDHUP;
ev.data.u64 = ref.u64;
@@ -1378,9 +1414,14 @@ void tap_backend_init(struct ctx *c)
return;
}
- if (c->mode == MODE_PASTA) {
+ switch (c->mode) {
+ case MODE_PASTA:
tap_sock_tun_init(c);
- } else {
+ break;
+ case MODE_VU:
+ vu_init(c);
+ /* fall through */
+ case MODE_PASST:
tap_sock_unix_init(c);
/* In passt mode, we don't know the guest's MAC address until it
@@ -1388,6 +1429,7 @@ void tap_backend_init(struct ctx *c)
* first packets will reach it.
*/
memset(&c->guest_mac, 0xff, sizeof(c->guest_mac));
+ break;
}
tap_backend_show_hints(c);
diff --git a/tap.h b/tap.h
index 8728cc5c09c3..dfbd8b9ebd72 100644
--- a/tap.h
+++ b/tap.h
@@ -40,7 +40,8 @@ static inline struct iovec tap_hdr_iov(const struct ctx *c,
*/
static inline void tap_hdr_update(struct tap_hdr *thdr, size_t l2len)
{
- thdr->vnet_len = htonl(l2len);
+ if (thdr)
+ thdr->vnet_len = htonl(l2len);
}
void tap_udp4_send(const struct ctx *c, struct in_addr src, in_port_t sport,
@@ -68,6 +69,8 @@ void tap_handler_pasta(struct ctx *c, uint32_t events,
void tap_handler_passt(struct ctx *c, uint32_t events,
const struct timespec *now);
int tap_sock_unix_open(char *sock_path);
+void tap_sock_reset(struct ctx *c);
+void tap_sock_update_pool(void *base, size_t size);
void tap_backend_init(struct ctx *c);
void tap_flush_pools(void);
void tap_handler(struct ctx *c, const struct timespec *now);
diff --git a/tcp.c b/tcp.c
index eae02b1647e3..fd2def0d8a39 100644
--- a/tcp.c
+++ b/tcp.c
@@ -304,6 +304,7 @@
#include "flow_table.h"
#include "tcp_internal.h"
#include "tcp_buf.h"
+#include "tcp_vu.h"
/* MSS rounding: see SET_MSS() */
#define MSS_DEFAULT 536
@@ -1328,6 +1329,9 @@ int tcp_prepare_flags(const struct ctx *c, struct tcp_tap_conn *conn,
static int tcp_send_flag(const struct ctx *c, struct tcp_tap_conn *conn,
int flags)
{
+ if (c->mode == MODE_VU)
+ return tcp_vu_send_flag(c, conn, flags);
+
return tcp_buf_send_flag(c, conn, flags);
}
@@ -1721,6 +1725,9 @@ static int tcp_sock_consume(const struct tcp_tap_conn *conn, uint32_t ack_seq)
*/
static int tcp_data_from_sock(const struct ctx *c, struct tcp_tap_conn *conn)
{
+ if (c->mode == MODE_VU)
+ return tcp_vu_data_from_sock(c, conn);
+
return tcp_buf_data_from_sock(c, conn);
}
diff --git a/tcp_vu.c b/tcp_vu.c
new file mode 100644
index 000000000000..1126fb39d138
--- /dev/null
+++ b/tcp_vu.c
@@ -0,0 +1,476 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* tcp_vu.c - TCP L2 vhost-user management functions
+ *
+ * Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ */
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#include <netinet/ip.h>
+
+#include <sys/socket.h>
+
+#include <linux/tcp.h>
+#include <linux/virtio_net.h>
+
+#include "util.h"
+#include "ip.h"
+#include "passt.h"
+#include "siphash.h"
+#include "inany.h"
+#include "vhost_user.h"
+#include "tcp.h"
+#include "pcap.h"
+#include "flow.h"
+#include "tcp_conn.h"
+#include "flow_table.h"
+#include "tcp_vu.h"
+#include "tap.h"
+#include "tcp_internal.h"
+#include "checksum.h"
+#include "vu_common.h"
+
+static struct iovec iov_vu[VIRTQUEUE_MAX_SIZE + 1];
+static struct vu_virtq_element elem[VIRTQUEUE_MAX_SIZE];
+
+/**
+ * tcp_vu_l2_hdrlen() - return the size of the header in level 2 frame (TDP)
+ * @v6: Set for IPv6 packet
+ *
+ * Return: Return the size of the header
+ */
+static size_t tcp_vu_l2_hdrlen(bool v6)
+{
+ size_t l2_hdrlen;
+
+ l2_hdrlen = sizeof(struct ethhdr) + sizeof(struct tcphdr);
+
+ if (v6)
+ l2_hdrlen += sizeof(struct ipv6hdr);
+ else
+ l2_hdrlen += sizeof(struct iphdr);
+
+ return l2_hdrlen;
+}
+
+/**
+ * tcp_vu_update_check() - Calculate TCP checksum
+ * @tapside: Address information for one side of the flow
+ * @iov: Pointer to the array of IO vectors
+ * @iov_used: Length of the array
+ */
+static void tcp_vu_update_check(const struct flowside *tapside,
+ struct iovec *iov, int iov_used)
+{
+ char *base = iov[0].iov_base;
+
+ if (inany_v4(&tapside->oaddr)) {
+ const struct iphdr *iph = vu_ip(base);
+
+ tcp_update_check_tcp4(iph, iov, iov_used,
+ (char *)vu_payloadv4(base) - base);
+ } else {
+ const struct ipv6hdr *ip6h = vu_ip(base);
+
+ tcp_update_check_tcp6(ip6h, iov, iov_used,
+ (char *)vu_payloadv6(base) - base);
+ }
+}
+
+/**
+ * tcp_vu_send_flag() - Send segment with flags to vhost-user (no payload)
+ * @c: Execution context
+ * @conn: Connection pointer
+ * @flags: TCP flags: if not set, send segment only if ACK is due
+ *
+ * Return: negative error code on connection reset, 0 otherwise
+ */
+int tcp_vu_send_flag(const struct ctx *c, struct tcp_tap_conn *conn, int flags)
+{
+ struct vu_dev *vdev = c->vdev;
+ struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
+ const struct flowside *tapside = TAPFLOW(conn);
+ size_t l2len, l4len, optlen, hdrlen;
+ struct ethhdr *eh;
+ int elem_cnt;
+ int nb_ack;
+ int ret;
+
+ hdrlen = tcp_vu_l2_hdrlen(CONN_V6(conn));
+
+ vu_init_elem(elem, iov_vu, 2);
+
+ elem_cnt = vu_collect_one_frame(vdev, vq, elem, 1,
+ hdrlen + OPT_MSS_LEN + OPT_WS_LEN + 1,
+ 0, NULL);
+ if (elem_cnt < 1)
+ return 0;
+
+ vu_set_vnethdr(vdev, &iov_vu[0], 1, 0);
+
+ eh = vu_eth(iov_vu[0].iov_base);
+
+ memcpy(eh->h_dest, c->guest_mac, sizeof(eh->h_dest));
+ memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source));
+
+ if (CONN_V4(conn)) {
+ struct tcp_payload_t *payload;
+ struct iphdr *iph;
+ uint32_t seq;
+
+ eh->h_proto = htons(ETH_P_IP);
+
+ iph = vu_ip(iov_vu[0].iov_base);
+ *iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP);
+
+ payload = vu_payloadv4(iov_vu[0].iov_base);
+ memset(&payload->th, 0, sizeof(payload->th));
+ payload->th.doff = offsetof(struct tcp_flags_t, opts) / 4;
+ payload->th.ack = 1;
+
+ seq = conn->seq_to_tap;
+ ret = tcp_prepare_flags(c, conn, flags, &payload->th,
+ (char *)payload->data, &optlen);
+ if (ret <= 0) {
+ vu_queue_rewind(vq, 1);
+ return ret;
+ }
+
+ l4len = tcp_fill_headers4(conn, NULL, iph, payload, optlen,
+ NULL, seq, true);
+ l2len = sizeof(*iph);
+ } else {
+ struct tcp_payload_t *payload;
+ struct ipv6hdr *ip6h;
+ uint32_t seq;
+
+ eh->h_proto = htons(ETH_P_IPV6);
+
+ ip6h = vu_ip(iov_vu[0].iov_base);
+ *ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP);
+
+ payload = vu_payloadv6(iov_vu[0].iov_base);
+ memset(&payload->th, 0, sizeof(payload->th));
+ payload->th.doff = offsetof(struct tcp_flags_t, opts) / 4;
+ payload->th.ack = 1;
+
+ seq = conn->seq_to_tap;
+ ret = tcp_prepare_flags(c, conn, flags, &payload->th,
+ (char *)payload->data, &optlen);
+ if (ret <= 0) {
+ vu_queue_rewind(vq, 1);
+ return ret;
+ }
+
+ l4len = tcp_fill_headers6(conn, NULL, ip6h, payload, optlen,
+ seq, true);
+ l2len = sizeof(*ip6h);
+ }
+ l2len += l4len + sizeof(struct ethhdr);
+
+ elem[0].in_sg[0].iov_len = l2len +
+ sizeof(struct virtio_net_hdr_mrg_rxbuf);
+ if (*c->pcap) {
+ tcp_vu_update_check(tapside, &elem[0].in_sg[0], 1);
+ pcap_iov(&elem[0].in_sg[0], 1,
+ sizeof(struct virtio_net_hdr_mrg_rxbuf));
+ }
+ nb_ack = 1;
+
+ if (flags & DUP_ACK) {
+ elem_cnt = vu_collect_one_frame(vdev, vq, &elem[1], 1, l2len,
+ 0, NULL);
+ if (elem_cnt == 1) {
+ memcpy(elem[1].in_sg[0].iov_base,
+ elem[0].in_sg[0].iov_base, l2len);
+ vu_set_vnethdr(vdev, &elem[1].in_sg[0], 1, 0);
+ nb_ack++;
+
+ if (*c->pcap)
+ pcap_iov(&elem[1].in_sg[0], 1, 0);
+ }
+ }
+
+ vu_flush(vdev, vq, elem, nb_ack);
+
+ return 0;
+}
+
+/** tcp_vu_sock_recv() - Receive datastream from socket into vhost-user buffers
+ * @c: Execution context
+ * @conn: Connection pointer
+ * @v4: Set for IPv4 connections
+ * @fillsize: Number of bytes we can receive
+ * @datalen: Size of received data (output)
+ *
+ * Return: Number of iov entries used to store the data
+ */
+static ssize_t tcp_vu_sock_recv(const struct ctx *c,
+ struct tcp_tap_conn *conn, bool v4,
+ size_t fillsize, ssize_t *dlen)
+{
+ struct vu_dev *vdev = c->vdev;
+ struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
+ struct msghdr mh_sock = { 0 };
+ uint16_t mss = MSS_GET(conn);
+ int s = conn->sock;
+ size_t l2_hdrlen;
+ int elem_cnt;
+ ssize_t ret;
+
+ *dlen = 0;
+
+ l2_hdrlen = tcp_vu_l2_hdrlen(!v4);
+
+ vu_init_elem(elem, &iov_vu[1], VIRTQUEUE_MAX_SIZE);
+
+ elem_cnt = vu_collect(vdev, vq, elem, VIRTQUEUE_MAX_SIZE, mss,
+ l2_hdrlen, fillsize);
+ if (elem_cnt < 0) {
+ tcp_rst(c, conn);
+ return -ENOMEM;
+ }
+
+ mh_sock.msg_iov = iov_vu;
+ mh_sock.msg_iovlen = elem_cnt + 1;
+
+ do
+ ret = recvmsg(s, &mh_sock, MSG_PEEK);
+ while (ret < 0 && errno == EINTR);
+
+ if (ret < 0) {
+ vu_queue_rewind(vq, elem_cnt);
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ ret = -errno;
+ tcp_rst(c, conn);
+ }
+ return ret;
+ }
+ if (!ret) {
+ vu_queue_rewind(vq, elem_cnt);
+
+ if ((conn->events & (SOCK_FIN_RCVD | TAP_FIN_SENT)) == SOCK_FIN_RCVD) {
+ int retf = tcp_vu_send_flag(c, conn, FIN | ACK);
+ if (retf) {
+ tcp_rst(c, conn);
+ return retf;
+ }
+
+ conn_event(c, conn, TAP_FIN_SENT);
+ }
+ return 0;
+ }
+
+ *dlen = ret;
+
+ return elem_cnt;
+}
+
+/**
+ * tcp_vu_prepare() - Prepare the packet header
+ * @c: Execution context
+ * @conn: Connection pointer
+ * @first: Pointer to the array of IO vectors
+ * @dlen: Packet data length
+ * @check: Checksum, if already known
+ */
+static void tcp_vu_prepare(const struct ctx *c,
+ struct tcp_tap_conn *conn, struct iovec *first,
+ size_t dlen, const uint16_t **check)
+{
+ const struct flowside *toside = TAPFLOW(conn);
+ char *base = first->iov_base;
+ struct ethhdr *eh;
+
+ /* we guess the first iovec provided by the guest can embed
+ * all the headers needed by L2 frame
+ */
+
+ eh = vu_eth(base);
+
+ memcpy(eh->h_dest, c->guest_mac, sizeof(eh->h_dest));
+ memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source));
+
+ /* initialize header */
+ if (inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)) {
+ struct tcp_payload_t *payload;
+ struct iphdr *iph;
+
+ ASSERT(first[0].iov_len >= sizeof(struct virtio_net_hdr_mrg_rxbuf) +
+ sizeof(struct ethhdr) + sizeof(struct iphdr) +
+ sizeof(struct tcphdr));
+
+ eh->h_proto = htons(ETH_P_IP);
+
+ iph = vu_ip(base);
+ *iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP);
+ payload = vu_payloadv4(base);
+ memset(&payload->th, 0, sizeof(payload->th));
+ payload->th.doff = offsetof(struct tcp_payload_t, data) / 4;
+ payload->th.ack = 1;
+
+ tcp_fill_headers4(conn, NULL, iph, payload, dlen,
+ *check, conn->seq_to_tap, true);
+ *check = &iph->check;
+ } else {
+ struct tcp_payload_t *payload;
+ struct ipv6hdr *ip6h;
+
+ ASSERT(first[0].iov_len >= sizeof(struct virtio_net_hdr_mrg_rxbuf) +
+ sizeof(struct ethhdr) + sizeof(struct ipv6hdr) +
+ sizeof(struct tcphdr));
+
+ eh->h_proto = htons(ETH_P_IPV6);
+
+ ip6h = vu_ip(base);
+ *ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP);
+
+ payload = vu_payloadv6(base);
+ memset(&payload->th, 0, sizeof(payload->th));
+ payload->th.doff = offsetof(struct tcp_payload_t, data) / 4;
+ payload->th.ack = 1;
+
+ tcp_fill_headers6(conn, NULL, ip6h, payload, dlen,
+ conn->seq_to_tap, true);
+ }
+}
+
+/**
+ * tcp_vu_data_from_sock() - Handle new data from socket, queue to vhost-user,
+ * in window
+ * @c: Execution context
+ * @conn: Connection pointer
+ *
+ * Return: Negative on connection reset, 0 otherwise
+ */
+int tcp_vu_data_from_sock(const struct ctx *c, struct tcp_tap_conn *conn)
+{
+ uint32_t wnd_scaled = conn->wnd_from_tap << conn->ws_from_tap;
+ struct vu_dev *vdev = c->vdev;
+ struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
+ const struct flowside *tapside = TAPFLOW(conn);
+ uint16_t mss = MSS_GET(conn);
+ size_t l2_hdrlen, fillsize;
+ int i, iov_cnt, iov_used;
+ int v4 = CONN_V4(conn);
+ uint32_t already_sent = 0;
+ const uint16_t *check;
+ struct iovec *first;
+ int frame_size;
+ int num_buffers;
+ ssize_t len;
+
+ if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) {
+ flow_err(conn,
+ "Got packet, but RX virtqueue not usable yet");
+ return 0;
+ }
+
+ already_sent = conn->seq_to_tap - conn->seq_ack_from_tap;
+
+ if (SEQ_LT(already_sent, 0)) {
+ /* RFC 761, section 2.1. */
+ flow_trace(conn, "ACK sequence gap: ACK for %u, sent: %u",
+ conn->seq_ack_from_tap, conn->seq_to_tap);
+ conn->seq_to_tap = conn->seq_ack_from_tap;
+ already_sent = 0;
+ }
+
+ if (!wnd_scaled || already_sent >= wnd_scaled) {
+ conn_flag(c, conn, STALLED);
+ conn_flag(c, conn, ACK_FROM_TAP_DUE);
+ return 0;
+ }
+
+ /* Set up buffer descriptors we'll fill completely and partially. */
+
+ fillsize = wnd_scaled;
+
+ if (peek_offset_cap)
+ already_sent = 0;
+
+ iov_vu[0].iov_base = tcp_buf_discard;
+ iov_vu[0].iov_len = already_sent;
+ fillsize -= already_sent;
+
+ /* collect the buffers from vhost-user and fill them with the
+ * data from the socket
+ */
+ iov_cnt = tcp_vu_sock_recv(c, conn, v4, fillsize, &len);
+ if (iov_cnt <= 0)
+ return iov_cnt;
+
+ len -= already_sent;
+ if (len <= 0) {
+ conn_flag(c, conn, STALLED);
+ vu_queue_rewind(vq, iov_cnt);
+ return 0;
+ }
+
+ conn_flag(c, conn, ~STALLED);
+
+ /* Likely, some new data was acked too. */
+ tcp_update_seqack_wnd(c, conn, 0, NULL);
+
+ /* initialize headers */
+ l2_hdrlen = tcp_vu_l2_hdrlen(!v4);
+ iov_used = 0;
+ num_buffers = 0;
+ check = NULL;
+ frame_size = 0;
+
+ /* iov_vu is an array of buffers and the buffer size can be
+ * smaller than the frame size we want to use but with
+ * num_buffer we can merge several virtio iov buffers in one packet
+ * we need only to set the packet headers in the first iov and
+ * num_buffer to the number of iov entries
+ */
+ for (i = 0; i < iov_cnt && len; i++) {
+
+ if (frame_size == 0)
+ first = &iov_vu[i + 1];
+
+ if (iov_vu[i + 1].iov_len > (size_t)len)
+ iov_vu[i + 1].iov_len = len;
+
+ len -= iov_vu[i + 1].iov_len;
+ iov_used++;
+
+ frame_size += iov_vu[i + 1].iov_len;
+ num_buffers++;
+
+ if (frame_size >= mss || len == 0 ||
+ i + 1 == iov_cnt || !vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
+ if (i + 1 == iov_cnt)
+ check = NULL;
+
+ /* restore first iovec base: point to vnet header */
+ vu_set_vnethdr(vdev, first, num_buffers, l2_hdrlen);
+
+ tcp_vu_prepare(c, conn, first, frame_size, &check);
+ if (*c->pcap) {
+ tcp_vu_update_check(tapside, first, num_buffers);
+ pcap_iov(first, num_buffers,
+ sizeof(struct virtio_net_hdr_mrg_rxbuf));
+ }
+
+ conn->seq_to_tap += frame_size;
+
+ frame_size = 0;
+ num_buffers = 0;
+ }
+ }
+
+ /* release unused buffers */
+ vu_queue_rewind(vq, iov_cnt - iov_used);
+
+ /* send packets */
+ vu_flush(vdev, vq, elem, iov_used);
+
+ conn_flag(c, conn, ACK_FROM_TAP_DUE);
+
+ return 0;
+}
diff --git a/tcp_vu.h b/tcp_vu.h
new file mode 100644
index 000000000000..6ab6057f352a
--- /dev/null
+++ b/tcp_vu.h
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ */
+
+#ifndef TCP_VU_H
+#define TCP_VU_H
+
+int tcp_vu_send_flag(const struct ctx *c, struct tcp_tap_conn *conn, int flags);
+int tcp_vu_data_from_sock(const struct ctx *c, struct tcp_tap_conn *conn);
+
+#endif /*TCP_VU_H */
diff --git a/udp.c b/udp.c
index 8fc5d8099310..1171d9d1a75b 100644
--- a/udp.c
+++ b/udp.c
@@ -628,6 +628,11 @@ void udp_listen_sock_handler(const struct ctx *c,
union epoll_ref ref, uint32_t events,
const struct timespec *now)
{
+ if (c->mode == MODE_VU) {
+ udp_vu_listen_sock_handler(c, ref, events, now);
+ return;
+ }
+
udp_buf_listen_sock_handler(c, ref, events, now);
}
@@ -697,6 +702,11 @@ static void udp_buf_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
void udp_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
uint32_t events, const struct timespec *now)
{
+ if (c->mode == MODE_VU) {
+ udp_vu_reply_sock_handler(c, ref, events, now);
+ return;
+ }
+
udp_buf_reply_sock_handler(c, ref, events, now);
}
diff --git a/udp_vu.c b/udp_vu.c
new file mode 100644
index 000000000000..edbd4438e145
--- /dev/null
+++ b/udp_vu.c
@@ -0,0 +1,332 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* udp_vu.c - UDP L2 vhost-user management functions
+ *
+ * Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ */
+
+#include <unistd.h>
+#include <assert.h>
+#include <net/ethernet.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
+#include <netinet/udp.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <sys/uio.h>
+#include <linux/virtio_net.h>
+
+#include "checksum.h"
+#include "util.h"
+#include "ip.h"
+#include "siphash.h"
+#include "inany.h"
+#include "passt.h"
+#include "pcap.h"
+#include "log.h"
+#include "vhost_user.h"
+#include "udp_internal.h"
+#include "flow.h"
+#include "flow_table.h"
+#include "udp_flow.h"
+#include "udp_vu.h"
+#include "vu_common.h"
+
+static struct iovec iov_vu [VIRTQUEUE_MAX_SIZE];
+static struct vu_virtq_element elem [VIRTQUEUE_MAX_SIZE];
+
+/**
+ * udp_vu_l2_hdrlen() - return the size of the header in level 2 frame (UDP)
+ * @v6: Set for IPv6 packet
+ *
+ * Return: Return the size of the header
+ */
+static size_t udp_vu_l2_hdrlen(bool v6)
+{
+ size_t l2_hdrlen;
+
+ l2_hdrlen = sizeof(struct ethhdr) + sizeof(struct udphdr);
+
+ if (v6)
+ l2_hdrlen += sizeof(struct ipv6hdr);
+ else
+ l2_hdrlen += sizeof(struct iphdr);
+
+ return l2_hdrlen;
+}
+
+static int udp_vu_sock_init(int s, union sockaddr_inany *s_in)
+{
+ struct msghdr msg = {
+ .msg_name = s_in,
+ .msg_namelen = sizeof(union sockaddr_inany),
+ };
+
+ return recvmsg(s, &msg, MSG_PEEK | MSG_DONTWAIT);
+}
+
+/**
+ * udp_vu_sock_recv() - Receive datagrams from socket into vhost-user buffers
+ * @c: Execution context
+ * @s: Socket to receive from
+ * @events: epoll events bitmap
+ * @v6: Set for IPv6 connections
+ * @datalen: Size of received data (output)
+ *
+ * Return: Number of iov entries used to store the datagram
+ */
+static int udp_vu_sock_recv(const struct ctx *c, int s, uint32_t events,
+ bool v6, ssize_t *dlen)
+{
+ struct vu_dev *vdev = c->vdev;
+ struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
+ int max_elem, iov_cnt, idx, iov_used;
+ struct msghdr msg = { 0 };
+ size_t off, l2_hdrlen;
+
+ ASSERT(!c->no_udp);
+
+ if (!(events & EPOLLIN))
+ return 0;
+
+ /* compute L2 header length */
+
+ if (vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
+ max_elem = VIRTQUEUE_MAX_SIZE;
+ else
+ max_elem = 1;
+
+ l2_hdrlen = udp_vu_l2_hdrlen(v6);
+
+ vu_init_elem(elem, iov_vu, max_elem);
+
+ iov_cnt = vu_collect_one_frame(vdev, vq, elem, max_elem,
+ ETH_MAX_MTU - l2_hdrlen,
+ l2_hdrlen, NULL);
+ if (iov_cnt == 0)
+ return 0;
+
+ msg.msg_iov = iov_vu;
+ msg.msg_iovlen = iov_cnt;
+
+ *dlen = recvmsg(s, &msg, 0);
+ if (*dlen < 0) {
+ vu_queue_rewind(vq, iov_cnt);
+ return 0;
+ }
+
+ /* count the numbers of buffer filled by recvmsg() */
+ idx = iov_skip_bytes(iov_vu, iov_cnt, *dlen, &off);
+
+ /* adjust last iov length */
+ if (idx < iov_cnt)
+ iov_vu[idx].iov_len = off;
+ iov_used = idx + !!off;
+
+ /* release unused buffers */
+ vu_queue_rewind(vq, iov_cnt - iov_used);
+
+ vu_set_vnethdr(vdev, &iov_vu[0], iov_used, l2_hdrlen);
+
+ return iov_used;
+}
+
+/**
+ * udp_vu_prepare() - Prepare the packet header
+ * @c: Execution context
+ * @toside: Address information for one side of the flow
+ * @datalen: Packet data length
+ *
+ * Return: Layer-4 length
+ */
+static size_t udp_vu_prepare(const struct ctx *c,
+ const struct flowside *toside, ssize_t dlen)
+{
+ struct ethhdr *eh;
+ size_t l4len;
+
+ /* ethernet header */
+ eh = vu_eth(iov_vu[0].iov_base);
+
+ memcpy(eh->h_dest, c->guest_mac, sizeof(eh->h_dest));
+ memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source));
+
+ /* initialize header */
+ if (inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)) {
+ struct iphdr *iph = vu_ip(iov_vu[0].iov_base);
+ struct udp_payload_t *bp = vu_payloadv4(iov_vu[0].iov_base);
+
+ eh->h_proto = htons(ETH_P_IP);
+
+ *iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_UDP);
+
+ l4len = udp_update_hdr4(iph, bp, toside, dlen, true);
+ } else {
+ struct ipv6hdr *ip6h = vu_ip(iov_vu[0].iov_base);
+ struct udp_payload_t *bp = vu_payloadv6(iov_vu[0].iov_base);
+
+ eh->h_proto = htons(ETH_P_IPV6);
+
+ *ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_UDP);
+
+ l4len = udp_update_hdr6(ip6h, bp, toside, dlen, true);
+ }
+
+ return l4len;
+}
+
+/**
+ * udp_vu_csum() - Calculate and set checksum for a UDP packet
+ * @toside: ddress information for one side of the flow
+ * @l4len: IPv4 Payload length
+ * @iov_used: Length of the array
+ */
+static void udp_vu_csum(const struct flowside *toside, int iov_used)
+{
+ const struct in_addr *src4 = inany_v4(&toside->oaddr);
+ const struct in_addr *dst4 = inany_v4(&toside->eaddr);
+ char *base = iov_vu[0].iov_base;
+ struct udp_payload_t *bp;
+
+ if (src4 && dst4) {
+ bp = vu_payloadv4(base);
+ csum_udp4(&bp->uh, *src4, *dst4, iov_vu, iov_used,
+ (char *)&bp->data - base);
+ } else {
+ bp = vu_payloadv6(base);
+ csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6,
+ iov_vu, iov_used, (char *)&bp->data - base);
+ }
+}
+
+/**
+ * udp_vu_listen_sock_handler() - Handle new data from socket
+ * @c: Execution context
+ * @ref: epoll reference
+ * @events: epoll events bitmap
+ * @now: Current timestamp
+ */
+void udp_vu_listen_sock_handler(const struct ctx *c, union epoll_ref ref,
+ uint32_t events, const struct timespec *now)
+{
+ struct vu_dev *vdev = c->vdev;
+ struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
+ int i;
+
+ if (udp_sock_errs(c, ref.fd, events) < 0) {
+ err("UDP: Unrecoverable error on listening socket:"
+ " (%s port %hu)", pif_name(ref.udp.pif), ref.udp.port);
+ return;
+ }
+
+ for (i = 0; i < UDP_MAX_FRAMES; i++) {
+ const struct flowside *toside;
+ union sockaddr_inany s_in;
+ flow_sidx_t batchsidx;
+ uint8_t batchpif;
+ ssize_t dlen;
+ int iov_used;
+ bool v6;
+
+ if (udp_vu_sock_init(ref.fd, &s_in) < 0)
+ break;
+
+ batchsidx = udp_flow_from_sock(c, ref, &s_in, now);
+ batchpif = pif_at_sidx(batchsidx);
+
+ if (batchpif != PIF_TAP) {
+ if (flow_sidx_valid(batchsidx)) {
+ flow_sidx_t fromsidx = flow_sidx_opposite(batchsidx);
+ struct udp_flow *uflow = udp_at_sidx(batchsidx);
+
+ flow_err(uflow,
+ "No support for forwarding UDP from %s to %s",
+ pif_name(pif_at_sidx(fromsidx)),
+ pif_name(batchpif));
+ } else {
+ debug("Discarding 1 datagram without flow");
+ }
+
+ continue;
+ }
+
+ toside = flowside_at_sidx(batchsidx);
+
+ v6 = !(inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr));
+
+ iov_used = udp_vu_sock_recv(c, ref.fd, events, v6, &dlen);
+ if (iov_used <= 0)
+ break;
+
+ udp_vu_prepare(c, toside, dlen);
+ if (*c->pcap) {
+ udp_vu_csum(toside, iov_used);
+ pcap_iov(iov_vu, iov_used,
+ sizeof(struct virtio_net_hdr_mrg_rxbuf));
+ }
+ vu_flush(vdev, vq, elem, iov_used);
+ }
+}
+
+/**
+ * udp_vu_reply_sock_handler() - Handle new data from flow specific socket
+ * @c: Execution context
+ * @ref: epoll reference
+ * @events: epoll events bitmap
+ * @now: Current timestamp
+ */
+void udp_vu_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
+ uint32_t events, const struct timespec *now)
+{
+ flow_sidx_t tosidx = flow_sidx_opposite(ref.flowside);
+ const struct flowside *toside = flowside_at_sidx(tosidx);
+ struct udp_flow *uflow = udp_at_sidx(ref.flowside);
+ int from_s = uflow->s[ref.flowside.sidei];
+ struct vu_dev *vdev = c->vdev;
+ struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
+ int i;
+
+ ASSERT(!c->no_udp);
+
+ if (udp_sock_errs(c, from_s, events) < 0) {
+ flow_err(uflow, "Unrecoverable error on reply socket");
+ flow_err_details(uflow);
+ udp_flow_close(c, uflow);
+ return;
+ }
+
+ for (i = 0; i < UDP_MAX_FRAMES; i++) {
+ uint8_t topif = pif_at_sidx(tosidx);
+ ssize_t dlen;
+ int iov_used;
+ bool v6;
+
+ ASSERT(uflow);
+
+ if (topif != PIF_TAP) {
+ uint8_t frompif = pif_at_sidx(ref.flowside);
+
+ flow_err(uflow,
+ "No support for forwarding UDP from %s to %s",
+ pif_name(frompif), pif_name(topif));
+ continue;
+ }
+
+ v6 = !(inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr));
+
+ iov_used = udp_vu_sock_recv(c, from_s, events, v6, &dlen);
+ if (iov_used <= 0)
+ break;
+ flow_trace(uflow, "Received 1 datagram on reply socket");
+ uflow->ts = now->tv_sec;
+
+ udp_vu_prepare(c, toside, dlen);
+ if (*c->pcap) {
+ udp_vu_csum(toside, iov_used);
+ pcap_iov(iov_vu, iov_used,
+ sizeof(struct virtio_net_hdr_mrg_rxbuf));
+ }
+ vu_flush(vdev, vq, elem, iov_used);
+ }
+}
diff --git a/udp_vu.h b/udp_vu.h
new file mode 100644
index 000000000000..ba7018d3bf01
--- /dev/null
+++ b/udp_vu.h
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ */
+
+#ifndef UDP_VU_H
+#define UDP_VU_H
+
+void udp_vu_listen_sock_handler(const struct ctx *c, union epoll_ref ref,
+ uint32_t events, const struct timespec *now);
+void udp_vu_reply_sock_handler(const struct ctx *c, union epoll_ref ref,
+ uint32_t events, const struct timespec *now);
+#endif /* UDP_VU_H */
diff --git a/vhost_user.c b/vhost_user.c
index 952e97efaae8..13decd33dacc 100644
--- a/vhost_user.c
+++ b/vhost_user.c
@@ -48,12 +48,13 @@
/* vhost-user version we are compatible with */
#define VHOST_USER_VERSION 1
+static struct vu_dev vdev_storage;
+
/**
* vu_print_capabilities() - print vhost-user capabilities
* this is part of the vhost-user backend
* convention.
*/
-/* cppcheck-suppress unusedFunction */
void vu_print_capabilities(void)
{
info("{");
@@ -163,9 +164,7 @@ static void vmsg_close_fds(const struct vhost_user_msg *vmsg)
*/
static void vu_remove_watch(const struct vu_dev *vdev, int fd)
{
- /* Placeholder to add passt related code */
- (void)vdev;
- (void)fd;
+ epoll_ctl(vdev->context->epollfd, EPOLL_CTL_DEL, fd, NULL);
}
/**
@@ -426,7 +425,6 @@ static bool map_ring(struct vu_dev *vdev, struct vu_virtq *vq)
*
* Return: 0 if the zone is in a mapped memory region, -1 otherwise
*/
-/* cppcheck-suppress unusedFunction */
int vu_packet_check_range(void *buf, size_t offset, size_t len,
const char *start)
{
@@ -516,6 +514,14 @@ static bool vu_set_mem_table_exec(struct vu_dev *vdev,
}
}
+ /* As vu_packet_check_range() has no access to the number of
+ * memory regions, mark the end of the array with mmap_addr = 0
+ */
+ ASSERT(vdev->nregions < VHOST_USER_MAX_RAM_SLOTS - 1);
+ vdev->regions[vdev->nregions].mmap_addr = 0;
+
+ tap_sock_update_pool(vdev->regions, 0);
+
return false;
}
@@ -644,9 +650,12 @@ static bool vu_get_vring_base_exec(struct vu_dev *vdev,
*/
static void vu_set_watch(const struct vu_dev *vdev, int fd)
{
- /* Placeholder to add passt related code */
- (void)vdev;
- (void)fd;
+ union epoll_ref ref = { .type = EPOLL_TYPE_VHOST_KICK, .fd = fd };
+ struct epoll_event ev = { 0 };
+
+ ev.data.u64 = ref.u64;
+ ev.events = EPOLLIN;
+ epoll_ctl(vdev->context->epollfd, EPOLL_CTL_ADD, fd, &ev);
}
/**
@@ -858,14 +867,14 @@ static bool vu_set_vring_enable_exec(struct vu_dev *vdev,
* @c: execution context
* @vdev: vhost-user device
*/
-/* cppcheck-suppress unusedFunction */
-void vu_init(struct ctx *c, struct vu_dev *vdev)
+void vu_init(struct ctx *c)
{
int i;
- vdev->context = c;
+ c->vdev = &vdev_storage;
+ c->vdev->context = c;
for (i = 0; i < VHOST_USER_MAX_QUEUES; i++) {
- vdev->vq[i] = (struct vu_virtq){
+ c->vdev->vq[i] = (struct vu_virtq){
.call_fd = -1,
.kick_fd = -1,
.err_fd = -1,
@@ -878,7 +887,6 @@ void vu_init(struct ctx *c, struct vu_dev *vdev)
* vu_cleanup() - Reset vhost-user device
* @vdev: vhost-user device
*/
-/* cppcheck-suppress unusedFunction */
void vu_cleanup(struct vu_dev *vdev)
{
unsigned int i;
@@ -925,8 +933,7 @@ void vu_cleanup(struct vu_dev *vdev)
*/
static void vu_sock_reset(struct vu_dev *vdev)
{
- /* Placeholder to add passt related code */
- (void)vdev;
+ tap_sock_reset(vdev->context);
}
static bool (*vu_handle[VHOST_USER_MAX])(struct vu_dev *vdev,
@@ -954,7 +961,6 @@ static bool (*vu_handle[VHOST_USER_MAX])(struct vu_dev *vdev,
* @fd: vhost-user message socket
* @events: epoll events
*/
-/* cppcheck-suppress unusedFunction */
void vu_control_handler(struct vu_dev *vdev, int fd, uint32_t events)
{
struct vhost_user_msg msg = { 0 };
diff --git a/vhost_user.h b/vhost_user.h
index 5af349ba58b8..464ba21e962f 100644
--- a/vhost_user.h
+++ b/vhost_user.h
@@ -183,7 +183,6 @@ struct vhost_user_msg {
*
* Return: true if the virqueue is enabled, false otherwise
*/
-/* cppcheck-suppress unusedFunction */
static inline bool vu_queue_enabled(const struct vu_virtq *vq)
{
return vq->enable;
@@ -195,14 +194,13 @@ static inline bool vu_queue_enabled(const struct vu_virtq *vq)
*
* Return: true if the virqueue is started, false otherwise
*/
-/* cppcheck-suppress unusedFunction */
static inline bool vu_queue_started(const struct vu_virtq *vq)
{
return vq->started;
}
void vu_print_capabilities(void);
-void vu_init(struct ctx *c, struct vu_dev *vdev);
+void vu_init(struct ctx *c);
void vu_cleanup(struct vu_dev *vdev);
void vu_control_handler(struct vu_dev *vdev, int fd, uint32_t events);
#endif /* VHOST_USER_H */
diff --git a/virtio.c b/virtio.c
index 380590afbca3..0598ff479858 100644
--- a/virtio.c
+++ b/virtio.c
@@ -328,7 +328,6 @@ static bool vring_can_notify(const struct vu_dev *dev, struct vu_virtq *vq)
* @dev: Vhost-user device
* @vq: Virtqueue
*/
-/* cppcheck-suppress unusedFunction */
void vu_queue_notify(const struct vu_dev *dev, struct vu_virtq *vq)
{
if (!vq->vring.avail)
@@ -504,7 +503,6 @@ static int vu_queue_map_desc(struct vu_dev *dev, struct vu_virtq *vq, unsigned i
*
* Return: -1 if there is an error, 0 otherwise
*/
-/* cppcheck-suppress unusedFunction */
int vu_queue_pop(struct vu_dev *dev, struct vu_virtq *vq, struct vu_virtq_element *elem)
{
unsigned int head;
@@ -565,7 +563,6 @@ void vu_queue_unpop(struct vu_virtq *vq)
* @vq: Virtqueue
* @num: Number of element to unpop
*/
-/* cppcheck-suppress unusedFunction */
bool vu_queue_rewind(struct vu_virtq *vq, unsigned int num)
{
if (num > vq->inuse)
@@ -621,7 +618,6 @@ void vu_queue_fill_by_index(struct vu_virtq *vq, unsigned int index,
* @len: Size of the element
* @idx: Used ring entry index
*/
-/* cppcheck-suppress unusedFunction */
void vu_queue_fill(struct vu_virtq *vq, const struct vu_virtq_element *elem,
unsigned int len, unsigned int idx)
{
@@ -645,7 +641,6 @@ static inline void vring_used_idx_set(struct vu_virtq *vq, uint16_t val)
* @vq: Virtqueue
* @count: Number of entry to flush
*/
-/* cppcheck-suppress unusedFunction */
void vu_queue_flush(struct vu_virtq *vq, unsigned int count)
{
uint16_t old, new;
diff --git a/vu_common.c b/vu_common.c
new file mode 100644
index 000000000000..0a71cafbd525
--- /dev/null
+++ b/vu_common.c
@@ -0,0 +1,358 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ *
+ * common_vu.c - vhost-user common UDP and TCP functions
+ */
+
+#include <unistd.h>
+#include <sys/uio.h>
+#include <sys/eventfd.h>
+#include <linux/virtio_net.h>
+
+#include "util.h"
+#include "passt.h"
+#include "tap.h"
+#include "vhost_user.h"
+#include "pcap.h"
+#include "vu_common.h"
+
+/**
+ * vu_init_elem() - initialize an array of virtqueue element with 1 iov in each
+ * @elem: Array of virtqueue element to initialize
+ * @iov: Array of iovec to assign to virtqueue element
+ * @elem_cnt: Number of virtqueue element
+ */
+void vu_init_elem(struct vu_virtq_element *elem, struct iovec *iov, int elem_cnt)
+{
+ int i;
+
+ for (i = 0; i < elem_cnt; i++) {
+ elem[i].out_num = 0;
+ elem[i].out_sg = NULL;
+ elem[i].in_num = 1;
+ elem[i].in_sg = &iov[i];
+ }
+}
+
+/**
+ * vu_collect_one_frame() - collect virtio buffers from a given virtqueue for
+ * one frame
+ * @vdev: vhost-user device
+ * @vq: virtqueue to collect from
+ * @elem: Array of virtqueue element
+ * each element must be initialized with one iovec entry
+ * in the in_sg array.
+ * @max_elem: Number of virtqueue element in the array
+ * @size: Maximum size of the data in the frame
+ * @hdrlen: Size of the frame header
+ */
+int vu_collect_one_frame(struct vu_dev *vdev, struct vu_virtq *vq,
+ struct vu_virtq_element *elem, int max_elem,
+ size_t size, size_t hdrlen, size_t *frame_size)
+{
+ size_t current_size;
+ struct iovec *iov;
+ int elem_cnt;
+ int ret;
+
+ /* header is at least virtio_net_hdr_mrg_rxbuf */
+ hdrlen += sizeof(struct virtio_net_hdr_mrg_rxbuf);
+
+ /* collect first (or unique) element, it will contain header */
+ ret = vu_queue_pop(vdev, vq, &elem[0]);
+ if (ret < 0)
+ return 0;
+
+ if (elem[0].in_num < 1) {
+ warn("virtio-net receive queue contains no in buffers");
+ vu_queue_detach_element(vq);
+ return 0;
+ }
+
+ iov = &elem[0].in_sg[0];
+
+ ASSERT(iov->iov_len >= hdrlen);
+
+ /* add space for header */
+ iov->iov_base = (char *)iov->iov_base + hdrlen;
+ iov->iov_len -= hdrlen;
+
+ if (iov->iov_len > size)
+ iov->iov_len = size;
+
+ elem_cnt = 1;
+ current_size = iov->iov_len;
+
+ if (!vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
+ goto out;
+
+ /* if possible, coalesce buffers to reach size */
+ while (current_size < size && elem_cnt < max_elem) {
+
+ ret = vu_queue_pop(vdev, vq, &elem[elem_cnt]);
+ if (ret < 0)
+ break;
+
+ if (elem[elem_cnt].in_num < 1) {
+ warn("virtio-net receive queue contains no in buffers");
+ vu_queue_detach_element(vq);
+ break;
+ }
+
+ iov = &elem[elem_cnt].in_sg[0];
+
+ if (iov->iov_len > size - current_size)
+ iov->iov_len = size - current_size;
+
+ current_size += iov->iov_len;
+ elem_cnt++;
+ }
+
+out:
+ if (frame_size)
+ *frame_size = current_size;
+
+ return elem_cnt;
+}
+
+/**
+ * vu_collect() - collect virtio buffers from a given virtqueue
+ * @vdev: vhost-user device
+ * @vq: virtqueue to collect from
+ * @elem: Array of virtqueue element
+ * each element must be initialized with one iovec entry
+ * in the in_sg array.
+ * @max_elem: Number of virtqueue element in the array
+ * @max_frame_size: Maximum size of the data in the frame
+ * @hdrlen: Size of the frame header
+ * @size: Total size of the buffers we need to collect
+ * (if size > max_frame_size, we collect several frame)
+ */
+int vu_collect(struct vu_dev *vdev, struct vu_virtq *vq,
+ struct vu_virtq_element *elem, int max_elem,
+ size_t max_frame_size, size_t hdrlen, size_t size)
+{
+ int elem_cnt = 0;
+
+ while (size > 0 && elem_cnt < max_elem) {
+ size_t frame_size;
+ int cnt;
+
+ if (max_frame_size > size)
+ max_frame_size = size;
+
+ cnt = vu_collect_one_frame(vdev, vq,
+ &elem[elem_cnt], max_elem - elem_cnt,
+ max_frame_size, hdrlen, &frame_size);
+ if (cnt == 0)
+ break;
+
+ size -= frame_size;
+ elem_cnt += cnt;
+
+ if (frame_size < max_frame_size)
+ break;
+ }
+
+ return elem_cnt;
+}
+
+/**
+ * vu_set_vnethdr() - set virtio-net headers in a given iovec
+ * @vdev: vhost-user device
+ * @iov: One iovec to initialize
+ * @num_buffers: Number of guest buffers of the frame
+ * @hdrlen: Size of the frame header
+ */
+void vu_set_vnethdr(const struct vu_dev *vdev, struct iovec *iov,
+ int num_buffers, size_t hdrlen)
+{
+ struct virtio_net_hdr_mrg_rxbuf *vnethdr;
+
+ /* header is at least virtio_net_hdr_mrg_rxbuf */
+ hdrlen += sizeof(struct virtio_net_hdr_mrg_rxbuf);
+
+ /* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */
+ iov->iov_base = (char *)iov->iov_base - hdrlen;
+ iov->iov_len += hdrlen;
+
+ vnethdr = iov->iov_base;
+ vnethdr->hdr = VU_HEADER;
+ if (vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
+ vnethdr->num_buffers = htole16(num_buffers);
+}
+
+/**
+ * vu_flush() - flush all the collected buffers to the vhost-user interface
+ * @vdev: vhost-user device
+ * @vq: vhost-user virtqueue
+ * @elem: virtqueue element array to send back to the virqueue
+ * @iov_used: Length of the array
+ */
+void vu_flush(const struct vu_dev *vdev, struct vu_virtq *vq,
+ struct vu_virtq_element *elem, int elem_cnt)
+{
+ int i;
+
+ for (i = 0; i < elem_cnt; i++)
+ vu_queue_fill(vq, &elem[i], elem[i].in_sg[0].iov_len, i);
+
+ vu_queue_flush(vq, elem_cnt);
+ vu_queue_notify(vdev, vq);
+}
+
+/**
+ * vu_handle_tx() - Receive data from the TX virtqueue
+ * @vdev: vhost-user device
+ * @index: index of the virtqueue
+ * @now: Current timestamp
+ */
+static void vu_handle_tx(struct vu_dev *vdev, int index,
+ const struct timespec *now)
+{
+ struct vu_virtq_element elem[VIRTQUEUE_MAX_SIZE];
+ struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
+ struct vu_virtq *vq = &vdev->vq[index];
+ int hdrlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
+ int out_sg_count;
+ int count;
+
+ if (!VHOST_USER_IS_QUEUE_TX(index)) {
+ debug("vhost-user: index %d is not a TX queue", index);
+ return;
+ }
+
+ tap_flush_pools();
+
+ count = 0;
+ out_sg_count = 0;
+ while (count < VIRTQUEUE_MAX_SIZE) {
+ int ret;
+
+ elem[count].out_num = 1;
+ elem[count].out_sg = &out_sg[out_sg_count];
+ elem[count].in_num = 0;
+ elem[count].in_sg = NULL;
+ ret = vu_queue_pop(vdev, vq, &elem[count]);
+ if (ret < 0)
+ break;
+ out_sg_count += elem[count].out_num;
+
+ if (elem[count].out_num < 1) {
+ debug("virtio-net header not in first element");
+ break;
+ }
+ ASSERT(elem[count].out_num == 1);
+
+ tap_add_packet(vdev->context,
+ elem[count].out_sg[0].iov_len - hdrlen,
+ (char *)elem[count].out_sg[0].iov_base + hdrlen);
+ count++;
+ }
+ tap_handler(vdev->context, now);
+
+ if (count) {
+ int i;
+
+ for (i = 0; i < count; i++)
+ vu_queue_fill(vq, &elem[i], 0, i);
+ vu_queue_flush(vq, count);
+ vu_queue_notify(vdev, vq);
+ }
+}
+
+/**
+ * vu_kick_cb() - Called on a kick event to start to receive data
+ * @vdev: vhost-user device
+ * @ref: epoll reference information
+ * @now: Current timestamp
+ */
+void vu_kick_cb(struct vu_dev *vdev, union epoll_ref ref,
+ const struct timespec *now)
+{
+ eventfd_t kick_data;
+ ssize_t rc;
+ int idx;
+
+ for (idx = 0; idx < VHOST_USER_MAX_QUEUES; idx++) {
+ if (vdev->vq[idx].kick_fd == ref.fd)
+ break;
+ }
+
+ if (idx == VHOST_USER_MAX_QUEUES)
+ return;
+
+ rc = eventfd_read(ref.fd, &kick_data);
+ if (rc == -1)
+ die_perror("vhost-user kick eventfd_read()");
+
+ debug("vhost-user: ot kick_data: %016"PRIx64" idx:%d",
+ kick_data, idx);
+ if (VHOST_USER_IS_QUEUE_TX(idx))
+ vu_handle_tx(vdev, idx, now);
+}
+
+/**
+ * vu_send_single() - Send a buffer to the front-end using the RX virtqueue
+ * @c: execution context
+ * @buf: address of the buffer
+ * @size: size of the buffer
+ *
+ * Return: number of bytes sent, -1 if there is an error
+ */
+int vu_send_single(const struct ctx *c, const void *buf, size_t size)
+{
+ struct vu_dev *vdev = c->vdev;
+ 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];
+ size_t total;
+ int elem_cnt, max_elem;
+ int i;
+
+ debug("vu_send_single size %zu", size);
+
+ if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) {
+ err("Got packet, but no available descriptors on RX virtq.");
+ return 0;
+ }
+
+ if (vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
+ max_elem = VIRTQUEUE_MAX_SIZE;
+ else
+ max_elem = 1;
+
+ vu_init_elem(elem, in_sg, max_elem);
+
+ elem_cnt = vu_collect_one_frame(vdev, vq, elem, max_elem, size,
+ 0, NULL);
+ total = iov_size(in_sg, elem_cnt);
+ if (total < size) {
+ debug("vu_send_single: no space to send the data "
+ "elem_cnt %d iov_size %zd", elem_cnt, total);
+ goto err;
+ }
+
+ vu_set_vnethdr(vdev, in_sg, elem_cnt, 0);
+
+ /* copy data from the buffer to the iovec */
+ iov_from_buf(in_sg, elem_cnt, sizeof(struct virtio_net_hdr_mrg_rxbuf),
+ buf, size);
+
+ if (*c->pcap) {
+ pcap_iov(in_sg, elem_cnt,
+ sizeof(struct virtio_net_hdr_mrg_rxbuf));
+ }
+
+ vu_flush(vdev, vq, elem, elem_cnt);
+
+ debug("vhost-user sent %zu", total);
+
+ return total;
+err:
+ for (i = 0; i < elem_cnt; i++)
+ vu_queue_detach_element(vq);
+
+ return 0;
+}
diff --git a/vu_common.h b/vu_common.h
new file mode 100644
index 000000000000..1d6048060059
--- /dev/null
+++ b/vu_common.h
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ *
+ * vhost-user common UDP and TCP functions
+ */
+
+#ifndef VU_COMMON_H
+#define VU_COMMON_H
+#include <linux/virtio_net.h>
+
+static inline void *vu_eth(void *base)
+{
+ return ((char *)base + sizeof(struct virtio_net_hdr_mrg_rxbuf));
+}
+
+static inline void *vu_ip(void *base)
+{
+ return (struct ethhdr *)vu_eth(base) + 1;
+}
+
+static inline void *vu_payloadv4(void *base)
+{
+ return (struct iphdr *)vu_ip(base) + 1;
+}
+
+static inline void *vu_payloadv6(void *base)
+{
+ return (struct ipv6hdr *)vu_ip(base) + 1;
+}
+
+void vu_init_elem(struct vu_virtq_element *elem, struct iovec *iov,
+ int elem_cnt);
+int vu_collect_one_frame(struct vu_dev *vdev, struct vu_virtq *vq,
+ struct vu_virtq_element *elem, int max_elem,
+ size_t size, size_t hdrlen, size_t *frame_size);
+int vu_collect(struct vu_dev *vdev, struct vu_virtq *vq,
+ struct vu_virtq_element *elem, int max_elem,
+ size_t max_frame_size, size_t hdrlen, size_t size);
+void vu_set_vnethdr(const struct vu_dev *vdev, struct iovec *iov,
+ int num_buffers, size_t hdrlen);
+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,
+ const struct timespec *now);
+int vu_send_single(const struct ctx *c, const void *buf, size_t size);
+#endif /* VU_COMMON_H */
--
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright Red Hat
+ * Author: Laurent Vivier <lvivier@redhat.com>
+ *
+ * vhost-user common UDP and TCP functions
+ */
+
+#ifndef VU_COMMON_H
+#define VU_COMMON_H
+#include <linux/virtio_net.h>
+
+static inline void *vu_eth(void *base)
+{
+ return ((char *)base + sizeof(struct virtio_net_hdr_mrg_rxbuf));
+}
+
+static inline void *vu_ip(void *base)
+{
+ return (struct ethhdr *)vu_eth(base) + 1;
+}
+
+static inline void *vu_payloadv4(void *base)
+{
+ return (struct iphdr *)vu_ip(base) + 1;
+}
+
+static inline void *vu_payloadv6(void *base)
+{
+ return (struct ipv6hdr *)vu_ip(base) + 1;
+}
+
+void vu_init_elem(struct vu_virtq_element *elem, struct iovec *iov,
+ int elem_cnt);
+int vu_collect_one_frame(struct vu_dev *vdev, struct vu_virtq *vq,
+ struct vu_virtq_element *elem, int max_elem,
+ size_t size, size_t hdrlen, size_t *frame_size);
+int vu_collect(struct vu_dev *vdev, struct vu_virtq *vq,
+ struct vu_virtq_element *elem, int max_elem,
+ size_t max_frame_size, size_t hdrlen, size_t size);
+void vu_set_vnethdr(const struct vu_dev *vdev, struct iovec *iov,
+ int num_buffers, size_t hdrlen);
+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,
+ const struct timespec *now);
+int vu_send_single(const struct ctx *c, const void *buf, size_t size);
+#endif /* VU_COMMON_H */
--
2.46.2
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH v7 8/8] test: Add tests for passt in vhost-user mode
2024-10-09 9:07 [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Laurent Vivier
` (6 preceding siblings ...)
2024-10-09 9:07 ` [PATCH v7 7/8] vhost-user: add vhost-user Laurent Vivier
@ 2024-10-09 9:07 ` Laurent Vivier
2024-10-09 13:07 ` [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Stefano Brivio
2024-10-09 17:37 ` Stefano Brivio
9 siblings, 0 replies; 18+ messages in thread
From: Laurent Vivier @ 2024-10-09 9:07 UTC (permalink / raw)
To: passt-dev; +Cc: Stefano Brivio, Laurent Vivier
From: Stefano Brivio <sbrivio@redhat.com>
Run functional and performance tests for vhost-user mode as well. For
functional tests, we add passt_vu and passt_vu_in_ns as symbolic links
to their non-vhost-user counterparts, as no differences are intended
but we want to distinguish them in test logs.
For performance tests, instead, we add separate perf/passt_vu_tcp and
perf/passt_vu_udp files, as we need longer test duration, as well as
higher UDP sending bandwidths and larger TCP windows, to actually get
the highest throughput vhost-user mode offers.
For valgrind tests, vhost-user mode needs two extra system calls:
statx and readlink. Add them as EXTRA_SYSCALLS for the valgrind
target.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
Makefile | 3 +-
test/lib/perf_report | 15 +++
test/lib/setup | 77 ++++++++++++---
test/lib/setup_ugly | 2 +-
test/passt_vu | 1 +
test/passt_vu_in_ns | 1 +
test/perf/passt_vu_tcp | 211 +++++++++++++++++++++++++++++++++++++++++
test/perf/passt_vu_udp | 159 +++++++++++++++++++++++++++++++
test/run | 25 +++++
test/two_guests_vu | 1 +
10 files changed, 479 insertions(+), 16 deletions(-)
create mode 120000 test/passt_vu
create mode 120000 test/passt_vu_in_ns
create mode 100644 test/perf/passt_vu_tcp
create mode 100644 test/perf/passt_vu_udp
create mode 120000 test/two_guests_vu
diff --git a/Makefile b/Makefile
index 1e8910dda1f4..ce8aa4302790 100644
--- a/Makefile
+++ b/Makefile
@@ -138,7 +138,8 @@ qrap: $(QRAP_SRCS) passt.h
valgrind: EXTRA_SYSCALLS += rt_sigprocmask rt_sigtimedwait rt_sigaction \
rt_sigreturn getpid gettid kill clock_gettime mmap \
- mmap2 munmap open unlink gettimeofday futex
+ mmap2 munmap open unlink gettimeofday futex statx \
+ readlink
valgrind: FLAGS += -g -DVALGRIND
valgrind: all
diff --git a/test/lib/perf_report b/test/lib/perf_report
index d1ef50bfe0d5..c4ec817bcd1e 100755
--- a/test/lib/perf_report
+++ b/test/lib/perf_report
@@ -49,6 +49,21 @@ td:empty { visibility: hidden; }
__passt_tcp_LINE__ __passt_udp_LINE__
</table>
+</li><li><p>passt with vhost-user support</p>
+<table class="passt" width="70%">
+ <tr>
+ <th/>
+ <th id="perf_passt_vu_tcp" colspan="__passt_vu_tcp_cols__">TCP, __passt_vu_tcp_threads__ at __passt_vu_tcp_freq__ GHz</th>
+ <th id="perf_passt_vu_udp" colspan="__passt_vu_udp_cols__">UDP, __passt_vu_udp_threads__ at __passt_vu_udp_freq__ GHz</th>
+ </tr>
+ <tr>
+ <td align="right">MTU:</td>
+ __passt_vu_tcp_header__
+ __passt_vu_udp_header__
+ </tr>
+ __passt_vu_tcp_LINE__ __passt_vu_udp_LINE__
+</table>
+
<style type="text/CSS">
table.pasta_local td { border: 0px solid; padding: 6px; line-height: 1; }
table.pasta_local td { text-align: right; }
diff --git a/test/lib/setup b/test/lib/setup
index 5338393ce35c..3409bd29cd81 100755
--- a/test/lib/setup
+++ b/test/lib/setup
@@ -15,8 +15,7 @@
INITRAMFS="${BASEPATH}/mbuto.img"
VCPUS="$( [ $(nproc) -ge 8 ] && echo 6 || echo $(( $(nproc) / 2 + 1 )) )"
-__mem_kib="$(sed -n 's/MemTotal:[ ]*\([0-9]*\) kB/\1/p' /proc/meminfo)"
-VMEM="$((${__mem_kib} / 1024 / 4))"
+MEM_KIB="$(sed -n 's/MemTotal:[ ]*\([0-9]*\) kB/\1/p' /proc/meminfo)"
QEMU_ARCH="$(uname -m)"
[ "${QEMU_ARCH}" = "i686" ] && QEMU_ARCH=i386
@@ -46,6 +45,7 @@ setup_passt() {
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p ${LOGDIR}/passt.pcap"
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
+ [ ${VHOST_USER} -eq 1 ] && __opts="${__opts} --vhost-user"
context_run passt "make clean"
context_run passt "make valgrind"
@@ -54,16 +54,29 @@ setup_passt() {
# pidfile isn't created until passt is listening
wait_for [ -f "${STATESETUP}/passt.pid" ]
+ __vmem="$((${MEM_KIB} / 1024 / 4))"
+ if [ ${VHOST_USER} -eq 1 ]; then
+ __vmem="$(((${__vmem} + 500) / 1000))G"
+ __qemu_netdev=" \
+ -chardev socket,id=c,path=${STATESETUP}/passt.socket \
+ -netdev vhost-user,id=v,chardev=c \
+ -device virtio-net,netdev=v \
+ -object memory-backend-memfd,id=m,share=on,size=${__vmem} \
+ -numa node,memdev=m"
+ else
+ __qemu_netdev="-device virtio-net-pci,netdev=s \
+ -netdev stream,id=s,server=off,addr.type=unix,addr.path=${STATESETUP}/passt.socket"
+ fi
+
GUEST_CID=94557
context_run_bg qemu 'qemu-system-'"${QEMU_ARCH}" \
' -machine accel=kvm' \
- ' -m '${VMEM}' -cpu host -smp '${VCPUS} \
+ ' -m '${__vmem}' -cpu host -smp '${VCPUS} \
' -kernel '"${KERNEL}" \
' -initrd '${INITRAMFS}' -nographic -serial stdio' \
' -nodefaults' \
' -append "console=ttyS0 mitigations=off apparmor=0" ' \
- ' -device virtio-net-pci,netdev=s0 ' \
- " -netdev stream,id=s0,server=off,addr.type=unix,addr.path=${STATESETUP}/passt.socket " \
+ " ${__qemu_netdev}" \
" -pidfile ${STATESETUP}/qemu.pid" \
" -device vhost-vsock-pci,guest-cid=$GUEST_CID"
@@ -142,6 +155,7 @@ setup_passt_in_ns() {
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p ${LOGDIR}/passt_in_pasta.pcap"
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
+ [ ${VHOST_USER} -eq 1 ] && __opts="${__opts} --vhost-user"
if [ ${VALGRIND} -eq 1 ]; then
context_run passt "make clean"
@@ -154,17 +168,30 @@ setup_passt_in_ns() {
fi
wait_for [ -f "${STATESETUP}/passt.pid" ]
+ __vmem="$((${MEM_KIB} / 1024 / 4))"
+ if [ ${VHOST_USER} -eq 1 ]; then
+ __vmem="$(((${__vmem} + 500) / 1000))G"
+ __qemu_netdev=" \
+ -chardev socket,id=c,path=${STATESETUP}/passt.socket \
+ -netdev vhost-user,id=v,chardev=c \
+ -device virtio-net,netdev=v \
+ -object memory-backend-memfd,id=m,share=on,size=${__vmem} \
+ -numa node,memdev=m"
+ else
+ __qemu_netdev="-device virtio-net-pci,netdev=s \
+ -netdev stream,id=s,server=off,addr.type=unix,addr.path=${STATESETUP}/passt.socket"
+ fi
+
GUEST_CID=94557
context_run_bg qemu 'qemu-system-'"${QEMU_ARCH}" \
' -machine accel=kvm' \
' -M accel=kvm:tcg' \
- ' -m '${VMEM}' -cpu host -smp '${VCPUS} \
+ ' -m '${__vmem}' -cpu host -smp '${VCPUS} \
' -kernel '"${KERNEL}" \
' -initrd '${INITRAMFS}' -nographic -serial stdio' \
' -nodefaults' \
' -append "console=ttyS0 mitigations=off apparmor=0" ' \
- ' -device virtio-net-pci,netdev=s0 ' \
- " -netdev stream,id=s0,server=off,addr.type=unix,addr.path=${STATESETUP}/passt.socket " \
+ " ${__qemu_netdev}" \
" -pidfile ${STATESETUP}/qemu.pid" \
" -device vhost-vsock-pci,guest-cid=$GUEST_CID"
@@ -214,6 +241,7 @@ setup_two_guests() {
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p ${LOGDIR}/passt_1.pcap"
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
+ [ ${VHOST_USER} -eq 1 ] && __opts="${__opts} --vhost-user"
context_run_bg passt_1 "./passt -s ${STATESETUP}/passt_1.socket -P ${STATESETUP}/passt_1.pid -f ${__opts} -t 10001 -u 10001"
wait_for [ -f "${STATESETUP}/passt_1.pid" ]
@@ -222,33 +250,54 @@ setup_two_guests() {
[ ${PCAP} -eq 1 ] && __opts="${__opts} -p ${LOGDIR}/passt_2.pcap"
[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
+ [ ${VHOST_USER} -eq 1 ] && __opts="${__opts} --vhost-user"
context_run_bg passt_2 "./passt -s ${STATESETUP}/passt_2.socket -P ${STATESETUP}/passt_2.pid -f ${__opts} -t 10004 -u 10004"
wait_for [ -f "${STATESETUP}/passt_2.pid" ]
+ __vmem="$((${MEM_KIB} / 1024 / 4))"
+ if [ ${VHOST_USER} -eq 1 ]; then
+ __vmem="$(((${__vmem} + 500) / 1000))G"
+ __qemu_netdev1=" \
+ -chardev socket,id=c,path=${STATESETUP}/passt_1.socket \
+ -netdev vhost-user,id=v,chardev=c \
+ -device virtio-net,netdev=v \
+ -object memory-backend-memfd,id=m,share=on,size=${__vmem} \
+ -numa node,memdev=m"
+ __qemu_netdev1=" \
+ -chardev socket,id=c,path=${STATESETUP}/passt_2.socket \
+ -netdev vhost-user,id=v,chardev=c \
+ -device virtio-net,netdev=v \
+ -object memory-backend-memfd,id=m,share=on,size=${__vmem} \
+ -numa node,memdev=m"
+ else
+ __qemu_netdev1="-device virtio-net-pci,netdev=s \
+ -netdev stream,id=s,server=off,addr.type=unix,addr.path=${STATESETUP}/passt_1.socket"
+ __qemu_netdev2="-device virtio-net-pci,netdev=s \
+ -netdev stream,id=s,server=off,addr.type=unix,addr.path=${STATESETUP}/passt_2.socket"
+ fi
+
GUEST_1_CID=94557
context_run_bg qemu_1 'qemu-system-'"${QEMU_ARCH}" \
' -M accel=kvm:tcg' \
- ' -m '${VMEM}' -cpu host -smp '${VCPUS} \
+ ' -m '${__vmem}' -cpu host -smp '${VCPUS} \
' -kernel '"${KERNEL}" \
' -initrd '${INITRAMFS}' -nographic -serial stdio' \
' -nodefaults' \
' -append "console=ttyS0 mitigations=off apparmor=0" ' \
- ' -device virtio-net-pci,netdev=s0 ' \
- " -netdev stream,id=s0,server=off,addr.type=unix,addr.path=${STATESETUP}/passt_1.socket " \
+ " ${__qemu_netdev1}" \
" -pidfile ${STATESETUP}/qemu_1.pid" \
" -device vhost-vsock-pci,guest-cid=$GUEST_1_CID"
GUEST_2_CID=94558
context_run_bg qemu_2 'qemu-system-'"${QEMU_ARCH}" \
' -M accel=kvm:tcg' \
- ' -m '${VMEM}' -cpu host -smp '${VCPUS} \
+ ' -m '${__vmem}' -cpu host -smp '${VCPUS} \
' -kernel '"${KERNEL}" \
' -initrd '${INITRAMFS}' -nographic -serial stdio' \
' -nodefaults' \
' -append "console=ttyS0 mitigations=off apparmor=0" ' \
- ' -device virtio-net-pci,netdev=s0 ' \
- " -netdev stream,id=s0,server=off,addr.type=unix,addr.path=${STATESETUP}/passt_2.socket " \
+ " ${__qemu_netdev2}" \
" -pidfile ${STATESETUP}/qemu_2.pid" \
" -device vhost-vsock-pci,guest-cid=$GUEST_2_CID"
diff --git a/test/lib/setup_ugly b/test/lib/setup_ugly
index 4b2a0774de1d..2802cc3bb43b 100755
--- a/test/lib/setup_ugly
+++ b/test/lib/setup_ugly
@@ -33,7 +33,7 @@ setup_memory() {
pane_or_context_run guest 'qemu-system-$(uname -m)' \
' -machine accel=kvm' \
- ' -m '${VMEM}' -cpu host -smp '${VCPUS} \
+ ' -m '$((${MEM_KIB} / 1024 / 4))' -cpu host -smp '${VCPUS} \
' -kernel ' "/boot/vmlinuz-$(uname -r)" \
' -initrd '${INITRAMFS_MEM}' -nographic -serial stdio' \
' -nodefaults' \
diff --git a/test/passt_vu b/test/passt_vu
new file mode 120000
index 000000000000..22f1840d1ad6
--- /dev/null
+++ b/test/passt_vu
@@ -0,0 +1 @@
+passt
\ No newline at end of file
diff --git a/test/passt_vu_in_ns b/test/passt_vu_in_ns
new file mode 120000
index 000000000000..3ff479e0436b
--- /dev/null
+++ b/test/passt_vu_in_ns
@@ -0,0 +1 @@
+passt_in_ns
\ No newline at end of file
diff --git a/test/perf/passt_vu_tcp b/test/perf/passt_vu_tcp
new file mode 100644
index 000000000000..b43400804e64
--- /dev/null
+++ b/test/perf/passt_vu_tcp
@@ -0,0 +1,211 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# PASST - Plug A Simple Socket Transport
+# for qemu/UNIX domain socket mode
+#
+# PASTA - Pack A Subtle Tap Abstraction
+# for network namespace/tap device mode
+#
+# test/perf/passt_vu_tcp - Check TCP performance in passt vhost-user mode
+#
+# Copyright (c) 2021 Red Hat GmbH
+# Author: Stefano Brivio <sbrivio@redhat.com>
+
+gtools /sbin/sysctl ip jq nproc seq sleep iperf3 tcp_rr tcp_crr # From neper
+nstools /sbin/sysctl ip jq nproc seq sleep iperf3 tcp_rr tcp_crr
+htools bc head sed seq
+
+set MAP_NS4 192.0.2.2
+set MAP_NS6 2001:db8:9a55::2
+
+test passt: throughput and latency
+
+guest /sbin/sysctl -w net.core.rmem_max=536870912
+guest /sbin/sysctl -w net.core.wmem_max=536870912
+guest /sbin/sysctl -w net.core.rmem_default=33554432
+guest /sbin/sysctl -w net.core.wmem_default=33554432
+guest /sbin/sysctl -w net.ipv4.tcp_rmem="4096 131072 268435456"
+guest /sbin/sysctl -w net.ipv4.tcp_wmem="4096 131072 268435456"
+guest /sbin/sysctl -w net.ipv4.tcp_timestamps=0
+
+ns /sbin/sysctl -w net.ipv4.tcp_rmem="4096 524288 134217728"
+ns /sbin/sysctl -w net.ipv4.tcp_wmem="4096 524288 134217728"
+ns /sbin/sysctl -w net.ipv4.tcp_timestamps=0
+
+gout IFNAME ip -j link show | jq -rM '.[] | select(.link_type == "ether").ifname'
+
+hout FREQ_PROCFS (echo "scale=1"; sed -n 's/cpu MHz.*: \([0-9]*\)\..*$/(\1+10^2\/2)\/10^3/p' /proc/cpuinfo) | bc -l | head -n1
+hout FREQ_CPUFREQ (echo "scale=1"; printf '( %i + 10^5 / 2 ) / 10^6\n' $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq) ) | bc -l
+hout FREQ [ -n "__FREQ_CPUFREQ__" ] && echo __FREQ_CPUFREQ__ || echo __FREQ_PROCFS__
+
+set THREADS 4
+set TIME 5
+set OMIT 0.1
+set OPTS -Z -P __THREADS__ -l 1M -O__OMIT__ -N
+
+info Throughput in Gbps, latency in µs, __THREADS__ threads at __FREQ__ GHz
+report passt_vu tcp __THREADS__ __FREQ__
+
+th MTU 256B 576B 1280B 1500B 9000B 65520B
+
+
+tr TCP throughput over IPv6: guest to host
+iperf3s ns 10002
+
+bw -
+bw -
+guest ip link set dev __IFNAME__ mtu 1280
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 16M
+bw __BW__ 1.2 1.5
+guest ip link set dev __IFNAME__ mtu 1500
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 32M
+bw __BW__ 1.6 1.8
+guest ip link set dev __IFNAME__ mtu 9000
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 64M
+bw __BW__ 4.0 5.0
+guest ip link set dev __IFNAME__ mtu 65520
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 64M
+bw __BW__ 7.0 8.0
+
+iperf3k ns
+
+tl TCP RR latency over IPv6: guest to host
+lat -
+lat -
+lat -
+lat -
+lat -
+nsb tcp_rr --nolog -6
+gout LAT tcp_rr --nolog -l1 -6 -c -H __MAP_NS6__ | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 200 150
+
+tl TCP CRR latency over IPv6: guest to host
+lat -
+lat -
+lat -
+lat -
+lat -
+nsb tcp_crr --nolog -6
+gout LAT tcp_crr --nolog -l1 -6 -c -H __MAP_NS6__ | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 500 400
+
+tr TCP throughput over IPv4: guest to host
+iperf3s ns 10002
+
+guest ip link set dev __IFNAME__ mtu 256
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 2M
+bw __BW__ 0.2 0.3
+guest ip link set dev __IFNAME__ mtu 576
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 4M
+bw __BW__ 0.5 0.8
+guest ip link set dev __IFNAME__ mtu 1280
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 8M
+bw __BW__ 1.2 1.5
+guest ip link set dev __IFNAME__ mtu 1500
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 16M
+bw __BW__ 1.6 1.8
+guest ip link set dev __IFNAME__ mtu 9000
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 64M
+bw __BW__ 4.0 5.0
+guest ip link set dev __IFNAME__ mtu 65520
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 64M
+bw __BW__ 7.0 8.0
+
+iperf3k ns
+
+# Reducing MTU below 1280 deconfigures IPv6, get our address back
+guest dhclient -6 -x
+guest dhclient -6 __IFNAME__
+
+tl TCP RR latency over IPv4: guest to host
+lat -
+lat -
+lat -
+lat -
+lat -
+nsb tcp_rr --nolog -4
+gout LAT tcp_rr --nolog -l1 -4 -c -H __MAP_NS4__ | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 200 150
+
+tl TCP CRR latency over IPv4: guest to host
+lat -
+lat -
+lat -
+lat -
+lat -
+nsb tcp_crr --nolog -4
+gout LAT tcp_crr --nolog -l1 -4 -c -H __MAP_NS4__ | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 500 400
+
+tr TCP throughput over IPv6: host to guest
+iperf3s guest 10001
+
+bw -
+bw -
+bw -
+bw -
+bw -
+iperf3 BW ns ::1 10001 __TIME__ __OPTS__ -w 32M
+bw __BW__ 6.0 6.8
+
+iperf3k guest
+
+tl TCP RR latency over IPv6: host to guest
+lat -
+lat -
+lat -
+lat -
+lat -
+guestb tcp_rr --nolog -P 10001 -C 10011 -6
+sleep 1
+nsout LAT tcp_rr --nolog -l1 -P 10001 -C 10011 -6 -c -H ::1 | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 200 150
+
+tl TCP CRR latency over IPv6: host to guest
+lat -
+lat -
+lat -
+lat -
+lat -
+guestb tcp_crr --nolog -P 10001 -C 10011 -6
+sleep 1
+nsout LAT tcp_crr --nolog -l1 -P 10001 -C 10011 -6 -c -H ::1 | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 500 350
+
+
+tr TCP throughput over IPv4: host to guest
+iperf3s guest 10001
+
+bw -
+bw -
+bw -
+bw -
+bw -
+iperf3 BW ns 127.0.0.1 10001 __TIME__ __OPTS__ -w 32M
+bw __BW__ 6.0 6.8
+
+iperf3k guest
+
+tl TCP RR latency over IPv4: host to guest
+lat -
+lat -
+lat -
+lat -
+lat -
+guestb tcp_rr --nolog -P 10001 -C 10011 -4
+sleep 1
+nsout LAT tcp_rr --nolog -l1 -P 10001 -C 10011 -4 -c -H 127.0.0.1 | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 200 150
+
+tl TCP CRR latency over IPv6: host to guest
+lat -
+lat -
+lat -
+lat -
+lat -
+guestb tcp_crr --nolog -P 10001 -C 10011 -4
+sleep 1
+nsout LAT tcp_crr --nolog -l1 -P 10001 -C 10011 -4 -c -H 127.0.0.1 | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 500 300
+
+te
diff --git a/test/perf/passt_vu_udp b/test/perf/passt_vu_udp
new file mode 100644
index 000000000000..943ac11b4a51
--- /dev/null
+++ b/test/perf/passt_vu_udp
@@ -0,0 +1,159 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# PASST - Plug A Simple Socket Transport
+# for qemu/UNIX domain socket mode
+#
+# PASTA - Pack A Subtle Tap Abstraction
+# for network namespace/tap device mode
+#
+# test/perf/passt_vu_udp - Check UDP performance in passt vhost-user mode
+#
+# Copyright (c) 2021 Red Hat GmbH
+# Author: Stefano Brivio <sbrivio@redhat.com>
+
+gtools /sbin/sysctl ip jq nproc sleep iperf3 udp_rr # From neper
+nstools ip jq sleep iperf3 udp_rr
+htools bc head sed
+
+set MAP_NS4 192.0.2.2
+set MAP_NS6 2001:db8:9a55::2
+
+test passt: throughput and latency
+
+guest /sbin/sysctl -w net.core.rmem_max=16777216
+guest /sbin/sysctl -w net.core.wmem_max=16777216
+guest /sbin/sysctl -w net.core.rmem_default=16777216
+guest /sbin/sysctl -w net.core.wmem_default=16777216
+
+hout FREQ_PROCFS (echo "scale=1"; sed -n 's/cpu MHz.*: \([0-9]*\)\..*$/(\1+10^2\/2)\/10^3/p' /proc/cpuinfo) | bc -l | head -n1
+hout FREQ_CPUFREQ (echo "scale=1"; printf '( %i + 10^5 / 2 ) / 10^6\n' $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq) ) | bc -l
+hout FREQ [ -n "__FREQ_CPUFREQ__" ] && echo __FREQ_CPUFREQ__ || echo __FREQ_PROCFS__
+
+set THREADS 2
+set TIME 1
+set OPTS -u -P __THREADS__ --pacing-timer 1000
+
+info Throughput in Gbps, latency in µs, __THREADS__ threads at __FREQ__ GHz
+
+report passt_vu udp __THREADS__ __FREQ__
+
+th pktlen 256B 576B 1280B 1500B 9000B 65520B
+
+tr UDP throughput over IPv6: guest to host
+iperf3s ns 10002
+# (datagram size) = (packet size) - 48: 40 bytes of IPv6 header, 8 of UDP header
+
+bw -
+bw -
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -b 3G -l 1232
+bw __BW__ 0.8 1.2
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -b 4G -l 1452
+bw __BW__ 1.0 1.5
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -b 10G -l 8952
+bw __BW__ 4.0 5.0
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -b 20G -l 64372
+bw __BW__ 4.0 5.0
+
+iperf3k ns
+
+tl UDP RR latency over IPv6: guest to host
+lat -
+lat -
+lat -
+lat -
+lat -
+nsb udp_rr --nolog -6
+gout LAT udp_rr --nolog -6 -c -H __MAP_NS6__ | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 200 150
+
+
+tr UDP throughput over IPv4: guest to host
+iperf3s ns 10002
+# (datagram size) = (packet size) - 28: 20 bytes of IPv4 header, 8 of UDP header
+
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -b 1G -l 228
+bw __BW__ 0.0 0.0
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -b 2G -l 548
+bw __BW__ 0.4 0.6
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -b 3G -l 1252
+bw __BW__ 0.8 1.2
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -b 4G -l 1472
+bw __BW__ 1.0 1.5
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -b 10G -l 8972
+bw __BW__ 4.0 5.0
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -b 20G -l 65492
+bw __BW__ 4.0 5.0
+
+iperf3k ns
+
+tl UDP RR latency over IPv4: guest to host
+lat -
+lat -
+lat -
+lat -
+lat -
+nsb udp_rr --nolog -4
+gout LAT udp_rr --nolog -4 -c -H __MAP_NS4__ | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 200 150
+
+
+tr UDP throughput over IPv6: host to guest
+iperf3s guest 10001
+# (datagram size) = (packet size) - 48: 40 bytes of IPv6 header, 8 of UDP header
+
+bw -
+bw -
+iperf3 BW ns ::1 10001 __TIME__ __OPTS__ -b 3G -l 1232
+bw __BW__ 0.8 1.2
+iperf3 BW ns ::1 10001 __TIME__ __OPTS__ -b 4G -l 1452
+bw __BW__ 1.0 1.5
+iperf3 BW ns ::1 10001 __TIME__ __OPTS__ -b 10G -l 8952
+bw __BW__ 3.0 4.0
+iperf3 BW ns ::1 10001 __TIME__ __OPTS__ -b 20G -l 64372
+bw __BW__ 3.0 4.0
+
+iperf3k guest
+
+tl UDP RR latency over IPv6: host to guest
+lat -
+lat -
+lat -
+lat -
+lat -
+guestb udp_rr --nolog -P 10001 -C 10011 -6
+sleep 1
+nsout LAT udp_rr --nolog -P 10001 -C 10011 -6 -c -H ::1 | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 200 150
+
+
+tr UDP throughput over IPv4: host to guest
+iperf3s guest 10001
+# (datagram size) = (packet size) - 28: 20 bytes of IPv4 header, 8 of UDP header
+
+iperf3 BW ns 127.0.0.1 10001 __TIME__ __OPTS__ -b 1G -l 228
+bw __BW__ 0.0 0.0
+iperf3 BW ns 127.0.0.1 10001 __TIME__ __OPTS__ -b 2G -l 548
+bw __BW__ 0.4 0.6
+iperf3 BW ns 127.0.0.1 10001 __TIME__ __OPTS__ -b 3G -l 1252
+bw __BW__ 0.8 1.2
+iperf3 BW ns 127.0.0.1 10001 __TIME__ __OPTS__ -b 4G -l 1472
+bw __BW__ 1.0 1.5
+iperf3 BW ns 127.0.0.1 10001 __TIME__ __OPTS__ -b 10G -l 8972
+bw __BW__ 3.0 4.0
+iperf3 BW ns 127.0.0.1 10001 __TIME__ __OPTS__ -b 20G -l 65492
+bw __BW__ 3.0 4.0
+
+iperf3k guest
+
+tl UDP RR latency over IPv4: host to guest
+lat -
+lat -
+lat -
+lat -
+lat -
+guestb udp_rr --nolog -P 10001 -C 10011 -4
+sleep 1
+nsout LAT udp_rr --nolog -P 10001 -C 10011 -4 -c -H 127.0.0.1 | sed -n 's/^throughput=\(.*\)/\1/p'
+lat __LAT__ 200 150
+
+te
diff --git a/test/run b/test/run
index 547a729b3fbe..f188d8eaf2e0 100755
--- a/test/run
+++ b/test/run
@@ -93,6 +93,7 @@ run() {
test memory/passt
teardown memory
+ VHOST_USER=0
setup passt
test passt/ndp
test passt/dhcp
@@ -115,7 +116,22 @@ run() {
test two_guests/basic
teardown two_guests
+ VHOST_USER=1
+ setup passt_in_ns
+ test passt_vu/ndp
+ test passt_vu_in_ns/dhcp
+ test passt_vu_in_ns/icmp
+ test passt_vu_in_ns/tcp
+ test passt_vu_in_ns/udp
+ test passt_vu_in_ns/shutdown
+ teardown passt_in_ns
+
+ setup two_guests
+ test two_guests_vu/basic
+ teardown two_guests
+
VALGRIND=0
+ VHOST_USER=0
setup passt_in_ns
test passt/ndp
test passt_in_ns/dhcp
@@ -126,6 +142,15 @@ run() {
test passt_in_ns/shutdown
teardown passt_in_ns
+ VHOST_USER=1
+ setup passt_in_ns
+ test passt_vu/ndp
+ test passt_vu_in_ns/dhcp
+ test perf/passt_vu_tcp
+ test perf/passt_vu_udp
+ test passt_vu_in_ns/shutdown
+ teardown passt_in_ns
+
# TODO: Make those faster by at least pre-installing gcc and make on
# non-x86 images, then re-enable.
skip_distro() {
diff --git a/test/two_guests_vu b/test/two_guests_vu
new file mode 120000
index 000000000000..144b7cac5438
--- /dev/null
+++ b/test/two_guests_vu
@@ -0,0 +1 @@
+test/two_guests
\ No newline at end of file
--
@@ -0,0 +1 @@
+test/two_guests
\ No newline at end of file
--
2.46.2
^ permalink raw reply related [flat|nested] 18+ messages in thread
* Re: [PATCH v7 0/8] Add vhost-user support to passt. (part 3)
2024-10-09 9:07 [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Laurent Vivier
` (7 preceding siblings ...)
2024-10-09 9:07 ` [PATCH v7 8/8] test: Add tests for passt in vhost-user mode Laurent Vivier
@ 2024-10-09 13:07 ` Stefano Brivio
2024-10-09 14:50 ` Laurent Vivier
2024-10-09 17:37 ` Stefano Brivio
9 siblings, 1 reply; 18+ messages in thread
From: Stefano Brivio @ 2024-10-09 13:07 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
On Wed, 9 Oct 2024 11:07:07 +0200
Laurent Vivier <lvivier@redhat.com> wrote:
> This series of patches adds vhost-user support to passt
> and then allows passt to connect to QEMU network backend using
> virtqueue rather than a socket.
>
> With QEMU, rather than using to connect:
>
> -netdev stream,id=s,server=off,addr.type=unix,addr.path=/tmp/passt_1.socket
>
> we will use:
>
> -chardev socket,id=chr0,path=/tmp/passt_1.socket
> -netdev vhost-user,id=netdev0,chardev=chr0
> -device virtio-net,netdev=netdev0
> -object memory-backend-memfd,id=memfd0,share=on,size=$RAMSIZE
> -numa node,memdev=memfd0
>
> The memory backend is needed to share data between passt and QEMU.
I just got the tests from 8/8 hanging like this (display with >= 212
columns):
guest$ socat -u TCP6-LISTEN:10001 OPEN:test_big.bin,create,trunc │Starting test: TCP/IPv6: host to ns: small transfer
guest$ cmp test_big.bin /root/big.bin │? cmp /tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_ns_small.bin /home/sbrivio/passt/test/small.bin
guest$ socat -u TCP6-LISTEN:10001 OPEN:test_big.bin,create,trunc │...passed.
guest$ cmp test_big.bin /root/big.bin │
guest$ socat -u TCP6-LISTEN:10001 OPEN:test_small.bin,create,trunc │Starting test: TCP/IPv6: guest to host: small transfer
guest$ cmp test_small.bin /root/small.bin │? cmp /tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_small.bin /home/sbrivio/passt/test/small.bin
guest$ socat -u OPEN:/root/small.bin TCP6:[2001:db8:9a55::1]:10003 │...passed.
guest$ socat -u OPEN:/root/small.bin TCP6:[2001:db8:9a55::2]:10002 │
guest$ socat -u TCP6-LISTEN:10001 OPEN:test_small.bin,create,trunc │Starting test: TCP/IPv6: guest to ns: small transfer
guest$ cmp test_small.bin /root/small.bin │? cmp /tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_ns_small.bin /home/sbrivio/passt/test/small.bin
guest$ socat -u TCP6-LISTEN:10001 OPEN:test_small.bin,create,trunc │...passed.
guest$ cmp test_small.bin /root/small.bin │
guest$ which socat ip jq >/dev/null │Starting test: TCP/IPv6: ns to host (spliced): small transfer
guest$ socat -u UDP4-LISTEN:10001,null-eof OPEN:test.bin,create,trunc │? cmp /tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_small.bin /home/sbrivio/passt/test/small.bin
│...passed.
──guest──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
ns$ socat -u OPEN:/home/sbrivio/passt/test/big.bin TCP6:[2001:db8:9a55::1]:10003 │Starting test: TCP/IPv6: ns to host (via tap): small transfer
ns$ socat -u OPEN:/home/sbrivio/passt/test/big.bin TCP6:[::1]:10001 │? cmp /tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_small.bin /home/sbrivio/passt/test/small.bin
ns$ ip -j link show | jq -rM '.[] | select(.link_type == "ether").ifname' │...passed.
enp9s0 │
ns$ ip -j -6 addr show|jq -rM '.[] | select(.ifname == "enp9s0").addr_info[0].local' │Starting test: TCP/IPv6: ns to guest (using loopback address): small transfer
2a01:4f8:222:904::2 │...passed.
ns$ socat -u OPEN:/home/sbrivio/passt/test/big.bin TCP6:[2a01:4f8:222:904::2]:10001 │
ns$ socat -u TCP6-LISTEN:10002 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_ns_small.bin,create,trunc │Starting test: TCP/IPv6: ns to guest (using namespace address): small transfer
ns$ socat -u TCP6-LISTEN:10002 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_ns_small.bin │...passed.
ns$ socat -u OPEN:/home/sbrivio/passt/test/small.bin TCP6:[::1]:10003 │
ns$ socat -u OPEN:/home/sbrivio/passt/test/small.bin TCP6:[2001:db8:9a55::1]:10003 │==================================================================================================================
ns$ socat -u OPEN:/home/sbrivio/passt/test/small.bin TCP6:[::1]:10001 │Starting tests in file: passt_vu_in_ns/udp
ns$ socat -u OPEN:/home/sbrivio/passt/test/small.bin TCP6:[2a01:4f8:222:904::2]:10001 │
ns$ which socat ip jq >/dev/null │Starting test: UDP/IPv4: host to guest
ns$ │
──namespace─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬────────────────────┴──passt_vu_in_ns/udp [1/16] - UDP/IPv4: host to guest─────────────────────────────────────────────────────────────
host$ socat -u OPEN:/home/sbrivio/passt/test/big.bin TCP4:127.0.0.1:10001 │NDP/DHCPv6:
host$ socat -u OPEN:/home/sbrivio/passt/test/big.bin TCP4:127.0.0.1:10002 │ assign: 2a01:4f8:222:904::2
host$ socat -u TCP4-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_big.bin,create,trunc │ router: fe80::1
host$ socat -u TCP4-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_big.bin,create,trunc │ our link-local: fe80::1
host$ socat -u TCP4-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_big.bin,create,trunc │DNS:
host$ socat -u OPEN:/home/sbrivio/passt/test/small.bin TCP4:127.0.0.1:10001 │ 2a01:4ff:ff00::add:2
host$ socat -u OPEN:/home/sbrivio/passt/test/small.bin TCP4:127.0.0.1:10002 │ 2a01:4ff:ff00::add:1
host$ socat -u TCP4-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_small.bin,create,trunc │You can start qemu with:
host$ socat -u TCP4-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_small.bin,create,trunc │ kvm ... -chardev socket,id=chr0,path=/tmp/passt-tests-KLvyGO/passt_in_ns/passt.socket -netdev vhost-user,id=netdev0,chardev=chr0 -d
host$ socat -u TCP4-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_small.bin,create,trunc │evice virtio-net,netdev=netdev0 -object memory-backend-memfd,id=memfd0,share=on,size=$RAMSIZE -numa node,memdev=memfd0
host$ socat -u OPEN:/home/sbrivio/passt/test/big.bin TCP6:[::1]:10001 │
host$ socat -u OPEN:/home/sbrivio/passt/test/big.bin TCP6:[::1]:10002 │accepted connection from PID 4848
host$ socat -u TCP6-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_big.bin,create,trunc │==4846== Warning: set address range perms: large range [0x59c8f000, 0x119c8f000) (defined)
host$ socat -u TCP6-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_big.bin,create,trunc │==4846== Warning: set address range perms: large range [0x119c8f000, 0x519c8f000) (defined)
host$ socat -u TCP6-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_big.bin,create,trunc │NDP: received RS, sending RA
host$ socat -u OPEN:/home/sbrivio/passt/test/small.bin TCP6:[::1]:10001 │DHCP: offer to discover
host$ socat -u OPEN:/home/sbrivio/passt/test/small.bin TCP6:[::1]:10002 │ from 52:54:00:12:34:56
host$ socat -u TCP6-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_small.bin,create,trunc │DHCP: ack to request
host$ socat -u TCP6-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_small.bin,create,trunc │ from 52:54:00:12:34:56
host$ socat -u TCP6-LISTEN:10003 OPEN:/tmp/passt-tests-KLvyGO/passt_vu_in_ns/tcp/test_small.bin,create,trunc │DHCPv6: received SOLICIT, sending ADVERTISE
host$ which socat ip jq >/dev/null │DHCPv6: received REQUEST/RENEW/CONFIRM, sending REPLY
host$ socat -u OPEN:/home/sbrivio/passt/test/medium.bin UDP4:127.0.0.1:10001,shut-null │NDP: received NS, sending NA
host$ │
──host──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──passt in pasta (namespace)───────────────────────────────────────────────────────────────────────────────────────────────────────────
Testing commit: 529d9fd test: Add tests for passt in vhost-user mode PASS: 192 | FAIL: 0 | 2024-10-09T12:39:37+00:00
...that is, in passt_vu_in_ns/udp, on the basic "UDP/IPv4: host to guest"
test, the client is already done, but the server gets nothing. It doesn't
look like a race condition in the test itself, because if I re-run the
client manually the server is still stuck, but I didn't really investigate,
yet.
The server is just waiting for data:
$ ssh -F /tmp/passt-tests-KLvyGO/passt_in_ns/context_guest.ssh guest sh
cd /proc
cat 562/cmdline
socat-uUDP6-LISTEN:10001,null-eofOPEN:test.bin,create,trunc
strace -p 562
strace: Process 562 attached
pselect6(6, [5], [], [], NULL, NULL
--
Stefano
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v7 0/8] Add vhost-user support to passt. (part 3)
2024-10-09 13:07 ` [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Stefano Brivio
@ 2024-10-09 14:50 ` Laurent Vivier
0 siblings, 0 replies; 18+ messages in thread
From: Laurent Vivier @ 2024-10-09 14:50 UTC (permalink / raw)
To: Stefano Brivio; +Cc: passt-dev
On 09/10/2024 15:07, Stefano Brivio wrote:
> On Wed, 9 Oct 2024 11:07:07 +0200
> Laurent Vivier <lvivier@redhat.com> wrote:
>
>> This series of patches adds vhost-user support to passt
>> and then allows passt to connect to QEMU network backend using
>> virtqueue rather than a socket.
>>
>> With QEMU, rather than using to connect:
>>
>> -netdev stream,id=s,server=off,addr.type=unix,addr.path=/tmp/passt_1.socket
>>
>> we will use:
>>
>> -chardev socket,id=chr0,path=/tmp/passt_1.socket
>> -netdev vhost-user,id=netdev0,chardev=chr0
>> -device virtio-net,netdev=netdev0
>> -object memory-backend-memfd,id=memfd0,share=on,size=$RAMSIZE
>> -numa node,memdev=memfd0
>>
>> The memory backend is needed to share data between passt and QEMU.
...
>
> ...that is, in passt_vu_in_ns/udp, on the basic "UDP/IPv4: host to guest"
> test, the client is already done, but the server gets nothing. It doesn't
> look like a race condition in the test itself, because if I re-run the
> client manually the server is still stuck, but I didn't really investigate,
> yet.
Yes, I've seen that. I didn't know if the problem was in the test or in my code.
I found it's a regression when I have introduced my new function vu_collect_one_frame().
I'm on it.
Thanks,
Laurent
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v7 0/8] Add vhost-user support to passt. (part 3)
2024-10-09 9:07 [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Laurent Vivier
` (8 preceding siblings ...)
2024-10-09 13:07 ` [PATCH v7 0/8] Add vhost-user support to passt. (part 3) Stefano Brivio
@ 2024-10-09 17:37 ` Stefano Brivio
2024-10-10 7:08 ` Stefano Brivio
9 siblings, 1 reply; 18+ messages in thread
From: Stefano Brivio @ 2024-10-09 17:37 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
On Wed, 9 Oct 2024 11:07:07 +0200
Laurent Vivier <lvivier@redhat.com> wrote:
> This series of patches adds vhost-user support to passt
> and then allows passt to connect to QEMU network backend using
> virtqueue rather than a socket.
>
> With QEMU, rather than using to connect:
>
> -netdev stream,id=s,server=off,addr.type=unix,addr.path=/tmp/passt_1.socket
>
> we will use:
>
> -chardev socket,id=chr0,path=/tmp/passt_1.socket
> -netdev vhost-user,id=netdev0,chardev=chr0
> -device virtio-net,netdev=netdev0
> -object memory-backend-memfd,id=memfd0,share=on,size=$RAMSIZE
> -numa node,memdev=memfd0
>
> The memory backend is needed to share data between passt and QEMU.
>
> Performance comparison between "-netdev stream" and "-netdev vhost-user":
On my setup, with a few tweaks (don't ask me why... we should figure
out eventually):
--
diff --git a/test/perf/passt_vu_tcp b/test/perf/passt_vu_tcp
index b434008..76bdd48 100644
--- a/test/perf/passt_vu_tcp
+++ b/test/perf/passt_vu_tcp
@@ -38,10 +38,10 @@ hout FREQ_PROCFS (echo "scale=1"; sed -n 's/cpu MHz.*: \([0-9]*\)\..*$/(\1+10^2\
hout FREQ_CPUFREQ (echo "scale=1"; printf '( %i + 10^5 / 2 ) / 10^6\n' $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq) ) | bc -l
hout FREQ [ -n "__FREQ_CPUFREQ__" ] && echo __FREQ_CPUFREQ__ || echo __FREQ_PROCFS__
-set THREADS 4
+set THREADS 4-6
set TIME 5
set OMIT 0.1
-set OPTS -Z -P __THREADS__ -l 1M -O__OMIT__ -N
+set OPTS -Z -O__OMIT__ -N
info Throughput in Gbps, latency in µs, __THREADS__ threads at __FREQ__ GHz
report passt_vu tcp __THREADS__ __FREQ__
@@ -55,16 +55,16 @@ iperf3s ns 10002
bw -
bw -
guest ip link set dev __IFNAME__ mtu 1280
-iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 16M
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 16M -l 1M -P 4
bw __BW__ 1.2 1.5
guest ip link set dev __IFNAME__ mtu 1500
-iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 32M
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 32M -l 1M -P 4
bw __BW__ 1.6 1.8
guest ip link set dev __IFNAME__ mtu 9000
-iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 64M
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 64M -l 1M -P 4
bw __BW__ 4.0 5.0
guest ip link set dev __IFNAME__ mtu 65520
-iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 64M
+iperf3 BW guest __MAP_NS6__ 10002 __TIME__ __OPTS__ -w 256M -l 1M -P 4
bw __BW__ 7.0 8.0
iperf3k ns
@@ -93,22 +93,22 @@ tr TCP throughput over IPv4: guest to host
iperf3s ns 10002
guest ip link set dev __IFNAME__ mtu 256
-iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 2M
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 2M -l 1M -P 4
bw __BW__ 0.2 0.3
guest ip link set dev __IFNAME__ mtu 576
-iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 4M
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 4M -l 1M -P 4
bw __BW__ 0.5 0.8
guest ip link set dev __IFNAME__ mtu 1280
-iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 8M
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 8M -l 1M -P 4
bw __BW__ 1.2 1.5
guest ip link set dev __IFNAME__ mtu 1500
-iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 16M
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 16M -l 1M -P 4
bw __BW__ 1.6 1.8
guest ip link set dev __IFNAME__ mtu 9000
-iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 64M
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 64M -l 1M -P 4
bw __BW__ 4.0 5.0
guest ip link set dev __IFNAME__ mtu 65520
-iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 64M
+iperf3 BW guest __MAP_NS4__ 10002 __TIME__ __OPTS__ -w 256M -l 1M -P 4
bw __BW__ 7.0 8.0
iperf3k ns
@@ -145,7 +145,7 @@ bw -
bw -
bw -
bw -
-iperf3 BW ns ::1 10001 __TIME__ __OPTS__ -w 32M
+iperf3 BW ns ::1 10001 __TIME__ __OPTS__ -w 256M -l 16k -P 6
bw __BW__ 6.0 6.8
iperf3k guest
@@ -181,7 +181,7 @@ bw -
bw -
bw -
bw -
-iperf3 BW ns 127.0.0.1 10001 __TIME__ __OPTS__ -w 32M
+iperf3 BW ns 127.0.0.1 10001 __TIME__ __OPTS__ -w 256M -l 16k -P 6
bw __BW__ 6.0 6.8
iperf3k guest
--
I'm getting an even bigger improvement in throughput (and also
significantly lower latency). Non-vhost-user first:
--
=== perf/passt_tcp
> passt: throughput and latency
Throughput in Gbps, latency in µs, 4 threads at 3.6 GHz
MTU: | 256B | 576B | 1280B | 1500B | 9000B | 65520B |
|--------|--------|--------|--------|--------|--------|
TCP throughput over IPv6: guest to host | - | - | 6.3 | 6.8 | 18.4 | 21.4 |
TCP RR latency over IPv6: guest to host | - | - | - | - | - | 52 |
TCP CRR latency over IPv6: guest to host | - | - | - | - | - | 141 |
|--------|--------|--------|--------|--------|--------|
TCP throughput over IPv4: guest to host | 0.8 | 3.0 | 5.6 | 7.4 | 19.6 | 21.3 |
TCP RR latency over IPv4: guest to host | - | - | - | - | - | 58 |
TCP CRR latency over IPv4: guest to host | - | - | - | - | - | 132 |
|--------|--------|--------|--------|--------|--------|
TCP throughput over IPv6: host to guest | - | - | - | - | - | 18.0 |
TCP RR latency over IPv6: host to guest | - | - | - | - | - | 50 |
TCP CRR latency over IPv6: host to guest | - | - | - | - | - | 115 |
|--------|--------|--------|--------|--------|--------|
TCP throughput over IPv4: host to guest | - | - | - | - | - | 17.8 |
TCP RR latency over IPv4: host to guest | - | - | - | - | - | 60 |
TCP CRR latency over IPv6: host to guest | - | - | - | - | - | 94 |
'--------'--------'--------'--------'--------'--------'
...passed.
=== perf/passt_udp
> passt: throughput and latency
Throughput in Gbps, latency in µs, 2 threads at 3.6 GHz
pktlen: | 256B | 576B | 1280B | 1500B | 9000B | 65520B |
|--------|--------|--------|--------|--------|--------|
UDP throughput over IPv6: guest to host | - | - | 3.4 | 4.1 | 12.3 | 18.2 |
UDP RR latency over IPv6: guest to host | - | - | - | - | - | 49 |
|--------|--------|--------|--------|--------|--------|
UDP throughput over IPv4: guest to host | 0.8 | 1.9 | 3.7 | 4.0 | 11.1 | 17.2 |
UDP RR latency over IPv4: guest to host | - | - | - | - | - | 52 |
|--------|--------|--------|--------|--------|--------|
UDP throughput over IPv6: host to guest | - | - | 2.6 | 3.1 | 5.4 | 17.9 |
UDP RR latency over IPv6: host to guest | - | - | - | - | - | 48 |
|--------|--------|--------|--------|--------|--------|
UDP throughput over IPv4: host to guest | 0.9 | 2.3 | 5.6 | 7.4 | 12.8 | 16.6 |
UDP RR latency over IPv4: host to guest | - | - | - | - | - | 48 |
'--------'--------'--------'--------'--------'--------'
...passed.
[...]
=== perf/passt_vu_tcp
> passt: throughput and latency
Throughput in Gbps, latency in µs, 4-6 threads at 3.6 GHz
MTU: | 256B | 576B | 1280B | 1500B | 9000B | 65520B |
|--------|--------|--------|--------|--------|--------|
TCP throughput over IPv6: guest to host | - | - | 8.2 | 10.1 | 14.7 | 22.3 |
TCP RR latency over IPv6: guest to host | - | - | - | - | - | 30 |
TCP CRR latency over IPv6: guest to host | - | - | - | - | - | 88 |
|--------|--------|--------|--------|--------|--------|
TCP throughput over IPv4: guest to host | 1.2 | 5.3 | 9.2 | 10.1 | 18.5 | 23.7 |
TCP RR latency over IPv4: guest to host | - | - | - | - | - | 31 |
TCP CRR latency over IPv4: guest to host | - | - | - | - | - | 93 |
|--------|--------|--------|--------|--------|--------|
TCP throughput over IPv6: host to guest | - | - | - | - | - | 42.1 |
TCP RR latency over IPv6: host to guest | - | - | - | - | - | 30 |
TCP CRR latency over IPv6: host to guest | - | - | - | - | - | 88 |
|--------|--------|--------|--------|--------|--------|
TCP throughput over IPv4: host to guest | - | - | - | - | - | 48.8 |
TCP RR latency over IPv4: host to guest | - | - | - | - | - | 35 |
TCP CRR latency over IPv6: host to guest | - | - | - | - | - | 79 |
'--------'--------'--------'--------'--------'--------'
...passed.
=== perf/passt_vu_udp
> passt: throughput and latency
Throughput in Gbps, latency in µs, 2 threads at 3.6 GHz
pktlen: | 256B | 576B | 1280B | 1500B | 9000B | 65520B |
|--------|--------|--------|--------|--------|--------|
UDP throughput over IPv6: guest to host | - | - | 2.2 | 2.6 | 14.1 | 33.4 |
UDP RR latency over IPv6: guest to host | - | - | - | - | - | 32 |
|--------|--------|--------|--------|--------|--------|
UDP throughput over IPv4: guest to host | 0.4 | 1.1 | 2.6 | 2.3 | 13.6 | 28.9 |
UDP RR latency over IPv4: guest to host | - | - | - | - | - | 31 |
|--------|--------|--------|--------|--------|--------|
UDP throughput over IPv6: host to guest | - | - | 3.1 | 3.7 | 18.7 | 29.4 |
UDP RR latency over IPv6: host to guest | - | - | - | - | - | 35 |
|--------|--------|--------|--------|--------|--------|
UDP throughput over IPv4: host to guest | 0.5 | 1.3 | 3.3 | 3.8 | 18.6 | 37.5 |
UDP RR latency over IPv4: host to guest | - | - | - | - | - | 35 |
'--------'--------'--------'--------'--------'--------'
...passed.
--
passt is CPU-bound only on host-to-guest tests. But there, iperf3 seems
to actually use more CPU time than passt itself.
--
Stefano
^ permalink raw reply related [flat|nested] 18+ messages in thread
* Re: [PATCH v7 0/8] Add vhost-user support to passt. (part 3)
2024-10-09 17:37 ` Stefano Brivio
@ 2024-10-10 7:08 ` Stefano Brivio
2024-10-10 7:43 ` Laurent Vivier
` (2 more replies)
0 siblings, 3 replies; 18+ messages in thread
From: Stefano Brivio @ 2024-10-10 7:08 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
On Wed, 9 Oct 2024 19:37:26 +0200
Stefano Brivio <sbrivio@redhat.com> wrote:
> On Wed, 9 Oct 2024 11:07:07 +0200
> Laurent Vivier <lvivier@redhat.com> wrote:
>
> > This series of patches adds vhost-user support to passt
> > and then allows passt to connect to QEMU network backend using
> > virtqueue rather than a socket.
> >
> > With QEMU, rather than using to connect:
> >
> > -netdev stream,id=s,server=off,addr.type=unix,addr.path=/tmp/passt_1.socket
> >
> > we will use:
> >
> > -chardev socket,id=chr0,path=/tmp/passt_1.socket
> > -netdev vhost-user,id=netdev0,chardev=chr0
> > -device virtio-net,netdev=netdev0
> > -object memory-backend-memfd,id=memfd0,share=on,size=$RAMSIZE
> > -numa node,memdev=memfd0
> >
> > The memory backend is needed to share data between passt and QEMU.
> >
> > Performance comparison between "-netdev stream" and "-netdev vhost-user":
>
> On my setup, with a few tweaks (don't ask me why... we should figure
> out eventually):
...I had a closer look with perf(1).
For outbound traffic (I checked with IPv6), a reasonably expanded
output (20 seconds of iperf3 with those parameters):
--
Samples: 80K of event 'cycles', Event count (approx.): 81191130978
Children Self Command Shared Object Symbol
- 89.20% 0.07% passt.avx2 [kernel.kallsyms] [k] entry_SYSCALL_64_after_hwframe ▒
89.13% entry_SYSCALL_64_after_hwframe ◆
- do_syscall_64 ▒
- 69.37% __sys_recvmsg ▒
- 69.26% ___sys_recvmsg ▒
- 68.11% ____sys_recvmsg ▒
- 67.80% inet6_recvmsg ▒
- tcp_recvmsg ▒
- 67.56% tcp_recvmsg_locked ▒
- 64.14% skb_copy_datagram_iter ▒
- __skb_datagram_iter ▒
- 51.42% _copy_to_iter ▒
48.74% copy_user_generic_string ▒
- 6.96% simple_copy_to_iter ▒
+ __check_object_size ▒
+ 1.09% __tcp_transmit_skb ▒
+ 0.75% tcp_rcv_space_adjust ▒
+ 0.61% __tcp_cleanup_rbuf ▒
+ 1.01% copy_msghdr_from_user ▒
- 16.60% __x64_sys_recvfrom ▒
- __sys_recvfrom ▒
- 16.43% inet6_recvmsg ▒
- tcp_recvmsg ▒
- 14.27% tcp_recvmsg_locked ▒
- 12.67% __tcp_transmit_skb ▒
- 12.64% __ip_queue_xmit ▒
- 12.56% ip_finish_output2 ▒
- 12.51% __local_bh_enable_ip ▒
- do_softirq.part.0 ▒
- 12.50% __softirqentry_text_start ▒
- 12.49% net_rx_action ▒
- 12.47% __napi_poll ▒
- process_backlog ▒
- 12.41% __netif_receive_skb_one_core ▒
- 9.75% ip_local_deliver_finish ▒
- 9.52% ip_protocol_deliver_rcu ▒
+ 9.28% tcp_v4_rcv ▒
- 2.02% ip_local_deliver ▒
- 1.92% nf_hook_slow ▒
+ 1.84% nft_do_chain_ipv4 ▒
0.70% __sk_mem_reduce_allocated ▒
+ 2.10% release_sock ▒
+ 1.16% __x64_sys_timerfd_settime ▒
+ 0.56% ksys_write ▒
+ 0.54% __x64_sys_epoll_wait ▒
+ 89.13% 0.06% passt.avx2 [kernel.kallsyms] [k] do_syscall_64 ▒
+ 84.23% 0.02% passt.avx2 [kernel.kallsyms] [k] inet6_recvmsg ▒
+ 84.21% 0.04% passt.avx2 [kernel.kallsyms] [k] tcp_recvmsg ▒
+ 81.84% 0.97% passt.avx2 [kernel.kallsyms] [k] tcp_recvmsg_locked ▒
+ 74.96% 0.00% passt.avx2 [unknown] [k] 0000000000000000 ▒
+ 69.78% 0.07% passt.avx2 libc.so.6 [.] __libc_recvmsg ▒
+ 69.37% 0.02% passt.avx2 [kernel.kallsyms] [k] __sys_recvmsg ▒
+ 69.26% 0.03% passt.avx2 [kernel.kallsyms] [k] ___sys_recvmsg ▒
+ 68.11% 0.12% passt.avx2 [kernel.kallsyms] [k] ____sys_recvmsg ▒
+ 64.14% 0.06% passt.avx2 [kernel.kallsyms] [k] skb_copy_datagram_iter ▒
+ 64.08% 5.60% passt.avx2 [kernel.kallsyms] [k] __skb_datagram_iter ▒
+ 51.44% 2.68% passt.avx2 [kernel.kallsyms] [k] _copy_to_iter ▒
+ 49.16% 49.08% passt.avx2 [kernel.kallsyms] [k] copy_user_generic_string ◆
+ 16.84% 0.00% passt.avx2 [unknown] [k] 0xffff000000000000 ▒
+ 16.77% 0.02% passt.avx2 libc.so.6 [.] __libc_recv ▒
+ 16.60% 0.00% passt.avx2 [kernel.kallsyms] [k] __x64_sys_recvfrom ▒
+ 16.60% 0.07% passt.avx2 [kernel.kallsyms] [k] __sys_recvfrom ▒
+ 13.81% 1.28% passt.avx2 [kernel.kallsyms] [k] __tcp_transmit_skb ▒
+ 13.76% 0.06% passt.avx2 [kernel.kallsyms] [k] __ip_queue_xmit ▒
+ 13.64% 0.10% passt.avx2 [kernel.kallsyms] [k] ip_finish_output2 ▒
+ 13.62% 0.14% passt.avx2 [kernel.kallsyms] [k] __local_bh_enable_ip ▒
+ 13.57% 0.01% passt.avx2 [kernel.kallsyms] [k] __softirqentry_text_start ▒
+ 13.56% 0.01% passt.avx2 [kernel.kallsyms] [k] do_softirq.part.0 ▒
+ 13.53% 0.01% passt.avx2 [kernel.kallsyms] [k] net_rx_action ▒
+ 13.51% 0.00% passt.avx2 [kernel.kallsyms] [k] __napi_poll ▒
+ 13.51% 0.04% passt.avx2 [kernel.kallsyms] [k] process_backlog ▒
+ 13.45% 0.02% passt.avx2 [kernel.kallsyms] [k] __netif_receive_skb_one_core ▒
+ 11.27% 0.04% passt.avx2 [kernel.kallsyms] [k] tcp_v4_do_rcv ▒
+ 10.96% 0.06% passt.avx2 [kernel.kallsyms] [k] tcp_rcv_established ▒
+ 10.74% 0.02% passt.avx2 [kernel.kallsyms] [k] ip_local_deliver_finish ▒
+ 10.51% 0.04% passt.avx2 [kernel.kallsyms] [k] ip_protocol_deliver_rcu ▒
+ 10.26% 0.17% passt.avx2 [kernel.kallsyms] [k] tcp_v4_rcv ▒
+ 8.14% 0.01% passt.avx2 [kernel.kallsyms] [k] __tcp_push_pending_frames ▒
+ 8.13% 0.73% passt.avx2 [kernel.kallsyms] [k] tcp_write_xmit ▒
+ 6.96% 0.26% passt.avx2 [kernel.kallsyms] [k] simple_copy_to_iter ▒
+ 6.79% 4.72% passt.avx2 [kernel.kallsyms] [k] __check_object_size ▒
+ 3.33% 0.16% passt.avx2 [kernel.kallsyms] [k] nf_hook_slow ▒
+ 3.09% 0.09% passt.avx2 [nf_tables] [k] nft_do_chain_ipv4 ▒
+ 3.00% 2.28% passt.avx2 [nf_tables] [k] nft_do_chain ▒
+ 2.85% 2.84% passt.avx2 passt.avx2 [.] vu_init_elem ▒
+ 2.22% 0.02% passt.avx2 [kernel.kallsyms] [k] release_sock ▒
+ 2.15% 0.02% passt.avx2 [kernel.kallsyms] [k] __release_sock ▒
+ 2.04% 0.08% passt.avx2 [kernel.kallsyms] [k] ip_local_deliver ▒
+ 1.80% 1.79% passt.avx2 [kernel.kallsyms] [k] __virt_addr_valid ▒
+ 1.57% 0.03% passt.avx2 libc.so.6 [.] timerfd_settime ▒
1.53% 1.53% passt.avx2 passt.avx2 [.] vu_queue_map_desc.isra.0 ▒
--
not much we can improve (and the throughput is anyway very close to
iperf3 to iperf3 on host's loopback, ~50 Gbps vs. ~70): the bulk of
it is copy_user_generic_string() reading from sockets into the queue
and related bookkeeping.
The only users of more than 1% of cycles are vu_init_elem() and
vu_queue_map_desc(), perhaps we could try to speed those up... one
day.
Full perf output (you can load it with perf -i ...), if you're curious,
at:
https://passt.top/static/vu_tcp_ipv6_inbound.perf
For outbound traffic (I tried with IPv4), which is much slower for some
reason (~25 Gbps):
--
Samples: 79K of event 'cycles', Event count (approx.): 73661070737
Children Self Command Shared Object Symbol
- 91.00% 0.23% passt.avx2 [kernel.kallsyms] [k] entry_SYSCALL_64_after_hwframe ◆
90.78% entry_SYSCALL_64_after_hwframe ▒
- do_syscall_64 ▒
- 78.75% __sys_sendmsg ▒
- 78.58% ___sys_sendmsg ▒
- 78.06% ____sys_sendmsg ▒
- sock_sendmsg ▒
- 77.58% tcp_sendmsg ▒
- 68.63% tcp_sendmsg_locked ▒
- 26.24% sk_page_frag_refill ▒
- skb_page_frag_refill ▒
- 25.87% __alloc_pages ▒
- 25.61% get_page_from_freelist ▒
24.51% clear_page_rep ▒
- 23.08% _copy_from_iter ▒
22.88% copy_user_generic_string ▒
- 8.77% tcp_write_xmit ▒
- 8.19% __tcp_transmit_skb ▒
- 7.86% __ip_queue_xmit ▒
- 7.13% ip_finish_output2 ▒
- 6.65% __local_bh_enable_ip ▒
- 6.60% do_softirq.part.0 ▒
- 6.51% __softirqentry_text_start ▒
- 6.40% net_rx_action ▒
- 5.43% __napi_poll ▒
+ process_backlog ▒
0.50% napi_consume_skb ▒
+ 5.39% __tcp_push_pending_frames ▒
+ 2.03% tcp_stream_alloc_skb ▒
+ 1.48% tcp_wmem_schedule ▒
+ 8.58% release_sock ▒
- 4.57% ksys_write ▒
- 4.41% vfs_write ▒
- 3.96% eventfd_write ▒
- 3.46% __wake_up_common ▒
- irqfd_wakeup ▒
- 3.15% kvm_arch_set_irq_inatomic ▒
- 3.11% kvm_irq_delivery_to_apic_fast ▒
- 2.01% __apic_accept_irq ▒
0.93% svm_complete_interrupt_delivery ▒
+ 3.91% __x64_sys_epoll_wait ▒
+ 1.20% __x64_sys_getsockopt ▒
+ 0.78% syscall_trace_enter.constprop.0 ▒
0.71% syscall_exit_to_user_mode ▒
+ 0.61% ksys_read ▒
--
...there are no users of more than 1% cycles in passt itself. The bulk of
it is sendmsg() as expected, one notable thing is that the kernel spends
an awful amount of cycles zeroing pages so that we can fill them. I looked
into that "issue" a long time ago,
https://github.com/netoptimizer/prototype-kernel/pull/39/commits/2c8223c30d7f280a9e456d8e690adb0869ed8c5c
...maybe I can try out a kernel with a version of that as
clear_page_rep() and see what happens. Anyway, same here, I don't
see anything we can really improve in passt.
Full output at: https://passt.top/static/vu_tcp_ipv4_outbound.perf
--
Stefano
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v7 0/8] Add vhost-user support to passt. (part 3)
2024-10-10 7:08 ` Stefano Brivio
@ 2024-10-10 7:43 ` Laurent Vivier
2024-10-10 7:45 ` Laurent Vivier
2024-10-11 18:07 ` Stefano Brivio
2 siblings, 0 replies; 18+ messages in thread
From: Laurent Vivier @ 2024-10-10 7:43 UTC (permalink / raw)
To: Stefano Brivio; +Cc: passt-dev
On 10/10/2024 09:08, Stefano Brivio wrote:
> The only users of more than 1% of cycles are vu_init_elem() and
> vu_queue_map_desc(), perhaps we could try to speed those up... one
> day.
I think vu_init_elem() could be executed only once at startup. I need to check how to do
that. It's an easy improvement, it's on my list.
Thanks,
Laurent
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v7 0/8] Add vhost-user support to passt. (part 3)
2024-10-10 7:08 ` Stefano Brivio
2024-10-10 7:43 ` Laurent Vivier
@ 2024-10-10 7:45 ` Laurent Vivier
2024-10-10 7:52 ` Stefano Brivio
2024-10-11 18:07 ` Stefano Brivio
2 siblings, 1 reply; 18+ messages in thread
From: Laurent Vivier @ 2024-10-10 7:45 UTC (permalink / raw)
To: Stefano Brivio; +Cc: passt-dev
On 10/10/2024 09:08, Stefano Brivio wrote:
> For outbound traffic (I tried with IPv4), which is much slower for some
> reason (~25 Gbps):
Perhaps because we can't bypass IPv4 header checksum whereas there is no IPv6 header
checksum ?
Thanks,
Laurent
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v7 0/8] Add vhost-user support to passt. (part 3)
2024-10-10 7:45 ` Laurent Vivier
@ 2024-10-10 7:52 ` Stefano Brivio
0 siblings, 0 replies; 18+ messages in thread
From: Stefano Brivio @ 2024-10-10 7:52 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
On Thu, 10 Oct 2024 09:45:36 +0200
Laurent Vivier <lvivier@redhat.com> wrote:
> On 10/10/2024 09:08, Stefano Brivio wrote:
> > For outbound traffic (I tried with IPv4), which is much slower for some
> > reason (~25 Gbps):
>
> Perhaps because we can't bypass IPv4 header checksum whereas there is no IPv6 header
> checksum ?
Ah, no, sorry, I meant slower than inbound traffic. For outbound
traffic, I get very similar numbers between IPv4 and IPv6. And judging
from perf output, it's all about copying and zeroing and stuff.
As far as I know the kernel doesn't actually compute checksum, delivery
is local.
--
Stefano
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH v7 0/8] Add vhost-user support to passt. (part 3)
2024-10-10 7:08 ` Stefano Brivio
2024-10-10 7:43 ` Laurent Vivier
2024-10-10 7:45 ` Laurent Vivier
@ 2024-10-11 18:07 ` Stefano Brivio
2024-10-17 0:10 ` Stefano Brivio
2 siblings, 1 reply; 18+ messages in thread
From: Stefano Brivio @ 2024-10-11 18:07 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
On Thu, 10 Oct 2024 09:08:01 +0200
Stefano Brivio <sbrivio@redhat.com> wrote:
> For outbound traffic (I tried with IPv4), which is much slower for some
> reason (~25 Gbps):
>
> --
> Samples: 79K of event 'cycles', Event count (approx.): 73661070737
> Children Self Command Shared Object Symbol
> - 91.00% 0.23% passt.avx2 [kernel.kallsyms] [k] entry_SYSCALL_64_after_hwframe ◆
> 90.78% entry_SYSCALL_64_after_hwframe ▒
> - do_syscall_64 ▒
> - 78.75% __sys_sendmsg ▒
> - 78.58% ___sys_sendmsg ▒
> - 78.06% ____sys_sendmsg ▒
> - sock_sendmsg ▒
> - 77.58% tcp_sendmsg ▒
> - 68.63% tcp_sendmsg_locked ▒
> - 26.24% sk_page_frag_refill ▒
> - skb_page_frag_refill ▒
> - 25.87% __alloc_pages ▒
> - 25.61% get_page_from_freelist ▒
> 24.51% clear_page_rep ▒
> - 23.08% _copy_from_iter ▒
> 22.88% copy_user_generic_string ▒
> - 8.77% tcp_write_xmit ▒
> - 8.19% __tcp_transmit_skb ▒
> - 7.86% __ip_queue_xmit ▒
> - 7.13% ip_finish_output2 ▒
> - 6.65% __local_bh_enable_ip ▒
> - 6.60% do_softirq.part.0 ▒
> - 6.51% __softirqentry_text_start ▒
> - 6.40% net_rx_action ▒
> - 5.43% __napi_poll ▒
> + process_backlog ▒
> 0.50% napi_consume_skb ▒
> + 5.39% __tcp_push_pending_frames ▒
> + 2.03% tcp_stream_alloc_skb ▒
> + 1.48% tcp_wmem_schedule ▒
> + 8.58% release_sock ▒
> - 4.57% ksys_write ▒
> - 4.41% vfs_write ▒
> - 3.96% eventfd_write ▒
> - 3.46% __wake_up_common ▒
> - irqfd_wakeup ▒
> - 3.15% kvm_arch_set_irq_inatomic ▒
> - 3.11% kvm_irq_delivery_to_apic_fast ▒
> - 2.01% __apic_accept_irq ▒
> 0.93% svm_complete_interrupt_delivery ▒
> + 3.91% __x64_sys_epoll_wait ▒
> + 1.20% __x64_sys_getsockopt ▒
> + 0.78% syscall_trace_enter.constprop.0 ▒
> 0.71% syscall_exit_to_user_mode ▒
> + 0.61% ksys_read ▒
> --
>
> ...there are no users of more than 1% cycles in passt itself. The bulk of
> it is sendmsg() as expected, one notable thing is that the kernel spends
> an awful amount of cycles zeroing pages so that we can fill them. I looked
> into that "issue" a long time ago,
>
> https://github.com/netoptimizer/prototype-kernel/pull/39/commits/2c8223c30d7f280a9e456d8e690adb0869ed8c5c
>
> ...maybe I can try out a kernel with a version of that as
> clear_page_rep() and see what happens.
...so I tried, it looks like this, but it doesn't boot for some reason:
--
diff --git a/arch/x86/include/asm/page_64.h b/arch/x86/include/asm/page_64.h
index f3d257c45225..4079012ce765 100644
--- a/arch/x86/include/asm/page_64.h
+++ b/arch/x86/include/asm/page_64.h
@@ -44,6 +44,17 @@ void clear_page_orig(void *page);
void clear_page_rep(void *page);
void clear_page_erms(void *page);
+#define MEMSET_AVX2_ZERO(reg) \
+ asm volatile("vpxor %ymm" #reg ", %ymm" #reg ", %ymm" #reg)
+#define MEMSET_AVX2_STORE(loc, reg) \
+ asm volatile("vmovdqa %%ymm" #reg ", %0" : "=m" (loc))
+
+#define YMM_BYTES (256 / 8)
+#define BYTES_TO_YMM(x) ((x) / YMM_BYTES)
+extern void kernel_fpu_begin_mask(unsigned int kfpu_mask);
+extern void kernel_fpu_end(void);
+extern bool irq_fpu_usable(void);
+
static inline void clear_page(void *page)
{
/*
@@ -51,6 +62,18 @@ static inline void clear_page(void *page)
* below clobbers @page, so we perform unpoisoning before it.
*/
kmsan_unpoison_memory(page, PAGE_SIZE);
+
+ if (irq_fpu_usable()) {
+ int i;
+
+ kernel_fpu_begin();
+ MEMSET_AVX2_ZERO(0);
+ for (i = 0; i < BYTES_TO_YMM(PAGE_SIZE); i++)
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * i], 0);
+ kernel_fpu_end();
+ return;
+ }
+
alternative_call_2(clear_page_orig,
clear_page_rep, X86_FEATURE_REP_GOOD,
clear_page_erms, X86_FEATURE_ERMS,
--
...I'm not sure if that's something we can do at early boot, so perhaps
I should add something specific in skb_page_frag_refill() instead. But
that's for another day/week/month...
--
@@ -44,6 +44,17 @@ void clear_page_orig(void *page);
void clear_page_rep(void *page);
void clear_page_erms(void *page);
+#define MEMSET_AVX2_ZERO(reg) \
+ asm volatile("vpxor %ymm" #reg ", %ymm" #reg ", %ymm" #reg)
+#define MEMSET_AVX2_STORE(loc, reg) \
+ asm volatile("vmovdqa %%ymm" #reg ", %0" : "=m" (loc))
+
+#define YMM_BYTES (256 / 8)
+#define BYTES_TO_YMM(x) ((x) / YMM_BYTES)
+extern void kernel_fpu_begin_mask(unsigned int kfpu_mask);
+extern void kernel_fpu_end(void);
+extern bool irq_fpu_usable(void);
+
static inline void clear_page(void *page)
{
/*
@@ -51,6 +62,18 @@ static inline void clear_page(void *page)
* below clobbers @page, so we perform unpoisoning before it.
*/
kmsan_unpoison_memory(page, PAGE_SIZE);
+
+ if (irq_fpu_usable()) {
+ int i;
+
+ kernel_fpu_begin();
+ MEMSET_AVX2_ZERO(0);
+ for (i = 0; i < BYTES_TO_YMM(PAGE_SIZE); i++)
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * i], 0);
+ kernel_fpu_end();
+ return;
+ }
+
alternative_call_2(clear_page_orig,
clear_page_rep, X86_FEATURE_REP_GOOD,
clear_page_erms, X86_FEATURE_ERMS,
--
...I'm not sure if that's something we can do at early boot, so perhaps
I should add something specific in skb_page_frag_refill() instead. But
that's for another day/week/month...
--
Stefano
^ permalink raw reply related [flat|nested] 18+ messages in thread
* Re: [PATCH v7 0/8] Add vhost-user support to passt. (part 3)
2024-10-11 18:07 ` Stefano Brivio
@ 2024-10-17 0:10 ` Stefano Brivio
0 siblings, 0 replies; 18+ messages in thread
From: Stefano Brivio @ 2024-10-17 0:10 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
[-- Attachment #1: Type: text/plain, Size: 48604 bytes --]
On Fri, 11 Oct 2024 20:07:30 +0200
Stefano Brivio <sbrivio@redhat.com> wrote:
> [...]
>
> > ...maybe I can try out a kernel with a version of that as
> > clear_page_rep() and see what happens.
>
> ...so I tried, it looks like this, but it doesn't boot for some reason:
I played with this a bit more. If I select the AVX2-based page clearing
with:
if (system_state >= SYSTEM_RUNNING && irq_fpu_usable()) {
instead of just irq_fpu_usable(), the kernel boots, and everything
works (also after init).
I tested this in a VM where I can't really get a baseline throughput
that's comparable to the host: iperf3 to iperf3 via loopback gives me
about 50 Gbps (instead of 70 as I get on the host), and the same iperf3
vhost-user test with outbound traffic from the nested, L2 guest yields
about 20 Gbps (instead of 25).
1. The VMOVDQA version I was originally trying looks like this:
Samples: 39K of event 'cycles:P', Event count (approx.): 34909065261
Children Self Command Shared Object Symbol
- 94.32% 0.87% passt.avx2 [kernel.kallsyms] [k] entry_SYSCALL_64 ◆
- 93.45% entry_SYSCALL_64 ▒
- 93.29% do_syscall_64 ▒
- 79.66% __sys_sendmsg ▒
- 79.52% ___sys_sendmsg ▒
- 78.88% ____sys_sendmsg ▒
- 78.46% tcp_sendmsg ▒
- 66.75% tcp_sendmsg_locked ▒
- 25.81% sk_page_frag_refill ▒
- 25.73% skb_page_frag_refill ▒
- 25.34% alloc_pages_mpol_noprof ▒
- 25.17% __alloc_pages_noprof ▒
- 24.91% get_page_from_freelist ▒
- 23.38% kernel_init_pages ▒
0.88% kernel_fpu_begin_mask ▒
- 15.37% tcp_write_xmit ▒
- 14.14% __tcp_transmit_skb ▒
- 13.31% __ip_queue_xmit ▒
- 11.06% ip_finish_output2 ▒
- 10.89% __dev_queue_xmit ▒
- 10.00% __local_bh_enable_ip ▒
- do_softirq.part.0 ▒
- handle_softirqs ▒
- 9.86% net_rx_action ▒
- 7.95% __napi_poll ▒
+ process_backlog ▒
+ 1.17% napi_consume_skb ▒
+ 0.61% dev_hard_start_xmit ▒
- 1.56% ip_local_out ▒
- __ip_local_out ▒
- 1.29% nf_hook_slow ▒
1.00% nf_conntrack_in ▒
+ 14.60% _copy_from_iter ▒
+ 3.97% __tcp_push_pending_frames ▒
+ 2.42% tcp_stream_alloc_skb ▒
+ 2.08% tcp_wmem_schedule ▒
0.64% __check_object_size ▒
+ 11.08% release_sock ▒
+ 4.48% ksys_write ▒
+ 3.57% __x64_sys_epoll_wait ▒
+ 2.26% __x64_sys_getsockopt ▒
1.09% syscall_exit_to_user_mode ▒
+ 0.90% ksys_read ▒
0.64% syscall_trace_enter ▒
...that's 24.91% clock cycles spent on get_page_from_freelist() instead of
25.61% I was getting with the original clear_page() implementation. Checking
the annotated output, it doesn't look very... superscalar:
Samples: 39K of event 'cycles:P', 4000 Hz, Event count (approx.): 34909065261
Percent│ { ▒
│ return page_to_virt(page); ▒
│32: mov %r12,%rbx ▒
│ sub vmemmap_base,%rbx ▒
0.32 │ sar $0x6,%rbx ▒
0.02 │ shl $0xc,%rbx ▒
0.02 │ add page_offset_base,%rbx ▒
│ clear_page(): ▒
│ if (system_state >= SYSTEM_RUNNING && irq_fpu_usable()) { ▒
0.05 │ cmpl $0x2,system_state ▒
0.47 │ jbe 21 ▒
0.01 │ call irq_fpu_usable ▒
0.20 │ test %al,%al ▒
0.56 │ je 21 ▒
│ kernel_fpu_begin_mask(0); ▒
0.07 │ xor %edi,%edi ▒
│ call kernel_fpu_begin_mask ▒
│ MEMSET_AVX2_ZERO(0); ▒
0.06 │ vpxor %ymm0,%ymm0,%ymm0 ▒
│ for (i = 0; i < BYTES_TO_YMM(PAGE_SIZE); i++) ▒
0.58 │ lea 0x1000(%rbx),%rax ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * i], 0); ▒
4.96 │6f: vmovdqa %ymm0,(%rbx) ▒
71.38 │ vmovdqa %ymm0,0x20(%rbx) ▒
│ for (i = 0; i < BYTES_TO_YMM(PAGE_SIZE); i++) ▒
2.81 │ add $0x40,%rbx ▒
0.06 │ cmp %rbx,%rax ▒
17.22 │ jne 6f ▒
│ kernel_fpu_end(); ▒
│ call kernel_fpu_end ▒
│ kernel_init_pages(): ▒
0.44 │ add $0x40,%r12 ▒
0.55 │ cmp %r12,%rbp ▒
0.07 │ jne 32 ▒
│ clear_highpage_kasan_tagged(page + i); ▒
│ kasan_enable_current(); ▒
│ } ◆
│8f: pop %rbx ▒
0.11 │ pop %rbp ▒
│ pop %r12 ▒
0.01 │ jmp __x86_return_thunk ▒
│98: jmp __x86_return_thunk ▒
2. Let's try to unroll it:
Samples: 39K of event 'cycles:P', Event count (approx.): 33598124504
Children Self Command Shared Object Symbol
+ 92.49% 0.33% passt.avx2 [kernel.kallsyms] [k] entry_SYSCALL_64_a◆
- 92.01% 0.47% passt.avx2 [kernel.kallsyms] [k] do_syscall_64 ▒
- 91.54% do_syscall_64 ▒
- 75.04% __sys_sendmsg ▒
- 74.85% ___sys_sendmsg ▒
- 74.26% ____sys_sendmsg ▒
- 73.68% tcp_sendmsg ▒
- 62.69% tcp_sendmsg_locked ▒
- 22.26% sk_page_frag_refill ▒
- 22.14% skb_page_frag_refill ▒
- 21.74% alloc_pages_mpol_noprof ▒
- 21.52% __alloc_pages_noprof ▒
- 21.25% get_page_from_freelist ▒
- 20.04% prep_new_page ▒
- 19.57% clear_page ▒
0.55% kernel_fpu_begin_mask ▒
+ 15.04% tcp_write_xmit ▒
+ 13.77% _copy_from_iter ▒
+ 5.12% __tcp_push_pending_frames ▒
+ 2.05% tcp_wmem_schedule ▒
+ 1.86% tcp_stream_alloc_skb ▒
0.73% __check_object_size ▒
+ 10.15% release_sock ▒
+ 0.62% lock_sock_nested ▒
+ 5.63% ksys_write ▒
+ 4.65% __x64_sys_epoll_wait ▒
+ 2.61% __x64_sys_getsockopt ▒
1.21% syscall_exit_to_user_mode ▒
+ 1.16% ksys_read ▒
+ 0.84% syscall_trace_enter ▒
annotated:
Samples: 39K of event 'cycles:P', 4000 Hz, Event count (approx.): 33598124504
clear_page /proc/kcore [Percent: local period]
Percent│
│ ffffffffb5198480 <load0>:
0.06 │ push %rbx
0.27 │ cmpl $0x2,0x1ab243c(%rip)
0.07 │ mov %rdi,%rbx
│ ja 1b
│ d: mov %rbx,%rdi
│ call clear_page_rep
│ pop %rbx
│ jmp srso_return_thunk
0.03 │ 1b: call irq_fpu_usable
0.14 │ test %al,%al
0.64 │ je d
0.04 │ xor %edi,%edi
│ call kernel_fpu_begin_mask
0.05 │ vpxor %ymm0,%ymm0,%ymm0
0.80 │ vmovdqa %ymm0,(%rbx)
1.12 │ vmovdqa %ymm0,0x20(%rbx)
0.06 │ vmovdqa %ymm0,0x40(%rbx)
1.39 │ vmovdqa %ymm0,0x60(%rbx)
0.24 │ vmovdqa %ymm0,0x80(%rbx)
0.58 │ vmovdqa %ymm0,0xa0(%rbx)
0.21 │ vmovdqa %ymm0,0xc0(%rbx)
0.77 │ vmovdqa %ymm0,0xe0(%rbx)
0.38 │ vmovdqa %ymm0,0x100(%rbx)
7.60 │ vmovdqa %ymm0,0x120(%rbx)
0.26 │ vmovdqa %ymm0,0x140(%rbx)
1.38 │ vmovdqa %ymm0,0x160(%rbx)
0.42 │ vmovdqa %ymm0,0x180(%rbx)
1.25 │ vmovdqa %ymm0,0x1a0(%rbx)
0.26 │ vmovdqa %ymm0,0x1c0(%rbx)
0.73 │ vmovdqa %ymm0,0x1e0(%rbx)
0.33 │ vmovdqa %ymm0,0x200(%rbx)
1.72 │ vmovdqa %ymm0,0x220(%rbx)
0.16 │ vmovdqa %ymm0,0x240(%rbx)
0.61 │ vmovdqa %ymm0,0x260(%rbx)
0.19 │ vmovdqa %ymm0,0x280(%rbx)
0.68 │ vmovdqa %ymm0,0x2a0(%rbx)
0.22 │ vmovdqa %ymm0,0x2c0(%rbx)
0.66 │ vmovdqa %ymm0,0x2e0(%rbx)
0.50 │ vmovdqa %ymm0,0x300(%rbx)
0.67 │ vmovdqa %ymm0,0x320(%rbx)
0.29 │ vmovdqa %ymm0,0x340(%rbx)
0.31 │ vmovdqa %ymm0,0x360(%rbx)
0.14 │ vmovdqa %ymm0,0x380(%rbx)
0.55 │ vmovdqa %ymm0,0x3a0(%rbx)
0.35 │ vmovdqa %ymm0,0x3c0(%rbx)
0.82 │ vmovdqa %ymm0,0x3e0(%rbx)
0.25 │ vmovdqa %ymm0,0x400(%rbx)
0.49 │ vmovdqa %ymm0,0x420(%rbx) ▒
0.18 │ vmovdqa %ymm0,0x440(%rbx) ▒
1.05 │ vmovdqa %ymm0,0x460(%rbx) ▒
0.08 │ vmovdqa %ymm0,0x480(%rbx) ▒
2.22 │ vmovdqa %ymm0,0x4a0(%rbx) ▒
0.20 │ vmovdqa %ymm0,0x4c0(%rbx) ▒
2.33 │ vmovdqa %ymm0,0x4e0(%rbx) ▒
0.03 │ vmovdqa %ymm0,0x500(%rbx) ▒
2.87 │ vmovdqa %ymm0,0x520(%rbx) ▒
0.08 │ vmovdqa %ymm0,0x540(%rbx) ▒
1.60 │ vmovdqa %ymm0,0x560(%rbx) ▒
0.01 │ vmovdqa %ymm0,0x580(%rbx) ▒
7.03 │ vmovdqa %ymm0,0x5a0(%rbx) ▒
0.42 │ vmovdqa %ymm0,0x5c0(%rbx) ▒
2.74 │ vmovdqa %ymm0,0x5e0(%rbx) ▒
0.69 │ vmovdqa %ymm0,0x600(%rbx) ▒
2.34 │ vmovdqa %ymm0,0x620(%rbx) ▒
0.37 │ vmovdqa %ymm0,0x640(%rbx) ▒
1.21 │ vmovdqa %ymm0,0x660(%rbx) ▒
0.22 │ vmovdqa %ymm0,0x680(%rbx) ▒
1.16 │ vmovdqa %ymm0,0x6a0(%rbx) ▒
0.29 │ vmovdqa %ymm0,0x6c0(%rbx) ▒
0.98 │ vmovdqa %ymm0,0x6e0(%rbx) ▒
0.19 │ vmovdqa %ymm0,0x700(%rbx) ▒
0.81 │ vmovdqa %ymm0,0x720(%rbx) ▒
0.47 │ vmovdqa %ymm0,0x740(%rbx) ▒
0.69 │ vmovdqa %ymm0,0x760(%rbx) ▒
0.23 │ vmovdqa %ymm0,0x780(%rbx) ▒
0.68 │ vmovdqa %ymm0,0x7a0(%rbx) ▒
0.30 │ vmovdqa %ymm0,0x7c0(%rbx) ▒
0.68 │ vmovdqa %ymm0,0x7e0(%rbx) ▒
0.25 │ vmovdqa %ymm0,0x800(%rbx) ◆
0.58 │ vmovdqa %ymm0,0x820(%rbx) ▒
0.19 │ vmovdqa %ymm0,0x840(%rbx) ▒
0.83 │ vmovdqa %ymm0,0x860(%rbx) ▒
0.27 │ vmovdqa %ymm0,0x880(%rbx) ▒
1.01 │ vmovdqa %ymm0,0x8a0(%rbx) ▒
0.16 │ vmovdqa %ymm0,0x8c0(%rbx) ▒
0.89 │ vmovdqa %ymm0,0x8e0(%rbx) ▒
0.24 │ vmovdqa %ymm0,0x900(%rbx) ▒
0.98 │ vmovdqa %ymm0,0x920(%rbx) ▒
0.28 │ vmovdqa %ymm0,0x940(%rbx) ▒
0.86 │ vmovdqa %ymm0,0x960(%rbx) ▒
0.23 │ vmovdqa %ymm0,0x980(%rbx) ▒
1.19 │ vmovdqa %ymm0,0x9a0(%rbx) ▒
0.28 │ vmovdqa %ymm0,0x9c0(%rbx) ▒
1.04 │ vmovdqa %ymm0,0x9e0(%rbx) ▒
0.33 │ vmovdqa %ymm0,0xa00(%rbx) ▒
0.90 │ vmovdqa %ymm0,0xa20(%rbx) ▒
0.35 │ vmovdqa %ymm0,0xa40(%rbx) ▒
0.87 │ vmovdqa %ymm0,0xa60(%rbx) ▒
0.25 │ vmovdqa %ymm0,0xa80(%rbx) ▒
0.89 │ vmovdqa %ymm0,0xaa0(%rbx) ▒
0.28 │ vmovdqa %ymm0,0xac0(%rbx) ▒
0.92 │ vmovdqa %ymm0,0xae0(%rbx) ▒
0.23 │ vmovdqa %ymm0,0xb00(%rbx) ▒
1.39 │ vmovdqa %ymm0,0xb20(%rbx) ▒
0.29 │ vmovdqa %ymm0,0xb40(%rbx) ▒
1.15 │ vmovdqa %ymm0,0xb60(%rbx) ▒
0.26 │ vmovdqa %ymm0,0xb80(%rbx) ▒
1.33 │ vmovdqa %ymm0,0xba0(%rbx) ▒
0.29 │ vmovdqa %ymm0,0xbc0(%rbx) ▒
1.05 │ vmovdqa %ymm0,0xbe0(%rbx) ▒
0.25 │ vmovdqa %ymm0,0xc00(%rbx) ▒
0.89 │ vmovdqa %ymm0,0xc20(%rbx) ▒
0.34 │ vmovdqa %ymm0,0xc40(%rbx) ▒
0.78 │ vmovdqa %ymm0,0xc60(%rbx) ▒
0.40 │ vmovdqa %ymm0,0xc80(%rbx) ▒
0.99 │ vmovdqa %ymm0,0xca0(%rbx) ▒
0.44 │ vmovdqa %ymm0,0xcc0(%rbx) ▒
1.06 │ vmovdqa %ymm0,0xce0(%rbx) ▒
0.35 │ vmovdqa %ymm0,0xd00(%rbx) ▒
0.85 │ vmovdqa %ymm0,0xd20(%rbx) ▒
0.46 │ vmovdqa %ymm0,0xd40(%rbx) ▒
0.88 │ vmovdqa %ymm0,0xd60(%rbx) ▒
0.38 │ vmovdqa %ymm0,0xd80(%rbx) ▒
0.82 │ vmovdqa %ymm0,0xda0(%rbx) ▒
0.40 │ vmovdqa %ymm0,0xdc0(%rbx) ▒
0.98 │ vmovdqa %ymm0,0xde0(%rbx) ▒
0.27 │ vmovdqa %ymm0,0xe00(%rbx) ▒
1.10 │ vmovdqa %ymm0,0xe20(%rbx) ▒
0.25 │ vmovdqa %ymm0,0xe40(%rbx) ▒
0.89 │ vmovdqa %ymm0,0xe60(%rbx) ▒
0.32 │ vmovdqa %ymm0,0xe80(%rbx) ▒
0.87 │ vmovdqa %ymm0,0xea0(%rbx) ▒
0.22 │ vmovdqa %ymm0,0xec0(%rbx) ▒
0.94 │ vmovdqa %ymm0,0xee0(%rbx) ▒
0.27 │ vmovdqa %ymm0,0xf00(%rbx) ▒
0.90 │ vmovdqa %ymm0,0xf20(%rbx) ▒
0.28 │ vmovdqa %ymm0,0xf40(%rbx) ▒
0.79 │ vmovdqa %ymm0,0xf60(%rbx) ▒
0.31 │ vmovdqa %ymm0,0xf80(%rbx) ▒
1.11 │ vmovdqa %ymm0,0xfa0(%rbx) ▒
0.25 │ vmovdqa %ymm0,0xfc0(%rbx) ▒
0.99 │ vmovdqa %ymm0,0xfe0(%rbx) ▒
0.10 │ pop %rbx ▒
│ jmp 0xffffffffb4e4b050 ▒
...that looks like progress: we now spend 21.25% of the clock cyles on
get_page_from_freelist() (non-AVX: 25.61%). But still, there seem to be
(somewhat unexpected) stalls. For example, after 8 VMOVDQA instructions:
7.60 │ vmovdqa %ymm0,0x120(%rbx)
we have one where we spend/wait a long time, and there are more later.
3. ...what if we use a non-temporal hint, that is, if we clear the page
without making it cache hot ("stream" instead of "store")?
That's vmovntdq m256, ymm ("nt" meaning non-temporal). It's not vmovntdqa
(where "a" stands for "aligned"), as one could expect from the vmovdqa above,
because there's no unaligned version.
The only vmovntdq_a_ instruction is vmovntdqa ymm, m256 (memory to register,
"stream load"), because there's an unaligned equivalent in that case.
Anyway, perf output:
Samples: 39K of event 'cycles:P', Event count (approx.): 33890710610
Children Self Command Shared Object Symbol
- 92.62% 0.88% passt.avx2 [kernel.vmlinux] [k] entry_SYSCALL◆
- 91.74% entry_SYSCALL_64 ▒
- 91.60% do_syscall_64 ▒
- 75.05% __sys_sendmsg ▒
- 74.88% ___sys_sendmsg ▒
- 74.22% ____sys_sendmsg ▒
- 73.65% tcp_sendmsg ▒
- 61.71% tcp_sendmsg_locked ▒
- 24.82% _copy_from_iter ▒
24.40% rep_movs_alternative ▒
- 14.69% sk_page_frag_refill ▒
- 14.57% skb_page_frag_refill ▒
- 14.19% alloc_pages_mpol_noprof ▒
- 14.03% __alloc_pages_noprof ▒
- 13.77% get_page_from_freelist ▒
- 12.56% prep_new_page ▒
- 12.19% clear_page ▒
0.68% kernel_fpu_begin_mask ▒
- 11.12% tcp_write_xmit ▒
- 10.17% __tcp_transmit_skb ▒
- 9.62% __ip_queue_xmit ▒
- 8.08% ip_finish_output2 ▒
- 7.96% __dev_queue_xmit ▒
- 7.26% __local_bh_enable_ip ▒
- 7.24% do_softirq.part.0 ▒
- handle_softirqs ▒
- net_rx_action ▒
+ 5.80% __napi_poll ▒
+ 0.87% napi_consume_skb ▒
- 1.06% ip_local_out ▒
- 1.05% __ip_local_out ▒
- 0.90% nf_hook_slow ▒
0.66% nf_conntrack_in ▒
+ 4.22% __tcp_push_pending_frames ▒
+ 2.51% tcp_wmem_schedule ▒
+ 1.99% tcp_stream_alloc_skb ▒
0.59% __check_object_size ▒
+ 11.21% release_sock ▒
0.52% lock_sock_nested ▒
+ 5.32% ksys_write ▒
+ 4.75% __x64_sys_epoll_wait ▒
+ 2.45% __x64_sys_getsockopt ▒
1.29% syscall_exit_to_user_mode ▒
+ 1.25% ksys_read ▒
+ 0.70% syscall_trace_enter ▒
...finally we cut down significantly on cycles spent to clear pages, with
get_page_from_freelist() taking 13.77% of the cycles instead of 25.61%.
That's about half the overhead.
This makes _copy_from_iter() the biggest consumer of cycles under
tcp_sendmsg_locked(), which is what I expected. Does this mean that we
increase the overhead there because we're increasing the amount of cache
misses there, or are we simply more efficient? I'm not sure yet.
For completeness, annotated version of clear_page():
│ if (system_state >= SYSTEM_RUNNING && irq_fpu_usable()) {
0.09 │ 1b: call irq_fpu_usable
0.11 │ test %al,%al
0.51 │ je d
│ kernel_fpu_begin_mask(0);
0.16 │ xor %edi,%edi
│ call kernel_fpu_begin_mask
│ MEMSET_AVX2_ZERO(0);
0.05 │ vpxor %ymm0,%ymm0,%ymm0
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x00], 0);
0.79 │ vmovntdq %ymm0,(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x01], 0);
2.46 │ vmovntdq %ymm0,0x20(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x02], 0);
0.07 │ vmovntdq %ymm0,0x40(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x03], 0);
1.35 │ vmovntdq %ymm0,0x60(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x04], 0);
0.18 │ vmovntdq %ymm0,0x80(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x05], 0);
1.40 │ vmovntdq %ymm0,0xa0(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x06], 0);
0.11 │ vmovntdq %ymm0,0xc0(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x07], 0);
0.81 │ vmovntdq %ymm0,0xe0(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x08], 0);
0.07 │ vmovntdq %ymm0,0x100(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x09], 0);
1.25 │ vmovntdq %ymm0,0x120(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0a], 0);
0.08 │ vmovntdq %ymm0,0x140(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0b], 0);
1.36 │ vmovntdq %ymm0,0x160(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0c], 0);
0.11 │ vmovntdq %ymm0,0x180(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0d], 0);
1.73 │ vmovntdq %ymm0,0x1a0(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0e], 0);
0.09 │ vmovntdq %ymm0,0x1c0(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0f], 0);
0.97 │ vmovntdq %ymm0,0x1e0(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x10], 0);
0.07 │ vmovntdq %ymm0,0x200(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x11], 0);
1.25 │ vmovntdq %ymm0,0x220(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x12], 0);
0.14 │ vmovntdq %ymm0,0x240(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x13], 0);
0.79 │ vmovntdq %ymm0,0x260(%rbx)
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x14], 0); ▒
0.09 │ vmovntdq %ymm0,0x280(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x15], 0); ▒
1.19 │ vmovntdq %ymm0,0x2a0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x16], 0); ▒
0.07 │ vmovntdq %ymm0,0x2c0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x17], 0); ▒
1.45 │ vmovntdq %ymm0,0x2e0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x18], 0); ▒
│ vmovntdq %ymm0,0x300(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x19], 0); ▒
1.45 │ vmovntdq %ymm0,0x320(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1a], 0); ▒
0.05 │ vmovntdq %ymm0,0x340(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1b], 0); ▒
1.49 │ vmovntdq %ymm0,0x360(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1c], 0); ▒
0.14 │ vmovntdq %ymm0,0x380(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1d], 0); ▒
1.34 │ vmovntdq %ymm0,0x3a0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1e], 0); ▒
0.09 │ vmovntdq %ymm0,0x3c0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1f], 0); ▒
1.69 │ vmovntdq %ymm0,0x3e0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x20], 0); ▒
0.16 │ vmovntdq %ymm0,0x400(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x21], 0); ▒
1.15 │ vmovntdq %ymm0,0x420(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x22], 0); ▒
0.13 │ vmovntdq %ymm0,0x440(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x23], 0); ▒
1.36 │ vmovntdq %ymm0,0x460(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x24], 0); ▒
0.07 │ vmovntdq %ymm0,0x480(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x25], 0); ▒
1.01 │ vmovntdq %ymm0,0x4a0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x26], 0); ▒
0.09 │ vmovntdq %ymm0,0x4c0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x27], 0); ◆
1.53 │ vmovntdq %ymm0,0x4e0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x28], 0); ▒
0.12 │ vmovntdq %ymm0,0x500(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x29], 0); ▒
1.45 │ vmovntdq %ymm0,0x520(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2a], 0); ▒
0.13 │ vmovntdq %ymm0,0x540(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2b], 0); ▒
0.97 │ vmovntdq %ymm0,0x560(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2c], 0); ▒
0.12 │ vmovntdq %ymm0,0x580(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2d], 0); ▒
1.21 │ vmovntdq %ymm0,0x5a0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2e], 0); ▒
0.15 │ vmovntdq %ymm0,0x5c0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2f], 0); ▒
1.42 │ vmovntdq %ymm0,0x5e0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x30], 0); ▒
0.19 │ vmovntdq %ymm0,0x600(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x31], 0); ▒
1.12 │ vmovntdq %ymm0,0x620(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x32], 0); ▒
0.04 │ vmovntdq %ymm0,0x640(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x33], 0); ▒
1.59 │ vmovntdq %ymm0,0x660(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x34], 0); ▒
0.07 │ vmovntdq %ymm0,0x680(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x35], 0); ▒
1.65 │ vmovntdq %ymm0,0x6a0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x36], 0); ▒
0.14 │ vmovntdq %ymm0,0x6c0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x37], 0); ▒
1.00 │ vmovntdq %ymm0,0x6e0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x38], 0); ▒
0.14 │ vmovntdq %ymm0,0x700(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x39], 0); ▒
1.31 │ vmovntdq %ymm0,0x720(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3a], 0); ▒
0.10 │ vmovntdq %ymm0,0x740(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3b], 0); ▒
1.21 │ vmovntdq %ymm0,0x760(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3c], 0); ▒
0.07 │ vmovntdq %ymm0,0x780(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3d], 0); ▒
1.27 │ vmovntdq %ymm0,0x7a0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3e], 0); ▒
0.09 │ vmovntdq %ymm0,0x7c0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3f], 0); ▒
1.28 │ vmovntdq %ymm0,0x7e0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x40], 0); ▒
0.11 │ vmovntdq %ymm0,0x800(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x41], 0); ▒
1.32 │ vmovntdq %ymm0,0x820(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x42], 0); ▒
0.09 │ vmovntdq %ymm0,0x840(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x43], 0); ▒
1.43 │ vmovntdq %ymm0,0x860(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x44], 0); ▒
0.11 │ vmovntdq %ymm0,0x880(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x45], 0); ▒
1.21 │ vmovntdq %ymm0,0x8a0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x46], 0); ▒
0.11 │ vmovntdq %ymm0,0x8c0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x47], 0); ▒
1.09 │ vmovntdq %ymm0,0x8e0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x48], 0); ▒
0.07 │ vmovntdq %ymm0,0x900(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x49], 0); ▒
1.26 │ vmovntdq %ymm0,0x920(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4a], 0); ▒
0.16 │ vmovntdq %ymm0,0x940(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4b], 0); ▒
1.58 │ vmovntdq %ymm0,0x960(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4c], 0); ▒
0.05 │ vmovntdq %ymm0,0x980(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4d], 0); ▒
1.54 │ vmovntdq %ymm0,0x9a0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4e], 0); ▒
0.07 │ vmovntdq %ymm0,0x9c0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4f], 0); ▒
1.66 │ vmovntdq %ymm0,0x9e0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x50], 0); ▒
0.16 │ vmovntdq %ymm0,0xa00(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x51], 0); ▒
1.31 │ vmovntdq %ymm0,0xa20(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x52], 0); ▒
0.20 │ vmovntdq %ymm0,0xa40(%rbx) ◆
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x53], 0); ▒
1.44 │ vmovntdq %ymm0,0xa60(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x54], 0); ▒
0.05 │ vmovntdq %ymm0,0xa80(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x55], 0); ▒
1.52 │ vmovntdq %ymm0,0xaa0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x56], 0); ▒
0.21 │ vmovntdq %ymm0,0xac0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x57], 0); ▒
1.09 │ vmovntdq %ymm0,0xae0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x58], 0); ▒
0.22 │ vmovntdq %ymm0,0xb00(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x59], 0); ▒
1.58 │ vmovntdq %ymm0,0xb20(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5a], 0); ▒
0.12 │ vmovntdq %ymm0,0xb40(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5b], 0); ▒
1.46 │ vmovntdq %ymm0,0xb60(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5c], 0); ▒
0.04 │ vmovntdq %ymm0,0xb80(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5d], 0); ▒
1.62 │ vmovntdq %ymm0,0xba0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5e], 0); ▒
0.07 │ vmovntdq %ymm0,0xbc0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5f], 0); ▒
1.71 │ vmovntdq %ymm0,0xbe0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x60], 0); ▒
0.19 │ vmovntdq %ymm0,0xc00(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x61], 0); ▒
1.89 │ vmovntdq %ymm0,0xc20(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x62], 0); ▒
0.11 │ vmovntdq %ymm0,0xc40(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x63], 0); ▒
1.98 │ vmovntdq %ymm0,0xc60(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x64], 0); ▒
0.16 │ vmovntdq %ymm0,0xc80(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x65], 0); ▒
1.58 │ vmovntdq %ymm0,0xca0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x66], 0); ▒
0.13 │ vmovntdq %ymm0,0xcc0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x67], 0); ▒
1.16 │ vmovntdq %ymm0,0xce0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x68], 0); ▒
0.09 │ vmovntdq %ymm0,0xd00(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x69], 0); ▒
1.67 │ vmovntdq %ymm0,0xd20(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6a], 0); ▒
0.11 │ vmovntdq %ymm0,0xd40(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6b], 0); ▒
1.82 │ vmovntdq %ymm0,0xd60(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6c], 0); ▒
0.07 │ vmovntdq %ymm0,0xd80(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6d], 0); ▒
1.57 │ vmovntdq %ymm0,0xda0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6e], 0); ▒
0.02 │ vmovntdq %ymm0,0xdc0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6f], 0); ▒
1.27 │ vmovntdq %ymm0,0xde0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x70], 0); ▒
│ vmovntdq %ymm0,0xe00(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x71], 0); ▒
1.48 │ vmovntdq %ymm0,0xe20(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x72], 0); ▒
0.11 │ vmovntdq %ymm0,0xe40(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x73], 0); ▒
1.87 │ vmovntdq %ymm0,0xe60(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x74], 0); ▒
0.16 │ vmovntdq %ymm0,0xe80(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x75], 0); ▒
1.45 │ vmovntdq %ymm0,0xea0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x76], 0); ▒
0.07 │ vmovntdq %ymm0,0xec0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x77], 0); ▒
1.65 │ vmovntdq %ymm0,0xee0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x78], 0); ▒
0.10 │ vmovntdq %ymm0,0xf00(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x79], 0); ▒
1.53 │ vmovntdq %ymm0,0xf20(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7a], 0); ▒
0.07 │ vmovntdq %ymm0,0xf40(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7b], 0); ▒
1.51 │ vmovntdq %ymm0,0xf60(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7c], 0); ▒
0.12 │ vmovntdq %ymm0,0xf80(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7d], 0); ▒
1.62 │ vmovntdq %ymm0,0xfa0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7e], 0); ▒
0.08 │ vmovntdq %ymm0,0xfc0(%rbx) ▒
│ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7f], 0); ▒
1.62 │ vmovntdq %ymm0,0xfe0(%rbx) ▒
│ } ▒
0.13 │ pop %rbx ▒
│ kernel_fpu_end(); ▒
│ jmp ffffffff8104b050 <kernel_fpu_end> ▒
...no stalls on any particular store.
Current patch attached if you want to fry^W test on your laptop.
--
Stefano
[-- Attachment #2: clear_page_avx2_stream.patch --]
[-- Type: text/x-patch, Size: 10192 bytes --]
diff --git a/arch/x86/include/asm/page_64.h b/arch/x86/include/asm/page_64.h
index f3d257c45225..d753740cb06c 100644
--- a/arch/x86/include/asm/page_64.h
+++ b/arch/x86/include/asm/page_64.h
@@ -9,6 +9,7 @@
#include <asm/alternative.h>
#include <linux/kmsan-checks.h>
+#include <linux/kernel.h>
/* duplicated to the one in bootmem.h */
extern unsigned long max_pfn;
@@ -44,6 +45,17 @@ void clear_page_orig(void *page);
void clear_page_rep(void *page);
void clear_page_erms(void *page);
+#define MEMSET_AVX2_ZERO(reg) \
+ asm volatile("vpxor %ymm" #reg ", %ymm" #reg ", %ymm" #reg)
+#define MEMSET_AVX2_STORE(loc, reg) \
+ asm volatile("vmovntdq %%ymm" #reg ", %0" : "=m" (loc))
+
+#define YMM_BYTES (256 / 8)
+#define BYTES_TO_YMM(x) ((x) / YMM_BYTES)
+extern void kernel_fpu_begin_mask(unsigned int kfpu_mask);
+extern void kernel_fpu_end(void);
+extern bool irq_fpu_usable(void);
+
static inline void clear_page(void *page)
{
/*
@@ -51,6 +63,182 @@ static inline void clear_page(void *page)
* below clobbers @page, so we perform unpoisoning before it.
*/
kmsan_unpoison_memory(page, PAGE_SIZE);
+
+ if (system_state >= SYSTEM_RUNNING && irq_fpu_usable()) {
+ kernel_fpu_begin_mask(0);
+ MEMSET_AVX2_ZERO(0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x00], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x01], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x02], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x03], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x04], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x05], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x06], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x07], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x08], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x09], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0a], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0b], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0c], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0d], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0e], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x0f], 0);
+
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x10], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x11], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x12], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x13], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x14], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x15], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x16], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x17], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x18], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x19], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1a], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1b], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1c], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1d], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1e], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x1f], 0);
+
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x20], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x21], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x22], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x23], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x24], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x25], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x26], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x27], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x28], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x29], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2a], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2b], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2c], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2d], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2e], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x2f], 0);
+
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x30], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x31], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x32], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x33], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x34], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x35], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x36], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x37], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x38], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x39], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3a], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3b], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3c], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3d], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3e], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x3f], 0);
+
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x40], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x41], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x42], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x43], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x44], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x45], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x46], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x47], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x48], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x49], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4a], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4b], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4c], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4d], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4e], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x4f], 0);
+
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x50], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x51], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x52], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x53], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x54], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x55], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x56], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x57], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x58], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x59], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5a], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5b], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5c], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5d], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5e], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x5f], 0);
+
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x60], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x61], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x62], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x63], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x64], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x65], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x66], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x67], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x68], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x69], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6a], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6b], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6c], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6d], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6e], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x6f], 0);
+
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x70], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x71], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x72], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x73], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x74], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x75], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x76], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x77], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x78], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x79], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7a], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7b], 0);
+
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7c], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7d], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7e], 0);
+ MEMSET_AVX2_STORE(((unsigned char *)page)[YMM_BYTES * 0x7f], 0);
+
+ kernel_fpu_end();
+ return;
+ }
+
alternative_call_2(clear_page_orig,
clear_page_rep, X86_FEATURE_REP_GOOD,
clear_page_erms, X86_FEATURE_ERMS,
^ permalink raw reply related [flat|nested] 18+ messages in thread