From: aerosouund <aerosound161@gmail.com>
To: passt-dev@passt.top
Cc: eperezma@redhat.com, Ammar Yasser <aerosound161@gmail.com>
Subject: [PATCH 5/7] tap, tcp, udp: Prepare the to-guest path for vhost-net
Date: Sat, 5 Sep 2026 00:28:24 +0300 [thread overview]
Message-ID: <20260904212826.41027-6-aerosound161@gmail.com> (raw)
In-Reply-To: <20260904212826.41027-1-aerosound161@gmail.com>
From: Ammar Yasser <aerosound161@gmail.com>
Frames sent through vhost-net carry a virtio-net header where pasta's
tap header would otherwise go, this necessitates the following two
changes:
- udp_tap_prepare() has been modified to take a context parameter so it
can check if vhost is set up. Only if it isn't should tap_hdr_update
be called because this means that the active union variant is vnet_len
no the virtio net header
- tap_l2_offset() a new helper, has been introduced which tells
pcap_multiple() how many bytes precede the L2 frame, which now varies
with vhost as well as with the mode
tap_send_single() calls tap_send_frames_passt() and
tap_send_frames_pasta() directly rather than going through
tap_send_frames(). Its callers - ARP, TCP resets, and DHCP, DHCPv6, NDP
and ICMP via tap_udp*_send()/tap_icmp*_send() - build their frame in a
buffer on the stack, not in the buffers registered with vhost-net. The
kernel can only read from registered memory, so these protocols can
never take the vhost path and shouldn't be tested for it on every frame.
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
Signed-off-by: Ammar Yasser <aerosound161@gmail.com>
---
tap.c | 112 ++++++++++++++++++++++++++++++++++--------------------
tcp_buf.c | 12 ++++--
udp.c | 27 +++++++++----
3 files changed, 99 insertions(+), 52 deletions(-)
diff --git a/tap.c b/tap.c
index f84cb0a..c730535 100644
--- a/tap.c
+++ b/tap.c
@@ -123,45 +123,6 @@ unsigned long tap_l2_max_len(const struct ctx *c)
return 0; /* Unreachable, for cppcheck's sake */
}
-/**
- * tap_send_single() - Send a single frame
- * @c: Execution context
- * @data: Packet buffer
- * @l2len: Total L2 packet length
- */
-void tap_send_single(const struct ctx *c, const void *data, size_t l2len)
-{
- uint8_t padded[ETH_ZLEN] = { 0 };
- struct iovec iov[2];
- size_t iovcnt = 0;
- uint32_t vnet_len;
-
- if (l2len < ETH_ZLEN) {
- memcpy(padded, data, l2len);
- data = padded;
- l2len = ETH_ZLEN;
- }
-
- vnet_len = htonl(l2len);
-
- switch (c->mode) {
- case MODE_PASST:
- iov[iovcnt] = IOV_OF_LVALUE(vnet_len);
- 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);
- break;
- case MODE_VU:
- vu_send_single(c, data, l2len);
- break;
- }
-}
-
/**
* tap_push_l2h() - Build an L2 header for an inbound packet
* @c: Execution context
@@ -497,6 +458,76 @@ static size_t tap_send_frames_passt(const struct ctx *c,
return i / bufs_per_frame;
}
+/**
+ * tap_l2_offset() - Backend specific header size preceding each L2 frame
+ * @c: Execution context
+ *
+ * Return: offset of the L2 frame within each frame's first buffer
+ */
+static size_t tap_l2_offset(const struct ctx *c)
+{
+ if (c->mode == MODE_PASST)
+ return sizeof(uint32_t); /* vnet_len */
+
+ if (c->vhost.fd != -1)
+ return VNET_HLEN;
+
+ return 0;
+}
+
+/**
+ * tap_send_single() - Send a single frame
+ * @c: Execution context
+ * @data: Packet buffer
+ * @l2len: Total L2 packet length
+ */
+void tap_send_single(const struct ctx *c, const void *data, size_t l2len)
+{
+ uint8_t padded[ETH_ZLEN] = { 0 };
+ struct iovec iov[2];
+ uint32_t vnet_len;
+ size_t iovcnt = 0;
+ size_t m = 0;
+
+ if (l2len < ETH_ZLEN) {
+ memcpy(padded, data, l2len);
+ data = padded;
+ l2len = ETH_ZLEN;
+ }
+
+ vnet_len = htonl(l2len);
+
+ switch (c->mode) {
+ case MODE_PASST:
+ /* create an iov for the length */
+ iov[iovcnt] = IOV_OF_LVALUE(vnet_len);
+ iovcnt++;
+ /* create the data iov */
+ iov[iovcnt].iov_base = (void *)data;
+ iov[iovcnt].iov_len = l2len;
+ iovcnt++;
+
+ m = tap_send_frames_passt(c, iov, iovcnt, 1);
+ break;
+ case MODE_PASTA:
+ /* don't create a length iov in the case of pasta */
+ iov[iovcnt].iov_base = (void *)data;
+ iov[iovcnt].iov_len = l2len;
+ iovcnt++;
+
+ m = tap_send_frames_pasta(c, iov, iovcnt, 1);
+ break;
+ case MODE_VU:
+ vu_send_single(c, data, l2len);
+ return;
+ }
+
+ if (!m)
+ debug("tap: failed to send a single frame");
+
+ pcap_multiple(iov, iovcnt, m, tap_l2_offset(c));
+}
+
/**
* tap_send_frames() - Send out multiple prepared frames
* @c: Execution context
@@ -537,8 +568,7 @@ size_t tap_send_frames(const struct ctx *c, const struct iovec *iov,
debug("tap: failed to send %zu frames of %zu",
nframes - m, nframes);
- pcap_multiple(iov, bufs_per_frame, m,
- c->mode == MODE_PASST ? sizeof(uint32_t) : 0);
+ pcap_multiple(iov, bufs_per_frame, m, tap_l2_offset(c));
return m;
}
diff --git a/tcp_buf.c b/tcp_buf.c
index 1f28728..9cc541a 100644
--- a/tcp_buf.c
+++ b/tcp_buf.c
@@ -21,6 +21,7 @@
#include <netinet/ip.h>
#include <netinet/tcp.h>
+#include <linux/virtio_net.h>
#include "util.h"
#include "ip.h"
@@ -42,7 +43,7 @@
/* Ethernet header for IPv4 and IPv6 frames */
static struct ethhdr tcp_eth_hdr[TCP_FRAMES_MEM];
-static struct tap_hdr tcp_payload_tap_hdr[TCP_FRAMES_MEM];
+static struct virtio_net_hdr_mrg_rxbuf tcp_payload_tap_hdr[TCP_FRAMES_MEM];
/* IP headers for IPv4 and IPv6 */
static struct iphdr tcp4_payload_ip[TCP_FRAMES_MEM];
@@ -107,7 +108,7 @@ void tcp_sock_iov_init(const struct ctx *c)
for (i = 0; i < TCP_FRAMES_MEM; i++) {
struct iovec *iov = tcp_l2_iov[i];
- iov[TCP_IOV_TAP] = tap_hdr_iov(c, &tcp_payload_tap_hdr[i]);
+ iov[TCP_IOV_TAP] = tap_hdr_iov(c, (struct tap_hdr *)&tcp_payload_tap_hdr[i]);
iov[TCP_IOV_ETH].iov_len = sizeof(struct ethhdr);
iov[TCP_IOV_PAYLOAD].iov_base = &tcp_payload[i];
iov[TCP_IOV_ETH_PAD].iov_base = eth_pad;
@@ -210,7 +211,12 @@ static void tcp_l2_buf_fill_headers(const struct ctx *c,
l2len = tcp_fill_headers(c, conn, eh, ip4h, ip6h, th, &tail,
iov_tail_size(&tail), csum_flags, seq);
- tap_hdr_update(taph, l2len);
+
+ /* With vhost-net this buffer holds a virtio-net header, which a tap
+ * one must not be written over
+ */
+ if (c->vhost.fd == -1)
+ tap_hdr_update(taph, l2len);
}
/**
diff --git a/udp.c b/udp.c
index 5e3bd86..e360ccf 100644
--- a/udp.c
+++ b/udp.c
@@ -103,6 +103,7 @@
#include <time.h>
#include <arpa/inet.h>
#include <linux/errqueue.h>
+#include <linux/virtio_net.h>
#include "checksum.h"
#include "util.h"
@@ -147,12 +148,16 @@ static struct ethhdr udp_eth_hdr[UDP_MAX_FRAMES];
* struct udp_meta_t - Pre-cooked headers for UDP packets
* @ip6h: Pre-filled IPv6 header (except for payload_len and addresses)
* @ip4h: Pre-filled IPv4 header (except for tot_len and saddr)
- * @taph: Tap backend specific header
+ * @vnet_hdr: virtio-net header, used when sending through vhost-net
+ * @taph: Tap backend specific header, used otherwise
*/
static struct udp_meta_t {
struct ipv6hdr ip6h;
struct iphdr ip4h;
- struct tap_hdr taph;
+ union {
+ struct virtio_net_hdr_mrg_rxbuf vnet_hdr;
+ struct tap_hdr taph;
+ };
}
#ifdef __AVX2__
__attribute__ ((aligned(32)))
@@ -354,13 +359,15 @@ static void udp_tap_pad(struct iovec *iov)
/**
* udp_tap_prepare() - Convert one datagram into a tap frame
+ * @c: Execution context
* @mmh: Receiving mmsghdr array
* @idx: Index of the datagram to prepare
* @tap_omac: MAC address of remote endpoint as seen from the guest
* @toside: Flowside for destination side
* @no_udp_csum: Do not set UDP checksum
*/
-static void udp_tap_prepare(const struct mmsghdr *mmh,
+static void udp_tap_prepare(const struct ctx *c,
+ const struct mmsghdr *mmh,
unsigned int idx,
const uint8_t *tap_omac,
const struct flowside *toside,
@@ -382,8 +389,10 @@ static void udp_tap_prepare(const struct mmsghdr *mmh,
udp_update_hdr6(&bm->ip6h, uh, &payload, toside,
mmh[idx].msg_len, no_udp_csum);
- l2len = MAX(l4len + sizeof(bm->ip6h) + ETH_HLEN, ETH_ZLEN);
- tap_hdr_update(&bm->taph, l2len);
+ if (c->mode == MODE_PASST) {
+ l2len = MAX(l4len + sizeof(bm->ip6h) + ETH_HLEN, ETH_ZLEN);
+ tap_hdr_update(&bm->taph, l2len);
+ }
eh->h_proto = htons_constant(ETH_P_IPV6);
(*tap_iov)[UDP_IOV_IP] = IOV_OF_LVALUE(bm->ip6h);
@@ -391,8 +400,10 @@ static void udp_tap_prepare(const struct mmsghdr *mmh,
udp_update_hdr4(&bm->ip4h, uh, &payload, toside,
mmh[idx].msg_len, no_udp_csum);
- l2len = MAX(l4len + sizeof(bm->ip4h) + ETH_HLEN, ETH_ZLEN);
- tap_hdr_update(&bm->taph, l2len);
+ if (c->mode == MODE_PASST) {
+ l2len = MAX(l4len + sizeof(bm->ip4h) + ETH_HLEN, ETH_ZLEN);
+ tap_hdr_update(&bm->taph, l2len);
+ }
eh->h_proto = htons_constant(ETH_P_IP);
(*tap_iov)[UDP_IOV_IP] = IOV_OF_LVALUE(bm->ip4h);
@@ -861,7 +872,7 @@ static void udp_buf_sock_to_tap(const struct ctx *c, int s, int n,
fwd_neigh_mac_get(c, &toside->oaddr, omac);
for (i = 0; i < n; i++)
- udp_tap_prepare(udp_mh_recv, i, omac, toside, false);
+ udp_tap_prepare(c, udp_mh_recv, i, omac, toside, false);
tap_send_frames(c, &udp_l2_iov[0][0], UDP_NUM_IOVS, n);
}
--
2.39.5 (Apple Git-154)
next prev parent reply other threads:[~2026-09-04 21:29 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-04 21:28 [PATCH 0/7] Add vhost-net kernel support to pasta aerosouund
2026-09-04 21:28 ` [PATCH 1/7] tap: Move the tap_hdr file to a separate file aerosouund
2026-09-05 1:52 ` David Gibson
2026-09-04 21:28 ` aerosouund [this message]
2026-09-04 21:28 ` [PATCH 6/7] tap: Implement the pasta vhost-net to-guest path aerosouund
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260904212826.41027-6-aerosound161@gmail.com \
--to=aerosound161@gmail.com \
--cc=eperezma@redhat.com \
--cc=passt-dev@passt.top \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).