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 2/4] tap: Simplify some casts in the tap "slow path" functions
Date: Fri,  8 Mar 2024 17:53:23 +1100	[thread overview]
Message-ID: <20240308065325.2181322-3-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240308065325.2181322-1-david@gibson.dropbear.id.au>

We can both remove some variables which differ from others only in type,
and slightly improve type safety.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 tap.c | 41 ++++++++++++++++++-----------------------
 1 file changed, 18 insertions(+), 23 deletions(-)

diff --git a/tap.c b/tap.c
index f9e2a8d9..38965842 100644
--- a/tap.c
+++ b/tap.c
@@ -146,11 +146,9 @@ static void *tap_push_l2h(const struct ctx *c, void *buf, uint16_t proto)
  *
  * Return: pointer at which to write the packet's payload
  */
-static void *tap_push_ip4h(char *buf, struct in_addr src, struct in_addr dst,
-			   size_t len, uint8_t proto)
+static void *tap_push_ip4h(struct iphdr *ip4h, struct in_addr src,
+			   struct in_addr dst, size_t len, uint8_t proto)
 {
-	struct iphdr *ip4h = (struct iphdr *)buf;
-
 	ip4h->version = 4;
 	ip4h->ihl = sizeof(struct iphdr) / 4;
 	ip4h->tos = 0;
@@ -181,9 +179,8 @@ void tap_udp4_send(const struct ctx *c, struct in_addr src, in_port_t sport,
 {
 	size_t udplen = len + sizeof(struct udphdr);
 	char buf[USHRT_MAX];
-	void *ip4h = tap_push_l2h(c, buf, ETH_P_IP);
-	void *uhp = tap_push_ip4h(ip4h, src, dst, udplen, IPPROTO_UDP);
-	struct udphdr *uh = (struct udphdr *)uhp;
+	struct iphdr *ip4h = tap_push_l2h(c, buf, ETH_P_IP);
+	struct udphdr *uh = tap_push_ip4h(ip4h, src, dst, udplen, IPPROTO_UDP);
 	char *data = (char *)(uh + 1);
 
 	uh->source = htons(sport);
@@ -208,14 +205,14 @@ void tap_icmp4_send(const struct ctx *c, struct in_addr src, struct in_addr dst,
 		    const void *in, size_t len)
 {
 	char buf[USHRT_MAX];
-	void *ip4h = tap_push_l2h(c, buf, ETH_P_IP);
-	char *data = tap_push_ip4h(ip4h, src, dst, len, IPPROTO_ICMP);
-	struct icmphdr *icmp4h = (struct icmphdr *)data;
+	struct iphdr *ip4h = tap_push_l2h(c, buf, ETH_P_IP);
+	struct icmphdr *icmp4h = tap_push_ip4h(ip4h, src, dst,
+					       len, IPPROTO_ICMP);
 
-	memcpy(data, in, len);
+	memcpy(icmp4h, in, len);
 	csum_icmp4(icmp4h, icmp4h + 1, len - sizeof(*icmp4h));
 
-	if (tap_send(c, buf, len + (data - buf)) < 0)
+	if (tap_send(c, buf, len + ((char *)icmp4h - buf)) < 0)
 		debug("tap: failed to send %zu bytes (IPv4)", len);
 }
 
@@ -230,13 +227,11 @@ void tap_icmp4_send(const struct ctx *c, struct in_addr src, struct in_addr dst,
  *
  * Return: pointer at which to write the packet's payload
  */
-static void *tap_push_ip6h(char *buf,
+static void *tap_push_ip6h(struct ipv6hdr *ip6h,
 			   const struct in6_addr *src,
 			   const struct in6_addr *dst,
 			   size_t len, uint8_t proto, uint32_t flow)
 {
-	struct ipv6hdr *ip6h = (struct ipv6hdr *)buf;
-
 	ip6h->payload_len = htons(len);
 	ip6h->priority = 0;
 	ip6h->version = 6;
@@ -268,9 +263,9 @@ void tap_udp6_send(const struct ctx *c,
 {
 	size_t udplen = len + sizeof(struct udphdr);
 	char buf[USHRT_MAX];
-	void *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6);
-	void *uhp = tap_push_ip6h(ip6h, src, dst, udplen, IPPROTO_UDP, flow);
-	struct udphdr *uh = (struct udphdr *)uhp;
+	struct ipv6hdr *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6);
+	struct udphdr *uh = tap_push_ip6h(ip6h, src, dst,
+					  udplen, IPPROTO_UDP, flow);
 	char *data = (char *)(uh + 1);
 
 	uh->source = htons(sport);
@@ -296,14 +291,14 @@ void tap_icmp6_send(const struct ctx *c,
 		    const void *in, size_t len)
 {
 	char buf[USHRT_MAX];
-	void *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6);
-	char *data = tap_push_ip6h(ip6h, src, dst, len, IPPROTO_ICMPV6, 0);
-	struct icmp6hdr *icmp6h = (struct icmp6hdr *)data;
+	struct ipv6hdr *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6);
+	struct icmp6hdr *icmp6h = tap_push_ip6h(ip6h, src, dst, len,
+						IPPROTO_ICMPV6, 0);
 
-	memcpy(data, in, len);
+	memcpy(icmp6h, in, len);
 	csum_icmp6(icmp6h, src, dst, icmp6h + 1, len - sizeof(*icmp6h));
 
-	if (tap_send(c, buf, len + (data - buf)) < 1)
+	if (tap_send(c, buf, len + ((char *)icmp6h - buf)) < 1)
 		debug("tap: failed to send %zu bytes (IPv6)", len);
 }
 
-- 
@@ -146,11 +146,9 @@ static void *tap_push_l2h(const struct ctx *c, void *buf, uint16_t proto)
  *
  * Return: pointer at which to write the packet's payload
  */
-static void *tap_push_ip4h(char *buf, struct in_addr src, struct in_addr dst,
-			   size_t len, uint8_t proto)
+static void *tap_push_ip4h(struct iphdr *ip4h, struct in_addr src,
+			   struct in_addr dst, size_t len, uint8_t proto)
 {
-	struct iphdr *ip4h = (struct iphdr *)buf;
-
 	ip4h->version = 4;
 	ip4h->ihl = sizeof(struct iphdr) / 4;
 	ip4h->tos = 0;
@@ -181,9 +179,8 @@ void tap_udp4_send(const struct ctx *c, struct in_addr src, in_port_t sport,
 {
 	size_t udplen = len + sizeof(struct udphdr);
 	char buf[USHRT_MAX];
-	void *ip4h = tap_push_l2h(c, buf, ETH_P_IP);
-	void *uhp = tap_push_ip4h(ip4h, src, dst, udplen, IPPROTO_UDP);
-	struct udphdr *uh = (struct udphdr *)uhp;
+	struct iphdr *ip4h = tap_push_l2h(c, buf, ETH_P_IP);
+	struct udphdr *uh = tap_push_ip4h(ip4h, src, dst, udplen, IPPROTO_UDP);
 	char *data = (char *)(uh + 1);
 
 	uh->source = htons(sport);
@@ -208,14 +205,14 @@ void tap_icmp4_send(const struct ctx *c, struct in_addr src, struct in_addr dst,
 		    const void *in, size_t len)
 {
 	char buf[USHRT_MAX];
-	void *ip4h = tap_push_l2h(c, buf, ETH_P_IP);
-	char *data = tap_push_ip4h(ip4h, src, dst, len, IPPROTO_ICMP);
-	struct icmphdr *icmp4h = (struct icmphdr *)data;
+	struct iphdr *ip4h = tap_push_l2h(c, buf, ETH_P_IP);
+	struct icmphdr *icmp4h = tap_push_ip4h(ip4h, src, dst,
+					       len, IPPROTO_ICMP);
 
-	memcpy(data, in, len);
+	memcpy(icmp4h, in, len);
 	csum_icmp4(icmp4h, icmp4h + 1, len - sizeof(*icmp4h));
 
-	if (tap_send(c, buf, len + (data - buf)) < 0)
+	if (tap_send(c, buf, len + ((char *)icmp4h - buf)) < 0)
 		debug("tap: failed to send %zu bytes (IPv4)", len);
 }
 
@@ -230,13 +227,11 @@ void tap_icmp4_send(const struct ctx *c, struct in_addr src, struct in_addr dst,
  *
  * Return: pointer at which to write the packet's payload
  */
-static void *tap_push_ip6h(char *buf,
+static void *tap_push_ip6h(struct ipv6hdr *ip6h,
 			   const struct in6_addr *src,
 			   const struct in6_addr *dst,
 			   size_t len, uint8_t proto, uint32_t flow)
 {
-	struct ipv6hdr *ip6h = (struct ipv6hdr *)buf;
-
 	ip6h->payload_len = htons(len);
 	ip6h->priority = 0;
 	ip6h->version = 6;
@@ -268,9 +263,9 @@ void tap_udp6_send(const struct ctx *c,
 {
 	size_t udplen = len + sizeof(struct udphdr);
 	char buf[USHRT_MAX];
-	void *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6);
-	void *uhp = tap_push_ip6h(ip6h, src, dst, udplen, IPPROTO_UDP, flow);
-	struct udphdr *uh = (struct udphdr *)uhp;
+	struct ipv6hdr *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6);
+	struct udphdr *uh = tap_push_ip6h(ip6h, src, dst,
+					  udplen, IPPROTO_UDP, flow);
 	char *data = (char *)(uh + 1);
 
 	uh->source = htons(sport);
@@ -296,14 +291,14 @@ void tap_icmp6_send(const struct ctx *c,
 		    const void *in, size_t len)
 {
 	char buf[USHRT_MAX];
-	void *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6);
-	char *data = tap_push_ip6h(ip6h, src, dst, len, IPPROTO_ICMPV6, 0);
-	struct icmp6hdr *icmp6h = (struct icmp6hdr *)data;
+	struct ipv6hdr *ip6h = tap_push_l2h(c, buf, ETH_P_IPV6);
+	struct icmp6hdr *icmp6h = tap_push_ip6h(ip6h, src, dst, len,
+						IPPROTO_ICMPV6, 0);
 
-	memcpy(data, in, len);
+	memcpy(icmp6h, in, len);
 	csum_icmp6(icmp6h, src, dst, icmp6h + 1, len - sizeof(*icmp6h));
 
-	if (tap_send(c, buf, len + (data - buf)) < 1)
+	if (tap_send(c, buf, len + ((char *)icmp6h - buf)) < 1)
 		debug("tap: failed to send %zu bytes (IPv6)", len);
 }
 
-- 
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 ` David Gibson [this message]
2024-03-08  6:53 ` [PATCH 3/4] tap: Implement tap_send() "slow path" in terms of fast path David Gibson
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-3-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).