public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: Laurent Vivier <lvivier@redhat.com>,
	David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 3/4] tap: Implement tap_send() "slow path" in terms of fast path
Date: Fri,  8 Mar 2024 17:53:24 +1100	[thread overview]
Message-ID: <20240308065325.2181322-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240308065325.2181322-1-david@gibson.dropbear.id.au>

Most times we send frames to the guest it goes via tap_send_frames().
However "slow path" protocols - ARP, ICMP, ICMPv6, DHCP and DHCPv6 - go
via tap_send().

As well as being a semantic duplication, tap_send() contains at least one
serious problem: it doesn't properly handle short sends, which can be fatal
on the qemu socket connection, since frame boundaries will get out of sync.

Rewrite tap_send() to call tap_send_frames().  While we're there, rename it
tap_send_single() for clarity.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 arp.c |  4 +---
 tap.c | 38 +++++++++++++++++---------------------
 tap.h |  2 +-
 3 files changed, 19 insertions(+), 25 deletions(-)

diff --git a/arp.c b/arp.c
index a35c1b61..113cda2f 100644
--- a/arp.c
+++ b/arp.c
@@ -44,7 +44,6 @@ int arp(const struct ctx *c, const struct pool *p)
 	struct arphdr *ah;
 	struct arpmsg *am;
 	size_t len;
-	int ret;
 
 	eh = packet_get(p, 0, 0,			 sizeof(*eh), NULL);
 	ah = packet_get(p, 0, sizeof(*eh),		 sizeof(*ah), NULL);
@@ -83,8 +82,7 @@ int arp(const struct ctx *c, const struct pool *p)
 	memcpy(eh->h_dest,	eh->h_source,	sizeof(eh->h_dest));
 	memcpy(eh->h_source,	c->mac,		sizeof(eh->h_source));
 
-	if ((ret = tap_send(c, eh, len)) < 0)
-		warn("ARP: send: %s", strerror(ret));
+	tap_send_single(c, eh, len);
 
 	return 1;
 }
diff --git a/tap.c b/tap.c
index 38965842..5e3c6b13 100644
--- a/tap.c
+++ b/tap.c
@@ -67,28 +67,28 @@ static PACKET_POOL_NOINIT(pool_tap6, TAP_MSGS, pkt_buf);
 #define FRAGMENT_MSG_RATE	10  /* # seconds between fragment warnings */
 
 /**
- * tap_send() - Send frame, with qemu socket header if needed
+ * tap_send_single() - Send a single frame
  * @c:		Execution context
  * @data:	Packet buffer
  * @len:	Total L2 packet length
- *
- * Return: return code from send() or write()
  */
-int tap_send(const struct ctx *c, const void *data, size_t len)
+void tap_send_single(const struct ctx *c, const void *data, size_t len)
 {
-	pcap(data, len);
+	uint32_t vnet_len = htonl(len);
+	struct iovec iov[2];
+	size_t iovcnt = 0;
 
 	if (c->mode == MODE_PASST) {
-		int flags = MSG_NOSIGNAL | MSG_DONTWAIT;
-		uint32_t vnet_len = htonl(len);
-
-		if (send(c->fd_tap, &vnet_len, 4, flags) < 0)
-			return -1;
-
-		return send(c->fd_tap, data, len, flags);
+		iov[iovcnt].iov_base = &vnet_len;
+		iov[iovcnt].iov_len = sizeof(vnet_len);
+		iovcnt++;
 	}
 
-	return write(c->fd_tap, (char *)data, len);
+	iov[iovcnt].iov_base = (void *)data;
+	iov[iovcnt].iov_len = len;
+	iovcnt++;
+
+	tap_send_frames(c, iov, iovcnt, 1);
 }
 
 /**
@@ -189,8 +189,7 @@ void tap_udp4_send(const struct ctx *c, struct in_addr src, in_port_t sport,
 	csum_udp4(uh, src, dst, in, len);
 	memcpy(data, in, len);
 
-	if (tap_send(c, buf, len + (data - buf)) < 0)
-		debug("tap: failed to send %zu bytes (IPv4)", len);
+	tap_send_single(c, buf, len + (data - buf));
 }
 
 /**
@@ -212,8 +211,7 @@ void tap_icmp4_send(const struct ctx *c, struct in_addr src, struct in_addr dst,
 	memcpy(icmp4h, in, len);
 	csum_icmp4(icmp4h, icmp4h + 1, len - sizeof(*icmp4h));
 
-	if (tap_send(c, buf, len + ((char *)icmp4h - buf)) < 0)
-		debug("tap: failed to send %zu bytes (IPv4)", len);
+	tap_send_single(c, buf, len + ((char *)icmp4h - buf));
 }
 
 /**
@@ -274,8 +272,7 @@ void tap_udp6_send(const struct ctx *c,
 	csum_udp6(uh, src, dst, in, len);
 	memcpy(data, in, len);
 
-	if (tap_send(c, buf, len + (data - buf)) < 1)
-		debug("tap: failed to send %zu bytes (IPv6)", len);
+	tap_send_single(c, buf, len + (data - buf));
 }
 
 /**
@@ -298,8 +295,7 @@ void tap_icmp6_send(const struct ctx *c,
 	memcpy(icmp6h, in, len);
 	csum_icmp6(icmp6h, src, dst, icmp6h + 1, len - sizeof(*icmp6h));
 
-	if (tap_send(c, buf, len + ((char *)icmp6h - buf)) < 1)
-		debug("tap: failed to send %zu bytes (IPv6)", len);
+	tap_send_single(c, buf, len + ((char *)icmp6h - buf));
 }
 
 /**
diff --git a/tap.h b/tap.h
index c45aab3e..aa3b1af2 100644
--- a/tap.h
+++ b/tap.h
@@ -72,7 +72,7 @@ void tap_udp6_send(const struct ctx *c,
 void tap_icmp6_send(const struct ctx *c,
 		    const struct in6_addr *src, const struct in6_addr *dst,
 		    const void *in, size_t len);
-int tap_send(const struct ctx *c, const void *data, size_t len);
+void tap_send_single(const struct ctx *c, const void *data, size_t len);
 size_t tap_send_frames(const struct ctx *c, const struct iovec *iov,
 		       size_t bufs_per_frame, size_t nframes);
 void eth_update_mac(struct ethhdr *eh,
-- 
@@ -72,7 +72,7 @@ void tap_udp6_send(const struct ctx *c,
 void tap_icmp6_send(const struct ctx *c,
 		    const struct in6_addr *src, const struct in6_addr *dst,
 		    const void *in, size_t len);
-int tap_send(const struct ctx *c, const void *data, size_t len);
+void tap_send_single(const struct ctx *c, const void *data, size_t len);
 size_t tap_send_frames(const struct ctx *c, const struct iovec *iov,
 		       size_t bufs_per_frame, size_t nframes);
 void eth_update_mac(struct ethhdr *eh,
-- 
2.44.0


  parent reply	other threads:[~2024-03-08  6:53 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-08  6:53 [PATCH 0/4] Some improvements to the tap send path David Gibson
2024-03-08  6:53 ` [PATCH 1/4] tap: Extend tap_send_frames() to allow multi-buffer frames David Gibson
2024-03-14  7:02   ` Stefano Brivio
2024-03-14  8:47     ` David Gibson
2024-03-08  6:53 ` [PATCH 2/4] tap: Simplify some casts in the tap "slow path" functions David Gibson
2024-03-08  6:53 ` David Gibson [this message]
2024-03-08  6:53 ` [PATCH 4/4] tap: Rename tap_iov_{base,len} David Gibson
2024-03-08  8:18 ` [PATCH 0/4] Some improvements to the tap send path Laurent Vivier
2024-03-08  8:34   ` Stefano Brivio
2024-03-08  8:55     ` Laurent Vivier
2024-03-08 15:49     ` Laurent Vivier
2024-03-08 16:24       ` Stefano Brivio
2024-03-08 12:42   ` David Gibson
2024-03-08 16:49     ` Laurent Vivier
2024-03-09  4:15       ` David Gibson
2024-03-11 11:02     ` Laurent Vivier
2024-03-14  2:22       ` David Gibson
2024-03-14 16:40 ` Stefano Brivio

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=20240308065325.2181322-4-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=lvivier@redhat.com \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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).